assemblynasmx86-16bootloaderbios

assembly program to print string to screen in a 16-bit bootloader


I have this program which I want to write the string to the screen, but the output is empty, I have added comments at every line.

[org 0x7c00] ; Tell the assembler the place to load this code

    mov bx, HELLO_MSG ; move the data to the bx register
    pusha             ; push all register values to stack?
    call printstr     ; call printstr routine

printstr:
    mov ah, 0x0e ; BIOS teletype output
    pop bx       ; pop to bx
    mov al, bl   ; put low byte of bx to al
    int 0x10     ; print it.
    cmp al, 0    ; Compare if its a string termination character?
    jne printstr ; if not go back to top
    ret          ; return to the caller?


; Data
HELLO_MSG:
    db 'Hello world!', 0

jmp $

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

I am running this program from the bootsector of a disk in qemu emulating an AT-compatible x86 based computer.


Solution

  • The POP BX instruction does not do what you seem to assume. It takes a value from the stack and stores it in BX. It does not access the memory that BX points to. You need something like MOV AL, [BX] to retrieve a character pointed to by BX, and then an instruction to make BX point to the next character.

    Also, you don't properly terminate your program. CALL saves the current position and then starts printing the string. When you RETurn, the processor continues execution after the CALL instruction. You need something to tell the processor to stop executing stuff, or it might try to print some more things it shouldn't print.

    As you mentioned in a comment, you use a boot sector in qemu. There seems to be no concept of telling qemu to exit, so you should block in your program. A typical way to achieve that is the sequence of the instruction CLI to tell the processor that it should not handle any interrupts, followed by the instruction HLT, which tells the processor to not do anything until it handled an interrupt. As you blocked interrupts, the HLT instruction will wait forever.