手机版
你好,游客 登录 注册
背景:
阅读新闻

从汇编的角度分析C语言

gcc代码优化

[日期:2012-02-20] 来源:Linux社区  作者:sg131971 [字体:大 中 小]
  1. #include <stdlib.h>   
  2. #include <stdio.h>   
  3.   
  4. int main()  
  5. {  
  6.     int m = 1;  
  7.     int n = 2;  
  8.     int r = m + n;  
  9.     return r;  
  10. }  

未使用优化:

[root@ShiGuang test]# gcc -S main.c

[html]
  1.     .file   "main.c"  
  2.     .text  
  3. .globl main  
  4.     .type   main, @function  
  5. main:  
  6.     pushl   %ebp  
  7.     movl    %esp, %ebp  
  8.     subl    $16, %esp  
  9.     movl    $1, -4(%ebp)  
  10.     movl    $2, -8(%ebp)  
  11.     movl    -8(%ebp), %eax  
  12.     movl    -4(%ebp), %edx  
  13.     leal    (%edx,%eax), %eax  
  14.     movl    %eax, -12(%ebp)  
  15.     movl    -12(%ebp), %eax  
  16.     leave  
  17.     ret  
  18.     .size   main, .-main  
  19.     .ident  "GCC: (GNU) 4.5.1 20100924 (Red Hat 4.5.1-4)"  
  20.     .section    .note.GNU-stack,"",@progbits  
优化处理:

[root@ShiGuang test]# gcc -S main.c -O2

[html]
  1. .file   "main.c"  
  2. .text  
  3. .p2align 4,,15  
  4. globl main  
  5. .type   main, @function  
  6. ain:  
  7. pushl   %ebp  
  8. movl    $3, %eax  
  9. movl    %esp, %ebp  
  10. popl    %ebp  
  11. ret  
  12. .size   main, .-main  
  13. .ident  "GCC: (GNU) 4.5.1 20100924 (Red Hat 4.5.1-4)"  
  14. .section    .note.GNU-stack,"",@progbits  
优化结果还是很明显的!gcc对常数的合并与常数的传播,具有较好的优化处理。

我们从上面的两段汇编代码就能看出。

linux
【内容导航】
第1页:char *p与char p[]的区别 第2页:gcc代码优化
第3页:指针的赋值 第4页:函数的调用过程
第5页:函数指针与指针的指针 第6页:switch和if else效率分析
相关资讯       C语言  C 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款