5
votes

I am having an issue while programming in NASM. I am learning how to develop an OS purely in assembly and have started by creating a boot loader.

My goal currently is to print "Hello, World!" and "Goodbye!" using the the BIOS interrupt 0x10.

The issue I seem to be having occurs while printing values on the screen. Two labels appear to be next to each other in memory causing printing one string to print the contents of the other string.

Why isn't hlen stopping the loop at the end of the first string?

    [org 0x7c00]

    mov ah, 0x0e

    mov bx, HELLO_MSG
    mov cx, hlen                                                                                                                                              
    call print_string

    mov bx, GOODBYE_MSG
    mov cx, glen                                                                                                                                              
    call print_string

    jmp $

    %include "print_string.asm"


    HELLO_MSG db 'Hello, World!',0
    GOODBYE_MSG db 'Goodbye!',0


    hlen equ $ - HELLO_MSG
    glen equ $ - GOODBYE_MSG

    times 510-($-$$) db 0
    dw 0xaa55

BUGS:

  1. Prints goodbye message twice

    This is due to the HELLO_MSG printing Hello, World! and Goodbye!. I believe this occurs because the Hello_MSG label is right next to the GOODBYE_MSG label in memory

;;;print_string.asm
print_string:                   ;cx = string length                                                                                                                                                          
                                ;bX = string label - memory offset                                                                                                                       
                                ; -- if you want the data at a memory adress use [bx]                                                                                                                        
        mov al, [bx]
        int 0x10

        inc bx

        loop print_string

        ret
1

1 Answers

8
votes

Your computation of hlen includes the string Goodbye! because it comes after the defintion of GOODBYE_MSG. The expression $ - HELLO_MSG is the number of bytes between the label HELLO_MSG and the line where hlen is defined. That is why your first call to print_string prints both messages.

Try this order instead:

HELLO_MSG db 'Hello, World!',0
hlen equ $ - HELLO_MSG

GOODBYE_MSG db 'Goodbye!',0
glen equ $ - GOODBYE_MSG

See How does $ work in NASM, exactly? for more details, including this as an example.