$ cc -g test.c -o test 无优化
YETIboy@~
$ gdb test
GNU gdb 2003-09-20-cvs (cygwin-special)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Publ
welcome to change it and/or distribute copies of it u
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show
This GDB was configured as "i686-pc-cygwin"...
(gdb) disass main
Dump of assembler code for function main:
0x00401050 <main+0>: push %ebp
0x00401051 <main+1>: mov %esp,%ebp
0x00401053 <main+3>: sub $0x18,%esp
0x00401056 <main+6>: and $0xfffffff0,%esp
0x00401059 <main+9>: mov $0x0,%eax
0x0040105e <main+14>: mov %eax,0xfffffff4(%ebp)
0x00401061 <main+17>: mov 0xfffffff4(%ebp),%eax
0x00401064 <main+20>: call 0x4013b0 <_alloca>
0x00401069 <main+25>: call 0x401440 <__main>
0x0040106e <main+30>: movl $0x0,0xfffffffc(%ebp)
0x00401075 <main+37>: movl $0x0,0xfffffff8(%ebp)
0x0040107c <main+44>: lea 0xfffffffc(%ebp),%eax
0x0040107f <main+47>: addl $0x3e8,(%eax) i += 1000;
0x00401085 <main+53>: lea 0xfffffff8(%ebp),%eax
0x00401088 <main+56>: addl $0x3e8,(%eax) j = j + 1000;
0x0040108e <main+62>: leave
0x0040108f <main+63>: ret
End of assembler dump.
(gdb) l
1 main() {
2 int i = 0, j = 0;
3 i += 1000;
4 j = j + 1000;
5 }
(gdb)
由此可见,在gcc中,i += 1与i = i + 1是等效的:)
再看看最强的优化是什么样子,还是这个程序
$ cc -g test.c -o test -O3-----O1,O2,O3优化3个等级,O3最高
YETIboy@ ~
$ ./test
i:1000,j:1000 验证结果,怕优化错误,呵呵,看来多余了
YETIboy@ ~
$ gdb test
GNU gdb 2003-09-20-cvs (cygwin-special)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License
welcome to change it and/or distribute copies of it under certa
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty"
This GDB was configured as "i686-pc-cygwin"...
(gdb) disass main
Dump of assembler code for function main:
0x00401060 <main+0>: push %ebp
0x00401061 <main+1>: xor %eax,%eax
0x00401063 <main+3>: mov %esp,%ebp
0x00401065 <main+5>: sub $0x18,%esp
0x00401068 <main+8>: and $0xfffffff0,%esp
**前面都省了很多步骤,上面主要分配局部变量和堆栈指针对齐能被16整除的地址(这样提高
寻址速度)***********************************************************
0x0040106b <main+11>: call 0x4013c0 <_alloca>
0x00401070 <main+16>: call 0x401450 <__main>
0x00401075 <main+21>: movl $0x3e8,0x8(%esp,1)!!!!直接赋值到变量地址!!!
0x0040107d <main+29>: movl $0x3e8,0x4(%esp,1)且用的是esp,堆栈指针!!
0x00401085 <main+37>: movl $0x401050,(%esp,1)
0x0040108c <main+44>: call 0x401460 <printf> !!
0x00401091 <main+49>: mov %ebp,%esp
0x00401093 <main+51>: pop %ebp
0x00401094 <main+52>: ret
***可以看见,类似于int i = 0的语句其实都不存在了*****************
*******下面是调用printf,可以看见,这是一个c library runtime,(应该和CLR还是有很大差别),也就是在连接时才确定地址的,gcc没有编译它,只负责连接****************************
运行时候才连接
End of assembler dump.
(gdb) disass printf
Dump of assembler code for function printf:
0x00401460 <printf+0>: jmp *0x4040a4
0x00401466 <printf+6>: nop
0x00401467 <printf+7>: nop
0x00401468 <printf+8>: add %al,(%eax)
0x0040146a <printf+10>: add %al,(%eax)
0x0040146c <printf+12>: add %al,(%eax)
0x0040146e <printf+14>: add %al,(%eax) ;这个看不懂:(
End of assembler dump.
**下面是源程序*****************************************
(gdb) l
1 #include <stdlib.h>
2 main() {
3 int i = 0, j = 0;
4 i += 1000;
5 j = j + 1000;
6 printf("i:%d,j:%d",i,j);
7 }
(gdb)
连这么简单的程序都这么多优化,可想而知,编译器是多么的强大:)