Write Your OWN OPERATING SYSTEM TUTORAL (1)

zhaozj2021-02-08  264

Lesson 1: The boot sector

In this Lesson We'll Learn About The Contents of The Boot Sector So That We Can Learn To Write Our Own Boot Program.

When the computer boots from a floppy, BIOS (Basic Input / Output System) reads the disk and loads the first sector into memory at address 0000: 7C00 This first sector is called the DOS Boot Record (DBR) BIOS jumps to the address.. 0x7c00 and begin executing instructions there. It is these instructions (The "Boot Loader") That Will Load The OPERATING SYSTEM (OS) INTO MEMORY AND BEGIN THE OS'S Boot Process.

The first thing to do is to take a look inside the Boot Record. The DOS utility DEBUG is a widely available tool that can be used to view the contents of memory and disks. We'll use DEBUG to look at a floppy disk's Boot Record .

AT A DOS (or Windows) Command Prompt Type Debug. This Will Leave You With Just A HYPHEN AS A PROMPT. IF you Enter Letter 'D' AS A Command and Press Enter, IT Will Show You a Portion of The Contents of Ram. Typing the question mark as a command will give you a list of all the available commands in DEBUG. (Be very careful when using the DEBUG utility. This utility can be used to overwrite data on any disk drive, possibly causing loss of data.)

Place A Freshly Formatted Disk in the A: Drive. To load the boot record off your floppy disk, Type The Following Command.

-L 0 0 0 1

(The first character is the letter 'l', not the number '1'.) This command loads sectors off a disk into a portion of RAM. The 4 numbers after the 'l' represent in order, the beginning address where you want the data loaded, the drive number (0 for first floppy driver), the first sector on the disk to load, and how many sectors to load. Typing this command will load the first sector of the floppy into memory starting at address 0.Now That We Have The Boot Record Loaded Into Memory, We Want To View ITS Contents. Type The Following Command.

-d 0

What You See 8 Lines That Repesent The First 128 (0x80 in HEX) BYtes in The Floppy's Boot Record. The results (for my floppy disk) Are the Following.

0AF6: 0000 EB 3C 90 4D 53 44 4F 53-35 2E 30 00 02 01 01 00. <. Msdos5.0 .....

0AF6: 0010 02 E0 00 40B f0 09 00-12 00 02 00 00 00 00 ... @ ............

0AF6: 0020 00 00 00 00 29 F6-63 30 88 4E 4F 20 4E 41 ...). C0.no na

0AF6: 0030 4D 45 20 20 20 20 46 41-54 31 32 20 20 20 33 C9 ME FAT12 3.

0AF6: 0040 8E D1 BC F0 7B 8e D9 B8-00 20 8E C0 FC BD 00 7c .... {.... .....

0AF6: 0050 38 4E 24 7D 24 8B C1 99-E8 3C 01 72 1C 83 EB 3A 8N $} $ ... <. R ...:

0AF6: 0060 66 A1 1C 7C 26 66 3B 07-26 8A 57 FC 75 06 80 CA f .. | & f;. &. W.u ...

0AF6: 0070 02 88 56 02 80 C3 10 73-EB 33 C9 8A 46 10 98 F7 ..V .... S.3..f ...

At First Glance, this Doesn't Tell Me Much. I Can See That IT Looks Like This Is A MS-DOS 5.0 Disk with no name and a FAT12 File System. The number in the far left column show the memory addresses in ram. The hexadecimal numbers in the middle show all the bytes in this portion of memory, and the column on the right shows the ASCII characters that the hex bytes represent (a period is shown if the byte does not translate to any visible character). Some of the bytes you see in this portion of the Boot Record are parts of instructions in the boot loader, and some of them hold information about the disk such as the number of bytes per sector, the number of sectors per track, etc ... Now it's time Take a glance at the code for the boot loading. type the folload.

-u 0

This performs an "unassemble" operation. This shows us the same bytes as before (starting with address 0), but this time DEBUG shows us the Intel instructions that these bytes represent. The results for my floppy are the following.

0AF6: 0000 EB3C JMP 003E

0AF6: 0002 90 NOP

0AF6: 0003 4D DEC BP

0AF6: 0004 53 PUSH BX

0AF6: 0005 44 INC SP

0AF6: 0006 4F DEC DI

0AF6: 0007 53 PUSH BX

0AF6: 0008 352E30 XOR AX, 302E

0AF6: 000B 0002 Add [BP Si], Al

0AF6: 000D 0101 ADD [BX DI], AX

0AF6: 000f 0002 Add [BP Si], Al

0AF6: 0011 E000 LOOPNZ 0013

0AF6: 0013 40 INC AX

0AF6: 0014 0BF0 or Si, AX

0AF6: 0016 0900 OR [BX Si], AX

0AF6: 0018 1200 ADC Al, [BX Si]

0AF6: 001A 0200 Add Al, [BX Si]

0AF6: 001C 0000 Add [BX Si], Al0af6: 001E 0000 Add [BX Si], Al

The first instruction says to jump to address 0x3E. The bytes after this are the data about the disk I mentioned before and do not really correspond to instructions, but DEBUG does its duty and tries to interpret them as such.

THE FIRST INSTRUCTION JUMPS OVER THIS DATA TOTORTING AT Address 0x3e. Let's Look at the instructions there. Type. Type

-u 3e

Here You Can See The Beginning of The Code That Will Load The Dos (or Windows) Operating System. This Code (for MS-DOS) Looks on The Disk for the Files IO.SYS AND MSDOS.SYS. THESE FILES Contain The Code for The Boot Loader Code Will Load There Files Into Memory and Begin Executing The M. If The Files Are Not Found ON The Disk, The Boot Loader Will Display The Famous Error Message.

INVALID SYSTEM DISK

Disk I / O Error

Replace the disk, and then press any key

.

-d 180

0AFC: 0180 18 01 27 0D 0A 49 6E 76-61 6C 69 64 20 73 79 73 .. '.. invalid sys

0AFC: 0190 74 65 6D 20 64 69 73 6B-FF 0D 0A 44 69 73 6B 20 TEM Disk ... Disk

0AFC: 01A0 49 2F 4F 20 65 72 72 6F-72 FF 0D 0A 52 65 70 6C I / O Error ... REPL

0AFC: 01B0 61 63 65 20 74 68 65 20-64 69 73 6B 2C 20 61 6e ace the disk, an

0AFC: 01C0 64 20 74 68 65 6e 20 70-72 65 73 73 20 61 6e 79 D THEN PRESS ANY

0AFC: 01D0 20 6B 65 79 0D 0A 00 00-49 4F 20 20 20 20 20 20 KEY .... IO

0AFC: 01E0 53 59 53 4D 53 44 4F 53-20 20 20 53 59 53 7F 01 SYSMSDOS SYS..

0AFC: 01F0 00 41 BB 00 07 60 66 6A-00 E9 3B FF 00 00 55 aa .a ... `fj ..; ... u.

This shows the very end of the Boot Record. The Boot Record is exactly one sector (512 bytes) on the disk. If it is loaded into memory starting with address 0, then the last byte will be in address 0x1FF. If you look at the last two bytes of the Boot Record (0x1FE and 0x1FF), you will notice that they are 0x55 and 0xAA. The last two bytes of the Boot Record must be set to these values ​​or else BIOS will not load the sector and begin executing it .So, to recap, the DOS Boot Record starts with an instruction to jump over the data that follows that instruction. These 60 bytes of data starts at address 0x02 and ends on 0x3D, with the boot code resuming at 0x3E and going all the way To 0x1fd, Which is Followed by the Two Bytes, 0x55 and 0xAA. in The next lesson we will.com..................

转载请注明原文地址:https://www.9cbs.com/read-3158.html

New Post(0)