程序集 如果我想切换值,我应该如何增加数组?

Assembly How should I increment the array if I want to switch values?

这是使用 Kip Irvine 在 Visual Studios 2015 上编译的。

代码应该将第一个元素与第二个元素切换,将第三个元素与第四个元素切换,依此类推。它向前切换值而不是仅仅切换两个值。我添加了带 2 的索引寄存器以跳过第二个元素,因为它不应该被切换。有什么我想念的吗?我应该以不同的方式递增数组索引还是将错误的值放入错误的寄存器中?提前致谢!请不要只给我答案。

输出为

Dump of offset 00AD6880


00020000 00050000 00090000 0000000A 0000000C

INCLUDE Irvine32.inc
.data
dwarray dword  0,2,5,9,10,12


.code
main proc
mov ebx, OFFSET dwarray
mov ecx, LENGTHOF dwarray
L1: cmp ebx, ecx
mov eax, [ebx]
mov edx, [ebx+1]
mov [ebx+1], eax 
mov [ebx], edx
add ebx, 2
loop L1

; The four instructions below are fixed, the only variable is the name of the array  
mov    esi,OFFSET dwarray
mov    ecx,LENGTHOF dwarray
mov    ebx,TYPE dwarray
call   DumpMem
call WaitMsg
exit
main ENDP
END main

你的数组元素是dword,双字,即4字节。因此,为了指向您需要将指针增加 4 的元素:

dwarray dword  0,2,5,9,10,12
.code
main proc
mov ebx, OFFSET dwarray
mov ecx, 3                    ◄■■■ THE ARRAY CONTAINS 6 ELEMENTS, BUT THEY ARE PROCESSED
                                   IN PAIRS, SO THE LOOP SHOULD REPEAT HALF 6 (3).
L1: ;cmp ebx, ecx             ◄■■■ UNNECESSARY?
mov eax, [ebx]
mov edx, [ebx+4]              ◄■■■ THE NEXT ELEMENT IS 4 BYTES AWAY.
mov [ebx+4], eax              ◄■■■ AGAIN, THE NEXT ELEMENT IS 4 BYTES.
mov [ebx], edx
add ebx, 8                    ◄■■■ INCREASE BY 8 (THE SIZE OF 2 ELEMENTS PROCESSED).
loop L1