Thiết kế và lập trình hệ thống - Chương
Systems Design & Programming Stack Basics CMPE 3101 (Feb. 21, 2002)UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6Purpose 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 ECXEBXESP beforeESP aftervalue1value2value3value3value1value2ECXvalue3ESP afterESP before Systems Design & Programming Stack Basics CMPE 3102 (Feb. 21, 2002)UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6Passing Parameters to Procedures section .dataPointer to the filename section .textmain: call GetCommandLine add esp, 4input_filename_ptr : dd 0STACKinput_filename_ptr : push dword input_filename_ptrESP0000001000000010(1)(2)(3)(1)Return Address(2)Push the address of the pointer to the filename(3)Return address pushed to the stack.Address of the add instruction.(2)(3) Systems Design & Programming Stack Basics CMPE 3103 (Feb. 21, 2002)UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6Call FramesParameter1Parameter2Parameter3Return AddressEBPLocal Var 1Local Var 2Local Var 3Parameter1Parameter2Parameter3Return AddressEBPParameter4ParameterspassedLocalvariablesParameterspassedProcedureCall 1ProcedureCall 2Reg1Reg2Reg3Reg1Reg2}}}}RegisterssavedRegisterssaved}}(no local variables)One call frame created per procedure callSTACKEBPESP} Systems Design & Programming Stack Basics CMPE 3104 (Feb. 21, 2002)UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6Setting up Call Frames00000010Return AddressGetCommandLine: Enter 0 Push_Regs ebx, ecx, edx%macro Enter 1 push ebp mov ebp, esp sub esp, %1%endmacro(1)(2)Push EBPMove ESP into EBPAllocate space for local variablesi.e. EBP points to the pushed EBP(none in this example)(1)EBPESPEBP(2)Push the registers that are to be savedEBX, ECX and EDX in this exampleEBXECXEDX} Systems Design & Programming Stack Basics CMPE 3105 (Feb. 21, 2002)UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6Reading Arguments00000010Return AddressEBP+16EBXECXEDXmov ebx, [ebp + 8]00000000mov ecx, [ebp + 16]cmp ecx, 2if nejmp gcl_doneendif00000010EBPDATA00000010[EBX]mov [ebx], dword 0argcECXEBP+8EBXargc (# of arg)}EBPExactly 2 argumentsrequiredProgram name andinput file nameELSE ERROR!!!STACKREGISTERS Systems Design & Programming Stack Basics CMPE 3106 (Feb. 21, 2002)UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6Reading Arguments00000010Return AddressEBP+16EBXECXEDXmov ecx, [ebp + 20]Pointer tomov ecx, [ebp + 20]EBPmov ebx, [ecx]ECXEBP+20EBXargc (# of arg)EBPmov ebx, [ecx + 4]EBP+8Pointer to args.pointersPointer to args.pointers[ECX]program namePointer toinput file namePointer toprogram namePointer toinput file nameprogram nameinput file name[ECX + 4]STACKREGISTERSDATAprintfDEBUGstuff Systems Design & Programming Stack Basics CMPE 3107 (Feb. 21, 2002)UMBCU M B CUNIVERSITY OF MARYLAND BALTIMORE COUNTY1 9 6 6Get argument and Return00000010Return AddressEBP+16EBXECXEDXmov edx, [ebp + 8]Pointer toPop_Regs ebx,ecx,edxEBPSTACKREGISTERSmov [edx], ebxECXEBP+20EBXargc (# of arg)EBPLeaveEBP+8Pointer to args.pointersPointer to args.pointers[ECX]program namePointer toinput file namePointer toinput file nameprogram nameinput file name[ECX + 4]retEDX0000001000000010Pointer toinput file nameESPDATA 123doc.vn