BIOS 加载到内存作为引导扇区的汇编程序中的内存引用和标签使用

Memory referencing and use of labels in an assembly program loaded by BIOS to memory as the boot sector

我正在研究 book by Nick Blundell OS 开发。它在 CHAPTER 3. BOOT SECTOR PROGRAMMING (IN 16-BIT REAL MODE) 中有一个示例,如下所示:

;
; A simple boot sector program that demonstrates addressing.
;
mov ah, 0x0e ; int 10/ ah = 0eh -> scrolling teletype BIOS routine

; First attempt
mov al, the_secret
int 0x10 ; Does this print an X?

; Second attempt
mov al, [the_secret]
int 0x10 ; Does this print an X?

; Third attempt
mov bx, the_secret
add bx, 0x7c00
mov al, [bx]
int 0x10 ; Does this print an X?

; Fourth attempt
mov al, [0x7c1e ]
int 0x10 ; Does this print an X?

jmp $ ; Jump forever.

the_secret :
        db "X" 

; Padding and magic BIOS number.
times 510 -( $ - $$ ) db 0

dw 0xaa55

书中提到:

If we run the program we see that only the second two attempts succeed in printing an ’X’.

嗯,这不是我得到的结果:

$ nasm BOOK_EXAMPLE.asm -f bin -o BOOK_EXAMPLE.bin
$ qemu-system-i386 BOOK_EXAMPLE.bin

我的结果是这样的:

暗示我的 QEMU BIOS 仅在第三次尝试时打印 "X"。我没有修改书上的例子,我想知道我错过了什么。


更新

我的二进制代码的对象转储显示 "X" 的十六进制 ASCII 代码,即 0x58 位于内存地址 0x7c00+0x001d=0x7c1d

$ od -t x1 -A x BOOK_EXAMPLE.bin 
000000 b4 0e b0 1d cd 10 a0 1d 00 cd 10 bb 1d 00 81 c3
000010 00 7c 8a 07 cd 10 a0 1e 7c cd 10 eb fe 58 00 00
000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
0001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa
000200

因此我用以下代码替换了一行代码:

mov al, [0x7c1d ]

现在输出符合预期:

最后一个正在使用的是固定地址。由于您的代码是如何组装的,X 可能不会在该地址结束。您可以使用 -l 选项到 nasm 来索取列表文件,然后自己查看。在这里,我得到:

23                                  the_secret :
24 0000001D 58                              db "X" 

您可以看到它的地址 0x1d 而不是 0x1e。本书的汇编程序可能对前面的 jmp $ 使用了 3 字节的跳转,因此存在 1 字节的差异。您可以将其更改为 jmp near $ 然后代码将按预期工作。当然你也可以固定地址。

PS:不初始化 ds 是个坏主意,你永远不知道 bios 是如何设置它的。常见的可能性是 0x7c00,此代码仅适用于后一种情况。如果 ds=0x7c0 那么只有第二个 X 出现。