Question

I have the following program that would print from A to Z with a space in between them. In the following program I understood the rest of the code but didn't understand why the PUSH DX and POP DX instructions are used. If I run the code without using PUSH DX and POP DX, it would just print "!" instead of the characters.

.model small
.stack
.data
    VAL DB 'A'

.code
    MAIN PROC
        SPACE MACRO
            MOV DL, ' '
            MOV AH, 02h;
            INT 21H    
        ENDM
        
        MOV AX, @DATA
        MOV DS, AX
        
        MOV CL, 26
        MOV DL, 65 ; MOV DL, VAL
        
        PRINT:
            MOV AH, 02H
            INT 21H
            PUSH DX
            SPACE
            POP DX
            INC DL
            DEC CL
            JNZ PRINT
            
        MOV AH, 4CH
        INT 21H        
            
    MAIN ENDP
    END MAIN 
Was it helpful?

Solution

The DX register starts out loaded with 65, the ASCII code for A, the DH and DL registers are the upper and lower half, respectively of DX.

The SPACE macro loads a 32 (the ASCII code for space) into DL, overwriting whatever is there. The PUSH DX POP DX saves and restores the state of the register while the space in-between characters is printed. As to why you see !, that is because without restoring DX after the space has been printed, you will simply increment 32 to 33 and print that character.

OTHER TIPS

In SPACE You set DL to space character, but in main loop You use DL as the A to Z chars. Expansion of SPACE overwrites DL, so You store it to the stack before it and retreive it back after the SPACE.

.MODEL SMALL
.STACK 100H
.DATA

MAIN PROC 

    MOV BL,65
    MOV CL,26


PRINT:

    MOV AH,2
    MOV DL,BL
    INC BL
    DEC CL
    INT 21H


    MOV DL,0DH
    INT 21H
    MOV DL,0AH
    INT 21H

 JNZ PRINT

  MOV AH,4CH
  INT 21H

MAIN ENDP
END MAIN
org 100h

.model small
.data 
     msg1 db 13,10, " Print A to Z characters: $"
     
.code 
main proc
    
    mov ax,@data
    mov dx,ax
    
    mov dx,offset msg1
    
    mov ah,9
    int 21h
    
 mov cx,25
mov dx,65
mov ah,2
int 21h
labelname:

 
   inc dx
mov ah,2
int 21h  

loop labelname  
               
  mov ah,4ch
int 21h             


main endp
end main

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top