Universal Concepts of ProgrammingLocal VariablesLocal Variable AdvantagesLocal Variable ConstraintsLocal DirectiveAutomatic Code GenerationAssembly Code listing of ProcThe Stack for the PROCHow big should the stack be?Stack ParametersProcedure Parameters (2)ADDR OperatorRecursionStack FramesUniversal Concepts of Programming•Creating and Initializing local variables on the stack•Variable Scope and Lifetime•Stack Parameters•Stack Frames•Passing parameters by value and by reference•Classifying parameters as input, output, and input-output•RecursionLocal VariablesVariables that are declared in the data segment are static global variables•Static – a variable’s lifetime is the same as the duration of the program•Global – visible from all procedures in the current source code file.•A local variable is a variable is created, used, and destroyed within a single procedure.Local Variable Advantages•Restricted access to a local variable helps when you are debugging, because only a limited number of program statements can modify the variable.•Make efficient use of memory, because their storage space can be released and made available to new variables.•The same variable name can appear in two or more procedures without creating a name clash.Local Variable Constraints•Created on the runtime stack.•They cannot be given default values at assembly time.•They will be initialized at runtime.Local Directive•Must be placed on the line immediately following a PROC directive.MySub PROCLOCAL var1 : BYTEBubbleSort PROCLOCAL temp:DWORD, SwapFlag:BYTE•Variables and variable types are separated by a colon•Variables are separated by commas, which may span multiple linesAutomatic Code Generation•Where are these variables saved – on the stack.•Assembling and debugging this procedureBubbleSort PROCLOCAL temp:DWORD, SwapFlag:BYTERetBubbleSort EndpAssembly Code listing of Proc•BubbleSort:–Push ebp–Mov ebp, esp–Add esp, 0FFFFFFF8h ;add –8 to ESP–Mov esp, ebp–Pop ebp–retThe Stack for the PROC•EBP = 4 bytes•Temp = 4 bytes•SwapFlag = 1 byte•=> The add instruction adds –8 to ESP, moving it downward, and creating an opening in the stack between ESP and EBP for the two local variables. Return address EBP temp SwapFlagEBP[EBP – 4][EBP – 8]ESPHow big should the stack be?•Need 2-4 bytes for each return address•Need enough space for local variables•If procedure calls are nested, the stack space must be large enough to hold the sum of all local variables active at any point in the program’s execution.Stack Parameters•There are two basic types of procedure parameters:–Register parameters•Optimized for program execution speed•Create code clutter in calling program (existing register contents often must be saved before they can be loaded with argument values)Procedure Parameters (2)•Stack parameters•The required arguments must be pushed on the stack by a calling program.Push OFFSET arrayPush LENGTHOF arrayPush TYPE arrayCall DumpMem•The INVOKE directive automatically pushes arguments on the stack and calls a procedure.INVOKE DumpMem, OFFSET array, LENGTHOF array, TYPE array•Nearly all high-level languages use stack parametersADDR Operator•Can be used to pass a pointer when calling a procedure with the INVOKE directive.•Passing by referenceINVOKE fillarray, ADDR myarray•ADDR returns either a near pointer or a far pointer, depending on what is called for by the program’s memory model.RecursionStack Frames•See other
View Full Document