Search code examples
assemblydosx86-16

What does DX + 2 mean in mov ah,9 int 21h?


mov dx, offset buffer
mov ah, 0ah
int 21h
jmp print
buffer db 10,?, 10 dup(' ')
print:
xor bx, bx
mov bl, buffer[1]
mov buffer[bx+2], '$'
mov dx, offset buffer + 2
mov ah, 9
int 21h

I know buffer[bx+2] stands for '$', but offset buffer +2 in mov ah,9 stands for what?

They said,

"Start printing from address DS:DX + 2". From address ds:dx +2.


Solution

  • When a string is captured from keyboard with int 21h, ah=0Ah, the string has next structure:

    enter image description here

    As you can see, the first two bytes are control, the characters entered by user start at the third byte (byte 2). The last char is chr(13) (ENTER key).

    To display this captured string with int 21h, ah=09h, you will have to replace the last chr(13) by '$', then make DX to point to the valid characters that start at the third byte :

    mov dx, offset buff + 2
    

    or this one (both are equivalent):

    mov dx, offset buff
    add dx, 2
    

    The way to replace chr(13) by '$' is explained in next image : notice the length of the captured string is in the second byte (byte 1), we have to add this length to reach the last byte chr(13), now we can replace it:

    enter image description here

    Next is the code :

    .stack 100h
    .data
    msg  db 'Enter text : $'
    text db 11        ;MAX LENGTH ALLOWED.
         db ?         ;LENGTH ENTERED.
         db 11 dup(?) ;CHARACTERES.
    .code
      mov  ax, @data
      mov  ds, ax
    
    ;DISPLAY MESSAGE TO USER.  
      mov  dx, offset msg
      mov  ah, 9
      int  21h
    
    ;CAPTURE TEXT.  
      mov  dx, offset text
      mov  ah, 0Ah
      int  21h            
    
    ;REPLACE ENTER WITH $.
      mov  bl, '$'
      mov  si, offset text + 1  ;◄■ POSITION OF LENGTH ENTERED.
      mov  al, [si]             ;◄■ GET LENGTH ENTERED.
      mov  ah, 0                ;◄■ CLEAR AH TO USE AX.
      add  si, ax               ;◄■ SI POINTS TO LAST CHAR.
      inc  si                   ;◄■ +1 TO POINT TO CHAR 13.
      mov  [si], bl             ;◄■ REPLACE 13 WITH '$'.
    
      mov  ax, 4c00h
      int  21h