组件x86-“离开"组件操作说明 [英] Assembly x86 - "leave" Instruction
问题描述
据说"leave"指令类似于:
It's said that the "leave" instruction is similar to:
movl %ebp, %esp
popl %ebp
我了解movl %ebp, %esp
部分,它的作用是释放存储的内存(如
I understand the movl %ebp, %esp
part, and that it acts to release stored up memory (as discussed in this question).
但是popl %ebp
代码的目的是什么?
But what is the purpose of the popl %ebp
code?
推荐答案
LEAVE
是ENTER
的副本. ENTER
指令通过首先将EBP
推入堆栈,然后将ESP
复制到EBP
来建立堆栈帧,因此LEAVE
必须执行相反的操作,即将EBP
复制到ESP
并然后从堆栈中恢复旧的EBP
.
LEAVE
is the counterpart to ENTER
. The ENTER
instruction sets up a stack frame by first pushing EBP
onto the stack and then copies ESP
into EBP
, so LEAVE
has to do the opposite, i.e. copy EBP
to ESP
and then restore the old EBP
from the stack.
See the section named PROCEDURE CALLS FOR BLOCK-STRUCTURED LANGUAGES in Intel's Software Developer's Manual Vol 1 if you want to read more about how ENTER
and LEAVE
work.
enter n,0
完全等同于(并应替换为)
enter n,0
is exactly equivalent to (and should be replaced with)
push %ebp
mov %esp, %ebp # ebp = esp, mov ebp,esp in Intel syntax
sub $n, %esp # allocate space on the stack. Omit if n=0
leave
完全等同于
leave
is exactly equivalent to
mov %ebp, %esp # esp = ebp, mov esp,ebp in Intel syntax
pop %ebp
enter
非常慢,编译器不使用它,但是leave
很好. ( http://agner.org/optimize ).如果编译器完全构成堆栈框架,则它们会使用leave
(至少gcc会这样做).但是,如果esp
已经等于ebp
,则仅pop ebp
效率最高.
enter
is very slow and compilers don't use it, but leave
is fine. (http://agner.org/optimize). Compilers do use leave
if they make a stack frame at all (at least gcc does). But if esp
is already equal to ebp
, it's most efficient to just pop ebp
.
这篇关于组件x86-“离开"组件操作说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!