x86 Assembly

Moderators: None (Apply to moderate this forum)
Number of threads: 4563
Number of posts: 16029

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Is the instruction after a RET always the one after CALL? Posted by mateustymbu on 6 Mar 2012 at 1:25 PM
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,

Mateus.
Report
Re: Is the instruction after a RET always the one after CALL? Posted by skaa7 on 7 Mar 2012 at 7:49 AM
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.
        call    fnu
        mov     eax,5
        nop
...
proc    fnu
        add     dword [esp],5
        ret
endp
. In C++ inline assembler - the same.
Report
Re: Is the instruction after a RET always the one after CALL? Posted by mateustymbu on 8 Mar 2012 at 8:17 AM
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.

Mateus.
Report
Re: Is the instruction after a RET always the one after CALL? Posted by skaa7 on 8 Mar 2012 at 8:48 AM
RET instruction can be replaced with JMP this way:
        pop      eax
        jmp     eax

, 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. :)
Report
Re: Is the instruction after a RET always the one after CALL? Posted by mateustymbu on 8 Mar 2012 at 10:01 AM
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...



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.