- 论坛徽章:
- 2
|
原帖由 mq110 于 2006-7-31 22:06 发表
__volatile__告诉编译器该条语句不被优化掉.
我记得asm关键字是直接把语句插入汇编,不做更改和优化。
我试验了下面的简单代码:
// file tt.c
#include <stdio.h>
int
f()
{
printf("nop");
asm ("nop");
asm ("nop");
printf("nop2");
return 0;
}
int
main()
{
printf("main");
f();
exit(0);
}
汇编出来,并且加最大优化
cc -S -O2 -o tt.S tt.c
.file "tt.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "nop"
.LC1:
.string "nop2"
.text
.p2align 2,,3
.globl f
.type f, @function
f:
pushl %ebp
movl %esp, %ebp
subl $20, %esp
pushl $.LC0
call printf
#APP
nop
nop
#NO_APP
movl $.LC1, (%esp)
call printf
xorl %eax, %eax
leave
ret
.size f, .-f
.section .rodata.str1.1
.LC2:
.string "main"
.text
.p2align 2,,3
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
subl $28, %esp
pushl $.LC2
call printf
call f
movl $0, (%esp)
call exit
.size main, .-main
.ident "GCC: (GNU) 3.4.4 [FreeBSD] 20050518"
红色表示并没有优化掉啊。
环境:
1 GCC: (GNU) 3.4.4 [FreeBSD] 20050518
2 GCC 3.4.6, Zenwalk Linux 2.6.16
[ 本帖最后由 gvim 于 2006-7-31 22:32 编辑 ] |
|