Download presentation
Presentation is loading. Please wait.
Published byBruno Cook Modified over 9 years ago
1
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
2
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.
3
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.
4
Local Variable Constraints Created on the runtime stack. They cannot be given default values at assembly time. They will be initialized at runtime.
5
Local Directive Must be placed on the line immediately following a PROC directive. MySub PROC LOCAL var1 : BYTE BubbleSort PROC LOCAL temp:DWORD, SwapFlag:BYTE Variables and variable types are separated by a colon Variables are separated by commas, which may span multiple lines
6
Automatic Code Generation Where are these variables saved – on the stack. Assembling and debugging this procedure BubbleSort PROC LOCAL temp:DWORD, SwapFlag:BYTE Ret BubbleSort Endp
7
Assembly Code listing of Proc BubbleSort: –Push ebp –Movebp, esp –Addesp, 0FFFFFFF8h ;add –8 to ESP –Movesp, ebp –Popebp –ret
8
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
9
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.
10
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)
11
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
12
ADDR Operator Can be used to pass a pointer when calling a procedure with the INVOKE directive. Passing by reference INVOKE 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.
13
Recursion
14
Stack Frames See other slides
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.