Use the XCHG instruction to reorder the integers to 10-8-6-4-2 x86 assembly

2.1k Views Asked by At

I'm trying to swap element 1 with element 5 and element 2 with element 4, in the dword array using xchg instruction. I'm only asking for guidance to help me get started. I have this so far. I'm trying to use these instructions for the dword array.

mov ax,val1
xchg ax,val2
mov val1,ax

.data
myArray BYTE 2,4,6,8,10
DArray DWORD 5 DUP(?)
.code
main PROC
mov esi, OFFSET myArray
mov edi, OFFSET DArray
mov ecx, 5

L1:  movzx eax, byte ptr [esi]
mov [edi], eax
inc esi
add edi, 4
call WriteDec
loop L1
mov al, byte ptr [esi]
xchg al, byte ptr [esi]+4
mov byte ptr[esi], al
call WriteDec
mov ah, byte ptr [esi]+1
xchg ah, byte ptr [esi]+3
mov byte ptr [esi]+1, ah
call WriteDec
1

There are 1 best solutions below

2
On BEST ANSWER

The swap in the byte array lacks the setting of the ESI register.

mov esi, OFFSET myArray     <<< Add this
mov al, byte ptr [esi]
xchg al, byte ptr [esi]+4
mov byte ptr[esi], al
call WriteDec               <<< Is this useful ???
mov ah, byte ptr [esi]+1
xchg ah, byte ptr [esi]+3
mov byte ptr [esi]+1, ah
call WriteDec               <<< Is this useful ???

I'm trying to swap element 1 with element 5 and element 2 with element 4, in the dword array using xchg instruction.

mov  edi, OFFSET DArray
mov  eax, dword ptr [edi]
xchg eax, dword ptr [edi]+16
mov  dword ptr[edi], eax
mov  eax, dword ptr [edi]+4
xchg eax, dword ptr [edi]+12
mov  dword ptr [edi]+4, eax