The problem is to swap every two variables in the array. Conditionals are not allowed.
Input: 1,2,3,4,5,6,7,8
Output: 2,1,4,3,6,5,8,7
Here's my code:
.data
arr BYTE 1,2,3,4,5,6,7,8
counter DWORD 0
.code
main proc
    xor eax, eax
    mov eax, LENGTHOF arr ; calculate length of array
    mov counter, eax
    xor eax, eax
    xor esi, esi 
    mov esi, OFFSET arr ; now esi contains address of arr
    xor ecx, ecx
    ; set ECX counter for Loop according to ECX
    mov ecx, counter
    mov counter, 0
    L1:
        xor eax, eax
        xor ebx, ebx
        mov eax, [esi]
        inc esi
        mov ebx, [esi]
        dec esi
        mov [esi], ebx
        inc esi
        mov [esi], eax
        inc esi
        dec ecx
    loop    L1
    invoke ExitProcess,0
Now instead of the expected output, I got some gibberish so I debugged the first swap and found something strange:
What should have essentially been 2,2,3,4,5,6,7,8, seems like has shifted a place instead!
Why is this behaviour? How to fix it?


