Stack - Reversing a String
The concept for reversing a string using a stack is to remember that the last item to be inserted will always be the first item to be removed (LIFO). We would just simply, push each character of the string into the stack starting from the beginning to the very last of the string then popping it out of the stack starting from the last item inserted then displaying the item.
Here is the sample code:
For the Data Segment:
STR1 DB 30, 31 DUP('$')
STR2 DB 0AH, 0DH, '$'
The pushing of the elements to the stack:
LEA DX, STR1
MOV CL, STR1[1]
MOV SI, DX
ADD SI, 02H
A: MOV DL, [SI]
PUSH DX
INC SI
LOOP A
The popping and displaying of each element from the stack:
MOV CL, STR1[1]
B: MOV AH, 02H
POP DX
INT 21H
LOOP B
0 comments:
Post a Comment