Possible duplicate:
What is the meaning of LEA EAX, [EAX]?
During disassembly practice, I noticed the following code:
test.cpp:
#include <stdio.h> int main(int argc, char * argv[]) { for (int i = 0; i < 10 ; ++i) { printf("%i\n", i); } int i = 0; while ( i < 10) { printf("%i\n", i); ++i; } return 0; }
compilation using vC ++ 2008 with optimization:
cl /Ox test.cpp
dismantling the main function:
.text:00401000 var_4 = dword ptr -4 ; BTW, IDA fails to see that esi is pushed to save it, not to allocate space to local variable .text:00401000 .text:00401000 push esi .text:00401001 xor esi, esi .text:00401003 .text:00401003 loc_401003: ; CODE XREF: sub_401000+15j .text:00401003 push esi .text:00401004 push offset byte_40A150 .text:00401009 call sub_401038 ; printf .text:0040100E inc esi .text:0040100F add esp, 8 .text:00401012 cmp esi, 0Ah .text:00401015 jl short loc_401003 .text:00401017 xor esi, esi .text:00401019 lea esp, [esp+0] .text:00401020 .text:00401020 loc_401020: ; CODE XREF: sub_401000+32j .text:00401020 push esi .text:00401021 push offset unk_40A154 .text:00401026 call sub_401038 ; printf .text:0040102B inc esi .text:0040102C add esp, 8 .text:0040102F cmp esi, 0Ah .text:00401032 jl short loc_401020 .text:00401034 xor eax, eax .text:00401036 pop esi .text:00401037 retn
Now I'm not quite an expert, as you can see from the example code, but I was able to figure out this list of assemblies, given that I wrote the source code. The only thing that bothers me is the following line:
.text:00401019 lea esp, [esp+0]
Why does the compiler do this? It does not affect any register or flag, and it looks like redundant code. The only thing I can think of is that the compiler aligns the code in which jmp is in the second loop (loc_401020), could this be the reason?