3

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?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
user10335564
  • 133
  • 3
  • 16
  • `mov dx, offset d2 + 2` would be more efficient: make the assembler add at build time instead of emitting an instruction to add at runtime. Anyway, with that add, you're passing a pointer to the end of `d2` so its size is irrelevant to what the string-output DOS function does. – Peter Cordes Mar 13 '19 at 02:49
  • FWIW, `mov dx,offset d2+2` is equivalent to `mov dx,offset d3` in the code above. And `db 'ABC'; db 'DE'; db 'F'; db '$'` (<-- where ; is newline) is equivalent to `db 'ABCDEF$'`, as they are all consecutive bytes in memory. – Rudy Velthuis Mar 13 '19 at 07:31

2 Answers2

4

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.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
3

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.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198