问题描述
我仍然不确定汇编程序如何使用寄存器
I'm still uncertain how registers are being used by the assembler
说我有一个程序:
int main(int rdi, int rsi, int rdx) {
rdx = rdi;
return 0;
}
请在汇编中将此翻译为:
Would this in assembly be translated into:
movq %rdx, %rdi
ret rax;
我是AT& T的新手,很难预测何时将使用某个寄存器.从计算机系统-程序员的观点(第三版,R.E.)查看此图表.科比和D. R.奥哈拉龙:
I'm new to AT&T and have hard time predicting when a certain register will be used.Looking at this chart from Computer Systems - A programmer's perspective, third edition, R.E. Bryant and D. R. O'Hallaron:
推荐答案
仅在函数的入口和出口处.
Only at entry and exit of a function.
即使对于作为函数参数的变量,也无法保证函数中将使用哪些寄存器.编译器可以(并且经常会)在寄存器之间移动变量,以优化寄存器/堆栈的使用,尤其是在x86之类的寄存器匮乏的体系结构上.
There is no guarantee as to what registers will be used within a function, even for variables which are parameters to the function. Compilers can (and often will) move variables around between registers to optimize register/stack usage, especially on register-starved architectures like x86.
在这种情况下,像rdx = rdi
这样的简单赋值操作可能根本无法编译为任何汇编代码,因为编译器将简单地识别出这两个值现在都可以在寄存器%rdi
中找到.即使对于像rdx = rdi + 1
这样的更复杂的操作,编译器也可以自由地将值存储在任何寄存器中,而不是专门存储在%rdx
中. (如果它识别出此后从未使用过原始值,它甚至可以将该值存储回%rdi
,例如inc %rdi
.)
In this case, a simple assignment operation like rdx = rdi
may not compile to any assembly code at all, because the compiler will simply recognize that both values can now be found in the register %rdi
. Even for a more complex operation like rdx = rdi + 1
, the compiler has the freedom to store the value in any register, not specifically in %rdx
. (It may even store the value back to %rdi
, e.g. inc %rdi
, if it recognizes that the original value is never used afterwards.)
这篇关于确定在哪个寄存器参数和变量中存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!