硬盘引导模块
主机按下启动键后,CPU读取BIOS,BIOS初始化,将启动盘的第一扇区512B数据读到内存的0x7c00-0x7d00,并跳转至0x7c00。
这一段的代码的作用是: 选择要启动的分区,并将要启动分区的数据读至合适的内存位置并跳转。
start:
// 初始化各寄存器
cli
mov ss, ax
mov sp, offset start
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
sti
//将代码复制到0x7c00
cld
mov cx, 512 / 4
mov si, HEX(7C00)
mov di, offset start
rep movsd
// Jump to the relocated code.
ljmp16 0, relocated
relocated:
// 利用BIOS中断功能,读取启动扇区代码
push 0
push 0
push dword ptr es:[pBootSectorLBA]
push es
push HEX(7C00)
push 4
push HEX(10)
// Call the BIOS function. Note that we haven't clobbered DL up to this point, so the Drive Number passed by the BIOS is still there.
// Read errors are indicated by the Carry Flag.
mov ah, HEX(42)
mov si, sp
int HEX(13)
jc read_error
// Verify the Hybrid Signature.
cmp dword ptr es:[pBootSectorHybridSignature], HybridSignature
jne invalid_signature
// 跳转至代码位置,
ljmp16 0, pBootSectorHybridEntryPoint
read_error:
call die_with_error
.ascii "ISOMBR: Read Error!", CR, LF
invalid_signature:
call die_with_error
.ascii "ISOMBR: Invalid Boot Sector Hybrid Signature!", CR, LF
die_with_error:
// Fetch the message to output stored at the return address on the stack.
pop si
// Call BIOS INT 10h, Function 0Eh to output a single character.
// Do this in a loop and stop after we have printed the newline LF character.
next_character:
lodsb
mov ah, HEX(0E)
xor bx, bx
int HEX(10)
cmp al, 10
jne next_character
// Die gracefully, that means in an infinite HLT loop to not put any stress on the CPU.
die:
hlt
jmp die
// The "isohybrid" tool will add the remaining information, including the infamous 0xAA55 MBR signature.
.org 431
.byte 0
.endcode16
END