I am trying to understand how for loops get converted to assembly. I have this for loop:
void forloop()
{
for (int i = 0; i < 5; i++) {
printf("Loop#: %d\n", i);
}
}
This is the disassembly:
void forloop()
{
00007FF6AC5247D0 push rbp
00007FF6AC5247D2 push rdi
00007FF6AC5247D3 sub rsp,108h
00007FF6AC5247DA lea rbp,[rsp+20h]
00007FF6AC5247DF lea rcx,[__95306CE6_looptest@cpp (07FF6AC554670h)]
00007FF6AC5247E6 call __CheckForDebuggerJustMyCode (07FF6AC521D0Ch)
for (int i = 0; i < 5; i++) {
00007FF6AC5247EB mov dword ptr [rbp+4],0
00007FF6AC5247F2 jmp forloop+2Ch (07FF6AC5247FCh)
00007FF6AC5247F4 mov eax,dword ptr [rbp+4]
00007FF6AC5247F7 inc eax
00007FF6AC5247F9 mov dword ptr [rbp+4],eax
00007FF6AC5247FC cmp dword ptr [rbp+4],5
00007FF6AC524800 jge forloop+43h (07FF6AC524813h)
printf("Loop#: %d\n", i);
00007FF6AC524802 mov edx,dword ptr [rbp+4]
00007FF6AC524805 lea rcx,[string "Loop#: %d\n" (07FF6AC5427C8h)]
00007FF6AC52480C call printf (07FF6AC5215EBh)
}
00007FF6AC524811 jmp forloop+24h (07FF6AC5247F4h)
}
00007FF6AC524813 lea rsp,[rbp+0E8h]
00007FF6AC52481A pop rdi
00007FF6AC52481B pop rbp
00007FF6AC52481C ret
I don't understand why the "inc" statement occurs before the printf. Shouldn't it be after?
Thank you.