Speaking straight, the stack is a memory! The access rules for this memory is the first out of the first out (LIFO), if only from the design angle of the CPU
Look, the function of the stack has only one: in order to support the nested and recursive of the function, save the return address when the function call is called. Of course, this is just a little shallow.
Thin understanding. On the X86 chip, the call command and return instructions of the function are Call and RET, respectively, in order to see the call and return action,
I will dismiss this two instructions.
Implement the function call and return _SUB ENDP _MAIN PROC MOV EAX, 1234 MOV EBX, 5678 CALL _SUB ... _MAIN ENDP Call and return _SUB Proc Add Eax , EBX; Remove the return address, jump past POP ECX JMP ECX _SUB ENDP _MAIN Proc MoV EAX, 1234 MOV EBX, 5678; return to the address into the stack, jump to the child function Push Offset _returnaddress JMP _SUB _RETURNADDRESS: ... _MAIN ENDP
The above code demonstrates the most basic functions of the stack. In the advanced language, it is usually used to transfer parameters and assign temporary variables, the compiler is
We have completed all these work. If you use a simple function to compile, we can clearly see these details.
A simple C function void swap (int * lpa, int * lpb) {int NTEMP = * LPA; * LPA = * LPB; * LPB = NTEMP;} Compiler compiles Swap Proc; the following two instructions are advanced languages Commonly used by the compiler; saves the original value of EBP, access the stack (parameter and temporary variable) with EBP; this is also to achieve the nested and recursive of the function. PUSH EBP MOV EBP, ESP; At this point, the state of the stack should be as follows: [EBP 12] = LPB; [EBP 8] = LPA; [EBP 4] = Return Address; [EBP] = EBP original value; int NTEMP; this instruction is to assign temporary variables SUB ESP, 4; NTEMP = * LPA, decomposed to three steps. EAX = LPA; EBX = * LPA; NTEMP = EBX; This particularly needs to be aware: 1.ASM how to access pointer variables; 2. Memory cannot directly pass data, need to be a case as a mediation MOV EAX, DWORD PTR [EBP 8] MOV EBX, DWORD PTR [EBP - 4], EBX; * LPA = * LPB MOV EBX, DWORD PTR [EBP 12] MOV ECX, DWORD PTR [EBX] MOV DWORD PTR [EAX ], ECX; * lpb = NTEMP MOV EAX, DWORD PTR [EBP - 4] MOV DWORD PTR [EBX], EAX; Release Temporary Variables, this order is not necessary, but I have tracked the function. Add ESP, 4; Restore ESP MOV ESP, EBP; Restore EBP POP EBP; Return RET SWAP ENDP
Below I will summarize several functions call rules. The call rules of the function include the pass and stack of the parameters, and must follow the system agreed call rules when writing a callback function (Callback) and calling WinAPI, otherwise it will definitely lead to exception! Here, I only summarize
Several common call rules. Rule Name Parameters Transfer Stack Correction __stdcall From Right to Left Transfiguration Function Correction Stack__cdecl From Right to Left Transfer Call Correction Stack__pascal From Left to Right Transfer Function Modified Stack or Swap Function Take the Swap Function:
__stdcall: (Most Win APIs are written in this rule) VOID __STDCALL SWAP (INT * LPA, INT * LPB); function format: swap proc ...; function returns a fixed stack RET 8 END PROC call method: Push LPB PUSH LPA CALL SWAP __CDECL: (Default rules in C / C ) VOID __CDECL SWAP (INT * LPA, INT * LPB); function format: swap proc ...; function returns a stack correction RET END PROC call method: Push LPB PUSH LPA CALL SWAP; Function Returns Repair Stack Add ESP, 8__Pascal: (WIN API and C / C ) VoID __Pascal Swap (INT * LPA, INT * LPB); Function Format: Swap proc ...; Function Returns Refraction Stack RET 8 End Proc Call Method: Push LPA Push LPB CALL SWAP