$ cat -n test.c
1
#include<stdio.h>
2
int main(void) {
3
int a, i = 2;
4
a = (++i) + (++i) + ( ++i);
5
printf("%d\n",a);
6
return 0;
7
}
8
9
wzj@U10:~$ gcc -g test.c
wzj@U10:~$ ./a.out
13
wzj@U10:~$ gdb -q ./a.out
Reading symbols from /home/wzj/a.out...done.
(gdb) disass main
Dump of assembler code for function main:
0x080483e4 <+0>:
push
%ebp
0x080483e5 <+1>:
mov
%esp,%ebp
0x080483e7 <+3>:
and
$0xfffffff0,%esp
0x080483ea <+6>:
sub
$0x20,%esp
0x080483ed <+9>:
movl
$0x2,0x18(%esp)
0x080483f5 <+17>:
addl
$0x1,0x18(%esp)
0x080483fa <+22>:
addl
$0x1,0x18(%esp)
0x080483ff <+27>:
mov
0x18(%esp),%eax
0x08048403 <+31>:
add
%eax,%eax
0x08048405 <+33>:
addl
$0x1,0x18(%esp)
0x0804840a <+38>:
add
0x18(%esp),%eax
0x0804840e <+42>:
mov
%eax,0x1c(%esp)
0x08048412 <+46>:
mov
$0x80484f0,%eax
0x08048417 <+51>:
mov
0x1c(%esp),%edx
0x0804841b <+55>:
mov
%edx,0x4(%esp)
0x0804841f <+59>:
mov
%eax,(%esp)
0x08048422 <+62>:
call
0x804831c <printf@plt>
0x08048427 <+67>:
mov
$0x0,%eax
0x0804842c <+72>:
leave
0x0804842d <+73>:
ret
End of assembler dump.
(gdb)
程序代码:
0x080483ed <+9>: movl $0x2,0x18(%esp) 把2 代入i
0x080483f5 <+17>: addl $0x1,0x18(%esp) i加1变为3
0x080483fa <+22>: addl $0x1,0x18(%esp) i 再加1变为4
0x080483ff <+27>: mov 0x18(%esp),%eax 不再加了,把4放入eax register中
0x08048403 <+31>: add %eax,%eax 然后 4+4为8,把8放入eax 中
0x08048405 <+33>: addl $0x1,0x18(%esp) 回到原来的i ,把i 加1得5
0x0804840a <+38>: add 0x18(%esp),%eax 然后把这个5和8相加 得13
我的gcc做这种运算的时候就是这样处理的
只是说明编译器对这种运算是怎么处理的,
这种代码别写
其实你可以试试
i= 2;
a = (++i ) + (++i)
是几?难道不是8么??