After reading a number of blog posts, forums, and watching tutorials I figured I would start learning to reverse software the old fashion way. Creating simple C files and looking at their disassembly. In my quest to truly understand reversing, I thought also comparing optimized and unoptimized code would be beneficial. While looking through I came across a couple lines of code that appear to do nothing.
I would love if someone can explain what the "mov"[es] in the unoptimized code is doing. All of these were disassembled using Hopper v4.
C Code:
#include <stdio.h>
int main(int arg, char** arg) {
printf("Hello World!\n");
return 0;
}
Unoptimized Code (gcc -m32) :
; Variables:
; arg_4: 12
; arg_0: 8
; var_4: -4
; var_8: -8
; var_C: -12
; var_10: -16
; var_18: -24
push ebp
mov ebp, esp
sub esp, 0x18
call _main+11
pop eax ; CODE XREF=_main+6
-- What purpose do these moves serve? --
mov ecx, dword [ebp+arg_4]
mov edx, dword [ebp+arg_0]
-- --
lea eax, dword [eax-0x1f5b+0x1fa6] ; "Hello World!\\n"
-- And what do these moves also serve? --
mov dword [ebp+var_4], 0x0
mov dword [ebp+var_8], edx
mov dword [ebp+var_C], ecx
-- --
mov dword [esp+0x18+var_18], eax ; method imp___symbol_stub__printf
call imp___symbol_stub__printf
xor ecx, ecx
mov dword [ebp+var_10], eax
mov eax, ecx
add esp, 0x18
pop ebp
ret
Optimized Code (gcc -m32 -O3):
push ebp
mov ebp, esp
sub esp, 0x8
call _main+11
pop eax ; CODE XREF=_main+6
lea eax, dword [eax-0x1f6b+0x1f9e] ; "Hello World!"
mov dword [esp+0x8+var_8], eax ; "%s" for imp___symbol_stub__puts
call imp___symbol_stub__puts
xor eax, eax
add esp, 0x8
pop eep
ret
int main(int arg, char** arg)
should beint main(int argc, char **argv)