Alright, Im going about this, with what is probably a really complicated solution, but its the first thing that popped into my head.
I need to write an assembly language program that reverses a “source” string, without using a “target” string ( a temp variable ) .. this is my attempt at it.
INCLUDE Irvine32.inc
.data
source BYTE "This is the source string", 0
count DWORD ?
.code
main PROC
mov ecx, LENGTHOF source
L1:
mov count, ecx ; save our outer loop count
mov al,[source+0] ; get the first character in the string
mov ecx,LENGTHOF source ; set out inner loop count
mov esi, OFFSET source
inc esi
L2:
mov bl,[esi]
dec esi
mov [esi],bl
add esi, 2
loop L2
mov ecx, count
loop L1
mov edx,OFFSET source
call WriteString
exit
main ENDP
END main
Now.. the “algorithm” for this is basically this: Take the first character out of the string, move all of the other characters down one space in the character array, put the character you first took out into the back of the array. Now, I’m to the point where this is much too complicated. Infact, how do I get to the back of the array.. I think I would need another loop? I certainly don’t need three loops or even want to deal with that.
Maybe I’m on the right track and don’t even know it. any suggestions, tips, code or a different algorithm would help!
You could do it your way with two loops. After doing your first loop, you then have to do it again but with the length reduced by one, so that the current first (originally second) character gets put at the second-last position, leaving the current last (originally first) character alone. Then keep going until the length drops to zero.
But that’s pretty inefficient since you have the nested loops, O(n2). Here’s a better algorithm that uses just one loop, O(n):
It basically maintains a start and end pointer which initially swaps the first and last character, then the pointers are moved towards each other.
So the second time through the loop, you swap the second and second-last character, the third time the third and third-last character and so on.
This process stops when the pointers are equal (for an odd-length string) or the start pointer is greater than the end pointer (for an even-length string).
Since this may be homework (and you seem up to speed with x86 anyway), you should perform the exercise of converting that to assembler.
If it turns out not to be homework, you can use the masm32 code below as a baseline. Please don’t try to pass this off as your own work for education since you’ll almost certainly be caught out. You’ll learn a great deal more (as homework or non-homework) if you tackle the conversion yourself, I’m just providing some fall-back code if you have trouble nutting it out.