user10335564
user10335564

Reputation: 143

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?

Upvotes: 3

Views: 182

Answers (2)

Rudy Velthuis
Rudy Velthuis

Reputation: 28806

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.

Upvotes: 4

Michael Petch
Michael Petch

Reputation: 47573

You move the offset of d2 into DX. That points at the two bytes starting at dw 3. 2 is then added to DX, thus DX now points just past the 2 byte word which happens to be the start of d3. Int 21/ah=9 will print characters up to (and not including the $) starting at the offset in DX. The characters starting at offset d3 (and ending at the $) should be printed. That output should be ABCDEF which are the 6 characters you should have seen displayed.

Upvotes: 3

Related Questions