C語言函數(shù)調(diào)用分析
代碼:
- #include
- typedef struct{
- doubled;
- float f;
- inti;
- char c;
- }return_value;
- return_value my_test_pass(return_value pass)
- {
- return_value rv;
- rv.d=pass.d;
- rv.f=pass.f;
- rv.i=pass.i;
- rv.c=pass.c;
- return rv;
- }
- return_value my_test_of_return()
- {
- return_value rv;
- rv.d=12.56;
- rv.f=3.1;
- rv.i=10;
- rv.c=a;
- return rv;
- }
- intmain()
- {
- return_value local=my_test_of_return();
- return_value local1=my_test_pass(local);
- return 0;
- }
編譯和反匯編過程:
[gong@Gong-Computer deeplearn]$ gcc -g structpass.c -o structpass
[gong@Gong-Computer deeplearn]$ objdump -S -d structpass > structpass_s
[gong@Gong-Computer deeplearn]$ objdump -S -d structpass > structpass_s
- ...
- int main()
- {
- 804841d: 8d 4c 24 04 lea 0x4(%esp),%ecx
- 8048421: 83 e4 f8 and $0xfffffff8,%esp
- 8048424: ff 71 fc pushl -0x4(%ecx)
- 8048427: 55 push %ebp
- 8048428: 89 e5 mov %esp,%ebp
- 804842a: 51 push %ecx
- 804842b: 83 ec 4c sub $0x4c,%esp
- return_value local = my_test_of_return();
- 804842e: 8d 45 e0 lea -0x20(%ebp),%eax
- 8048431: 89 04 24 mov %eax,(%esp)
- 8048434: e8 9e ff ff ff call 80483d7
- 8048439: 83 ec 04 sub $0x4,%esp
- return_value local1 = my_test_pass(local);
- 804843c: 8d 45 c8 lea -0x38(%ebp),%eax
- 804843f: 8b 55 e0 mov -0x20(%ebp),%edx
- 8048442: 89 54 24 04 mov %edx,0x4(%esp)
- 8048446: 8b 55 e4 mov -0x1c(%ebp),%edx
- 8048449: 89 54 24 08 mov %edx,0x8(%esp)
- 804844d: 8b 55 e8 mov -0x18(%ebp),%edx
- 8048450: 89 54 24 0c mov %edx,0xc(%esp)
- 8048454: 8b 55 ec mov -0x14(%ebp),%edx
- 8048457: 89 54 24 10 mov %edx,0x10(%esp)
- 804845b: 8b 55 f0 mov -0x10(%ebp),%edx
- 804845e: 89 54 24 14 mov %edx,0x14(%esp)
- 8048462: 89 04 24 mov %eax,(%esp)
- 8048465: e8 2a ff ff ffcall8048394
- 804846a: 83 ec 04 sub $0x4,%esp
- return 0;
- 804846d: b8 00 00 00 00 mov $0x0,%eax
- }
由上面的反匯編代碼可以知道結(jié)構(gòu)體的傳遞參數(shù)是依據(jù)堆棧實現(xiàn)的。這也說明了多參數(shù)的傳遞過程并不是按著固定的模式實現(xiàn)的,這也是我們需要注意的問題。參數(shù)的傳遞需要根據(jù)實際情況分析。
總結(jié):
函數(shù)的調(diào)用是有一定的方式的,各個函數(shù)都有一定的堆??臻g,而且每一個堆??臻g的分布情況也是類似的,但是大小要根據(jù)實際的情況分析。一般一個函數(shù)的堆??臻g中包含下面幾個部分:1、棧幀(用來表示該堆??臻g的棧底,也就是指開始的地址EBP),局部變量的空間,下一個被調(diào)用函數(shù)的參數(shù)傳遞,最后是返回地址(實質(zhì)上也是一個EBP)。就是依據(jù)EBP和相對位置就能知道每一個函數(shù)的基本分布,而ESP就能知道堆棧空間的大小。
被調(diào)用參數(shù)的獲取主要是依據(jù)EBP指針的相對位置獲得,因為被調(diào)用函數(shù)的堆??臻g上一個堆棧空間就是調(diào)用函數(shù)的堆??臻g。根據(jù)函數(shù)的棧幀指針(EBP)和相對位置(-4,-8等)找到對應的參數(shù),但是相對位置也是不固定的,這需要考慮結(jié)構(gòu)體的對齊等方式,具體的要在實際中計算。
返回值一般都是采用EAX返回的,但是對于結(jié)構(gòu)體等則是采用堆棧的方式一個元算一個元素的返回的,但是還是運用了EAX的特性。
函數(shù)調(diào)用的分布打開如下:
評論