Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"enter" vs "push ebp; mov ebp, esp; sub esp, imm" and "leave" vs "mov esp, ebp; pop ebp"

What is the difference between the enter and

push ebp mov  ebp, esp sub  esp, imm 

instructions? Is there a performence difference? If so, which is faster and why do compilers always use the latter?

Similarily with the leave and

mov  esp, ebp pop  ebp 

instructions.

like image 281
小太郎 Avatar asked May 11 '11 05:05

小太郎


People also ask

What is the difference between ESP and EBP?

The register 'ESP' is used to point to the next item on the stack and is referred to as the 'stack pointer'. EBP aka the 'frame pointer' serves as an unchanging reference point for data on the stack. This allows the program to work out how far away something in the stack is from this point.

What is EBP in ASM?

base pointer (EBP): register containing the. address of the bottom of the stack frame. instruction pointer (EIP): register containing. the address of the instruction to be executed. Other examples: EAX (return value), etc.

What is ESP assembly?

The ESP register is the stack pointer for the system stack. It is rarely changed directly by a program but is changed when data is pushed onto the stack or popped from the stack. One use for the stack is in procedure calls. the address of the instructions following the procedure call instruction is stored on the stack.

What is EBP GCC?

%ebp is the "base pointer" for your stack frame. It's the pointer used by the C runtime to access local variables and parameters on the stack. Here's some typical function prologue code generated by GCC (g++ to be precise) First the C++ source. // junk.c++ int addtwo(int a) { int x = 2; return a + x; }


2 Answers

There is a performance difference, especially for enter. On modern processors this decodes to some 10 to 20 µops, while the three instruction sequence is about 4 to 6, depending on the architecture. For details consult Agner Fog's instruction tables.

Additionally the enter instruction usually has a quite high latency, for example 8 clocks on a core2, compared to the 3 clocks dependency chain of the three instruction sequence.

Furthermore the three instruction sequence may be spread out by the compiler for scheduling purposes, depending on the surrounding code of course, to allow more parallel execution of instructions.

like image 59
Gunther Piez Avatar answered Sep 19 '22 18:09

Gunther Piez


There is no real speed advantage using either of them, though the long method will probably run better due to the fact CPU's these days are more 'optimized' to the shorter simpler instructions that are more generic in use (plus it allows saturation of the execution ports if your lucky).

The advantage of LEAVE (which is still used, just see the windows dlls) is that its smaller than manually tearing down a stack frame, this helps a lot when your space is limited.

The Intel instruction manuals (volume 2A to be precise) will have more nitty gritty details on the instructions, so should Dr Agner Fogs Optimization manuals

like image 43
Necrolis Avatar answered Sep 17 '22 18:09

Necrolis