Assembly Program to print a String in Reverse Order


This program will print the entered string by the user in reverse order. The basic idea in the program is that this push every new character entered by the user onto the stack and count the number of characters. When the user ends the string by pressing enter the program start popping the characters from the stack and print it one by one.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB "ENTER A STRING: $"
MSG2 DB 0DH, 0AH, "REVERSED STRING: $"
.CODE
MAIN PROC
    
    MOV AX, @DATA
    MOV DS, AX
    
    MOV AH, 9
    LEA DX, MSG1
    INT 21H
     
    XOR CX, CX
    
    MOV AH, 1
    READ:
    INT 21H 
    CMP AL, 0DH 
    JE ENDOFSTRING
    INC CX
    MOV DL, AL 
    MOV DH, 0H
    PUSH DX
    JMP READ 
     
    ENDOFSTRING:
    MOV AH, 9
    LEA DX, MSG2
    INT 21H
    
    PRINT:
    MOV AH, 2
    POP DX
    INT 21H
    DEC CX
    CMP CX, 0
    JNE PRINT

    MAIN ENDP
END MAIN

Output:


No comments