引导加载程序的大小
我正在阅读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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
I am reading brokenthorn.com ‘s O/S development tutorials one of the tutorials, the following code is there.
http://www.brokenthorn.com/Resources/OSDev3.html
I don’t understand why this code clear 510 bytes. org, bits, cli, hlt are there in the code too. Shouldn’t it be changed to less than 510 bytes? Could it be typo or something?
Thanks.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;*********************************************
; 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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它不是清除 510 字节,而是清除
510 - ($-$$)
字节。由于$
是当前位置,而$$
是该部分的开头,因此它会清除510 - (该部分到该点的长度)字节。
这将从 512 字节限制中正确填充块最多两个字节,并将签名放在最后两个字节上。
It's not clearing 510 bytes, it's clearing
510 - ($-$$)
bytes. Since$
is the current position, and$$
is the start of the section, it's clearing510 - (length of the section up to that point)
bytes.This will fill the block correctly up to two bytes from the 512 byte limit, and put the signature on the two last bytes.
引导扇区长 512 字节,通过最后两个字节开始设置为 0xAA55 来识别。这为加载程序的实际代码留下了 510 个字节,这正是所提供的示例在组装时所填充的内容。如果生成的二进制文件长度不精确为 512 字节,那么您可能需要指定纯二进制输出格式,但对于 nasm,这是默认设置。
在实践中,还需要为分区表等提供其他魔术字节,通常第一阶段加载器仅用于读入和执行更多代码。
The boot sector is 512 bytes long, and is identified as such by the final two bytes begin set to 0xAA55. This leaves 510 bytes for the loader's actual code, which is precisely what the provided example fills when assembled. If your resulting binary isn't precisely 512 bytes long then you may need to specify a plain binary output format, though in the case of nasm this is the default setting.
In practice there are other magic bytes which need to be present for partition tables and such, and typically the first stage loader is used for little more than reading in and executing some more code.