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

从汇编的角度分析C语言

函数的调用过程

[日期:2012-02-20] 来源:Linux社区  作者:sg131971 [字体:大 中 小]

main.c

[cpp]
  1. #include <stdlib.h>   
  2. #include <stdio.h>   
  3.   
  4. int add(int a, int b)  
  5. {  
  6.   int c;  
  7.   c = a;  
  8.   c += b;  
  9.   return c;  
  10. }  
  11.   
  12. int main()  
  13. {  
  14.     int a, b, c;  
  15.     a = 0x12;  
  16.     b = 0x34;  
  17.     c = add(a, b);  
  18.     return 0;  
  19. }  
main.s

[html]
  1.     .file   "main.c"  
  2.     .text  
  3. .globl add  
  4.     .type   add, @function  
  5. add:  
  6.     pushl   %ebp  
  7.     movl    %esp, %ebp  
  8.     subl    $16, %esp  
  9.     movl    8(%ebp), %eax  
  10.     movl    %eax, -4(%ebp)  
  11.     movl    12(%ebp), %eax  
  12.     addl    %eax, -4(%ebp)  
  13.     movl    -4(%ebp), %eax  
  14.     leave  
  15.     ret  
  16.     .size   add, .-add  
  17. .globl main  
  18.     .type   main, @function  
  19. main:  
  20.     pushl   %ebp  
  21.     movl    %esp, %ebp  
  22.     subl    $24, %esp  
  23.     movl    $18, -4(%ebp)  
  24.     movl    $52, -8(%ebp)  
  25.     movl    -8(%ebp), %eax  
  26.     movl    %eax, 4(%esp)  
  27.     movl    -4(%ebp), %eax  
  28.     movl    %eax, (%esp)  
  29.     call    add  
  30.     movl    %eax, -12(%ebp)  
  31.     movl    $0, %eax  
  32.     leave  
  33.     ret  
  34.     .size   main, .-main  
  35.     .ident  "GCC: (GNU) 4.5.1 20100924 (Red Hat 4.5.1-4)"  
  36.     .section    .note.GNU-stack,"",@progbits  
gdb调试:

[html]
  1. (gdb) disassemble main  
  2. Dump of assembler code for function main:  
  3.    0x080483ab <+0>:     push   %ebp  
  4.    0x080483ac <+1>:     mov    %esp,%ebp  
  5.    0x080483ae <+3>:     sub    $0x18,%esp  
  6.    0x080483b1 <+6>:     movl   $0x12,-0x4(%ebp)  
  7.    0x080483b8 <+13>:    movl   $0x34,-0x8(%ebp)  
  8.    0x080483bf <+20>:    mov    -0x8(%ebp),%eax  
  9.    0x080483c2 <+23>:    mov    %eax,0x4(%esp)  
  10.    0x080483c6 <+27>:    mov    -0x4(%ebp),%eax  
  11.    0x080483c9 <+30>:    mov    %eax,(%esp)  
  12.    0x080483cc <+33>:    call   0x8048394 <add>  
  13.    0x080483d1 <+38>:    mov    %eax,-0xc(%ebp)  
  14.    0x080483d4 <+41>:    mov    $0x0,%eax  
  15.    0x080483d9 <+46>:    leave    
  16.    0x080483da <+47>:    ret      
  17. End of assembler dump.  
  18. (gdb) list  
  19. 6         int c;  
  20. 7         c = a;  
  21. 8         c += b;  
  22. 9         return c;  
  23. 10      }  
  24. 11  
  25. 12      int main()  
  26. 13      {  
  27. 14          int a, b, c;  
  28. 15          a = 0x12;  
  29. 16          b = 0x34;  
  30. 17          c = add(a, b);  
  31. 18          return 0;  
  32. 19      }  
  33. (gdb) break 17  
  34. Breakpoint 1 at 0x80483bf: file main.c, line 17.  
  35. (gdb) run  
  36. Starting program: /home/my_project/test/main   
  37.   
  38. Breakpoint 1, main () at main.c:17  
  39. 17          c = add(a, b);  
  40. Missing separate debuginfos, use: debuginfo-install glibc-2.12.90-17.i686  
  41. (gdb) info registers esp  
  42. esp            0xbffff1e0       0xbffff1e0  
  43. (gdb) info registers ebp  
  44. ebp            0xbffff1f8       0xbffff1f8  
  45. (gdb) x/6xw 0xbffff1e0  
  46. 0xbffff1e0:     0x080483e0      0x080482e0      0x080483eb      0x00394ff4  
  47. 0xbffff1f0:     0x00000034      0x00000012  
  48. (gdb) step  
  49. add (a=18, b=52) at main.c:7  
  50. 7         c = a;  
  51. (gdb) x/6xw 0xbffff1e0  
  52. 0xbffff1e0:     0x00000012      0x00000034      0x080483eb      0x00394ff4  
  53. 0xbffff1f0:     0x00000034      0x00000012  
  54. (gdb) info registers esp  
  55. esp            0xbffff1c8       0xbffff1c8  
  56. (gdb) info registers ebp  
  57. ebp            0xbffff1d8       0xbffff1d8  
  58. (gdb) tep  
  59. Undefined command: "tep".  Try "help".  
  60. (gdb) step  
  61. 8         c += b;  
  62. (gdb) step  
  63. 9         return c;  
  64. (gdb) x/16xw 0xbffff1c0  
  65. 0xbffff1c0:     0x00235f95      0x08049624      0xbffff1f8      0x080483f9  
  66. 0xbffff1d0:     0x08048215      0x00000046      0xbffff1f8      0x080483d1  
  67. 0xbffff1e0:     0x00000012      0x00000034      0x080483eb      0x00394ff4  
  68. 0xbffff1f0:     0x00000034      0x00000012      0xbffff278      0x0021ce16  
  69. (gdb)step  
  70. 10      }  
  71. (gdb) step  
  72. main () at main.c:18  
  73. 18          return 0;  
  74. (gdb) info registers esp  
  75. esp            0xbffff1e0       0xbffff1e0  
  76. (gdb) info registers ebp  
  77. ebp            0xbffff1f8       0xbffff1f8  
  78. (gdb)   
具体堆栈情况如下图所示:


红色是main函数分配的堆栈,绿色是add函数分配的堆栈

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

       

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