引导加载程序的大小

时间:2011-04-16 12:42:53

标签: operating-system bootloader instructions

我正在阅读brokenthorn.com的O / S开发教程其中一个教程,下面是代码。

http://www.brokenthorn.com/Resources/OSDev3.html

我不明白为什么这段代码清除510字节。 org,bits,cli,hlt也在代码中。它不应该改为小于510字节?可能是拼写错误还是什么?

感谢。

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;*********************************************
;   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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

2 个答案:

答案 0 :(得分:1)

它没有清除510个字节,它正在清除510 - ($-$$)个字节。由于$是当前位置,而$$是该部分的开头,因此它正在清除510 - (length of the section up to that point)个字节。

这将从512字节限制中正确填充块中的两个字节,并将签名放在最后两个字节上。

答案 1 :(得分:0)

引导扇区长度为512字节,并且由最后两个字节开始设置为0xAA55来识别。这为加载器的实际代码留下了510个字节,这正是所提供的示例在组装时填充的内容。如果生成的二进制文件不是精确的512字节长,那么您可能需要指定纯二进制输出格式,但在nasm的情况下这是默认设置。

在实践中,还需要为分区表等提供其他魔术字节,通常第一阶段加载器仅用于读入和执行更多代码。

相关问题