Chapter 3
Assembly Language Fundamentals
Assembly Language Programming Exercise
Problem # 7:
Write a program with a loop and indirect addressing that copies a string from source to target, reversing the character order in the process. Use the following variables:
source BYTE "This is the source string",0
target BYTE SIZEOF source DUP('#')
Solution:
.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD
.data
source BYTE "This is the source string",0
target BYTE SIZEOF source DUP('#')
.code
main PROC
mov esi,0
mov edi,LENGTHOF source - 1
mov ecx,SIZEOF source
L1:
mov eax, 0
mov al,source[esi]
mov target[edi],al
inc esi
dec edi
loop L1
INVOKE ExitProcess,0
main ENDP
END main
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD
.data
source BYTE "This is the source string",0
target BYTE SIZEOF source DUP('#')
.code
main PROC
mov esi,0
mov edi,LENGTHOF source - 1
mov ecx,SIZEOF source
L1:
mov eax, 0
mov al,source[esi]
mov target[edi],al
inc esi
dec edi
loop L1
INVOKE ExitProcess,0
main ENDP
END main
Previous Post:
Program to Reverse an Array in Assembly Language using Visual Studio
hi, thank you for this tutorial.
ReplyDeleteI'd like to know how to display source and target strings.
You can use WriteString function from Irvine32 library as follows:
Deletemov edx,OFFSET source
call WriteString
mov edx,OFFSET target
call WriteString
Please subscribe and share the blog if you like it.
The second line of code within the loop should be "mov al, source[edi]" not "mov al, source[esi]".
ReplyDeleteits -2 not -1
ReplyDeleteWhy is it -2 instead of -1?
Delete