I know that the call
/ ret
is something that is done internally, but I wanted to understand a bit more about how it might be translated in assembly code. Here is what I have so far for call
:
# Verbose way of doing `call func`
lea (%rip), %rax # get the current %rip. It cannot be directly accessed, so need to do a roundabout way
add $14, %rax # now that we have the previous %rip address in a register, add the offset to after the jmp
sub $8, %rsp # do pop %rax to move the value in the register onto the stack
mov %rax, (%rsp) # ... (doing it verbosely)
jmp func # jump to the function label
And for ret
:
func:
nop # pretend we did something in the function
# Verbose way of returning from a function, `ret`
mov (%rsp), %r11 # move the memory address stored at the top of the stack into %r11
add $8, %rsp # move the stack pointer back to where we started from
jmp %r11 # jump to the return address, now held in %r11
Is this more-or-less what call
/ ret
does?