Upload
bruno-cook
View
214
Download
1
Embed Size (px)
Citation preview
Universal 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• Recursion
Local Variables
Variables 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 : BYTE
BubbleSort PROCLOCAL temp:DWORD, SwapFlag:BYTE
• Variables and variable types are separated by a colon
• Variables are separated by commas, which may span multiple lines
Automatic Code Generation
• Where are these variables saved – on the stack.
• Assembling and debugging this procedureBubbleSort PROC
LOCAL temp:DWORD, SwapFlag:BYTE
Ret
BubbleSort Endp
Assembly Code listing of Proc
• BubbleSort:– Push ebp– Mov ebp, esp– Add esp, 0FFFFFFF8h ;add –8 to ESP– Mov esp, ebp– Pop ebp– ret
The 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
SwapFlag
EBP
[EBP – 4]
[EBP – 8]ESP
How 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 array
Push LENGTHOF array
Push TYPE array
Call 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 parameters
ADDR 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.
Recursion
Stack Frames
• See other slides