I'd very much appreciate any input or guidance with a problem I've been stuck on for awhile. I am asked to write a program that will use a loop and indirect addressing that switches the "1st" and "2nd" elements, "3rd" and "4th" elements, "5th" and "6th" elements of an array, and so on. The array is a WORD array that has the decimal values 4, 9, 13, 7, 5, 12, 17, 2, 15, 8. You then simply output the contents of the array to prove you've done so.
I will attach my current code, but let me try to quickly elaborate my own logic and reasoning, I am using the LENGTHOF the array divided by two as the loop counter because I am trying to work with the array in pairs. I am then essentially trying to use registers to load the values in before I try swapping them.
INCLUDE Irvine32.Inc
.data
; Creating the WORD array "warray" with necessary decimal values.
warray WORD 4, 9, 13, 7, 5, 12, 17, 2, 15, 8
.code
main PROC
; Clear the registers that are going to be used.
xor ECX, ECX
xor ESI, ESI
xor EDI, EDI
xor EDX, EDX
; Preparing the loop and then executing.
mov ECX, LENGTHOF warray / 2
; Because the numbers are processed in "pairs"
mov ESI, OFFSET warray
myLoop :
; Saving the "first" swap element in the EDX register.
mov EDX, [ESI]
; Saving the "second" swap element in the EDI register.
mov EDI, [ESI + 2]
; Swapping those elements now.
mov[ESI], EDI
mov[ESI + 2], EDX
; Incrementing ESI now to go through with the next "pair".
add ESI, 4
LOOP myLoop
; Printing outputs of array.
mov ESI, OFFSET warray
mov ECX, LENGTHOF warray
mov EBX, TYPE warray
call DumpMem
I currently have the program "half-working" you could say because the swapping of my pairs is working for one of the elements in each respective pair. My output is currently...
Dump of offset 00406000
-------------------------------
0009 0004 0007 0009 000C 0007 0002 000C 0008 0002
So it appears to be breaking after the first pair with regards to the "2nd" element of the new pairs being incorrect. I should get ...
9 4 7 13 12 5 2 17 8 15
and I currently have ...
9 4 7 9 2 12 7 2 12 8 2
I would appreciate any feedback and I definitely am open to me perhaps making a very apparent mistake that I just don't understand.