;I\'m suppose to create a simple code in assembly where it would read a string f
ID: 3835971 • Letter: #
Question
;I'm suppose to create a simple code in assembly where it would read a string from the user and swap the left and right of that string till it is completely in reverse order.
; (For Example: "Hello" to "olleH", "abcdef" to "fedcba", etc.)
;This is my code so far, could you let me know to configure the loop or what i'm missing/doing wrong.
INCLUDE Irvine32.inc
.data
MAX = 80
myString BYTE MAX+1 DUP (?)
myLength DWORD ?
numLoops DWORD ?
.code
Main PROC
call Clrscr
mov edx,OFFSET myString ;buffer size -1
mov ecx,MAX
call ReadString
call StrLength ;stores string length in eax
mov myLength,eax
xor edx,edx
mov ebx,2
div ebx
mov numLoops,eax
;ebx is left, edx is right, ecx current
mov ebx,OFFSET myString
mov edx,OFFSET myString
add edx,myLength
dec edx
mov ecx,numLoops
L1:
mov eax,0
mov al,[numLoops]
mov [eax],al
dec numLoops
inc eax
call WriteString
call Crlf
;inc ebx
;dec edx
loop L1
mov numLoops,OFFSET myString
mov ebx, 1
mov ecx, myLength-ebx
call DumpRegs
exit
main ENDP
END main
Explanation / Answer
You can use this code to print the reverse string of the given string.
Data Segment
str1 db 'String_Reverse','$'
strlen1 dw $-str1
strrev db 20 dup(' ')
Data Ends
Code Segment
Assume cs:code, ds:data
Begin:
mov ax, data
mov ds, ax
mov es, ax
mov cx, strlen1
add cx, -2
lea si, str1
lea di, strrev
add si, strlen1
add si, -2
L1:
mov al, [si]
mov [di], al
dec si
inc di
loop L1
mov al, [si]
mov [di], al
inc di
mov dl, '$'
mov [di], dl
Print:
mov ah, 09h
lea dx, strrev
int 21h
Exit:
mov ax, 4c00h
int 21h
Code Ends
End Begin
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.