CC ++ parameter stack order

xiaoxiao2021-03-06  20

Once I saw an article says: C / C parameter stack order is from right to left, and the Pascal parameter stack is from left to right.

For this sentence, I have lost many times. It doesn't matter, anyway, my face is thick.

Summarize: The parameter stack order of the compiled C / C program is only related to the compiler! Here, some common compiler call agreements are listed below.

VC6:

Call the agreed stack clearance parameter transfer __cdecl caller from right to left, pass the __stdcall function body from right to left, by stack pass __fastcall function body from right to left, prioritize the register (ECX, EDX), then use the stack THISCALL function Body THIS pointer default via ECX, other parameters from right to left in the stack

__cdecl is the default call agreement of C / C ; there is no THISCALL in the invocation convention, which is the default call agreement of the class member function; the call agreement of the main (or wmain) function in C / C must be __cdecl, Do not allow changes; the default call agreement can generally be changed through the compiler settings. If your code rely on the call agreement, please clearly indicate the invocation agreement required to use;

C Builder6:

Call the agreed stack clearance parameter pass __fastcall function body from left to right, prioritize register (EAX, EDX, ECX), then use the stack (compatible with Delphi's register) (Register and __fastcall equivalent) __pascal function body from left to right, pass Stack transfer __cdecl caller from right to left, through stack delivery (compatible with C / C default calls) __stdcall function bodies from right to left, pass the stack pass (compatible with __stdcall in VC) __msfastcall function body from right to left , Prioritize the register (ECX, EDX), then use the stack (compatible with the VC __fastcall)

The above information is derived from Housisong an article: http://www.Allaboutprogram.com/index.php? Option = content & task = view & id = 29 & itemid = 31. Due to the limited capacity and resources, only these things can be found, the main difference is reflected In FastCall, VC is the first two parameters placed in the register. The back of the stack, BCB is the first 3 brother parameters use registers, more metamorphous, a friend friend said that some parameters have more than 7 before the top 5 from left to left To the right, the back from the right to go, the above is not believed, it is not a letter. How to determine your compilation using the order? #Include int F (int i, int J, int K ); int main () {static int i = 0; f (i , i , i ); return 0;}

INT F (INT I, INT J, INT K) {Int L; INT G; Printf ("K =% D: [% x] / N", K, & K); Printf ("J =% D: [% X] / n ", J, & J); Printf (" i =% D: [% x] / n ", I, & I); printf (" ___________ / n "); Printf (" L:% x / n ", & l); Printf (" g:% x / n ", & g);} Take a look at the address of the address of K-> i and whether the order of the L-> G is the same, if the same is from right to left, Otherwise, from left to right. Ps: By printing the value of the parameters to determine the first in the stack, the result is criticized by a friend, he said: Stack order and parameter calculation order is not one thing, so look at the address is more guaranteed. INT __CDECL Add (Int A, INT B);

Int Add (int A, int b) (__cdecl);

转载请注明原文地址:https://www.9cbs.com/read-61134.html

New Post(0)