寄存器存储类用于快速访问变量,其内存分配在 CPU 中。但是cpu中的寄存器是有限的。我使用英特尔酷睿 i5-4260U 处理器。我已经访问了英特尔网站以获取注册表的详细信息。但是我找不到任何关于 cpu 包含多少寄存器的规范(访问网站请单击此处)。即使我能找到寄存器的数量((来自8086/8088 中有多少寄存器?))但我无法弄清楚其中有多少被 c 存储类使用。
2 回答
But I couldn't find any of such specification of how many registers does the cpu contain
Just look for "ia32 programming model" or "amd64 programming model".
I couldn't figure out how many of these are used by c storage classes.
That is implementation dependent. A compiler can even ignore that. Some of them use automatic register mapping if invoked with a high level of optimization, regardless of the way the variable has been declared.
For example: the programming model for user-mode applications on IA32 is composed of the registers EAX, EBX, ECX, EDX, ESI, EDI, EBP, ESP and EIP. EAX and EDX are used as accumulators, they are implicit operands for some instructions (MUL, DIV) and they hold the return value of a function. EBP and ESP are reserved for stack and frame management. EIP is the instruction pointer. So this leaves us with EBX, ECX, EDI and ESI for register mapping. Depending upon the code generated, one or more of these registers may be needed, so reducing even more the number of available registers for mapping variables.
包含 C 中的register关键字是因为在创建 C 时,编译器并不总是能很好地分配寄存器。寄存器分配是编译器将程序变量映射到 CPU 寄存器的部分。
如今,编译器用于寄存器分配的算法总体上非常出色。以至于编译器经常忽略register关键字,原因是编译器比程序员更了解如何映射寄存器以最大化性能。
我不确定编译器“mcleod_ideafix”在他编写时指的是什么,EAX并且EDX不能用于寄存器分配。gcc编译器在 32 位 x86 代码(EAX、EBX、ECX、EDX、ESI和EDI)中使用 6 个整数寄存器。EBP如果该函数不进行任何函数调用并且您提供了正确的编译器选项,它甚至会使用。64 位模式R8通过增加 8 个寄存器R15。如果您使用gcc,只需使用该-S选项编译您的文件,然后查看生成的代码以查看使用了哪些寄存器。
要考虑的另一件事是英特尔处理器使用称为寄存器重命名的功能来减少没有足够寄存器的性能损失。