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

查看:191
本文介绍了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.

我认识到,每当我编写一个函数时,我都会使用这 3 行代码(正如我从 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

我有两个问题:

  1. 首先,为什么我需要使用%rbp?使用 %rsp 不是更简单,因为它的内容被移动到 %rbp 的第二行?
  2. 为什么我必须从 %rsp 中减去任何东西?我的意思是它并不总是 16,当我 printfing 像 7 或 8 个变量时,我会减去 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天全站免登陆