Failed to access NASM storage correctly?

So I'm trying to print a simple hello world string using NASM in real mode. As you can define with org 0000: 7C00 define, this is a test bootloader. For one reason or another, "Hello World" is not printed correctly. Tried it in VirtualBox and real hardware.

When launched, it ends up printing a bunch of random shapes and shapes that bear no resemblance to real letters, let alone "Hello World". I think it has to do with my segment registers not being set up properly as I noticed that moving over the MESSAGE definition changed the values ​​that were printed. I looked at this question:

NASM's simple boot program "Isn't it correct to access memory?

But I didn't have any answers to my problem and I set ds to 0. Any ideas what's going on?

It's also worth noting that I am compiling it into a flat binary. The reason it prints an "L" at the end is because I know that everything that should have printed before it worked. Or, I guess in this case it was not.

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

      

+3


source to share


1 answer


Try this to actually stop the program instead of running garbage after the last int 10h



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

EndlessLoop:
jmp EndlessLoop

      

+1


source







All Articles