assembly - 简单的 NASM "boot program"没有正确访问内存?

标签 assembly memory-management nasm bootloader

**请注意,当我说启动程序时,并不是指启动操作系统的程序。我的意思是,当您启动计算机并执行某些操作时运行的简单程序。

好吧,所以我不是非常精通汇编/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)。我不能让这该死的东西工作。

现在,对于规范(因为它们可能是相关的):
  • 我正在运行带有 Intel Pentium II 处理器的 Dell Latitude CPi,因为这就是我要测试的全部内容(我不是用我的普通计算机测试汇编程序。 hell 没有。)。我很确定说处理器是 x86,因为我已经在它上面运行了 Windows XP、Ubuntu 和 Arch Linux。
  • 我目前正在使用 NASM 在 Arch Linux 上编写和编译程序。
  • bootstrap 从软盘运行
  • 我使用' nasm -f bin 文件名 ' 来编译代码。
  • 然后,我使用 AL 的“mtools”包中的“mformat”命令通过“”将编译后的 bootstrap 传输到软盘。 mformat -f 1440 -B 启动程序 A: '。

  • 那么,这次我搞砸了什么?还是我的处理器/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/

    相关文章:

    c - 围栏密码算法c

    assembly - 无法在 INT 10h/AH = 0Ch 的情况下在 Y 轴上绘制像素

    c - elf .rel.text 部分 R_386_32/R_386_PC32 的含义

    xcode - 如何在 OS X 中使用 masm 编译并运行汇编代码?

    c - 遵循结构指针

    c - 将变量传递给 nasm 过程

    java - 跨编程语言的动态内存分配

    java - 为什么在 Android 中使用 Activity 的简单示例会导致内存泄漏?

    linux - 组装中的强项

    linux - 如何更改 gcc 生成的 asm 代码的入口点?