交换两个通过引用传递的变量不会合作
Exhange two variables passed by reference won't cooperate
请考虑以下 MASM 汇编代码片段。照原样,它将跳转到 skip2 并且工作正常。注释掉 jmp skip2 行,前半部分执行 - 实际上它没有。相反,在 VS2015 调试模式下,我收到访问冲突错误。我附上了我对堆栈激活记录的解释的 excel 图。 (1) 下是 esp 和偏移量 - 如果你跳转到 skip2,这很好用。
(2) 下是 ebp 及其偏移量(我认为),也就是说 [ebp+40] 和 [ebp+44] 应该有效。事实并非如此,我的其他幼稚想法 (3) 也不是,也许对于 pushad,这是从 ebp 开始的正确位置。绿色显示了 pushad 指令之后的堆栈结果。黄色显示目标,即@I 和@k,显然需要取消引用。
我哪里错了?
Exchange PROC
pushad
jmp skip2 ; comment this jmp instruction out and works fine.
push ebp
mov ebp, esp
mov eax, [ebp+44] ; eax gets @k
mov ecx, [eax] ; ecx gets k.
mov ebx, [ebp+40] ; ebx gets @i.
mov edx, [ebx] ; edx gets i.
mov [eax], edx ; k = i.
mov [ebx], ecx ; i = k.
pop ebp ; EDITED: THIS IS THE FIX
jmp ende
skip2:
mov eax, [esp+40] ; eax gets @k
mov ecx, [eax] ; ecx gets k.
mov ebx, [esp+36] ; ebx gets @i.
mov edx, [ebx] ; edx gets i.
mov [eax], edx ; k = i.
mov [ebx], ecx ; i = k.
ende:
popad
ret 8
Exchange ENDP
只是缺少评论中指出的 pop ebp...
请考虑以下 MASM 汇编代码片段。照原样,它将跳转到 skip2 并且工作正常。注释掉 jmp skip2 行,前半部分执行 - 实际上它没有。相反,在 VS2015 调试模式下,我收到访问冲突错误。我附上了我对堆栈激活记录的解释的 excel 图。 (1) 下是 esp 和偏移量 - 如果你跳转到 skip2,这很好用。
(2) 下是 ebp 及其偏移量(我认为),也就是说 [ebp+40] 和 [ebp+44] 应该有效。事实并非如此,我的其他幼稚想法 (3) 也不是,也许对于 pushad,这是从 ebp 开始的正确位置。绿色显示了 pushad 指令之后的堆栈结果。黄色显示目标,即@I 和@k,显然需要取消引用。
我哪里错了?
Exchange PROC
pushad
jmp skip2 ; comment this jmp instruction out and works fine.
push ebp
mov ebp, esp
mov eax, [ebp+44] ; eax gets @k
mov ecx, [eax] ; ecx gets k.
mov ebx, [ebp+40] ; ebx gets @i.
mov edx, [ebx] ; edx gets i.
mov [eax], edx ; k = i.
mov [ebx], ecx ; i = k.
pop ebp ; EDITED: THIS IS THE FIX
jmp ende
skip2:
mov eax, [esp+40] ; eax gets @k
mov ecx, [eax] ; ecx gets k.
mov ebx, [esp+36] ; ebx gets @i.
mov edx, [ebx] ; edx gets i.
mov [eax], edx ; k = i.
mov [ebx], ecx ; i = k.
ende:
popad
ret 8
Exchange ENDP
只是缺少评论中指出的 pop ebp...