C ++ function call convention stdcall, cdecl, pascal, naked, thiscall, fastcall

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

C ++ function calling convention
_cdecl convention:
Parameters: right to left stack
stacks balance: call Fang Pingheng

#include "pch.h"
#include <iostream>

int __cdecl getMaxNumber(int a, int b, int c)
{
    int temp = a > b ? a : b;
    return temp > c ? temp : c;
}

int main()
{
    int iMax = 0;
    iMax = getMaxNumber(10, 30, 16);
    printf("iMAx = %d\n", iMax);
}

 

.text:00411980 sub_411980      proc near               ; CODE XREF: sub_4112B7↑j
.text:00411980
.text:00411980 var_CC          = byte ptr -0CCh
.text:00411980 var_8           = dword ptr -8
.text:00411980
.text:00411980                 push    ebp
.text:00411981                 mov     ebp, esp
.text:00411983                 sub     esp, 0CCh
.text:00411989                 push    ebx
.text:0041198A                 push    esi
.text:0041198B                 push    edi
.text:0041198C                 lea     edi, [ebp+var_CC]
.text:00411992                 mov     ecx, 33h
.text:00411997                 mov     eax, 0CCCCCCCCh
.text:0041199C                 rep stosd
.text:0041199E                 mov     ecx, offset unk_41C009
.text:004119A3                 call    sub_411221
.text:004119A8                 mov     [EBP + var_8], 0 
.text: // 004119AF 16 from right to left Push stack 
.text: 004119B1 30 Push 
.text: 004119B3 Push 10 
. text: 004119B5                  Call     sub_411087
 . text: 004119BA the Add      ESP, 0Ch // balance stack 
. text: 004119BD                  MOV      [EBP + var_8], EAX 
. text: 004119C0                  MOV      EAX, [EBP + var_8] 
. text: 004119C3                  Push     EAX 
. text: 004119C4                  Push     offset aImaxD    ; "D IMAX =% \ n-"
.text:004119C9                 call    sub_41104B
.text:004119CE                 add     esp, 8
.text:004119D1                 xor     eax, eax
.text:004119D3                 pop     edi
.text:004119D4                 pop     esi
.text:004119D5                 pop     ebx
.text:004119D6                 add     esp, 0CCh
.text:004119DC                 cmp     ebp, esp
.text:004119DE                 call    sub_41122B
.text:004119E3                 mov     esp, ebp
.text:004119E5                 pop     ebp
.text:004119E6                 retn
.text:004119E6 sub_411980      endp

 

Internal function

 

 

Parameters Parameter // push a stack order: right to left on the stack
push two parameters
push. . .
push parameter N
Call Fun // call the function
add esp, XX // stack balance


_stdcall convention:
Parameters: right to left stack
stacks balance: the called party's own balance

#include "pch.h"
#include <iostream>

int __stdcall getMaxNumber(int a, int b, int c)
{
    int temp = a > b ? a : b;
    return temp > c ? temp : c;
}

int main()
{
    int iMax = 0;
    iMax = getMaxNumber(10, 30, 16);
    printf("iMAx = %d\n", iMax);
}

 

 

.text:004119A8                 mov     [ebp+var_8], 0
.text:004119AF                 push    16
.text:004119B1                 push    30
.text:004119B3                 push    10
.text:004119B5                 call    sub_411393
.text:004119BA                 mov     [ebp+var_8], eax
.text:004119BD                 mov     eax, [ebp+var_8]
.text:004119C0                 push    eax
.text:004119C1                 push    aImaxD offset    ; "D IMAX =% \ n-" 
. text: 004119C6                  Call     sub_41104B // restore function within the stack 
. text: 004119CB                  the Add      ESP, . 8 
. text: 004119CE                  XOR      EAX, EAX 
. text: 004119D0                  POP      EDI 
. text: 004119D1                  POP      ESI 
. text: 004119D2                  POP      EBX 
. text: 004119D3                  the Add      ESP, 0CCh 
. text: 004119D9                  cmp       ebp, ESP
.text:004119DB                 call    sub_41122B
.text:004119E0                 mov     esp, ebp
.text:004119E2                 pop     ebp
.text:004119E3                 retn
.text:004119E3 sub_411980      endp

 

function

text:004117D0 var_D0          = dword ptr -0D0h
.text:004117D0 var_8           = dword ptr -8
.text:004117D0 arg_0           = dword ptr  8
.text:004117D0 arg_4           = dword ptr  0Ch
.text:004117D0 arg_8           = dword ptr  10h
.text:004117D0
.text:004117D0                 push    ebp
.text:004117D1                 mov     ebp, esp
.text:004117D3                 sub     esp, 0D0h
.text:004117D9                 push    ebx
.text:004117DA                 push    esi
.text:004117DB                 push    edi
.text:004117DC                 lea     edi, [ebp+var_D0]
.text:004117E2                 mov     ecx, 34h
.text:004117E7                 mov     eax, 0CCCCCCCCh
.text:004117EC                 rep stosd
.text:004117EE                 mov     ecx, offset unk_41C009
.text:004117F3                 call    sub_411221
.text:004117F8                 mov     eax, [ebp+arg_0]
.text:004117FB                 cmp     eax, [ebp+arg_4]
.text:004117FE                 jle     short loc_41180B
.text:00411800                 mov     ecx, [ebp+arg_0]
.text:00411803                 mov     [ebp+var_D0], ecx
.text:00411809                 jmp     short loc_411814
.text:0041180B ; ---------------------------------------------------------------------------
.text:0041180B
.text:0041180B loc_41180B:                             ; CODE XREF: sub_4117D0+2E↑j
.text:0041180B                 mov     edx, [ebp+arg_4]
.text:0041180E                 mov     [ebp+var_D0], edx
.text:00411814
.text:00411814 loc_411814:                             ; CODE XREF: sub_4117D0+39↑j
.text:00411814                 mov     eax, [ebp+var_D0]
.text:0041181A                 mov     [ebp+var_8], eax
.text:0041181D                 mov     eax, [ebp+var_8]
.text:00411820                 cmp     eax, [ebp+arg_8]
.text:00411823                 jle     short loc_411830
.text:00411825                 mov     ecx, [ebp+var_8]
.text:00411828                 mov     [ebp+var_D0], ecx
.text:0041182E                 jmp     short loc_411839
.text:00411830 ; ---------------------------------------------------------------------------
.text:00411830
.text:00411830 loc_411830:                             ; CODE XREF: sub_4117D0+53↑j
.text:00411830                 mov     edx, [ebp+arg_8]
.text:00411833                 mov     [ebp+var_D0], edx
.text:00411839
.text:00411839 loc_411839:                             ; CODE XREF: sub_4117D0+5E↑j
.text:00411839                 mov     eax, [ebp+var_D0]
.text:0041183F                 pop     edi
.text:00411840                 pop     esi
.text:00411841                 pop     ebx
.text:00411842                 the Add      esp, 0D0h
. text: 00,411,848                  cmp      ebp, ESP 
. text: 0041184A                  Call     sub_41122B 
. text: 0041184F                  mov      ESP, ebp 
. text: 00,411,851                  POP      ebp 
. text: 00,411,852                  RETN     0Ch // restore the stack 
. text: 00,411,852 sub_4117D0 ENDP 
. text: 00,411,852

 

Parameters Parameter // push a stack order: right to left on the stack
push two parameters
push. . .
push parameter N
Call Fun // call the function, the function performed within the stack is balanced, retn XX

_fastcall convention:
Parameters: right to left stack, when the parameters are not more than two of the general parameters into edx, ecx inside, more than two time parameters on the stack inside
the stack balance: the called party's own balance

.text:00411980 var_CC          = byte ptr -0CCh
.text:00411980 var_8           = dword ptr -8
.text:00411980
.text:00411980                 push    ebp
.text:00411981                 mov     ebp, esp
.text:00411983                 sub     esp, 0CCh
.text:00411989                 push    ebx
.text:0041198A                 push    esi
.text:0041198B                 push    edi
.text:0041198C                 lea     edi, [ebp+var_CC]
.text:00411992                 mov     ecx, 33h
.text:00411997                 mov     eax, 0CCCCCCCCh
.text:0041199C                 rep stosd
.text:0041199E                 mov     ecx, offset unk_41C009
.text:004119A3                 call    sub_411221
.text:004119A8                 mov     [ebp+var_8], 0
.text:004119AF                 push    16
.text:004119B1                 mov     edx, 30
.text:004119B6                 mov     ecx, 10
.text:004119BB                 call    sub_411398
.text:004119C0                 mov     [ebp+var_8], eax
.text:004119C3                 mov     eax, [ebp+var_8]
.text:004119C6                 push    eax
.text:004119C7                 push    offset aImaxD   ; "iMAx = %d\n"
.text:004119CC                 call    sub_41104B
.text:004119D1                 add     esp, 8
.text:004119D4                 xor     eax, eax
.text:004119D6                 pop     edi
.text:004119D7                 pop     esi
.text:004119D8                 pop     ebx
.text:004119D9                 add     esp, 0CCh
.text:004119DF                 cmp     ebp, esp
.text:004119E1                 call    sub_41122B
.text:004119E6                 mov     esp, ebp
.text:004119E8                 pop     ebp
.text:004119E9                 retn
.text:004119E9 sub_411980      endp

 

Internal function

text:004117D0 sub_4117D0      proc near               ; CODE XREF: .text:00411087↑j
.text:004117D0                                         ; .text:00411393↑j ...
.text:004117D0
.text:004117D0 var_E8          = dword ptr -0E8h
.text:004117D0 var_20          = dword ptr -20h
.text:004117D0 var_14          = dword ptr -14h
.text:004117D0 var_8           = dword ptr -8
.text:004117D0 arg_0           = dword ptr  8
.text:004117D0
.text:004117D0                 push    ebp
.text:004117D1                 mov     ebp, esp
.text:004117D3                 sub     esp, 0E8h
.text:004117D9                 push    ebx
.text:004117DA                 push    esi
.text:004117DB                 push    edi
.text:004117DC                 push    ecx
.text:004117DD                 lea     edi, [ebp+var_E8]
.text:004117E3                 mov     ecx, 3Ah
.text:004117E8                 mov     eax, 0CCCCCCCCh
.text:004117ED                 rep stosd
.text:004117EF                 pop     ecx
.text:004117F0                 mov     [ebp+var_14], edx
.text:004117F3                 mov     [ebp+var_8], ecx
.text:004117F6                 mov     ecx, offset unk_41C009
.text:004117FB                 call    sub_411221
.text:00411800                 mov     eax, [ebp+var_8]
.text:00411803                 cmp     eax, [ebp+var_14]
.text:00411806                 jle     short loc_411813
.text:00411808                 mov     ecx, [ebp+var_8]
.text:0041180B                 mov     [ebp+var_E8], ecx
.text:00411811                 jmp     short loc_41181C
.text:00411813 ; ---------------------------------------------------------------------------
.text:00411813
.text:00411813 loc_411813:                             ; CODE XREF: sub_4117D0+36↑j
.text:00411813                 mov     edx, [ebp+var_14]
.text:00411816                 mov     [ebp+var_E8], edx
.text:0041181C
.text:0041181C loc_41181C:                             ; CODE XREF: sub_4117D0+41↑j
.text:0041181C                 mov     eax, [ebp+var_E8]
.text:00411822                 mov     [ebp+var_20], eax
.text:00411825                 mov     eax, [ebp+var_20]
.text:00411828                 cmp     eax, [ebp+arg_0]
.text:0041182B                 jle     short loc_411838
.text:0041182D                 mov     ecx, [ebp+var_20]
.text:00411830                 mov     [ebp+var_E8], ecx
.text:00411836                 jmp     short loc_411841
.text:00411838 ; ---------------------------------------------------------------------------
.text:00411838
.text:00411838 loc_411838:                             ; CODE XREF: sub_4117D0+5B↑j
.text:00411838                 mov     edx, [ebp+arg_0]
.text:0041183B                 mov     [ebp+var_E8], edx
.text:00411841
.text:00411841 loc_411841:                             ; CODE XREF: sub_4117D0+66↑j
.text:00411841                 mov     eax, [ebp+var_E8]
.text:00411847                 pop     edi
.text:00411848                 pop     esi
.text:00411849                 pop     ebx
.text:0041184A                 add     esp, 0E8h
.text:00411850                 cmp     ebp, esp
.text:00411852                 call    sub_41122B
.text:00411857                 mov     esp, ebp
.text:00411859                 pop     ebp
.text:0041185A                 retn    4
.text:0041185A sub_4117D0      endp

 

Parameters Parameter // push a stack order: right to left on the stack
push two parameters
push. . .
EDX Push
Push // ECX register transfer
call Fun // call the function, the function performed within the stack is balanced, retn XX


naked convention:
Parameters: right to left stack
stacks balance: Call Fang Pingheng

Parameters Parameter // push a stack order: right to left on the stack
push two parameters
push. . .
push parameter N
Call Fun // call the function
add esp, XX // stack balance

Guess you like

Origin www.cnblogs.com/xiangtingshen/p/11299104.html