3

So I am trying to print a simple hello world string using NASM in real mode. As you might be able to tell by the org 0000:7C00 define, it is a test bootloader. For some reason or another though, 'Hello World' is not being printed correctly. Tried in VirtualBox and real hardware.

When ran, it ends up printing a bunch of random shapes and figures, which has no resemblance to real letters, let alone 'Hello World'. I'm thinking that it has to do with my segment registers not being set up properly, as I noticed that moving around the definition of MESSAGE changed the values that were being printed out. I looked at this question:

Simple NASM "boot program" not accessing memory correctly?

But there were no answers there to my problem, and I do set up ds to be 0. Any ideas what's going on?

Also worth noting, i am compiling it into a flat binary. The reason why it prints 'L' at the end is so I know that everything that was supposed to print before it worked. Or, I guess in this case, didn't.

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
Community
  • 1
  • 1
  • `org 0x0000:7C00` isn't valid syntax, although nasm doesn't complain. What you want is `org 0x7c00`. Also `mov sp, 0000:7C00` isn't valid either and at least my nasm **does** complain about that. You want `mov sp, 0x7c00` there. – Jester Jun 15 '15 at 20:28
  • Ah yes, the thing is that I'm writing this off the top of my head because I'm away from the source code at the moment. I know that my NASM doesn't complain at all, so it's likely that that's what the real source code says. I'll edit it now. (Still doesn't solve the problem I'm having, but thanks for the feedback!) – Jeremy Hahn Jun 15 '15 at 20:34
  • The updated code works here. – Jester Jun 15 '15 at 20:39
  • Yeah, maybe I'll just have to retry it when I return home. I have tried very similar code on my machine earlier though, and I know THAT prints random junk, which means that maybe [MESSAGE] isn't pointing to memory that I think it is. As stated earlier, I did write this code from memory, and I'm fairly certain that this is fundamentally the same as the code at home. (Save for the mistakes you mentioned xD) – Jeremy Hahn Jun 15 '15 at 20:43
  • possible duplicate of [Print a number in NASM - building an x86 Bootsector](http://stackoverflow.com/questions/30764183/print-a-number-in-nasm-building-an-x86-bootsector) – David Hoelzer Jun 16 '15 at 00:24
  • Doing what you are asking is covered in the answers to http://stackoverflow.com/questions/30764183/print-a-number-in-nasm/30770127#30770127 – David Hoelzer Jun 16 '15 at 00:25
  • Never mind guys, I solved it. – Jeremy Hahn Jun 16 '15 at 13:28
  • GAS version: http://stackoverflow.com/questions/32508919/how-to-produce-a-minimal-bios-hello-world-boot-sector-with-gcc-that-works-from-a and a repository with working examples: https://github.com/cirosantilli/x86-bare-metal-examples – Ciro Santilli OurBigBook.com Sep 11 '15 at 09:10

1 Answers1

1

Try this to really stop the program in stead of executing garbage after the last int 10h

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

EndlessLoop:
jmp EndlessLoop
Fifoernik
  • 9,779
  • 1
  • 21
  • 27