I am trying to get into ASM concepts and while observing the disassembly generated by MSVC there`s something I cannot fully understand. Here is my test case:
#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;
}
which generates something like:
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
(...)
So, my question is:
What does the compiler do at the line marked with "<<<<<<" ? My
guess is that it has created a variable to store the register value
in. Is looks it is on the stack as it uses the ebp
, but is it
something like a "global variable", or is it a variable in current
scope (frame) only?
Thanks in advance.
MSVC will spill registers to the appropriate memory: to the stack when it's spilling a register that held a variable with block scope, or to a fixed offset when the register held a global. At any point, the compiler knows which variables are in which register.
You can't spill local variables to global memory because there might an unpredictable number of them, due to threads and reentrancy. A compiler may be able to spill a global variable to a stack slot, temporarily, but this is quite complex in the presence of threads.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With