【发布时间】:2020-12-05 14:22:24
【问题描述】:
我正在尝试开发一个基本的引导加载程序,但是当我尝试创建一个用于从硬盘驱动器读取其他扇区的函数时遇到了问题。我正在 NASM 中的 Kali Linux 上开发它,并使用 QEMU 作为我的模拟器。 这是我的主要引导加载程序文件:
[org 0x7c00]
mov bp, 0x8000
mov sp, bp
call read_disk
mov si, my_string
call print ;prints a string, si points to the string to be printed
jmp $
read_disk
mov ah, 0x02 ;read from disk
mov al, 0x01 ;read one sector
mov ch, 0x00 ;read from cylinder 0
mov dh, 0x00 ;read from head 0
mov cl, 0x02 ;read the second sector
mov bx, 0
mov es, bx
mov bx, 0x7c00+512
int 0x13
jc disk_error ;BIOS sets the carry flag if disk read was unsuccessful
ret
disk_error:
mov si, error_msg
call print
jmp $
;
;Functions
;
%include "functions/print.asm"
%include "functions/print_hex.asm"
%include "functions/print_nl.asm"
%include "functions/calc_len.asm"
%include "functions/find_string.asm"
;
;Data
;
error_msg:
db 'Error reading disk', 0
times 510-($-$$) db 0 ;pad out the rest of the bootloader with zeros to increase the size to 512 bytes
dw 0xaa55 ;Magic bytes so BIOS recognizes the hard drive as bootable
;
;SECOND SECTOR
;
my_string:
db 'Disk read successful', 0
times 512 db 0 ;need to pad out the rest of the sector with zeros since QEMU requires it
如您所见,my_string 位于 512 字节之后,在模拟硬盘的第二个扇区中。但是当我编译并运行引导加载程序时,它不会输出任何东西。在我上面提供的代码中,我在read_disk 函数结束后打印my_string 。但奇怪的是,如果我移动打印my_string inside 函数的两行,它就可以工作。
这是有效的代码:
[org 0x7c00]
mov bp, 0x8000
mov sp, bp
call read_disk
jmp $
read_disk
mov ah, 0x02 ;read from disk
mov al, 0x01 ;read one sector
mov ch, 0x00 ;read from cylinder 0
mov dh, 0x00 ;read from head 0
mov cl, 0x02 ;read the second sector
mov bx, 0
mov es, bx
mov bx, 0x7c00+512
int 0x13
jc disk_error ;BIOS sets the carry flag if disk read was unsuccessful
mov si, my_string
call print ;prints a string, si points to the string to be printed
ret
disk_error:
mov si, error_msg
call print
jmp $
;
;Functions
;
%include "functions/print.asm"
%include "functions/print_hex.asm"
%include "functions/print_nl.asm"
%include "functions/calc_len.asm"
%include "functions/find_string.asm"
;
;Data
;
error_msg:
db 'Error reading disk', 0
times 510-($-$$) db 0 ;pad out the rest of the bootloader with zeros to increase the size to 512 bytes
dw 0xaa55 ;Magic bytes so BIOS recognizes the hard drive as bootable
;
;SECOND SECTOR
;
my_string:
db 'Disk read successful', 0
times 512 db 0 ;need to pad out the rest of the sector with zeros since QEMU requires it
如果有人能向我解释这个奇怪的怪事,我将不胜感激。
【问题讨论】:
-
QEMU 允许您附加调试器,如 GDB;用它来查看执行的去向。这是使用模拟器的一半。它会显示
ret会去你不想要的地方,给正确的方向一个很大的提示。
标签: assembly x86 nasm bootloader osdev