Linux汇编---函数调用过程


或许习惯于用高级语言编程的大部分同学都会忽略了函数调用的具体过程是怎样的,如果想知道这个过程就不得不从汇编入手,但汇编语言又晦涩难懂。在这里谨以一个简单的例子说说我对函数调用过程的学习心得。

先上C语言写的代码:

#include<stdio.h>
 
 
 unsigned int test(int a,int b)
 {
     int c,d;
     c = a;
     d = b;
     return c;
 }
 
 int main()
 {
     unsigned int r;
 
     r = test(1,2);
    
     return 0;
 }

很简单,就是在main()函数里调用test()函数。通过下面的命令编译:

gcc -g -o test test.c    //加-g选项是为了反编译时可以混合显示源码和汇编代码

再通过以下命令将test反编译:

objdump -d -S test

截取其中反编译后的一个片段,如下:

08048394 <test>:
 #include<stdio.h>
 
 
 unsigned int test(int a,int b)
 {
  8048394:    55                       push   %ebp
  8048395:    89 e5                    mov    %esp,%ebp
  8048397:    83 ec 10                 sub    $0x10,%esp
     int c,d;
     c = a;
  804839a:    8b 45 08                 mov    0x8(%ebp),%eax
  804839d:    89 45 fc                 mov    %eax,-0x4(%ebp)
     d = b;
  80483a0:    8b 45 0c                 mov    0xc(%ebp),%eax
  80483a3:    89 45 f8                 mov    %eax,-0x8(%ebp)
     return c;
  80483a6:    8b 45 fc                 mov    -0x4(%ebp),%eax
 }
  80483a9:    c9                       leave 
  80483aa:    c3                       ret   
 
 080483ab <main>:
 
 int main()
 {
  80483ab:    55                       push   %ebp
  80483ac:    89 e5                    mov    %esp,%ebp
  80483ae:    83 ec 18                 sub    $0x18,%esp
     unsigned int r;
 
     r = test(1,2);
  80483b1:    c7 44 24 04 02 00 00     movl   $0x2,0x4(%esp)
  80483b8:    00
  80483b9:    c7 04 24 01 00 00 00     movl   $0x1,(%esp)
  80483c0:    e8 cf ff ff ff           call   8048394 <test>
  80483c5:    89 45 fc                 mov    %eax,-0x4(%ebp)
    
     return 0;
  80483c8:    b8 00 00 00 00           mov    $0x0,%eax
 }
  80483cd:    c9                       leave 
  80483ce:    c3                       ret   
  80483cf:    90                       nop

  • 1
  • 2
  • 下一页

相关内容