URL: https://www.overclockers.at/coding-stuff/calling-conventions_226954/page_1 - zur Vollversion wechseln!
Ich habe heute etwas Recherche in Hinsicht für meine Bachelorarbeit betrieben und mich etwas umfangreicher mit calling conventions beschäftigt. Um mir das Ganze auch zu merken habe ich gleich ein kleines Kapitel darüber geschrieben.
Ich gebe zu beachten, dass es 1.) Bestimmt viele viele Rechtschreibfehler darin gibt weil ich es im Word ohne Rechtschreibprüfer geschrieben habe
und 2.) Womöglich nicht alle Informationen 100% richtig sind. Ist die Arbeit von einem Tag und ich bin selbst erst am Lernen.
Wenn sich jedoch jemand dafür interessiert der auch noch eher im Anfängerstadium ist, ist sicher was hilfreiches dabei.
Auf eventuell Falschinterpretationen/Informationen kann man mich gerne hinweisen
Achja und nachdem ich meine Bakkarbeit in Englisch schreib habe ich diesen Text in Englisch geschrieben als kleine "Vorbereitung"
Calling conventions
Everytime a function is called within a programm the parameters and the local variables have to be stored somewhere. It is also very important to save to address from which the function was called so that the program knows where to continue after the function is done and mostly also the current state of some cpu register. These informations are therefore stored on the stack. Now there are a few differnt methods how compiler handle function calls an their data, called "Calling convetions". The four most important ones are:
• C calling Convetion __cdecl
Standard calling convetion by C compiler. The parameters are being pushed onto the stack from right to left and the caller is responsible for cleaning up the stack afterwards.
• Standard Calling Convention __stdcall
This calling convetion is als known as WIN32 calling convention since the Win32 API primarily uses this convention. The WINAPI declaration after the return type oft he function in Win32 API also is just a #define for __stdcall. In this calling covention the callee cleans up the stack.
• Fastcall Calling Convention __fastcall
In Fastcall Calling Convention the first two paramters passed to the function will not be pushed on the stack but the CPU will use register ECX and EDX to store them. The other parameters will be pushed ont the stack from right to left if there are more than two. If any parameter got pushed on the stack the callee will clean up the stack. The implementation of __fastcall may vary on several compiler.
• This calling convetion __thiscall
As the name probably implies this calling convention is exclusive used by memberfunction from any C++ class which can be accessed via this pointer. ECX register is used to store the this pointer, other parameter are pushed on the stack. The callee cleans up the stack. This calling convention has some commonalities with _fastcall but the big differnce is that is only used ECX register to store first parameter (this pointer) and not EDX for second parameter too.
Since it is not possible to see how these differnt calling convetions behave from a high level perspective I show a dissasembly of __cdecl function call.
C calling convetion:
Code: Cint __cdecl cdecl_sum(int a, int b) { return a+b; }
Code:011713E0 push ebp 011713E1 mov ebp,esp 011713E3 sub esp,0C0h 011713E9 push ebx 011713EA push esi 011713EB push edi 011713EC lea edi,[ebp-0C0h] 011713F2 mov ecx,30h 011713F7 mov eax,0CCCCCCCCh 011713FC rep stos dword ptr es:[edi] 011713FE mov eax,dword ptr [a] 01171401 add eax,dword ptr [b] 01171404 pop edi 01171405 pop esi 01171406 pop ebx 01171407 mov esp,ebp 01171409 pop ebp 0117140A ret
Dissasemly:Code: Ccdecl_sum(1,4);
Code:01171491 push 4 01171493 push 1 01171495 call cdecl_sum (117114Fh) 0117149A add esp,8
Code:[ebp+0xC] [ebp+0x8] [ebp+0x4] [ebp] <- ebp, esp point here
Code:[ebp+0xC] [ebp+0x8] [ebp+0x4] [ebp] <- ebp points here [ebp-0x4] [ebp-0x8] [ebp-0xC] <- esp points here
Code:011713C4 pop edi 011713C5 pop esi 011713C6 pop ebx 011713C7 mov esp,ebp 011713C9 pop ebp 011713CA ret 8
Code:01171485 push 4 01171487 push 1 01171489 call std_sum (11710D7h)
Falls das so dein Bacc-Text ist, würd ich schauen bezüglich groß und klein Schreibung zB den stack pointer schreibst manchmal klein, groß oder ganze capital. Ausserdem kenn (zumindest persönlich) ich den Stack nur als Stapel, da gibts kein seitlich reinschieben sondern drauf.
(Und in dem Fall wird dir ein Stack Diagramm mit Ablauf einiges helfen, natürlich nur wenn du motiviert genug bist für die Arbeit weil die Diagramme sind halt zeitintensiv)
Aufn Inhalt kann ich nicht eingehen ich habs jetzt nur überflogen, aber thx für die Infos
Das ist eh nicht so mein Bacc-Text keine Sorge^^
Mit rechts nach links ist nur gemeint das der zweite Paramter (rechts) zuerst auf den Stack gepushed wird und der erste (link) danach, aber stimmt das ist etwas verwirrend.
Und danke für den Tipp trotzdem, das werde ich dann in den richtigen Arbeit natürlich einheitlich halten.
- WINAPI ist natürlich kein typedef.
- von fastcall gibts eine ganze Reihe von Varianten auf verschiedenen Plattformen und Compilern
Vielleicht auch noch interessant ist, dass auf x86_64 alles ziemlich anders ist.
Empfehlenswerte Links:
http://en.wikipedia.org/wiki/Calling_conventions
http://en.wikipedia.org/wiki/X86_calling_conventions
Da geb ich dir natürlich Recht WINAPI ist kein typedef sondern ein #define.
Und ich gehe nur auf die x86 Architektur ein, dass habe ich vergessen zu erwähnen.
overclockers.at v4.thecommunity
© all rights reserved by overclockers.at 2000-2025