使用循环交换 array1 和 array2 的第 n 个位置元素
Use a loop to swap the nth position elements from array1 and array2
我目前正在参加装配课程,我有一个家庭作业问题,我想确保它是正确的。我的问题是:
Give an array named array1 with the values 1000h, 2000h, 3000h, 4000h, 5000h and another array named array2 with the values 11111h, 22222h, 33333h, 44444h, 55555h. Use a loop to swap the nth position elements from array1 and array2.
我写了这段代码:
; AddTwo.asm - adds two 32-bit integers.
; Chapter 3 example
.386
.model flat,stdcall
.stack 4096
INCLUDE Irvine32.inc ; including the library onto the program
ExitProcess proto,dwExitCode:dword
.data
array1 WORD 1000h, 2000h, 3000h, 4000h, 5000h
array2 DWORD 11111h, 22222h, 33333h, 44444h, 55555h
.code
main proc
mov ecx, 5
mov esi, offset Array1 ; esi points to beginning of Array1
mov edi, offset Array2
L1:
xchg edi, esi ; swaps values of array1 (esi) and array2 (edi)
add esi, 4 ; increments the address to next element
add edi, 4
loop L1 ; will loop through label 1 amount of times ecx is
call DumpRegs ; main utility call to display all registers and status flags
invoke ExitProcess,0
main endp
end main
我的代码可以编译,但我不能 100% 确定这是否正确。任何帮助将不胜感激。
array1 WORD 1000h, 2000h, 3000h, 4000h, 5000h
array2 DWORD 11111h, 22222h, 33333h, 44444h, 55555h
如果你想要成功交换的机会,那么你需要定义相同大小的两个数组。如何将 DWORD 存储在 WORD 大小的位置?
不要根据您获得的示例数字来选择数据格式,而是根据您的程序应该实现的目标来选择数据格式。
xchg edi, esi ; swaps values of array1 (esi) and array2 (edi)
这只会交换指针,不会交换它们所引用的数据!
下一个代码交换 2 个 DWORD 大小的元素:
mov eax, [esi]
mov edx, [edi]
mov [esi], edx
mov [edi], eax
作为优化,您应该将 loop L1
指令替换为
dec ecx
jnz L1
速度更快!
我目前正在参加装配课程,我有一个家庭作业问题,我想确保它是正确的。我的问题是:
Give an array named array1 with the values 1000h, 2000h, 3000h, 4000h, 5000h and another array named array2 with the values 11111h, 22222h, 33333h, 44444h, 55555h. Use a loop to swap the nth position elements from array1 and array2.
我写了这段代码:
; AddTwo.asm - adds two 32-bit integers.
; Chapter 3 example
.386
.model flat,stdcall
.stack 4096
INCLUDE Irvine32.inc ; including the library onto the program
ExitProcess proto,dwExitCode:dword
.data
array1 WORD 1000h, 2000h, 3000h, 4000h, 5000h
array2 DWORD 11111h, 22222h, 33333h, 44444h, 55555h
.code
main proc
mov ecx, 5
mov esi, offset Array1 ; esi points to beginning of Array1
mov edi, offset Array2
L1:
xchg edi, esi ; swaps values of array1 (esi) and array2 (edi)
add esi, 4 ; increments the address to next element
add edi, 4
loop L1 ; will loop through label 1 amount of times ecx is
call DumpRegs ; main utility call to display all registers and status flags
invoke ExitProcess,0
main endp
end main
我的代码可以编译,但我不能 100% 确定这是否正确。任何帮助将不胜感激。
array1 WORD 1000h, 2000h, 3000h, 4000h, 5000h array2 DWORD 11111h, 22222h, 33333h, 44444h, 55555h
如果你想要成功交换的机会,那么你需要定义相同大小的两个数组。如何将 DWORD 存储在 WORD 大小的位置?
不要根据您获得的示例数字来选择数据格式,而是根据您的程序应该实现的目标来选择数据格式。
xchg edi, esi ; swaps values of array1 (esi) and array2 (edi)
这只会交换指针,不会交换它们所引用的数据!
下一个代码交换 2 个 DWORD 大小的元素:
mov eax, [esi]
mov edx, [edi]
mov [esi], edx
mov [edi], eax
作为优化,您应该将 loop L1
指令替换为
dec ecx
jnz L1
速度更快!