-6

How can I reverse a string using two separate byte arrays?

Write a program using the LOOP instruction with 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 ('#')
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
sinan
  • 5
  • 2
  • 7
    We're not doing your homework for you. – GAntoine Apr 05 '16 at 15:59
  • 1
    Possible duplicate of [How to reverse a string in assembly language?](http://stackoverflow.com/questions/26368586/how-to-reverse-a-string-in-assembly-language) – akashrajkn Apr 05 '16 at 17:01
  • I agree that this is close to the How to reverse a string, but it is a bit different since it's how to copy it (to a new location) rather than how to output a string backwards. – David Hoelzer Apr 05 '16 at 20:30

1 Answers1

2

The problem is pretty straightforward. Use indirect addressing (in other words, don't use the memory addresses directly but reference them, through a register perhaps) and reverse the string. For example, here are some barebones assuming that you have already defined source and target:

MOV SI, source   ; Get source address
MOV DI, (target + SIZEOF source)   ; Get the ending address for target
LOOP:
  MOV AL, [SI]   ; Get a byte
  MOV [DI], AL   ; Store a byte
  INC SI         ; Move forward one in source
  DEC DI         ; Move back one in target
  CMP AL, 0      ; Have we reached the zero termination?
  JNZ LOOP
RET

This is by no means meant to be complete or functional. You may, for instance, need to figure out a better way to figure out the length of SOURCE dynamically. :) However, I don't want to take the joy of learning away from you. This should be at least a good starting point.

David Hoelzer
  • 15,862
  • 4
  • 48
  • 67