How is return address specified in stack?

Mask picture Mask · Mar 30, 2010 · Viewed 19.3k times · Source

This is what I see by disassemble for the statement function(1,2,3);:

movl   $0x3,0x8(%esp)
movl   $0x2,0x4(%esp)
movl   $0x1,(%esp)
call   0x4012d0 <_Z8functioniii>

It seems the ret address is not pushed into stack at all,then how does ret work?

Answer

Michael Burr picture Michael Burr · Mar 30, 2010

On an x86 processor (as for your assembly language example), the call instruction pushes the return address on the stack and transfers control to the function.

So on entry to a function, the stack pointer is pointing at a return address, ready for ret to pop it into the program counter (EIP / RIP).


Not all processor architectures put the return address on the stack- often there's a set of one or more registers designed to hold return addresses. On ARM processors, the BL instruction places the return address in a specific register (LR, or the 'link register') and transfers control to the function.

The ia64 processor does something similar, except that there are several possible registers (b0-b7) that can receive the return address and one will be specified in the instruction (with b0 being the default).