Search code examples
assemblyx86dosx86-16real-mode

Why are this many bytes written to the standard output device?


I have the following data declarations, and the data segment and code segment registers are all correctly initialized:

d1  db 1,2
d2  dw 3
d3  db 'ABC'
d4  db 'DE'
d5  db 'F'
d6  db '$'

I run this set of instructions on DOSbox:

mov dx, offset d2
add dx, 2
mov ah, 9
int 21h

Why is that the standard output device would write 6 bytes? I understand that d2 is a word, so it is 2 bytes of data. But I do not completely understand why there would be an output for 6 bytes?


Solution

  • Your code:

    mov dx, offset d2
    add dx, 2
    mov ah, 9
    int 21h
    

    does the same as:

    mov dx, offset d3 ; offset d3 equals offset d2 + 2, because d2 is a word.
    mov ah, 9
    int 21h
    

    The several data instructions db produce consecutive bytes in memory (here), so this:

    d3  db 'ABC'
    d4  db 'DE'
    d5  db 'F'
    d6  db '$'
    

    is equivalent to the following:

    d3  db 'ABCDEF$' ; assuming you don't need labels d4, d5, d6 somewhere else
    

    So you are passing the string 'ABCDEF$' to int 21h, function AH=9, "Display string", and that prints all characters of the string you pass in DX, up to the final '$'. So it prints

    ABCDEF
    

    as expected.