I'm using a AMD64 computer(Intel Pentium Gold 4415U) to compare some assembly instructions converted from C language(of course, exactly, disassembly).
With Windows 10, I used Visual Studio 2017(15.2) with their C compiler. My example code is shown below:
int main() {
int i = 0;
if(++i == 4);
if(i++ == 4);
return 0;
}
The disassembly shows as below:
mov eax,dword ptr [i] // if (++i == 4);
inc eax
mov dword ptr [i],eax
mov eax,dword ptr [i] // if (i++ == 4);
mov dword ptr [rbp+0D4h],eax ; save old i to a temporary
mov eax,dword ptr [i]
inc eax
mov dword ptr [i],eax
cmp dword ptr [rbp+0D4h],4 ; compare with previous i
jne main+51h (07FF7DDBF3601h)
mov dword ptr [rbp+0D8h],1
jmp main+5Bh (07FF7DDBF360Bh)
*mov dword ptr [rbp+0D8h],0
07FF7DDBF3601 goes to the last line instruction(*).
07FF7DDBF360B goes to 'return 0;'.
In if (++i == 4)
, the program doesn't observes whether 'added' i satisfies the condition.
However in if (i++ == 4)
, the program saves the 'previous' i to the stack, and then does the increment. After, the program compare 'previous' i with the constant integer 4.
What was the cause of the difference of two C codes? Is it just a compiler's mechanism? Will it be different with more complex code?
I tried to find about this with Google, however I failed to find the origin of the difference. Have to I understand 'This is just a compiler behavior'?