1

Why is there no companion instruction to leave on the x86? That way,

pushl   %ebp
movl    %esp,%ebp
pushl   $3
popl    %eax
leave
ret

could become:

enter #or something
pushl   $3
popl    %eax
leave
ret

Isn't that just faster in general?

Keeley Hoek
  • 543
  • 4
  • 18
  • 1
    first code is faster . see http://stackoverflow.com/questions/5959890/enter-vs-push-ebp-mov-ebp-esp-sub-esp-imm-and-leave-vs-mov-esp-ebp – qwr Jul 05 '13 at 06:16

3 Answers3

5

In fact, there is an enter instruction. As to why it's not seeing much use, "Intel® 64 and IA-32 Architectures Optimization Reference Manual" offers a hint:

Assembly/ Compiler Coding Rule 32. (MH impact, M generality) Use push/pop to manage stack space and address adjustments between function calls/returns instead of enter/leave. Using enter instruction with non-zero immediates can experience significant delays in the pipeline in addition to misprediction.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • `leave` is not slow so GCC *does* use it when it made a frame pointer, if ESP isn't already pointing at a saved EBP. `leave` is 3 uops, vs. 2 uops for separate `mov %ebp,%esp` / `pop %ebp`. But yes, `enter` is terrible, even `enter 0,0` is 12 uops on Haswell/Skylake. (https://agner.org/optimize/ ; https://uops.info/) – Peter Cordes Feb 10 '20 at 21:30
4

There is indeed a matching ENTER (Art of Assembly Language) instruction. It's seldom used however, because it is very slow, as explained in the link.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

There is enter, for some processors.

alex
  • 479,566
  • 201
  • 878
  • 984