(nasm x86 real mode) How to write/read strings in boot-loaded sector?

旧街凉风 提交于 2019-12-24 05:38:12

问题


I'm using NASM to write a minimal OS for x86 real mode for educational purposes. I want to use the 512-byte boot sector to load a larger sector that contains the rest of the OS. I've successfully created a boot sector that loads another sector, but I cannot seem to write/read strings within the loaded sector. Here is my code:

    bits 16

    mov ax, 0x7c0
    mov ds, ax

    jmp code

    ;; Write bootStr to boot sector.
    bootStr db "AAA"

code:   

    ;; for int 0x10
    mov ah, 0x0e

    ;; Print first char of bootStr.
    mov di, bootStr
    mov BYTE al, [di]
    int 0x10    ; prints A

    ;; Load next sector.
    ;; adapted from:
    ;; https://blog.benjojo.co.uk/post/interactive-x86-bootloader-tutorial
    mov ah, 0x02
    mov al, 1   
    mov ch, 0    
    mov cl, 2    
    mov dh, 0   
    mov bx, new 
    mov es, bx  
    xor bx, bx
    int 0x13
    jmp new:0

    new equ 0x0500

    ;; Pad boot sector.
    times 510-($-$$) db 0 
    db 0x55
    db 0xaa

nextSector: 

    ;; for int 0x10
    mov ah, 0x0e

    ;; Try to print first char of nextStr (defined below).
    mov di, nextStr
    mov BYTE al, [di]
    int 0x10    ; should print B

    ;; Move 'C' into nextStr and print it.
    mov BYTE [di], 'C'
    mov BYTE al, [di]
    int 0x10    ; prints C

    ;; Print first char of bootStr again.
    mov di, bootStr
    mov BYTE al, [di]
    int 0x10    ; prints A

    hlt

    nextStr db "BBB"

When I run (on Debian Stretch):

nasm -f bin -o boot.bin boot.asm
qemu-system-x86_64 boot.bin

I get:

Booting from Hard Disk...
A CA

So it looks like some invisible character is printed where I expect to see B.

What I don't understand is why, from the loaded sector, I can write characters to nextStr with mov BYTE [di] 'C' and then print them, but I can't seem to define nextStr with db and then print its characters. After several hours I've had no luck fixing the issue.

Questions:

  • Is nextStr never written with db in the first place, or am I simply failing to read/print it?
  • How should I fix this so that I can write/read/print strings within the loaded sector?

来源:https://stackoverflow.com/questions/52461308/nasm-x86-real-mode-how-to-write-read-strings-in-boot-loaded-sector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!