umbc
play

UMBC A B M A L T F O U M B C I M Y O R T 1 (Feb. - PowerPoint PPT Presentation

Systems Design & Programming Stack Basics CMPE 310 Purpose of Stack Memory used to pass parameters to procedures. Memory used for allocating space for local variables. Save return address in procedure calls. Save registers


  1. Systems Design & Programming Stack Basics CMPE 310 Purpose of Stack • Memory used to pass parameters to procedures. • 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 L A N R Y D UMBC A B M A L T F O U M B C I M Y O R T 1 (Feb. 21, 2002) I E S R C E O V U I N N U T Y 1 6 9 6

  2. Systems Design & Programming Stack Basics 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. L A N R Y D UMBC A B M A L T F O U M B C I M Y O R T 2 (Feb. 21, 2002) I E S R C E O V U I N N U T Y 1 6 9 6

  3. Systems Design & Programming Stack Basics CMPE 310 Call Frames One call frame created per procedure call STACK Parameter1 } } Parameters Parameter2 passed Parameter3 Return Address EBP Procedure Local Var 1 } Local Local Var 2 Call 1 variables Local Var 3 } Reg1 Registers Reg2 saved Reg3 } Parameter1 } Parameters Parameter2 passed Procedure Parameter3 Parameter4 Call 2 Return Address (no local variables) EBP EBP } Reg1 Registers Reg2 ESP saved L A N R Y D UMBC A B M A L T F O U M B C I M Y O R T 3 (Feb. 21, 2002) I E S R C E O V U I N N U T Y 1 6 9 6

  4. Systems Design & Programming Stack Basics CMPE 310 Setting up Call Frames %macro Enter 1 GetCommandLine: push ebp Enter 0 (1) mov ebp, esp Push_Regs ebx, ecx, edx (2) sub esp, %1 %endmacro } 00000010 Push EBP (1) 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 L A N R Y D UMBC A B M A L T F O U M B C I M Y O R T 4 (Feb. 21, 2002) I E S R C E O V U I N N U T Y 1 6 9 6

  5. Systems Design & Programming Stack Basics CMPE 310 Reading Arguments STACK REGISTERS mov ebx, [ebp + 8] mov [ebx], dword 0 ECX mov ecx, [ebp + 16] argc argc (# of arg) cmp ecx, 2 EBP+16 } 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 L A N R Y D UMBC A B M A L T F O U M B C I M Y O R T 5 (Feb. 21, 2002) I E S R C E O V U I N N U T Y 1 6 9 6

  6. Systems Design & Programming Stack Basics CMPE 310 Reading Arguments STACK REGISTERS mov ecx, [ebp + 20] DEBUG ECX mov ebx, [ecx] stuff Pointer to args. printf 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 input file name DATA Return Address Pointer to [ECX] EBP program name EBP EBX Pointer to input file name [ECX + 4] ECX EDX program name input file name L A N R Y D UMBC A B M A L T F O U M B C I M Y O R T 6 (Feb. 21, 2002) I E S R C E O V U I N N U T Y 1 6 9 6

  7. Systems Design & Programming Stack Basics 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 00000010 EBP+8 DATA 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 L A N R Y D UMBC A B M A L T F O U M B C I M Y O R T 7 (Feb. 21, 2002) I E S R C E O V U I N N U T Y 1 6 9 6

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend