问题描述
因此,我尝试学习一些汇编语言,因为计算机架构课程需要它.我写了一些程序,例如打印斐波那契数列.
So I'm trying to learn a little bit of assembly, because I need it for Computer Architecture class. I wrote a few programs, like printing the Fibonacci sequence.
我认识到,每当我编写一个函数时,我都会使用这三行代码(从比较从gcc
生成的汇编代码到等效于C
的汇编代码中学到)
I recognized that whenever I write a function I use those 3 lines (as I learned from comparing assembly code generated from gcc
to its C
equivalent):
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
我对此有2个问题:
- 首先,为什么我需要使用
%rbp
?将%rsp
的内容移至第二行的%rbp
,使用它不是更简单吗? - 为什么我必须从
%rsp
中减去任何东西?我的意思是并不总是16
,当我像7或8个变量一样printf
时,我会减去24
或28
.
- First of all, why do I need to use
%rbp
? Isn't it simpler to use%rsp
, as its contents are moved to%rbp
on the 2nd line? - Why do I have to subtract anything from
%rsp
? I mean it's not always16
, when I wasprintf
ing like 7 or 8 variables, then I would subtract24
or28
.
我在虚拟机(4 GB RAM)和Intel 64位处理器上使用Manjaro 64位
I use Manjaro 64 bit on a Virtual Machine (4 GB RAM), Intel 64 bit processor
推荐答案
rbp
是x86_64上的帧指针.在生成的代码中,它会获取堆栈指针(rsp
)的快照,以便在对rsp
进行调整(即为局部变量保留空间或将push
的值保留在堆栈上),局部变量和仍可以从rbp
的恒定偏移量访问功能参数.
rbp
is the frame pointer on x86_64. In your generated code, it gets a snapshot of the stack pointer (rsp
) so that when adjustments are made to rsp
(i.e. reserving space for local variables or push
ing values on to the stack), local variables and function parameters are still accessible from a constant offset from rbp
.
许多编译器都提供了省略帧指针作为优化选项.这将使生成的汇编代码访问变量相对于rsp
,并释放rbp
作为另一个通用寄存器供功能使用.
A lot of compilers offer frame pointer omission as an optimization option; this will make the generated assembly code access variables relative to rsp
instead and free up rbp
as another general purpose register for use in functions.
在GCC的情况下(我猜您是从AT& T汇编程序语法使用的),该开关为-fomit-frame-pointer
.尝试使用该开关编译代码,然后查看获得的汇编代码.您可能会注意到,当访问相对于rsp
而不是rbp
的值时,指针的偏移量在整个函数中都不同.
In the case of GCC, which I'm guessing you're using from the AT&T assembler syntax, that switch is -fomit-frame-pointer
. Try compiling your code with that switch and see what assembly code you get. You will probably notice that when accessing values relative to rsp
instead of rbp
, the offset from the pointer varies throughout the function.
这篇关于x86_64汇编器中RBP寄存器的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!