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?
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.
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.
%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.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With