我最近开始了一个编写操作系统的项目,直到“磁盘读取”为止进展顺利。
代码应该加载下一个扇区并打印字母“H”,该字母存储在引导加载程序之外的字节中,位于BIOS默认加载的55AA位签名之外。
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
代码应该打印一个单独的“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
事实证明整个程序完全是一团糟 值得庆幸的是,命令中的 Micheal Petch 决定重写整个内容
org 0x7C00
bits 16
mov ax, 0
mov es, ax
mov ds, ax
mov ss, ax
mov sp, 0x7c00 ; Stack will grow down below the bootloader.
mov [bdv], dl
jmp main
; lbaRead
; Reads sectors using LBA
; bx: LBA Address
; ax: Number of sectors
; dl: Drive number
lbaRead:
; Save AX and BX
mov [snm], ax
push bx
; Read the Disk Geometry
mov ah, 8
mov di, 0
int 0x13
; Error Handling
jc err
; continue
; Int 0x13/ah=8 can set ES to non zero segment
; set ES back to 0
mov ax, 0
mov es, ax
inc dh
mov [hct], dh
and cl, 0x3F
mov [spt], cl
; Restore BX clobbered by previous int 13h
pop bx
; Convert LBA to CHS
; Calculate temporary value (tmp) and sector
mov dx, 0
mov ax, bx
mov bx, [spt]
div bx
inc dl
mov [sct], dl
; Calculate Head and Cylinder
mov dx, 0
mov bx, [hct]
div bx
mov [hed], dl
mov [cyl], ax
; Read the disk
mov ah, 0x02
mov al, [snm]
mov ch, [cyl]
mov cl, [sct]
mov dh, [hed]
mov dl, [bdv]
mov bx, 0x7E00
int 0x13
; Error handling
jc err
ret
main:
pusha
mov bx, 1
mov ax, 1
call lbaRead
popa
mov ah, 0x0E
mov al, [0x7E00]
int 0x10
jmp $
err:
mov ah, 0x0B
mov bl, 0x4
int 0x10
jmp $
; Variables for CHS conversion
tmp: dw 0
sct: db 0
hed: db 0
cyl: dw 0
spt: dw 0
hct: dw 0
bdv: db 0
snm: dw 0
; Pad to 512 bytes and add 0x55AA cap
times 510-($-$$) db 0
db 0x55, 0xaa