一段简单的 bootloader 和 解释
Developing a simple BootloaderLets take another look at our list:
Are stored with the Master Boot Record (MBR).
Are in the first sector of the disk.
Is the size of a single sector (512) bytes.
Are loaded by the BIOS INT 0x19 at address 0x7C00.
Open up any ordinary text editor (I am using Visual Studio 2005), but Notepad will suffice.
Heres the bootloader (Boot1.asm)...
;*********************************************
; Boot1.asm
; - A Simple Bootloader
;
; Operating Systems Development Tutorial
;*********************************************
org 0x7c00 ; We are loaded by BIOS at 0x7C00
bits 16 ; We are still in 16 bit Real Mode
Start:
cli ; Clear all Interrupts
hlt ; halt the system
times 510 - ($-$$) db 0 ; We have to be 512 bytes. Clear the rest of the bytes with 0
dw 0xAA55 ; Boot Signiture
Some of this should not come to much of a surprise. Lets analize line by line:
org 0x7c00 ; We are loaded by BIOS at 0x7C00
Remember: The BIOS loads us at 0x7C00. The above code tells NASM to insure all addresses are relitave to 0x7C00. This means, the first instruction will be at 0x7C00.
bits 16 ; We are still in 16 bit Real Mode
I explained how the x86 family is backward compatible with the old DOS systems. Because the old DOS systems were
16 bit, All x86 compatible computers boot into 16 bit mode. This means:
We are limited to 1 MB of memory.
We are limited to the 16 bit registers.
We will need to switch the computer into a 32 bit mode. We will do this later.
times 510 - ($-$$) db 0 ; We have to be 512 bytes. Clear the rest of the bytes with 0
In NASM, the dollar operator ($) represents the address of the current line.
$$ represents the address of the first instruction (Should be 0x7C00).
So, $-$$ returns the number of bytes from the current line to the start (In this case, the size of the program).
dw 0xAA55 ; Boot Signiture
Remember that the BIOS INT 0x19 searches for a bootable disk.
How does it know if the disk is bootable? The boot signiture.
If the 511 byte is 0xAA and the 512 byte is 0x55, INT 0x19 will load and execute the bootloader.
Because the boot signiture must be the last two bytes in the bootsector,
We use the times keyword to calculate the size different to fill in up to the 510th byte,
rather then the 512th byte.