C語(yǔ)言函數調用分析
代碼:
- #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;
- }
編譯和反匯編過(guò)程:
[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
- }
由上面的反匯編代碼可以知道結構體的傳遞參數是依據堆棧實(shí)現的。這也說(shuō)明了多參數的傳遞過(guò)程并不是按著(zhù)固定的模式實(shí)現的,這也是我們需要注意的問(wèn)題。參數的傳遞需要根據實(shí)際情況分析。
總結:
函數的調用是有一定的方式的,各個(gè)函數都有一定的堆??臻g,而且每一個(gè)堆??臻g的分布情況也是類(lèi)似的,但是大小要根據實(shí)際的情況分析。一般一個(gè)函數的堆??臻g中包含下面幾個(gè)部分:1、棧幀(用來(lái)表示該堆??臻g的棧底,也就是指開(kāi)始的地址EBP),局部變量的空間,下一個(gè)被調用函數的參數傳遞,最后是返回地址(實(shí)質(zhì)上也是一個(gè)EBP)。就是依據EBP和相對位置就能知道每一個(gè)函數的基本分布,而ESP就能知道堆??臻g的大小。
被調用參數的獲取主要是依據EBP指針的相對位置獲得,因為被調用函數的堆??臻g上一個(gè)堆??臻g就是調用函數的堆??臻g。根據函數的棧幀指針(EBP)和相對位置(-4,-8等)找到對應的參數,但是相對位置也是不固定的,這需要考慮結構體的對齊等方式,具體的要在實(shí)際中計算。
返回值一般都是采用EAX返回的,但是對于結構體等則是采用堆棧的方式一個(gè)元算一個(gè)元素的返回的,但是還是運用了EAX的特性。
函數調用的分布打開(kāi)如下:
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放

關(guān)鍵詞:
C語(yǔ)言函數調
相關(guān)推薦
技術(shù)專(zhuān)區
- FPGA
- DSP
- MCU
- 示波器
- 步進(jìn)電機
- Zigbee
- LabVIEW
- Arduino
- RFID
- NFC
- STM32
- Protel
- GPS
- MSP430
- Multisim
- 濾波器
- CAN總線(xiàn)
- 開(kāi)關(guān)電源
- 單片機
- PCB
- USB
- ARM
- CPLD
- 連接器
- MEMS
- CMOS
- MIPS
- EMC
- EDA
- ROM
- 陀螺儀
- VHDL
- 比較器
- Verilog
- 穩壓電源
- RAM
- AVR
- 傳感器
- 可控硅
- IGBT
- 嵌入式開(kāi)發(fā)
- 逆變器
- Quartus
- RS-232
- Cyclone
- 電位器
- 電機控制
- 藍牙
- PLC
- PWM
- 汽車(chē)電子
- 轉換器
- 電源管理
- 信號放大器
評論