I searched for x++ vs ++x and I found a great answer here, So I decide to see the assembly output of gcc to see how x++ and ++x implemented:
main() { int s = 0; ++s; return 0; }
Compiling the example:
gcc mul.c -masm=intel -o mul.asm
Output of ++s:
.file "mul.c"
.intel_syntax
.text
.p2align 4,,15
.globl main
.type main, @function
main:
lea %ecx, [%esp+4]
and %esp, -16
push DWORD PTR [%ecx-4]
push %ebp
mov %ebp, %esp
push %ecx
sub %esp, 16
mov DWORD PTR [%ebp-8], 0
add DWORD PTR [%ebp-8], 1
mov %eax, 0
add %esp, 16
pop %ecx
pop %ebp
lea %esp, [%ecx-4]
ret
.size main, .-main
.ident "GCC: (GNU) 4.2.1 20070719 [FreeBSD]"
Output for x++:
.file "mul.c"
.intel_syntax
.text
.p2align 4,,15
.globl main
.type main, @function
main:
lea %ecx, [%esp+4]
and %esp, -16
push DWORD PTR [%ecx-4]
push %ebp
mov %ebp, %esp
push %ecx
sub %esp, 16
mov DWORD PTR [%ebp-8], 0
add DWORD PTR [%ebp-8], 1
mov %eax, 0
add %esp, 16
pop %ecx
pop %ebp
lea %esp, [%ecx-4]
ret
.size main, .-main
.ident "GCC: (GNU) 4.2.1 20070719 [FreeBSD]"
So, I am asking if x++ and ++x have different meaning, why GCC outputs the some assembly for them, shouldn't have different output?