【发布时间】:2019-08-19 01:17:19
【问题描述】:
我认为我真正的问题是我不完全了解堆栈帧机制,因此我希望了解为什么以下代码会导致程序执行在应用程序结束时恢复。
这段代码是从一个有几个调用级别的 C 函数调用的,pushf 导致程序执行通过堆栈返回几个级别并完全退出程序。
由于我的工作按预期工作,我想知道为什么使用 pushf 指令似乎(我认为)会破坏堆栈。
在例程中,我通常设置和清理堆栈: 副 rsp,28 小时 ... 添加 rsp, 28h
但我注意到只有在汇编代码调用 C 函数时才需要这样做。
所以我尝试从这两个例程中删除它,但没有任何区别。 SaveFlagsCmb 是一个汇编函数,但很容易成为一个宏。
代码代表一个模拟的 6809 CPU Rora(右移寄存器 A)。
PUBLIC Rora_I_A ; Op 46 - Rotate Right through Carry A reg
Rora_I_A PROC
sub rsp, 28h
; Restore Flags
mov cx, word ptr [x86flags]
push cx
popf
; Rotate Right the byte and save the FLAGS
rcr byte ptr [q_s+AREG], 1
; rcr only affects Carry. Save the Carry first in dx then
; add 0 to result to trigger Zero and Sign/Neg flags
pushf ; this causes jump to end of program ????
pop dx ; this line never reached
and dx, CF ; Save only Carry Flag
add [q_s+AREG], 0 ; trigger NZ flags
mov rcx, NF+ZF+CF ; Flag Mask NZ
Call SaveFlagsCmb ; NZ from the add and CF saved in dx
add rsp, 28h
ret
Rora_I_A ENDP
但是,如果我使用此代码,它会按预期工作:
PUBLIC Rora_I_A ; Op 46 - Rotate Right through Carry A reg
Rora_I_A PROC
; sub rsp, 28h ; works with or without this!!!
; Restore Flags
mov ah, byte ptr [x86flags+LSB]
sahf
; Rotate Right the byte and save the FLAGS
rcr byte ptr [q_s+AREG], 1
; rcr only affects Carry. Save the Carry first in dx then
; add 0 to result to trigger Zero and Sign/Neg flags
lahf
mov dl, ah
and dx, CF ; Save only Carry Flag
add [q_s+AREG], 0 ; trigger NZ flags
mov rcx, NF+ZF+CF ; Flag Mask NZ
Call SaveFlagsCmb ; NZ from the add and CF saved in dx
; add rsp, 28h ; works with or without this!!!
ret
Rora_I_A ENDP
【问题讨论】:
标签: visual-studio assembly x86 masm