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?


after study assembly code, here is my thought: let's look at a sample:

fun:
push %rbp
mov %rsp,%rbp
...
...
pop %rbp
retq

main:
...
...
callq  "address" <fun>
...
...

we can see there is a instruction before retq. the "pop %rbp" (sometime it is a leave instruction but there are similar) instruction will

  • move the content of current stack pointer(rsp) points to and save to base stack pointer(rsp).
  • move the rsp and pointer to previous address on stack.
  • for example: before pop command: the rsp was pointed to 0x0000 0000 0000 00D0

    after pop command: 0x0000 0000 0000 00D8 (assume the stack grows from high address to low address)

    after the pop command, now rsp point to a new address and retq take this address as return address.

    链接地址: http://www.djcxy.com/p/15276.html

    上一篇: 如何实现Google Play图书中使用的页面卷曲类型?

    下一篇: Retq指令,它在哪里返回