Thursday, 10 August 2017

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


Answer



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.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...