Is the instruction after a RET always the one after CALL?

Hi guys!

In a well-behaved C program, shall the return statement (RET) always return to the instruction following the CALL statement? I know this is the default, but I would like to check if anyone knows or remembers authentic examples of cases where this standard does not apply (common compiler optimization or other things...).

Thank you very much,



  • In assembly language RET instruction jumps to the address that is on top of ESP register (and changes ESP value). In this example RET will not return to the following the CALL statement, but to the statement with NOP instruction.
    [code] call fnu
    mov eax,5
    proc fnu
    add dword [esp],5
    endp[/code]. In C++ inline assembler - the same.
  • Hi skaa7!

    Thanks for answering my question!

    I understand your code and I know that we can use other structures to change the execution flow (including manipulating the stack)... I understand that if we change the return address written on the stack the execution flow will change to the address that was written on the stack. What I need to know is: is there any not unusual execution situation where the next instruction is not the one that follows the CALL? I mean, I would like to be sure that it doesn't happen, unless something unexpected occurs (like human intervention).

    My concern is whether the commercial application programs in general, compiled by tools (in contrast to programs written directly in assembly by skilled programmers, like you) ALWAYS follow the mentioned pattern. Notice that in this case I have a fixation for exceptions (it is important to know whether they exist in this case, for a research project I'm developing into a M. Sc. program's discipline). I know, for example, that a compiler may, sometimes, change a RET to a JMP (tail-call optimization). I would like to know if something like this may change the order of the instruction that is executed after the RET and, mainly, if the CALL will always be just before the instruction executed after the RET.

    Thanks again for your reply.

  • RET instruction can be replaced with JMP this way:
    [code] pop eax
    jmp eax[/code]
    , or JMP with other top of stack correction. In this case I do not see any problem.
    Compilers are made by people, so no one can guarantee if these people did not make mistakes. :)
  • OK! I understand your point. It is true that if the compiler developer wants, he can change the RET to something else (for example, the POP + JMP, like you showed). I also understand that he can also make mistakes...

    I'm concerned about some case where the default pattern (established as the best practice) does not follow the usual RET pattern (someone told me that the RET pattern is the best way of returning from a function because the processor is optimized to work this way). I'm not sure if I'm explaining it well...
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!