【问题标题】:Memory referencing and use of labels in an assembly program loaded by BIOS to memory as the boot sectorBIOS 加载到内存作为引导扇区的汇编程序中的内存引用和标签使用
【发布时间】:2017-08-06 11:43:46
【问题描述】:

我正在学习关于操作系统开发的book by Nick Blundell。它在第 3 章中有一个示例。引导扇区编程(在 16 位实模式下),如下所示:

;
; 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

书中提到:

如果我们运行程序,我们会看到只有后两次尝试打印成功 一个“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 ]

现在输出如预期:

【问题讨论】:

    标签: assembly x86 nasm bios


    【解决方案1】:

    最后一个使用的是固定地址。由于您的代码是如何组装的,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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      • 1970-01-01
      • 2015-08-22
      • 2012-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多