Stack Basics and Procedure Calls Systems Design & Programming CMPE 310 Purpose of Stack � Memory used to pass parameters to procedures (including C function calls) � Memory used for allocating space for local variables � Save return address in procedure calls � Save registers to be preserved across procedure calls PUSH EBX POP ECX value1 value1 ESP after value2 value2 ESP before ESP before EBX value3 ESP after value3 ECX value3 1
Stack Basics and Procedure Calls Systems Design & Programming CMPE 310 Passing Parameters to Procedures section .data ESP STACK (1) input_filename_ptr : dd 0 section .text main: 00000010 (2) (2) push dword input_filename_ptr Return Address call GetCommandLine (3) (3) add esp, 4 (1) input_filename_ptr : Pointer to the filename 00000010 (2) Push the address of the pointer to the filename (3) Return address pushed to the stack. Address of the add instruction. 2
Stack Basics and Procedure Calls Systems Design & Programming CMPE 310 Call Frames } One call frame created per procedure call } Parameter1 Parameters Parameter1 passed Parameter3 Return Address EBP } Procedure Local Var 1 Local Call 1 Local Var 2 variables Local Var 3 } EBX Registers } ECX saved } EDX Parameter1 Parameters Parameter1 passed Procedure Parameter3 Parameter4 Call 2 Return Address (no local variables) EBP EBP } ECX Registers EBX ESP saved STACK 3
Stack Basics and Procedure Calls Systems Design & Programming CMPE 310 Setting up Call Frames %macro Enter 1 GetCommandLine: push ebp (1) Enter 0 mov ebp, esp Push_Regs ebx, ecx, edx (2) sub esp, %1 %endmacro } 00000010 (1) Push EBP Return Address EBP Move ESP into EBP i.e. EBP points to the pushed EBP EBP Allocate space for local variables EBX (none in this example) ECX ESP (2) Push the registers that are to be saved EDX EBX, ECX and EDX in this example 4
Stack Basics and Procedure Calls Systems Design & Programming CMPE 310 Reading Arguments STACK mov ebx, [ebp + 8] mov [ebx], dword 0 REGISTERS ECX mov ecx, [ebp + 16] } argc argc (# of arg) EBP+16 cmp ecx, 2 if ne EBX jmp gcl_done 00000010 00000010 EBP+8 endif Return Address EBP Exactly 2 arguments EBP required EBX Program name and [EBX] input file name ECX EDX ELSE ERROR!!! DATA 00000000 00000010 5
Stack Basics and Procedure Calls Systems Design & Programming CMPE 310 Reading Arguments STACK REGISTERS mov ecx, [ebp + 20] ECX mov ebx, [ecx] Pointer to args. Pointer to args. pointers EBP+20 pointers mov ecx, [ebp + 20] argc (# of arg) mov ebx, [ecx + 4] EBP+16 EBX Pointer to program name 00000010 EBP+8 Pointer to DATA input file name Return Address Pointer to [ECX] EBP program name EBP EBX Pointer to [ECX + 4] input file name ECX EDX program name input file name 6
Stack Basics and Procedure Calls Systems Design & Programming CMPE 310 Get argument and Return STACK REGISTERS mov edx, [ebp + 8] ECX mov [edx], ebx Pointer to args. Pointer to args. pointers EBP+20 pointers Pop_Regs ebx,ecx,edx argc (# of arg) Leave EBX EBP+16 ret Pointer to input file name DATA 00000010 EBP+8 EDX Return Address Pointer to 00000010 [ECX] program name EBP Pointer to [ECX + 4] input file name EBX EBP ECX program name EDX input file name ESP Pointer to input file name 00000010 7
Stack Basics and Procedure Calls Systems Design & Programming CMPE 310 Procedure Calls (Steps Recap) Caller: Before Call � Save registers that are needed (for C functions save EAX, ECX, EDX) � Push arguments, last first � CALL the function Callee: � Save caller's EBP and set up callee stack frame (ENTER macro) � Allocate space for local variables and temporary storage � Save registers as needed (C functions save EBX, ESI, EDI) � Perform the task � Store return value in EAX � Restore registers (C functions restore EBX, ESI, EDI) � Restore caller's stack frame (LEAVE macro) � Return Caller: After Return � POP arguments, get return value in EAX, restore registers (for C EAX, ECX, EDX) 8
Recommend
More recommend