0

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
jedikitty
  • 1
  • 1
  • `mov[ESI], EDI` is a dword store (4 bytes) into your array of word elements (2 bytes). Use `movzx edx, word ptr [esi]` for the loads, and word stores from DX and DI. – Peter Cordes Oct 16 '19 at 19:27
  • Or use `rol dword ptr [esi], 16` instead of word loads/stores. – Peter Cordes Oct 16 '19 at 19:30
  • 1
    @PeterCordes thank you very much! I looked up the documentation for your suggestions and so forth and was able to make sense of it even more. I appreciate it, have a good day. – jedikitty Oct 17 '19 at 17:50
  • Your edit removed the stores which were the actual bug. Dword loads aren't truly a correctness bug (just performance from store-fowarding stalls for later pairs), you still could have just done word stores. More importantly, it removed the best summary of the problem, about breaking after the first pair, making this a worse [mcve]. So I rolled it back. – Peter Cordes Oct 17 '19 at 18:14

0 Answers0