Operating system 引导加载程序';s码

Operating system 引导加载程序';s码,operating-system,bootloader,instructions,Operating System,Bootloader,Instructions,我正在阅读brokenthorn.com的O/s开发教程,其中有一个教程,下面是代码 我不明白为什么这段代码能清除510字节。代码中也有org、bits、cli和hlt。不应该改为小于510字节吗?可能是打字错误还是什么 谢谢 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;********************************************* ; Boot1.asm ; - A Simple Bootloade

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

我不明白为什么这段代码能清除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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

它不是清除510字节,而是清除
510-($-$$)
字节。由于
$
是当前位置,
$
是节的开始,因此它正在清除
510-(节到该点的长度)
字节


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

引导扇区的长度为512字节,并通过最后两个字节begin设置为0xAA55来标识。这将为加载程序的实际代码留下510字节,这正是所提供的示例在组装时填充的内容。如果生成的二进制文件的长度不是512字节,则可能需要指定普通二进制输出格式,不过对于nasm,这是默认设置

实际上,分区表等还需要其他魔法字节,通常第一阶段加载程序只用于读取和执行更多代码