<> Want to learn more about assembly language to understand memory mechanism

<> One , Problem guidance :C The sequence of function parameters in language

C High address at the bottom of program stack , Low address at top of stack .

C Mode parameter stack order ( right to left ) The advantage is that the number of parameters can be changed dynamically . The stack analysis shows that , Push left to right , The first parameter is pressed at the bottom of the stack . Unless you know the number of parameters , Otherwise, the leftmost parameter cannot be obtained through the relative displacement of the stack pointer . So the number of parameters on the left is uncertain , Just opposite to the direction of the number of dynamic parameters .

therefore ,C Right to left stack order for language function parameters , The main reason is to support variable length parameter form .

<> Two ,, Function calling convention (Calling Convention)

The function calling convention not only determines the stack order of function parameters when a function call occurs , It also determines whether the caller function or the called function is responsible for clearing the parameters in the stack , Restore stack . There are many ways to call a function , Except for the common __cdecl,__fastcall and __stdcall outside ,C++ The compiler for also supports thiscall mode , not a few C/C++ The compiler also supports
naked call mode . So many function calling conventions often confuse many programmers , What's the matter with them , Under what circumstances are they used ? Here are the function calling conventions .

<>1.__cdecl

The compiler's command line arguments are /Gd.__cdecl The way is C/C++ Compiler default function calling conventions , All non C++ Member functions and those are useless __stdcall or __fastcall All declared functions default to __cdecl mode , It uses C Function call mode , Function parameters are pushed in right to left order , The function caller is responsible for clearing the parameters in the stack , Because every function call is cleared by the compiler ( reduction ) Stack code , So use __cdecl Program compiled by using __stdcall The program compiled in this way is much larger , however
__cdecl The function caller is responsible for clearing the function parameters in the stack , So variable parameters are supported in this way , such as printf and windows Of API
wsprintf namely __cdecl Call mode . about C function ,__cdecl The name modification convention of method is to add an underscore before the function name ; about C++ function , Unless specifically used extern
“C”,C++ Functions are decorated with different names .

<>2.__fastcall

The compiler's command line arguments are /Gr.__fastcall The function calling convention uses registers to pass parameters when possible , Usually the first two
DWORD Type or smaller parameter use ECX and EDX Register transfer , The rest of the parameters are pushed in right to left order , The called function is responsible for clearing the parameters in the stack before returning . The compiler uses two @ Modify function name , Function parameter list size followed by a decimal number , for example :@function_name@number. It should be noted that __fastcall Function calling conventions may have different implementations on different compilers , such as 16 Bit compiler and 32 Bit compiler , in addition , When using embedded assembly code , Also note that it cannot conflict with the registers used by the compiler .

<>3.__stdcall

The compiler's command line arguments are /Gz,__stdcall yes Pascal The default calling mode of program , majority Windows Of API It's also __stdcall Calling convention .__stdcall Function calling convention to stack function parameters from right to left , Unless you use a pointer or a parameter of a reference type , All parameters are passed by value transmission , The called function is responsible for clearing the parameters in the stack . about C function ,__stdcall The name of is decorated by underlining the function name , Add after function name @ And the size of function parameters , for example :_functionname@number

<> Three , Differences among the three

__stdcall,__cdecl and __fastcall Three function call protocols , Function call protocol will affect the way of function parameters entering stack , How to clear data in stack , Modification rules of compiler function names, etc .

<>1, Call protocol common occasions

__stdcall:Windows API Default function call protocol .
__cdecl:C/C++ Default function call protocol .
__fastcall: Suitable for occasions with high performance requirements .

<>2, How to stack function parameters

__stdcall: Function parameters are pushed from right to left .
__cdecl: Function parameters are pushed from right to left .
__fastcall: From left not greater than 4 Byte parameter put CPU Of ECX and EDX register , Other parameters are pushed from right to left .

Question 1 :__fastcall Put no more than in register 4 Byte parameters , Therefore, it has high performance , Suitable for situations requiring high performance .

<>3, In stack data clearing mode

__stdcall: After the function call, the called function clears the data in the stack .
__cdecl: After the function call, the function caller clears the data in the stack .
__fastcall: After the function call, the called function clears the data in the stack .

Question 1 : Different compiler sets different stack structures , It is infeasible for function callers to clear the data in the stack across development platforms .
Question 2 : Some functions have variable parameters , as printf function , Such a function can only be cleared by the function caller .
Question three : When the data in the stack is cleared by the caller , Each call contains code to clear the data in the stack , Therefore, the executable documents are large .

<>4,C Language compiler function name modification rules

__stdcall: After compilation , The function name is modified to “_functionname@number”.
__cdecl: After compilation , The function name is modified to “_functionname”.
__fastcall: After compilation , Modify function name to “@functionname@nmuber”.

notes :“functionname” Is the function name ,“number” Is the number of parameter bytes .
notes : If different function call protocols are used in function implementation and function definition , Function call cannot be implemented .

<>5,C++ Language compiler function name modification rules

__stdcall: After compilation , The function name is modified to “?functionname@@YG******@Z”.
__cdecl: After compilation , The function name is modified to “?functionname@@YA******@Z”.
__fastcall: After compilation , The function name is modified to “?functionname@@YI******@Z”.

notes :“******” Returns a table of value types and parameter types for a function .
notes : If different function call protocols are used in function implementation and function definition , Function call cannot be implemented .
C Language and C++ Without special treatment between languages , It can't realize the mutual calling of functions .

Technology
©2019-2020 Toolsou All rights reserved,
Linux File name validity detection Share has surpassed Ningde Era !LG Chemical confirmation to spin off battery business unit rk3399_android7.1 debugging USB Summary of Bluetooth module jmeter-while Summary of controller use 【JAVA】【 Huawei campus recruitment written examination - Software 】2020-09-09( Essence )2020 year 6 month 26 day C# Class library Enum( Extension method )Three.js - OrbitControls Orbit control around target target parameter ELementUI select Multi select drop-down box to get all properties of the selected item airflow Question series 2 —— task keep running Suspended animation Python read Excel A column | Transfer deposit json