memory - NASM 内存未被正确访问?

标签 memory assembly x86 nasm bootloader

所以我尝试在实模式下使用 NASM 打印一个简单的 hello world 字符串。正如您可以通过 org 0000:7C00 定义判断的那样,它是一个测试引导加载程序。但是由于某种原因,“Hello World”未正确打印。在 VirtualBox 和真实硬件中尝试过。

运行时,它最终会打印出一堆随机的形状和数字,与真实的字母毫无相似之处,更不用说“Hello World”了。我认为这与我的段寄存器设置不正确有关,因为我注意到围绕 MESSAGE 的定义移动改变了打印出的值。我看了这个问题:

Simple NASM "boot program" not accessing memory correctly?

但我的问题没有答案,我确实将 ds 设置为 0。知道发生了什么事吗?

另外值得注意的是,我正在将它编译成平面二进制文件。它在最后打印“L”的原因是我知道在它工作之前应该打印的所有内容。或者,我想在这种情况下,没有。

BITS 16

org 0x0000:7C00

start:
        mov ax, 0
        mov ds, ax
        mov es, ax
        mov fs, ax
        mov gs, ax

        mov ss, ax ;Puts 0 into the segment pointer, we are using real memory.
        mov sp, 0000:7C00 ;Moves 7C00 into the stack pointer, so that all data <7C00 is stack.

        call print_string ;Calls print string.
        jmp Exit


;Prints the test string for now.
print_string:

        mov si, MESSAGE

    .nextChar:

        mov ah, 0x0E
        mov al, [si]
        cmp al, 0x0
        je .end

        int 10h
        add si, 1
        jmp .nextChar
    .end:
        ret


    MESSAGE db "Hello world!", 0

Exit:
    mov ah, 0x0E
    mov al, 'L'
    int 10h  


times 510-($-$$) db 0   ; Pad remainder of boot sector with 0s
dw 0xAA55      ; The standard PC boot signature

最佳答案

在最后一个 int 10h

之后尝试真正停止程序而不是执行垃圾
Exit:
mov ah, 0x0E
mov al, 'L'
int 10h

EndlessLoop:
jmp EndlessLoop

关于memory - NASM 内存未被正确访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30854247/

相关文章:

operating-system - bios和实模式

c - Valgrind 无效写入大小 8

java - 为 Java jar 分配更多堆空间

linux - Linux下使用amd64的汇编语言程序

c - 两遍汇编程序修复

assembly - 使用 gdb 比较汇编跟踪

assembly - 每个 AT&T 语法操作数访问内存或寄存器中的什么值?

linux - 一个 C hello world 的汇编输出的每一行是什么意思?

python - 易于实现内存高效元组列表的结构

c++ - 如何在 Os X 上用 C++ 获取模块的基地址