• Uncategorized

About assembly : In-NASM-labels-next-to-each-other-in-memory-are-printing-both-strings-instead-of-first-one

Question Detail

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


  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:                   ;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


Question Answer

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.