Write a function in NASM Pentium assembly language(save as reverse. ASM file) to
ID: 3685809 • Letter: W
Question
Write a function in NASM Pentium assembly language(save as reverse.ASM file) to output reverse string from user input.
The main procedure reads the string from the user and pass it to the reverse procedure. The reverse procedure returns the reversed string back to main to be displayed.
my latest attempt:
BUF_LEN EQU 41
%include "io.mac"
.STACK 200H
.DATA
prompt_msg db "Input a string: ",0
output_msg db "The reverse is: ",0
.UPDATA
string resb BUF_LEN
.CODE
.STARTUP
PutStr prompt_msg
GetStr string,BUF_LEN
mov ebx,string
call ReverseString
PutStr output_msg
PutStr eax
nwln
.done:
.EXIT
ReverseString:
push ebp
mov ebp,esp
push ecx
push esi
push edi
mov edi,[ebp+8]
mov esi, edi
push eax
CountLoop:
cmp byte [esi], 0
je CountLoopEnd
inc esi
jmp CountLoop
CountLoopEnd:
mov ecx, esi
sub ecx, edi
cmp ecx, 0
je ReverseStringEnd
dec esi
SwapLoop:
mov al, [esi]
mov ah, [edi]
mov [esi], ah
mov [edi], al
cmp edi, esi
jl SwapLoop
ReverseStringEnd:
pop edi
pop esi
pop ebp
pop ecx
pop eax
ret
I don't know what is my problem. please helpme to fix it :(
Explanation / Answer
DATA SEGMENT
STRING1 DB “PAULINE$”
STRING2 DB 7 DUP (‘$’)
MESSAGE1 DB 10,13,’THE STORED STRING IN THE MEMORY IS : $’
MESSAGE2 DB 10,13,’THE REVERSE STRING IS THE FOLLOWING : $’
DATA ENDS
DISPLAY MACRO MESSAGE
MOV AH,9
LEA DX,MESSAGE
INT 21H
ENDM
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX
DISPLAY MESSAGE1
DISPLAY STRING1
LEA SI,STRING2
LEA DI,STRING1
ADD DI,5
MOV CX,6
REVERSESTRING:
MOV AL,[DI]
MOV [SI],AL
INC SI
DEC DI
LOOP REVERSESTRING
DISPLAY MESSAGE2
DISPLAY STRING2
MOV AH,4CH
INT 21H
CODE ENDS
END START
OUTPUT:
THE STORED STRING IN THE MEMORY IS:PAULINE
THE REVERSE STRING IS THE FOLLOWING:ENILUAP
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.