7. Program to Copy a String in Reverse Order in Assembly Language using Visual Studio

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

Let me know in the comment section if you have any question.

Previous Post:
Program to Reverse an Array in Assembly Language using Visual Studio

Comments

  1. hi, thank you for this tutorial.

    I'd like to know how to display source and target strings.

    ReplyDelete
    Replies
    1. You can use WriteString function from Irvine32 library as follows:
      mov edx,OFFSET source
      call WriteString
      mov edx,OFFSET target
      call WriteString

      Please subscribe and share the blog if you like it.

      Delete
  2. The second line of code within the loop should be "mov al, source[edi]" not "mov al, source[esi]".

    ReplyDelete

Post a Comment