这个答案是基于经验和有根据的猜测。如果没有实际的硬件,测试是相当困难的。我假设 OP 正在将他的引导扇区写入 MBR 而不是 VBR。
多年来创建的 BIOS 既愚蠢又聪明(恕我直言,有些太聪明了)。有些人试图根据是否存在 BIOS 参数块和/或带有可引导分区的分区表来区分可能是软盘驱动器或硬盘驱动器的介质。不幸的是,这不是标准化的,许多 BIOS 制造商只针对 Windows 分区的磁盘测试他们的代码。
一些认为他们看到有效分区表的 BIOS 将假定 HDD 仿真并尝试从活动分区加载卷引导记录 (VBR),而不是在 MBR 上执行代码。我怀疑这是机器上的情况,尽管将代码归零并保留了分区表,但它似乎仍然可以启动。执行的代码可能直接来自 VBR。
如果使用充当硬盘驱动器(而不是软盘)的设备,您可以创建一个带有分区表的 MBR,其中唯一的活动分区从驱动器的开头开始(CHS=0,0,1 或 LBA=0);被标记为可启动;并且具有非零分区类型。如果您遇到一台机器打算直接加载 VBR,那么此方法会诱骗它将 MBR 作为 VBR 加载。
在您似乎正在测试的机器上,应该在您的 USB 上作为 HDD 媒体启动的示例代码可能如下所示:
bits 16
org 0x7c00
boot_start:
xor ax, ax ; DS=0 since we use ORG 0x7c00. 0x0000<<4+0x7c00=0x7c00
mov ds, ax
mov es, ax
; If you will be reading data into memory outside of 0x7c00 to 0x7dff
; then you want to set the stack SS:SP - uncomment these lines
; mov ss, ax ; Stack at 0x0000:0x7c00
; mov sp, 0x7c00 ; Just below bootloader
cld ; Forward movement of string instructions
; (MOVSB, SCASB, etc)
mov si, HelloWorldMsg ; Print hello world
call print_string
end_loop: ; Loop forever to terminate
hlt
jmp end_loop
; Function: print_string
; Display a string to the console on display page 0
;
; Inputs: SI = Offset of address to print
; Clobbers: AX, BX, SI
print_string:
mov ah, 0x0e ; BIOS tty Print
xor bx, bx ; Set display page to 0 (BL)
jmp .getch
.repeat:
int 0x10 ; print character
.getch:
lodsb ; Get character from string
test al,al ; Have we reached end of string?
jnz .repeat ; if not process next character
.end:
ret
HelloWorldMsg: db "Hello, world!", 0x0d, 0x0a, 0
times 446-($-$$) db 0 ; Pad with 0s up until first partition entry
part1_entry:
db 0x80 ; 0x80 = Active boot partition, 0x00=inactive
db 0x00, 0x01, 0x00 ; CHS of first absolute sector (MBR) of hard drive
; Head=0, Sector=1, Cylinder=0
db 0x0c ; Partition type (has to be non-zero)
; 0x0c = Win 95 FAT32 (LBA)
db 0x00, 0x01, 0x00 ; CHS of last absolute sector (MBR) of hard drive
; Head=0, Sector=1, Cylinder=0
; We are effectively saying Size of partition is 1 sector
dd 0x0 ; LBA of first absolute sector (0=MBR)
dd 0x1 ; Number of sectors in partition. We set it to 1 but if you
; wish you could set it to the number of sectors on the disk
times 510-($-$$) db 0 ; Pad remainder of boot sector up to boot signature. This zeroes
; partition entries 2,3,4 effectively making them inactive
dw 0xAA55 ; The standard PC boot signature after partition table
您可以使用以下命令构建 MBR:
nasm -f bin boot.asm -o boot.bin
如果成功放置在媒体上,则应打印此代码:
你好世界!