Retq指令,它在哪里返回 [英] Retq instruction, where does it return
问题描述
我无法理解汇编指令retq
返回的位置.
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?
简而言之,它使用rbp还是esp在堆栈上查找地址?
In short, does it use rbp or esp for finding the address on the stack?
推荐答案
学习汇编代码后,这是我的想法, 我们来看一个示例:
After studying assembly code, here are my thoughts, let's look at a sample:
fun:
push %rbp
mov %rsp,%rbp
...
...
pop %rbp
retq
main:
...
...
callq "address" <fun>
...
...
我们可以看到retq
之前有一条指令. pop %rbp
(有时是请假指令,但它们是相似的)指令将
We can see there is a instruction before retq
. The pop %rbp
(sometimes it is a leave instruction but they are similar) instruction will
- 将当前堆栈指针
%rsp
的内容保存到基本堆栈指针%rbp
. - 将
%rsp
指针移动到堆栈上的先前地址.
- save the content of current stack pointer
%rsp
to base stack pointer%rbp
. - move the
%rsp
pointer to previous address on stack.
例如:在弹出命令之前,%rsp
指向0x0000 0000 0000 00D0
.在pop
命令之后,它指向0x0000 0000 0000 00D8
(假定堆栈从高地址增长到低地址).
For example: before pop command, the %rsp
pointed to 0x0000 0000 0000 00D0
. After the pop
command it points to 0x0000 0000 0000 00D8
(assume the stack grows from high address to low address).
在执行pop
命令之后,现在%rsp
指向一个新地址,而retq
将该地址用作返回地址.
After the pop
command, now %rsp
points to a new address and retq
takes this address as return address.
这篇关于Retq指令,它在哪里返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!