x86_64汇编器中RBP寄存器的用途是什么? [英] What is the purpose of the RBP register in x86_64 assembler?

查看:3237
本文介绍了x86_64汇编器中RBP寄存器的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我尝试学习一些汇编语言,因为计算机架构课程需要它.我写了一些程序,例如打印斐波那契数列.

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个问题:

  1. 首先,为什么我需要使用%rbp?将%rsp的内容移至第二行的%rbp,使用它不是更简单吗?
  2. 为什么我必须从%rsp中减去任何东西?我的意思是并不总是16,当我像7或8个变量一样printf时,我会减去2428.
  1. 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?
  2. Why do I have to subtract anything from %rsp? I mean it's not always 16, when I was printfing like 7 or 8 variables, then I would subtract 24 or 28.

我在虚拟机(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 pushing 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寄存器的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆