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

NASM合并引导扇区后QEMU无法显示字符串问题求助

问题分析与解决方法

一、命令行拼写错误

你执行的命令中存在多余的英文句号,导致操作的文件不匹配:

  • 错误的合并命令:dd if=./data.bin conv=notrunc oflag=append of=os.bin.(文件名末尾多了点)
  • 错误的QEMU运行命令:qemu-system-x86_64 -hda os.bin.(文件名多了点)

正确命令应为:

# 生成虚拟硬盘文件并写入引导扇区
dd if=./boot.bin of=./os.bin
# 追加第二个扇区内容
dd if=./data.bin conv=notrunc oflag=append of=./os.bin

# 启动模拟器
qemu-system-x86_64 -hda os.bin

二、boot.asm中的磁盘读取参数错误

原代码错误设置了bh=0x0200,但int 0x13(ah=0x02,读扇区功能)的缓冲区地址由es:bx指定,bh仅用于视频输出功能的页码设置。需将mov bh,0x0200改为mov bx,0x0200,确保读取的扇区数据被加载到es:bx = 0x7c00 + 0x200 = 0x7e00地址,与后续mov si,0x200(对应ds:si=0x7c00+0x200=0x7e00)的读取地址匹配。

同时建议增加磁盘读取失败的检查逻辑,便于排查问题:

ORG 0
BITS 16
start:
cli ;disables interrupts

mov ax,0x7c0
mov ds,ax
mov es, ax
mov ax,0x00
mov ss,ax
sti ;enables interrupts

;setup registers to read from disk
mov bx,0x0200  ; 修正:用bx指定缓冲区偏移
mov ah, 0x02
mov al, 0x01
mov ch ,0x00
mov cl,0x02
mov dh,0x00
mov dl,0x80
int 0x13 ;reads disk

jc disk_error ; 新增:CF置位表示读取失败,跳转到错误处理

mov si,0x200
call print
jmp $

; 新增磁盘错误处理
disk_error:
mov si, disk_err_msg
call print
jmp $

disk_err_msg: db "Disk read error!", 0

print:
mov bx,0 ;clears bx register

.loop
lodsb ;load byte at address si into al and increment si
cmp al,0 ;compare value in al to 0
je .done ;is al is 0 , end of string
call print_char ;prints charcatar in al
jmp .loop ; 修正:原代码缺少循环跳转,只会打印第一个字符
.done
ret ;return from print function

print_char:
mov ah,0eh ;move 0eh into ah
int 0x10 ;handles screen operations
ret
times 510 -($-$$) db 0
dw 0xAA55

三、data.asm的冗余引导签名

第二个扇区不是引导扇区,无需添加dw 0xAA55签名。原代码中times 512 -($-$$) db 0已将文件填充至512字节,额外添加签名会导致文件大小超过512字节,破坏扇区结构。

修正后的data.asm:

ORG 0
message: db 'hello world',0
times 512 -($-$$) db 0 ; 填充至512字节

四、验证流程

  1. 重新编译文件:
nasm -f bin boot.asm -o boot.bin
nasm -f bin data.asm -o data.bin
  1. 按正确命令合并虚拟硬盘并启动QEMU,即可正常显示hello world字符串。

内容的提问来源于stack exchange,提问作者nix

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 09:26:05