Search code examples
assemblyx86returninstructionsstack-pointer

Does the ret instruction add 4 to esp register?


Does the ret instruction cause "esp" register to be increased by 4?


Solution

  • Yes, it performs

    pop eip
    

    You can use

    mov eax, [esp]
    jmp eax
    

    to avoid it.

    EDIT: It's exactly what ret does. For example, jmp rel_offet is nothing than a hidden add eip, offset, or jmp absolute_offset is mov eip, absolute_offset. Sure there are differences in the way the processor treats them, but from programmer's point of view it's all that happens.

    Also, there is a special form of ret : ret imm8 that also adds this imm8 value to esp : for example a __stdcall function uses it to discard its parameters from the stack. Not to mention retf version, used in 16bit mode, that also pops the cs from the stack.

    EDIT2:

    pop register
    

    means:

    mov register, [esp]
    add esp, 4