ben*_*nji 5 c++ cpu-registers visual-c++
我试图进入ASM概念,同时观察MSVC生成的反汇编,我无法完全理解.这是我的测试用例:
#include <tchar.h>
#include <conio.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int v1 = 1;
int v2 = 2;
int v3 = 3;
int v4 = 4;
int v5 = 5;
int v6 = 6;
int v7 = 7;
int v8 = 8;
int v9 = 9;
int v10 = 10;
int v11 = 11;
int v12 = 12;
int v13 = 13;
int v14 = 14;
int v15 = 15;
int v16 = 16;
int sum = v1+v2 * (v3+v4 * (v5+v6 * (v7+v8 * (v9+v10 * (v11+v12 * (v13+v14 * (v15+v16)))))));
_getch();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它会产生类似于:
mov eax, v1
mov edx, v3
mov ecx, v5
mov ebx, v7
mov esi, v9
mov edi, v11 // all 6 available registers are filled
mov dword ptr [ebp-60h), eax // free eax <<<<<<
mov eax, v15 // fill it again
add eax, v16
imul eax, v12
(...)
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是:编译器在标有"<<<<<<"的行上做了什么?我的猜测是它已经创建了一个变量来存储寄存器值.看起来它是在堆栈上,因为它使用了ebp,但它是类似"全局变量",还是它是当前范围(框架)中的变量只要?
提前致谢.
MSVC 会将寄存器溢出到适当的内存:当它溢出保存具有块作用域的变量的寄存器时,溢出到堆栈;或者当寄存器保存全局变量时,溢出到固定偏移量。在任何时候,编译器都知道哪些变量位于哪个寄存器中。
您不能将局部变量溢出到全局内存,因为由于线程和可重入性,局部变量的数量可能不可预测。编译器可能能够暂时将全局变量溢出到堆栈槽,但这在线程存在的情况下相当复杂。