5. 函数调用过程汇编分析

函数调用约定

image.png

__cdecl 调用方式

image.png

__stdcall 调用方式

image.png

__fastcall 调用方式

image.png

函数调用栈帧分析

image.png

补充说明

  1. 不同的编译器实现不一样,上述情况只是VC++6.0的编译实现
  2. 即便是在同一个编译器,开启优化和关闭优化也不一样
  3. 即便是同一个编译器同一种模式,32位和64位下情况也会不一样

fastcall 实例分析

[[gnu::fastcall]] int fun1(int a, int b, int c, int d) {
  return a * a + b * b + 2 * a * b + c + d;
}
int main() {
  int c = fun1(3, 4, 5, 6);
  return 0;
}
#!/bin/bash
set -ex

/mnt/e/code/llvm-project/build/bin/clang fastcallTest.c -o fastcallTest -O0 -g -m32 -c
# 编译生成汇编代码
objdump -dS fastcallTest &> fastcallTest.asm
# sudo apt-get install -y build-essential module-assistant  
# sudo apt-get install -y gcc-multilib g++-multilib  

参考 GCC GNU 文档属性描述

fastcall
On x86-32 targets, the fastcall attribute causes the compiler to pass the first argument (if of integral type) in the register ECX and the second argument (if of integral type) in the register EDX. Subsequent and other typed arguments are passed on the stack. The called function pops the arguments off the stack. If the number of arguments is variable all arguments are pushed on the stack.

翻译: 在 x86-32 目标机上,fastcall 属性会导致编译器在寄存器 ECX 中传递第一个参数(如果是 INT 类型),在寄存器 EDX 中传递第二个参数(如果是INT类型)。后面的参数和其他类型的参数会传递到堆栈中。被调用函数将参数从堆栈中弹出。如果参数个数不固定,所有参数都会被推入堆栈。

image.png

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值