引导加载程序在实际计算机上不起作用

Bootloader does not work on actual computer

我知道这个问题以前有人问过,但是 none 的其他答案似乎已经解决了我的问题。也许我错过了什么?

我知道 .iso 有效,因为我 运行 它在 QEMU 中并且它成功运行。那我做错了什么?

bits 16

xor ax, ax

start:
    cld               ; Set direction flag to forward

    ; Set up registers
    mov ax, 07c0h     ; Segment location which BIOS loads
    add ax, 288       ; (4096 + 512) / 16 bytes
    mov ss, ax        ; Sets stack segment register
    mov sp, 4096      ; Sets stack pointer register (offset of stack)

    mov ax, 07c0h
    mov ds, ax        ; Sets data segment to where we're loaded

    mov si, text      ; Puts string into source index
    call print_string ; Calls print string

    jmp $             ; Infinite loop to prevent shutdown

print_string:
    mov ah, 0eh       ; System call for printing
    xor bh, bh        ; Sets BH register to 0

.repeat:
    lodsb             ; Loads byte into AL
    cmp al, 0         ; Sees if AL is 0
    je .done          ; Jumps to done if AL is zero

    int 10h           ; Otherwise, print
    jmp .repeat       ; Repeat

.done:
    ret

text db 'Test', 0

times 510 - ($ - $$) db 0 ; Pads 510 - (current location - start location) zeros
dw 0xAA55                 ; Standard PC boot signature (takes up 2 bytes)

编辑:我已将以下内容添加到我的代码中:

xor ax, ax
cld
xor bh, bh

为了创建 iso,我 运行 使用了以下命令:

dd if=/dev/zero of=floppy.img bs=1024 count=1440
dd if=bootloader.bin of=floppy.img seek=0 count=1 conv=notrunc
mkdir iso
cp floppy.img iso/
mkisofs -o file.iso -b floppy.img iso

为了将 iso 刻录到我的 usb,我 运行 以下命令:

umount /dev/sdX
dd if=/home/mint/Downloads/file.iso of=/dev/sdX bs=4M && sync

您的问题是您创建的 "iso" 是光盘映像。只有当它刻录到光盘(例如 CD-R)时,它才能在真正的计算机上启动。当您将它与 QEMU 一起使用时,您显然是在将它用作模拟 CD-ROM。当您将它复制到您的 USB 驱动器时,它的格式不正确,无法在 USB 驱动器上启动。

幸运的是,从 USB 驱动器启动的正确格式很简单:您的引导加载程序只需要位于驱动器的第一个扇区,就像在软盘或硬盘上一样。所以你可以跳过创建 "iso" 的部分,直接将引导扇区写入 U 盘。例如:

dd if=bootloader.bin of=/dev/sdX