https://www.cnblogs.com/xiangtingshen/p/11014514.html

C++函数调用约定
_cdecl约定:
参数:从右向左依次入栈
堆栈平衡:调用方平衡

  1. #include "pch.h"
  2. #include <iostream>
  3.  
  4. int __cdecl getMaxNumber(int a, int b, int c)
  5. {
  6. int temp = a > b ? a : b;
  7. return temp > c ? temp : c;
  8. }
  9.  
  10. int main()
  11. {
  12. int iMax = ;
  13. iMax = getMaxNumber(, , );
  14. printf("iMAx = %d\n", iMax);
  15. }
  1. .text: sub_411980 proc near ; CODE XREF: sub_4112B7j
  2. .text:
  3. .text: var_CC = byte ptr -0CCh
  4. .text: var_8 = dword ptr -
  5. .text:
  6. .text: push ebp
  7. .text: mov ebp, esp
  8. .text: sub esp, 0CCh
  9. .text: push ebx
  10. .text:0041198A push esi
  11. .text:0041198B push edi
  12. .text:0041198C lea edi, [ebp+var_CC]
  13. .text: mov ecx, 33h
  14. .text: mov eax, 0CCCCCCCCh
  15. .text:0041199C rep stosd
  16. .text:0041199E mov ecx, offset unk_41C009
  17. .text:004119A3 call sub_411221
  18. .text:004119A8 mov [ebp+var_8],
  19. .text:004119AF push 16 // 从右到左入栈
  20. .text:004119B1 push 30
  21. .text:004119B3 push 10
  22. .text:004119B5 call sub_411087
  23. .text:004119BA add esp, 0Ch // 平衡堆栈
  24. .text:004119BD mov [ebp+var_8], eax
  25. .text:004119C0 mov eax, [ebp+var_8]
  26. .text:004119C3 push eax
  27. .text:004119C4 push offset aImaxD ; "iMAx = %d\n"
  28. .text:004119C9 call sub_41104B
  29. .text:004119CE add esp,
  30. .text:004119D1 xor eax, eax
  31. .text:004119D3 pop edi
  32. .text:004119D4 pop esi
  33. .text:004119D5 pop ebx
  34. .text:004119D6 add esp, 0CCh
  35. .text:004119DC cmp ebp, esp
  36. .text:004119DE call sub_41122B
  37. .text:004119E3 mov esp, ebp
  38. .text:004119E5 pop ebp
  39. .text:004119E6 retn
  40. .text:004119E6 sub_411980 endp

函数内部

push 参数一 //参数入栈顺序:从右向左依次入栈
push 参数二
push 。。。
push 参数N
call Fun //调用函数
add esp,XX //堆栈平衡

_stdcall约定:
参数:从右向左依次入栈
堆栈平衡:被调用方自己平衡

  1. #include "pch.h"
  2. #include <iostream>
  3.  
  4. int __stdcall getMaxNumber(int a, int b, int c)
  5. {
  6. int temp = a > b ? a : b;
  7. return temp > c ? temp : c;
  8. }
  9.  
  10. int main()
  11. {
  12. int iMax = ;
  13. iMax = getMaxNumber(, , );
  14. printf("iMAx = %d\n", iMax);
  15. }
  1. .text:004119A8 mov [ebp+var_8],
  2. .text:004119AF push
  3. .text:004119B1 push
  4. .text:004119B3 push
  5. .text:004119B5 call sub_411393
  6. .text:004119BA mov [ebp+var_8], eax
  7. .text:004119BD mov eax, [ebp+var_8]
  8. .text:004119C0 push eax
  9. .text:004119C1 push offset aImaxD ; "iMAx = %d\n"
  10. .text:004119C6 call sub_41104B // 在函数内部恢复堆栈
  11. .text:004119CB add esp,
  12. .text:004119CE xor eax, eax
  13. .text:004119D0 pop edi
  14. .text:004119D1 pop esi
  15. .text:004119D2 pop ebx
  16. .text:004119D3 add esp, 0CCh
  17. .text:004119D9 cmp ebp, esp
  18. .text:004119DB call sub_41122B
  19. .text:004119E0 mov esp, ebp
  20. .text:004119E2 pop ebp
  21. .text:004119E3 retn
  22. .text:004119E3 sub_411980 endp

函数

  1. text:004117D0 var_D0 = dword ptr -0D0h
  2. .text:004117D0 var_8 = dword ptr -
  3. .text:004117D0 arg_0 = dword ptr
  4. .text:004117D0 arg_4 = dword ptr 0Ch
  5. .text:004117D0 arg_8 = dword ptr 10h
  6. .text:004117D0
  7. .text:004117D0 push ebp
  8. .text:004117D1 mov ebp, esp
  9. .text:004117D3 sub esp, 0D0h
  10. .text:004117D9 push ebx
  11. .text:004117DA push esi
  12. .text:004117DB push edi
  13. .text:004117DC lea edi, [ebp+var_D0]
  14. .text:004117E2 mov ecx, 34h
  15. .text:004117E7 mov eax, 0CCCCCCCCh
  16. .text:004117EC rep stosd
  17. .text:004117EE mov ecx, offset unk_41C009
  18. .text:004117F3 call sub_411221
  19. .text:004117F8 mov eax, [ebp+arg_0]
  20. .text:004117FB cmp eax, [ebp+arg_4]
  21. .text:004117FE jle short loc_41180B
  22. .text: mov ecx, [ebp+arg_0]
  23. .text: mov [ebp+var_D0], ecx
  24. .text: jmp short loc_411814
  25. .text:0041180B ; ---------------------------------------------------------------------------
  26. .text:0041180B
  27. .text:0041180B loc_41180B: ; CODE XREF: sub_4117D0+2Ej
  28. .text:0041180B mov edx, [ebp+arg_4]
  29. .text:0041180E mov [ebp+var_D0], edx
  30. .text:
  31. .text: loc_411814: ; CODE XREF: sub_4117D0+39j
  32. .text: mov eax, [ebp+var_D0]
  33. .text:0041181A mov [ebp+var_8], eax
  34. .text:0041181D mov eax, [ebp+var_8]
  35. .text: cmp eax, [ebp+arg_8]
  36. .text: jle short loc_411830
  37. .text: mov ecx, [ebp+var_8]
  38. .text: mov [ebp+var_D0], ecx
  39. .text:0041182E jmp short loc_411839
  40. .text: ; ---------------------------------------------------------------------------
  41. .text:
  42. .text: loc_411830: ; CODE XREF: sub_4117D0+53j
  43. .text: mov edx, [ebp+arg_8]
  44. .text: mov [ebp+var_D0], edx
  45. .text:
  46. .text: loc_411839: ; CODE XREF: sub_4117D0+5Ej
  47. .text: mov eax, [ebp+var_D0]
  48. .text:0041183F pop edi
  49. .text: pop esi
  50. .text: pop ebx
  51. .text: add esp, 0D0h
  52. .text: cmp ebp, esp
  53. .text:0041184A call sub_41122B
  54. .text:0041184F mov esp, ebp
  55. .text: pop ebp
  56. .text: retn 0Ch // 恢复堆栈
  57. .text: sub_4117D0 endp
  58. .text:

push 参数一 //参数入栈顺序:从右向左依次入栈
push 参数二
push 。。。
push 参数N
call Fun //调用函数,在函数内进行堆栈平衡,retn XX

_fastcall约定:
参数:从右向左依次入栈,当参数不大于二的时候一般把参数放到edx,ecx里面,大于二的时候参数放在堆栈里面
堆栈平衡:被调用方自己平衡

  1. .text: var_CC = byte ptr -0CCh
  2. .text: var_8 = dword ptr -
  3. .text:
  4. .text: push ebp
  5. .text: mov ebp, esp
  6. .text: sub esp, 0CCh
  7. .text: push ebx
  8. .text:0041198A push esi
  9. .text:0041198B push edi
  10. .text:0041198C lea edi, [ebp+var_CC]
  11. .text: mov ecx, 33h
  12. .text: mov eax, 0CCCCCCCCh
  13. .text:0041199C rep stosd
  14. .text:0041199E mov ecx, offset unk_41C009
  15. .text:004119A3 call sub_411221
  16. .text:004119A8 mov [ebp+var_8],
  17. .text:004119AF push 16
  18. .text:004119B1 mov edx, 30
  19. .text:004119B6 mov ecx, 10
  20. .text:004119BB call sub_411398
  21. .text:004119C0 mov [ebp+var_8], eax
  22. .text:004119C3 mov eax, [ebp+var_8]
  23. .text:004119C6 push eax
  24. .text:004119C7 push offset aImaxD ; "iMAx = %d\n"
  25. .text:004119CC call sub_41104B
  26. .text:004119D1 add esp,
  27. .text:004119D4 xor eax, eax
  28. .text:004119D6 pop edi
  29. .text:004119D7 pop esi
  30. .text:004119D8 pop ebx
  31. .text:004119D9 add esp, 0CCh
  32. .text:004119DF cmp ebp, esp
  33. .text:004119E1 call sub_41122B
  34. .text:004119E6 mov esp, ebp
  35. .text:004119E8 pop ebp
  36. .text:004119E9 retn
  37. .text:004119E9 sub_411980 endp

函数内部

  1. text:004117D0 sub_4117D0 proc near ; CODE XREF: .text:00411087j
  2. .text:004117D0 ; .text:00411393j ...
  3. .text:004117D0
  4. .text:004117D0 var_E8 = dword ptr -0E8h
  5. .text:004117D0 var_20 = dword ptr -20h
  6. .text:004117D0 var_14 = dword ptr -14h
  7. .text:004117D0 var_8 = dword ptr -
  8. .text:004117D0 arg_0 = dword ptr
  9. .text:004117D0
  10. .text:004117D0 push ebp
  11. .text:004117D1 mov ebp, esp
  12. .text:004117D3 sub esp, 0E8h
  13. .text:004117D9 push ebx
  14. .text:004117DA push esi
  15. .text:004117DB push edi
  16. .text:004117DC push ecx
  17. .text:004117DD lea edi, [ebp+var_E8]
  18. .text:004117E3 mov ecx, 3Ah
  19. .text:004117E8 mov eax, 0CCCCCCCCh
  20. .text:004117ED rep stosd
  21. .text:004117EF pop ecx
  22. .text:004117F0 mov [ebp+var_14], edx
  23. .text:004117F3 mov [ebp+var_8], ecx
  24. .text:004117F6 mov ecx, offset unk_41C009
  25. .text:004117FB call sub_411221
  26. .text: mov eax, [ebp+var_8]
  27. .text: cmp eax, [ebp+var_14]
  28. .text: jle short loc_411813
  29. .text: mov ecx, [ebp+var_8]
  30. .text:0041180B mov [ebp+var_E8], ecx
  31. .text: jmp short loc_41181C
  32. .text: ; ---------------------------------------------------------------------------
  33. .text:
  34. .text: loc_411813: ; CODE XREF: sub_4117D0+36j
  35. .text: mov edx, [ebp+var_14]
  36. .text: mov [ebp+var_E8], edx
  37. .text:0041181C
  38. .text:0041181C loc_41181C: ; CODE XREF: sub_4117D0+41j
  39. .text:0041181C mov eax, [ebp+var_E8]
  40. .text: mov [ebp+var_20], eax
  41. .text: mov eax, [ebp+var_20]
  42. .text: cmp eax, [ebp+arg_0]
  43. .text:0041182B jle short loc_411838
  44. .text:0041182D mov ecx, [ebp+var_20]
  45. .text: mov [ebp+var_E8], ecx
  46. .text: jmp short loc_411841
  47. .text: ; ---------------------------------------------------------------------------
  48. .text:
  49. .text: loc_411838: ; CODE XREF: sub_4117D0+5Bj
  50. .text: mov edx, [ebp+arg_0]
  51. .text:0041183B mov [ebp+var_E8], edx
  52. .text:
  53. .text: loc_411841: ; CODE XREF: sub_4117D0+66j
  54. .text: mov eax, [ebp+var_E8]
  55. .text: pop edi
  56. .text: pop esi
  57. .text: pop ebx
  58. .text:0041184A add esp, 0E8h
  59. .text: cmp ebp, esp
  60. .text: call sub_41122B
  61. .text: mov esp, ebp
  62. .text: pop ebp
  63. .text:0041185A retn 4
  64. .text:0041185A sub_4117D0 endp

push 参数一 //参数入栈顺序:从右向左依次入栈
push 参数二
push 。。。
push edx
push ecx //寄存器传递
call Fun //调用函数,在函数内进行堆栈平衡,retn XX

naked约定:
参数:从右向左依次入栈
堆栈平衡:调用方平衡

push 参数一 //参数入栈顺序:从右向左依次入栈
push 参数二
push 。。。
push 参数N
call Fun //调用函数
add esp,XX //堆栈平衡

C++函数调用方式约定stdcall,cdecl,pascal,naked,thiscall,fastcall的更多相关文章

  1. stdcall, cdecl, pascal 区别(转载)

    转载自:http://www.cnblogs.com/lidabo/archive/2012/11/21/2781484.html stdcall, cdecl, pascal 区别 这三个参数都是告 ...

  2. Atitit 函数调用的原理与本质attilax总结 stdcall cdecl区别

    Atitit 函数调用的原理与本质attilax总结 stdcall cdecl区别 通常来说函数调用要用到的两条基本的指令:”CALL”指令和”RET”指令.”CALL”指令将当前的指令指针(这个指 ...

  3. 函数调用方式--__thiscall调用方式和__cdecl,__stdcall有什么区别

    函数调用方式--__thiscall调用方式和__cdecl,__stdcall有什么区别 首先,__thiscall是关于类的一种调用方式,它与其他调用方式的最大区别是:    __thiscall ...

  4. (转)函数调用方式与extern "C"

    原文:http://patmusing.blog.163.com/blog/static/13583496020103233446784/ (VC编译器下) 1. CALLBACK,WINAPI和AF ...

  5. 函数调用方式__stdcall、__cdel

    函数调用方式关系对比如下: 关键字 调用规则 参数传递方向 返回 参数寄存器 堆栈的清除 __cdecl C语言 从右向左 EAX 无 调用者 __stdcall Win32标准  从右向左 EAX ...

  6. 64位只有一种调用约定stdcall

    procedure TForm2.Button1Click(Sender: TObject); function EnumWindowsProc(Ahwnd: hwnd; AlParam: lPara ...

  7. JavaScript中七种函数调用方式及对应 this 的含义

    this 在 JavaScript 开发中占有相当重要的地位,不过很多人对this这个东西都感觉到琢磨不透.要真正理解JavaScript的函数机制,就非常有必要搞清楚this到底是怎么回事. 函数调 ...

  8. 前端JS面试题汇总 Part 3 (宿主对象与原生对象/函数调用方式/call与apply/bind/document.write)

    原文:https://github.com/yangshun/front-end-interview-handbook/blob/master/questions/javascript-questio ...

  9. 【Python】利用递归函数调用方式,将所输入的字符串,以相反的顺序显示出来

    源代码: """ 利用递归函数调用方式,将所输入的字符串,以相反的顺序显示出来 string_reverse_output():反向输出字符串的自定义函数 pending ...

随机推荐

  1. idea单行注释优化成不在行首注释

  2. JAVA线程Disruptor核心链路应用(八)

    import java.util.concurrent.atomic.AtomicInteger; /** * Disruptor中的 Event * @author Alienware * */ p ...

  3. 通俗易懂的join、left join、right join、full join、cross join

    内连接:列出与连接条件匹配的数据行(join\inner join) 外连接:两表合并,如有不相同的列,另外一个表显示null(left join\right join\full outer join ...

  4. sharepoint中的Power Shell命令创建、删除文档库列表

    ListTemplateType 枚举: 自定义列表-GenericList.文档库-DocumentLibrary.图片库-PictureLibrary.公告-Announcements.联系人-C ...

  5. Postgres 多实例实例部署方式

    Postgres 数据库在原有示例正常运行情况下,新增一个端口示例,主要目的解决新的项目和原有项目的数据库部署不在冲突,可以独立运行,备份和还原数据互不影响,主要用的的命令有  initdb 数据库初 ...

  6. UI5-技术篇-签字板

    签字板应用是通过创建自定义控件实现的,相关代码如下: 1.HTML <!DOCTYPE HTML> <html> <head> <meta http-equi ...

  7. Tortoise SVN常见图标含义及图标无法正常解决方法!

    转自:https://blog.csdn.net/xh16319/article/details/10582455 绿色的勾:图标表示这是一个最新取出的工作副本,他的Subversion状态是norm ...

  8. iOS加密之AES

    心急的童鞋直接看这里Demo 运行之后可以去在线加密网站验证 AES(Advanced Encryption Standard)高级加密标准,又称Rijndael加密法,是美国联邦政府采用的一种区块加 ...

  9. Linux环境下安装RabbitMQ

    首先RabbitMQ是使用erLang编写的开源消息中间件.所以需要先安装erlang环境. 我使用的是CentOS的系统安装erlang21.0的步骤如下: #下载安装包 (下面是我用的比较匹配的版 ...

  10. MaxScale ERROR 2006 (HY000): MySQL server has gone away

    Error: MaxScale cannot be run as root.Failed to write child process message!解决办法:# maxscale -f /etc/ ...