**请注意,当我说启动程序时,并不是指启动操作系统的程序。我的意思是,当您启动计算机并执行某些操作时运行的简单程序。
好吧,所以我不是非常精通汇编/NASM,但我认为我对它有足够的掌握来编写简单的 bootstrap 。
好吧,我以为我掌握了足够的知识。显然不是。
我尝试了一个我在网上找到的简单启动程序。它运行良好(打印字母“A”)。然后我修改它以打印存储在内存中的字母。它失败了;它不是打印“A”,而是打印笑脸。 (我发誓,电脑现在在 mock 我。)
这是源文件中的代码:
[BITS 16] ; We start up in 16-bit real mode
[ORG 0x7C00] ; We're booted into memory at this address. (Or so I'm told)
mov ah, 0x0E ; Teletype command
mov bh, 0x00 ; Page number
mov bl, 0x07 ; Attributes (7 == white foreground, black background)
mov al, [testChar] ; Character to print; load it from the memory referenced by testChar.
int 0x10 ; Tell the BIOS to execute the teletype command.
jmp $ ; Infinite loop prevents us from going off and executing the other junk in memory
testChar db 65 ; This is the character we want to print. 'A'.
; The following code pads the rest of the outputted binary file
; and concludes it with the bootloader signature so I don't have
; to do so manually.
times 510-($-$$) db 0
dw 0xAA55
如果我替换' 移动 al, [testChar] ' 与 ' 移动 al, 65 ',字母 'A' 打印正确。我已经尝试移动内存声明,我已经尝试了 BITS 和 ORG 周围的括号组合或没有括号,并且我已经尝试增加和减少 testChar(即 [testChar+1])。每次,它都会打印笑脸、反笑脸(当我增加 testChar 时)或根本不打印(当我将内存声明放在代码之前,可能是因为没有执行代码 =P)。我不能让这该死的东西工作。
现在,对于规范(因为它们可能是相关的):
那么,这次我搞砸了什么?还是我的处理器/BIOS 有问题?
最佳答案
DS 可能充满了一些垃圾值,所以只需执行以下操作:
push cs
pop ds
或者
mov ax, cs
mov ds, ax
mov es, ax
更好的是,不要相信 CS 并执行以下操作:
xor ax, ax
mov ds, ax
见 this discussion : 某些 BIOS 可能使用 07c0:0000 而不是传统的 0000:7c00,特别是在使用 ElTorito 从 CD-ROM 引导时。
关于assembly - 简单的 NASM "boot program"没有正确访问内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6644881/