歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux匯編---函數調用過程

Linux匯編---函數調用過程

日期:2017/3/1 10:20:28   编辑: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

Copyright © Linux教程網 All Rights Reserved