首页 > 解决方案 > x86 程序集:引导加载程序不加载程序

问题描述

在过去的几天里,我一直在尝试制作操作系统,并从制作引导加载程序开始。我特别想加载一个小示例程序来测试它。这是引导加载程序代码:

org 0x7c00
bits 16
start: jmp boot

boot:
    cli                               ; Disable interrupts
    cld                               ; Clear direction flags


    mov al, 2                         ; Read 2 sectors
    mov ch, 0                         ; Track 0
    mov cl, 2                         ; Read 2nd sector (1st sector is bootloader)
    mov dh, 0                         ; Head number
    mov dl, 0                         ; Drive number (0 = floppy drive)

    ; Specify memory address to read floppy to
    mov bx, 0x5000
    mov es, bx
    xor bx, bx

    mov ah, 0x2                       ; INT 0x13 with AH=0x2 means read sector
    int 0x13                          ; Call BIOS to read sector
    jmp 0x5000:0x0000                 ; Jump to sector

  ; Must be 512 bytes
  times 510 - ($-$$) db 0
  dw 0xAA55                           ; Boot Signature

这是示例程序代码:

msg db "Welcome to kOS!", 0ah, 0dh, 0h

start: jmp moveCursor

moveCursor:
    mov ah, 0x2                       ; INT 0x10 with AH=0x2 means set cursor position
    mov bh, 0                         ; Page number
    mov dh, 12                        ; Row
    mov dl, 0                         ; Column
    int 0x10                          ; Call BIOS to set position
    jmp putChar

putChar:
    mov ah, 0xA                       ; INT 0x10/AH=0xA means write character
    mov al, 0x48                      ; Character H
    mov bh, 0                         ; Page number
    mov cx, 1                         ; Times to write character
    int 0x10                          ; Call BIOS to write character
    jmp print

print:
    mov si, msg          ; Move starting address of message into SI
    jmp printstring

printstring:
    xor ax, ax           ; Set AX to 0
    mov ds, ax           ; Set Data Segment to 0
    lodsb                ; Load byte at Data Segment into AL, increment SI
    or al, al            ; Check if AL is 0
    jz exitloop          ; If zero exit the loop
    mov ah, 0xE          ; INT 0x10/AH=0xE means teletype output
    int 0x10             ; Call BIOS to write character
    jmp printstring      ; Repeat for next character

exitloop:
    hlt                  ; Halt the system

但是,使用 QEMU/GDB 进行调试时,我发现引导加载程序代码的跳转从未发生(我在地址 0x5000 处设置了一个断点,但它从未到达它),并且程序永远不会运行。我在这里发现了许多具有相同问题的问题,但他们的解决方案对我没有帮助。感谢所有帮助。谢谢!

标签: x86bootloader

解决方案


推荐阅读