Published: November 20, 2009Posted in: The Binary Auditor™ Short ExercisesTags: exercise, modulo
Damn it. Just installed Visual Studio and did a simple compile. Have a look at my source! What the hell is doing Visual Studio with my code? Maybe you can guess why I got this difference between source and binary?
1
2
3
4
5
6
| int main(int argc, char* argv[])
{
int a;
a = 11 % 3;
return 0;
} |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| .text:00401000 _main proc near
.text:00401000
.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
.text:00401000 push ebp
.text:00401001 mov ebp, esp
.text:00401003 push ecx
.text:00401004 mov [ebp+var_4], 2
.text:0040100B xor eax, eax
.text:0040100D mov esp, ebp
.text:0040100F pop ebp
.text:00401010 retn
.text:00401010 _main endp |
preprocessor works :)
GCC does it also on linux :)
I am still a newby on this, but it seems to me that the assembly code does not shows how the calculation (11%3) is being done. Instead it is moving the result (11%3=2) to variable a (ebp+var_4).
Thus, I am assuming that when the code is compile, VS automatically generates the result for this simple calculation.
Yes, algemy, it’s because the compiler optimized the simple math calculation to reduce the amount of instructions and therefore making the program more efficient.
Yeah what Steve said, optimization took out some instructions. Further instructions remove the variable size build up and tear down and even further optimization turns this whole function into 0×90 :-)