Function Convention call

Source: Internet
Author: User

The function call Convention mainly limits two things:
1. parameter transfer order
2. who calls the stack (call function or called function) to clear

Common function call Conventions: stdcall cdecl fastcall thiscall naked call

Focus on stdcall cdecl fastcall,

Stdcall and cdecl share the same order of passing parameters. The parameter is pushed from right to left into the stack. The difference is who completes stack cleaning. Stdcall is completed by the called sub-function, and cdecl is completed by the called function. Www.2cto.com

The default call Convention for C/C ++ in VC is cdecl.

The Code is as follows.

# Include <stdio. h>
// _ Fastcall call
Int _ fastcall sum_fas (int a, int B, int c)
{
Return (a + B + c );
}

// _ Stdcall call
Int _ stdcall sum_std (int a, int B, int c)
{
Return (a + B + c );
}

// _ Cdecl call
Int _ cdecl sum_cde (int a, int B, int c)
{
Return (a + B + c );
}

// C/C ++ in VC is called by default
Int sum (int a, int B, int c)
{
Return (a + B + c );
}

Int main ()
{
Int c1, c2, c3, c4;

C1 = sum_fas (2, 3, 5 );
C2 = sum_std (2, 3, 5 );
C3 = sum_cde (2, 3, 5 );
C4 = sum (2, 3, 5 );

Return 0;
}
Assembly code:

Int main ()
{
011D1500 push ebp
011D1501 mov ebp, esp
011D1503 sub esp, 0F0h
011D1509 push ebx
011D150A push esi
011D150B push edi
011D150C lea edi, [ebp-0F0h]
011D1512 mov ecx, 3Ch
011D1517 mov eax, 0 CCCCCCCCh
011D151C rep stos dword ptr es: [edi]
Int c1, c2, c3, c4;

C1 = sum_fas (2, 3, 5 );
011D151E push 5
011D1520 mov edx, 3
011D1525 mov ecx, 2
011D152A call sum_fas (011D1037h)
011D152F mov dword ptr [c1], eax
C2 = sum_std (2, 3, 5 );
011D1532 push 5
011D1534 push 3
011D1536 push 2
011D1538 call sum_std (011D10E6h)
011D153D mov dword ptr [c2], eax
C3 = sum_cde (2, 3, 5 );
011D1540 push 5
011D1542 push 3 www.2cto.com
011D1544 push 2
011D1546 call sum_cde (011D10B4h)
011D154B add esp, 0Ch
011D154E mov dword ptr [c3], eax
C4 = sum (2, 3, 5 );
011D1551 push 5
011D1553 push 3
011D1555 push 2
011D1557 call sum (011D1113h)
011D155C add esp, 0Ch
011D155F mov dword ptr [c4], eax

Return 0;
011D1562 xor eax, eax
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.