Home >Backend Development >C++ >How Do Different Calling Conventions Impact C/C Function Execution?

How Do Different Calling Conventions Impact C/C Function Execution?

Barbara Streisand
Barbara StreisandOriginal
2024-11-20 03:10:02298browse

How Do Different Calling Conventions Impact C/C   Function Execution?

Different Calling Conventions in C/C

C/C offers various calling conventions to specify how function arguments and return values are handled. Understanding these conventions is crucial for optimizing code and ensuring efficient interaction between different parts of a program.

Available Calling Conventions

There are several calling conventions available in C/C :

  • cdecl: Arguments passed on stack, return value in registers (EAX for integers, ST0 for floats)
  • syscall: Similar to cdecl, except EAX, ECX, and EDX registers are not preserved
  • pascal: Parameters pushed left-to-right, callee balances stack
  • stdcall: Caller-cleanup stdcall, parameters pushed right-to-left, return value in EAX
  • fastcall: First two arguments passed in ECX and EDX, remaining arguments on stack
  • vectorcall: For passing vector arguments using SIMD registers
  • safecall: Error handling encapsulated, HResults returned in EAX
  • Microsoft X64 Calling Convention: Used on Windows and UEFI, 64-bit return value in RAX, 64-bit arguments passed in RCX-R9
  • thiscall: Similar to cdecl, but this pointer passed via hidden argument

Detailed Explanation

cdecl:
In cdecl (call by value), the called function assumes responsibility for cleaning up the stack before returning. Arguments are pushed onto the stack from right to left.

pascal:
In pascal (call by value-result), the callee is responsible for balancing the stack before return. The return value is also passed back on the stack.

stdcall:
In stdcall (call by value), the callee is responsible for cleaning up the stack. Arguments are pushed onto the stack from right to left, while return values are stored in the EAX register.

fastcall:
In fastcall (call by register), the first two arguments are passed in ECX and EDX registers. The rest of the arguments are pushed onto the stack from right to left.

**For further details on the other calling conventions, please refer to the following comprehensive answer:]

The above is the detailed content of How Do Different Calling Conventions Impact C/C Function Execution?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn