Search code examples
gdbelf

What is the difference between leave and ret


I am trying to disassemble a simple program which contains a simple function. This program is compiled with gcc for a 32 bits x86 target. The function is called via call instruction. At the end of the function, i see a ret instruction, which is normal, but there is also a leave instruction. There is no enter instruction anywhere is the program. I am wondering what does this leave function....


Solution

  • enter is a slow synonym for

    push ebp
    mov  ebp, esp
    sub  esp, imm
    

    leave is a reasonably fast synonym for

    mov  esp, ebp
    pop  ebp
    

    You don't have to have an enter to use leave. It is just a shorthand for the stack-cleanup register dance.