You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何BIOS磁盘读取程序停滞而非打印目标字符?

操作系统开发:磁盘扇区读取失败问题

我最近在做一个操作系统开发项目,前期进展顺利,但到磁盘读取环节卡壳了。原本代码的预期逻辑是:加载下一个扇区,打印BIOS默认加载的引导程序(55AA签名之后)的第一个字节——字符"H",但程序直接停滞,没有输出任何内容。

初始代码

org 0x7C00
bits 16

; Variables for CHS conversion
tmp: db 0
sct: db 0
hed: db 0
cyl: db 0
spt: db 0
hct: db 0
bdv: db 0
snm: db 0

mov [bdv], dl

jmp main


; lbaRead
; Reads sectors using LBA
; bx: LBA Address
; ax: Number of sectors
; dl: Drive number
lbaRead:
    ; Read the Disk Geometry
    mov ah, 8
    int 0x13
    inc dh
    mov [hct], dh
    and cl, 0x3F
    mov [spt], cl
    ; Error Handling
    jc err

    ; Convert LBA to CHS
    ; Store Sector to read number (snm)
    mov [snm], ax
    ; Calculate temporary value (tmp) and sector
    mov ax, bx
    mov bx, [spt]
    div bx
    mov [tmp], al
    inc ah
    mov [sct], ah
    ; Calculate Head and Cylinder
    mov ax, [tmp]
    mov bx, [hct]
    div bx
    mov [hed], ah
    mov [cyl], al

    ; Read the disk
    mov ah, 0x02
    mov al, [snm]
    mov ch, [cyl]
    mov cl, [sct]
    mov dh, [hed]
    mov bx, 0x7E00
    int 0x13
    ; Error handling
    jc err
    
main:
    mov bx, 1
    mov ax, 1
    call lbaRead
    mov ah, 0x0E
    mov al, [0x7E00]
    int 0x10
    jmp $

err:
    mov ah, 0x0B
    mov bl, 0x4
    int 0x10
    jmp $

; Pad to 512 bytes and add 0x55AA cap
times 510-($-$$) db 0
db 0x55, 0xaa

磁盘数据情况

磁盘的十六进制编辑器数据显示,引导扇区(前512字节,末尾为0x55AA签名)之后的下一个扇区起始字节为0x48,对应ASCII字符"H"。

修改后的尝试代码

我已经做了一些修改尝试修复问题,但仍未解决,修改后的代码如下:

org 0x7C00
bits 16

mov ax, 0
mov es, ax
mov ds, ax

; Variables for CHS conversion
tmp: db 0
sct: db 0
hed: db 0
cyl: db 0
spt: db 0
hct: db 0
bdv: db 0
snm: db 0

mov [bdv], dl

jmp main


; lbaRead
; Reads sectors using LBA
; bx: LBA Address
; ax: Number of sectors
; dl: Drive number
lbaRead:
    ; Read the Disk Geometry
    mov ah, 8
    int 0x13
    ; Error Handling
    jc err
    ; continue
    inc dh
    mov [hct], dh
    and cl, 0x3F
    mov [spt], cl

    ; Convert LBA to CHS
    ; Store Sectors to read number (snm)
    mov [snm], ax
    ; Calculate temporary value (tmp) and sector
    mov ax, bx
    mov bx, [spt]
    div bx
    mov [tmp], al
    inc ah
    mov [sct], ah
    ; Calculate Head and Cylinder
    mov ax, 0
    mov dx, ax
    mov ax, [tmp]
    mov bx, [hct]
    div bx
    mov [hed], ax
    mov [cyl], dx

    ; Read the disk
    mov ah, 0x02
    mov al, [snm]
    mov ch, [cyl]
    mov cl, [sct]
    mov dh, [hed]
    mov bx, 0x7E00
    int 0x13
    ; Error handling
    jc err
    ret
    
main:
    pusha
    mov bx, 1
    mov ax, 1
    mov dl, [bdv]
    call lbaRead
    popa
    
    mov ah, 0x0E
    mov al, [0x7E00]
    int 0x10
    jmp $

err:
    mov ah, 0x0B
    mov bl, 0x4
    int 0x10
    jmp $

; Pad to 512 bytes and add 0x55AA cap
times 510-($-$$) db 0
db 0x55, 0xaa

内容的提问来源于stack exchange,提问作者Heavy Weapons Guy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 19:52:49