MASM中交换指定DWORD数组的代码实现问题咨询
Hey there! Let's walk through refining your array swap code to match your requirements and make it more robust.
First: Correct the Array Initialization
Your stated requirement is array1 = [10,9,8,7,6] and array2 = [5,4,3,2,1], but your original .data section had them reversed. Let's fix that first:
.data array1 DWORD 10, 9, 8, 7, 6 ; Matches your target array1 array2 DWORD 5, 4, 3, 2, 1 ; Matches your target array2
Option 1: Complete Your Manual Element Swap
Your initial code was on the right track for swapping elements one by one, but it was incomplete and had some offset mix-ups. Here's the fully working version that swaps every DWORD element between the two arrays:
.code main proc ; Swap first element (array1[0] <-> array2[0]) mov eax, array2 mov edx, array1 mov array1, eax mov array2, edx ; Swap second element (array1[1] <-> array2[1]) mov eax, array2+4 mov edx, array1+4 mov array1+4, eax mov array2+4, edx ; Swap third element (array1[2] <-> array2[2]) mov eax, array2+8 mov edx, array1+8 mov array1+8, eax mov array2+8, edx ; Swap fourth element (array1[3] <-> array2[3]) mov eax, array2+12 mov edx, array1+12 mov array1+12, eax mov array2+12, edx ; Swap fifth element (array1[4] <-> array2[4]) mov eax, array2+16 mov edx, array1+16 mov array1+16, eax mov array2+16, edx ; Exit the program (critical to avoid crashes!) mov eax, 0 ret main endp end main
Quick breakdown of why this works:
- Each DWORD is 4 bytes, so we increment the memory offset by 4 to access the next element.
- We use
eaxandedxas temporary storage registers—since you can't directly swap two memory locations without an intermediate value.
Option 2: Optimize with a Loop (Better for Scalability)
Writing 5 identical blocks of code isn't ideal if you ever need to swap longer arrays. A loop makes your code cleaner, shorter, and easier to maintain:
.data array1 DWORD 10, 9, 8, 7, 6 array2 DWORD 5, 4, 3, 2, 1 elementCount DWORD 5 ; Track number of elements for the loop .code main proc mov ecx, elementCount ; Set loop counter to total elements mov esi, 0 ; Start at offset 0 (first element) swapLoop: ; Load elements from both arrays into registers mov eax, array1[esi] mov edx, array2[esi] ; Store values in the opposite array mov array1[esi], edx mov array2[esi], eax add esi, 4 ; Move to the next DWORD (4 bytes) loop swapLoop ; Decrement ecx, repeat until ecx = 0 ; Exit program mov eax, 0 ret main endp end main
Why this is a better approach:
- If you ever change the array size, you only need to update the
elementCountvariable instead of rewriting multiple swap blocks. - It cuts down on redundant code, making debugging and reading much easier.
Verification of Your Original Logic
Your core idea—swapping elements one by one using registers as temporary storage—is totally valid. You just needed to finish the last swap operation and correct a few offset orderings. This manual approach works perfectly for small fixed-size arrays; the loop version is just a more scalable alternative.
内容的提问来源于stack exchange,提问作者Ryan

