ChatGPT解决这个技术问题 Extra ChatGPT

Why does leave do "mov esp,ebp" in x86 assembly?

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...


A
Andrew Barber

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.


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.
@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.
Ah, of course. Thank you very much for your explanation.
Your site 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.)
@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.)
A
Abhay Buch

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.


It would be movd instead of mov, too.
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.
@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
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
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.
k
kabab

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.


Surely enter and leave cannot both be for the purpose of freeing the used space on the stack?
Yes you are right, just the leave instruction, I made a mistake