Write a program that reads a string from the user then prints it reversed. by (a
ID: 3689782 • Letter: W
Question
Write a program that reads a string from the user then prints it reversed. by (assembly language)
Program Structure:
Input: a string.
Output: The string after reversing it.
Definitions: Define a variable to hold the string.
Algorithm: 1- Read string from user.
2- Initialize index by 0
3- Do:
4- Push string[index] in stack
5- Increment index
6- Repeat until string[index]=" "
7- Initialize index by 0 again
8- Do:
9- Pop from stack
10- Store in string[index]
11- Increment index
12- Repeat until stack empty
13- Print string
Sample Run: Please enter a string: computer
The string after reversing it is : retupmoc
Explanation / Answer
prnstring macro msg
mov ah, 09h
lea dx, msg
int 21h
endm
data segment
buffer1 db "Enter the String : $"
buffer2 db 0ah, "The Reversed String is : $"
buffer3 db 41
db 0
db 41 dup(' ')
data ends
stack_string segment stack
db 40 dup(0)
stack_top label word
stack_string ends
code segment
assume cs:code, ds:data
start :
mov ax, data
mov ds, ax
mov es, ax
mov ax, stack_string
mov ss, ax
mov sp, offset stack_top
prnstring buffer1
mov ah, 0ah
lea dx, buffer3
int 21h
mov si, offset buffer3 + 2
mov cl, byte ptr [si-1]
mov ch, 00h
mov ah, 00h
again :
mov al, byte ptr [si]
push ax
inc si
loop again
mov si, offset buffer3 + 2
mov cl, byte ptr [si-1]
mov ah, 00h
next :
pop ax
mov byte ptr [si], al
inc si
loop next
mov byte ptr [si], '$'
prnstring buffer2
prnstring buffer3+2
mov ax, 4c00h
int 21h
code ends
end start
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.