Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Assembly 使用循环从array1和array2交换第n个位置元素_Assembly_X86_Masm_Swap_Irvine32 - Fatal编程技术网

Assembly 使用循环从array1和array2交换第n个位置元素

Assembly 使用循环从array1和array2交换第n个位置元素,assembly,x86,masm,swap,irvine32,Assembly,X86,Masm,Swap,Irvine32,我目前正在学习一门组装课程,我有一个家庭作业问题,我想确定它是否正确。我的问题是: 给出一个名为array1的数组,其值为1000h、2000h、3000h、4000h、5000h,另一个名为array2的数组,其值为11111h、2222h、33333h、44444h、55555h。使用循环从array1和array2交换第n个位置元素 我写了这段代码: ; AddTwo.asm - adds two 32-bit integers. ; Chapter 3 example .386 .mo

我目前正在学习一门组装课程,我有一个家庭作业问题,我想确定它是否正确。我的问题是:

给出一个名为array1的数组,其值为1000h、2000h、3000h、4000h、5000h,另一个名为array2的数组,其值为11111h、2222h、33333h、44444h、55555h。使用循环从array1和array2交换第n个位置元素

我写了这段代码:

; 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%确定这是否正确。任何帮助都将不胜感激

如果希望有成功交换的机会,则需要定义两个大小相同的数组。如何将DWORD存储在单词大小的位置

不要根据您获得的示例数字选择数据格式,而是根据您的程序应该实现的目标进行选择

这只交换指针,而不是指针引用的数据

下一个代码交换2个DWORD大小的元素:

mov eax, [esi]
mov edx, [edi]
mov [esi], edx
mov [edi], eax

作为优化,您应该将
循环L1
指令替换为

dec ecx
jnz L1

更快

使用调试器查看内存内容。感谢您的帮助,非常感谢。这更有意义!
mov eax, [esi]
mov edx, [edi]
mov [esi], edx
mov [edi], eax
dec ecx
jnz L1