ASM? Absolutely.
For the program I wrote (the simple one above, not your example), MinGW dumps the assembly to this, with optimizations turned off:
Code:
.file "undef.c"
.def ___main; .scl 2; .type 32; .endef
.section .rdata,"dr"
LC0:
.ascii "c = %d\12a = %d\12\12\0"
.text
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
andl $-16, %esp
movl $0, %eax
addl $15, %eax
addl $15, %eax
shrl $4, %eax
sall $4, %eax
movl %eax, -12(%ebp)
movl -12(%ebp), %eax
call __alloca
call ___main
movl $0, -4(%ebp)
movl $5, -8(%ebp)
leal -8(%ebp), %eax
incl (%eax)
leal -8(%ebp), %eax
incl (%eax)
movl -8(%ebp), %eax
addl -8(%ebp), %eax
movl %eax, -4(%ebp)
movl -8(%ebp), %eax
movl %eax, 8(%esp)
movl -4(%ebp), %eax
movl %eax, 4(%esp)
movl $LC0, (%esp)
call _printf
movl $0, %eax
leave
ret
.def _printf; .scl 3; .type 32; .endef
Key part is in bold.
I would disagree with you and say that c = 13 makes sense, but that's because that's the way I personally evaluated it.
I recognize the reason 14 is evaluated this way is because the compiler performs the increments one after the other and then does the add. So 5 is loaded, inc to 6, and then inc to 7. So now it adds a + a where a is 7. Adding produces 14. BTW, Borland's command line tools produced this value, too.
LCC on the other hand.... their assembly output is cool because it intersperses the C with the assembly (never knew that ).
Code:
.file "\include\stdio.h"
_$M0:
.file "\undef.c"
.text
.file "\include\safelib.h"
_$M1:
.file "\include\stdio.h"
_$M2:
.file "\undef.c"
_$M3:
.text
; 1 #include <stdio.h>
; 2
; 3 int main(void)
.type _main,function
_main:
pushl %ebp
movl %esp,%ebp
pushl %ecx
pushl %eax
pushl %esi
pushl %edi
; 4 {
.line 4
; 5 int c = 0, a = 5;
.line 5
movl $0,-8(%ebp)
movl $5,-4(%ebp)
; 6
; 7 c = ++a + ++a;
.line 7
movl -4(%ebp),%edi
addl $1,%edi
movl %edi,-4(%ebp)
movl -4(%ebp),%esi
addl $1,%esi
movl %esi,-4(%ebp)
addl %esi,%edi
movl %edi,-8(%ebp)
; 8
; 9 printf("c = %d\na = %d\n\n", c, a);
.line 9
pushl -4(%ebp)
pushl -8(%ebp)
pushl $_$2
call _printf
addl $12,%esp
; 10
; 11 return 0;
.line 11
movl $0,%eax
_$1:
; 12 }
.line 12
popl %edi
popl %esi
leave
ret
_$5:
.size _main,_$5-_main
.globl _main
.extern _printf
.data
_$2:
; "c = %d\na = %d\n\n\x0"
.byte 99,32,61,32,37,100,10,97,32,61,32,37,100,10,10,0
LCC uses separate registers and separates the adding of the numbers. Loads 5, adds one. Loads 6, and adds one. Then adds 6 + 7 together to get 13.
This is more fun than I thought. Get your project leader in on this discussion.