Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retq instruction, where does it return

I am unable to understand where the assembly instruction retq returns to.

I understand that when my normal code executes then it return to the address specified in the stack. But how does it know where in the stack is the return address located?

In short, does it use rbp or esp for finding the address on the stack?

like image 410
Kaushal Shah Avatar asked Aug 19 '13 10:08

Kaushal Shah


People also ask

When the Callq instruction executes what value will be pushed onto the stack?

The callq instruction takes one operand, the address of the function being called. It pushes the return address (current value of %rip , which is the next instruction after the call) onto the stack and then jumps to the address of the function being called.

Does RSP point to the top of the stack?

Recall that %rsp is the stack pointer and always points to the top of the stack. The register %rbp represents the base pointer (also known as the frame pointer) and points to the base of the current stack frame.

What RBP contains?

%rbp is the base pointer, which points to the base of the current stack frame, and %rsp is the stack pointer, which points to the top of the current stack frame. %rbp always has a higher value than %rsp because the stack starts at a high memory address and grows downwards.

What is RET command?

The ret instruction transfers control to the return address located on the stack. This address is usually placed on the stack by a call instruction. Issue the ret instruction within the called procedure to resume execution flow at the instruction following the call .


1 Answers

ret is how you spell pop rip on x86: a stack pop and an indirect branch to that value. https://www.felixcloutier.com/x86/ret documents exactly what it does and doesn't do.

It's effectively pop %tmp / jmp *%tmp where tmp is an internal temporary register.

ret depends only on RSP.

Using RBP as a frame pointer is a totally optional software convention that modern compilers don't even do when optimization is enabled.

like image 183
Peter Cordes Avatar answered Sep 21 '22 06:09

Peter Cordes