调用约定

调用约定

规定了参数的传递

一、常见的调用约定

调用约定 参数压栈顺序 平衡堆栈
__cdecl 从右至左入栈 调用者清理栈
__stdcall 从右至左入栈 自身清理堆栈
__fastcall ECX/EDX传送前两个
剩下的从右至左入栈
自身清理堆栈

__cdecl为默认调用约定

二、各调用约定的区别

实验代码如下

#include "stdafx.h"

int __cdecl plus1(int a, int b) {
	return a+b;
}

int __stdcall plus2(int a, int b) {
	return a+b;
}

int __fastcall plus3(int a, int b) {
	return a+b;
}

int main(int argc, char* argv[]) {
	int num;
	num = plus1(1, 2);
	//num = plus2(1, 2);
	//num = plus3(1, 2);
	return 0;
}

  1. __cdecl

参数从右至左入栈;外平栈,即调用者平衡堆栈

  1. __stdcall

参数从右至左入栈;

ret 8内平栈,即自身平衡堆栈

ps: ret 8相当于ret+add esp,0x8




  1. __fastcall

前两个参数存入ecx/edx,当只有两个参数时,无需平衡堆栈

当多个参数时,从右至左入栈,前两个参数放入ecxedx;内平栈



posted @ 2021-08-16 14:11  Ybitsec  阅读(69)  评论(0)    收藏  举报