65

It's said that the leave instruction is the same as :

mov esp,ebp
pop ebp

But what is mov esp,ebp here for? It doesn't seem valid to me...

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
compile-fan
  • 16,885
  • 22
  • 59
  • 73

3 Answers3

94

mov esp,ebp sets the stack pointer to the base frame address, effectively releasing the whole frame. (Don't forget that this is Intel syntax, the destination comes first.) If you didn't do it, once you call ret, you would still be using the called function's stack frame with your calling function, with crashtastic consequences.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
zneak
  • 134,922
  • 42
  • 253
  • 328
  • Sorry, but doesn't `mov esp,ebp` set the base pointer to the stack pointer's address? `mov ebp,esp` would update the stack pointer to point at the base frame. – Byte Lab Jun 10 '15 at 18:37
  • 18
    @Decave, it depends on if you use AT&T-style disassembly or Intel-style disassembly. Since the instructions don't have length suffixes and the registers are not prefixed with `%`, we're talking about Intel-style, where destination comes first. The equivalent AT&T-style disassembly for this question, which you are probably thinking of and where destination comes last, would be `movl %ebp, %esp`. – zneak Jun 10 '15 at 20:11
  • Ah, of course. Thank you very much for your explanation. – Byte Lab Jun 10 '15 at 20:29
  • Your site https://www.felixcloutier.com/x86/ is down (so is the whole domain). Are you planning to get it back up, or should I start editing links in old answers? (Unfortunately old comments linking instructions aren't editable.) – Peter Cordes Feb 28 '22 at 01:27
  • @PeterCordes this is not intentional and news to me, will look into it this evening (PST). – zneak Feb 28 '22 at 21:36
  • 1
    @PeterCordes DNS did not autorenew for sad reasons, should be back online as soon as TTL expires and refreshes. (It's already back online for me.) – zneak Feb 28 '22 at 21:43
  • Excellent, thanks for hosting that nice resource. :) – Peter Cordes Feb 28 '22 at 21:52
  • @PeterCordes of course! thank you for letting me know it was down. If you want, you can send me an email (address on website) and I'll give you other addresses to try if felixcloutier.com goes down; I'm usually more responsive over email than over SO. – zneak Feb 28 '22 at 22:13
4

I think your issue is the fact that there are two different ways of writing x86 assembly. One is the AT&T notation and the other is the Intel notation. The order of the arguments to an instruction are reversed in Intel notation as opposed to AT&T. Your version of the assembly appears to be in Intel notation, which means that mov esp, ebp actaully moves the value in ebp to esp. In the more logical (in my opinion) AT&T notation it would be mov %ebp, %esp.

Abhay Buch
  • 4,548
  • 1
  • 21
  • 26
  • 1
    It would be `movd` instead of `mov`, too. – zneak Mar 29 '11 at 14:54
  • 12
    It's "more logical" if you view the semantics as `move ebp into esp`. The "Intel" notation (which predates Intel by a loooooooooooong time -- for example the Interdata 16-bit series which dates back to the '60s uses exactly this format and was by no means the first...) has semantics more like `move such that esp = ebp`. – JUST MY correct OPINION Mar 29 '11 at 16:15
  • 7
    @zneak, incorrect. `movd` is actually an MMX instruction. If you wanted to include a size suffix (which in this case is *optional*, mind you) you'd use `movl` – bug Oct 16 '12 at 03:42
  • 6
    In fact Intel style notation is much more common. All other assembly languages I know have destination comes first. You can think it as an assignment. `esp = ebp` – phuclv Aug 12 '13 at 09:15
  • 1
    I realized early on that Intel X86 assembly uses what we used to call Reverse Polish Notation, which caused much confusion among business majors who were accustomed to the more "English-like" right to left notation used in algebra, and to operate Texas Instruments calculators. Conversely, the Hewlett-Packard calculators catered to engineers and used Reverse Polish Notation. As I began to learn to use various assemblers, I soon discovered that most of them also use Reverse Polish. That included Univac Exec-8 assembly and IBM BAL (Basic Assembly Language), used on IBM 360/370 class mainframes. – David A. Gray May 28 '16 at 06:44
  • 1
    @DavidA.Gray: Not RPN for sure, in RPN the operands precede the operation, I don't know of any assembly language that does this. – Ben Voigt Apr 09 '18 at 00:25
2

The compiler use this instruction to free the used space by the function in the stack, the leave instruction has the same behavior as mov esp, ebp with pop ebp.

kabab
  • 169
  • 1
  • 9