assemblyx86nasminterruptbios

Getting a string to print via bios interrupt


I'm learning assembly and of course I'm experimenting with the classic 0x10 bios interrupt. The resources I've looked at show using lodsb to print a string, so to understand that opcode I'm trying to mimic it's behavior. This works fine with lodsb, but not with what I have. What am I doing wrong?:

start:
    mov ah, 0Eh ;for bios interrupt
    mov si, text_string ;set source index to begining of text_string

.repeat:
    ;I'm trying to emulate the behavior of lodsb to learn how it works:
    mov al, [si] ;put character at si in al register
    add si, 1 ;increment source index

    cmp al, 0 ;if the character is a zero (end of the string)
    je done ;end execution

    int 10h ;bios interrupt to put character on screen
    jmp .repeat

    text_string db 'Hello, World!', 0

done:
    ret

Solution

  • It's not usually a good idea to:

    1. Assume that all registers are preserved across interrupt calls (or any calls, really); or
    2. Not set up all the required registers for a given call.

    On that first note, I would set ah to 0eh immediately before the int 10h.

    Int 10h/0eh requires the bh and bl be set to the page number and foreground color respectively. I would also do that immediately before the int 10h to ensure they're set correctly for the call.


    As an aside, you may need to ensure that the ds segment register is correct, with something like:

    push cs
    pop ds
    

    That's because it looks like you're putting the string into your code segment. However, since the lodsb version apparently works, I assume you've got that covered (such as if this code is a com file rather than an exe file).

    And, from (admittedly faded) memory, 8086 has an inc si instruction that might save a small amount of space, not that it matters that much anymore but I was forged in a time when every byte counted :-)