I compile C
code snippet with VS2010
by two ways:
int g_arra[3];
int main() {
int idx = 2;
g_arra[0] = 10;
g_arra[1] = 20;
g_arra[2] = 30;
g_arra[idx] = 40;
return 0;
}
- With
/O2
optimization - Without
/O2
optimization
About /O2
optimization:
This option enables optimizations for speed. This is the generally recommended optimization level. The compiler vectorization is enabled at O2 and higher levels. With this option, the compiler performs some basic loop optimizations, inlining of intrinsic, Intra-file interprocedural optimization, and most common compiler optimization technologies.
When tried to reverse it:
With
/O2
optimization:; int __cdecl main(int argc, const char **argv, const char **envp) .text:00401000 _main proc near ; CODE XREF: ___tmainCRTStartup+11Dp .text:00401000 mov dword_403390, 10 .text:0040100A mov dword_403394, 20 .text:00401014 mov dword_403398, 40 .text:0040101E xor eax, eax .text:00401020 retn .text:00401020 _main endp
Without
/O2
:; int __cdecl main(int argc, const char **argv, const char **envp) .text:00401000 _main proc near ; CODE XREF: ___tmainCRTStartup+11Dp .text:00401000 var_4 = dword ptr -4 .text:00401000 argc = dword ptr 8 .text:00401000 argv = dword ptr 0Ch .text:00401000 envp = dword ptr 10h .text:00401000 push ebp .text:00401001 mov ebp, esp .text:00401003 push ecx .text:00401004 mov [ebp+var_4], 2 .text:0040100B mov dword_403390, 0Ah .text:00401015 mov dword_403394, 14h .text:0040101F mov dword_403398, 1Eh .text:00401029 mov eax, [ebp+var_4] .text:0040102C mov dword_403390[eax*4], 28h .text:00401037 xor eax, eax .text:00401039 mov esp, ebp .text:0040103B pop ebp .text:0040103C retn .text:0040103C _main endp
Code compiled without /O2
given me clear explanation about global array, i can compute the size of it(4 byte each [eax*4]
) and how many elements it has.
My question is, how to deal with the first case? Where is compiler hide other instructions? How to detect, function has a global allocated array or a stack allocated array?