14
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

Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing

Embed Size (px)

Citation preview

Page 1: Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing

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

Page 2: Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing

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.

Page 3: Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing

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.

Page 4: Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing

Local Variable Constraints

• Created on the runtime stack.

• They cannot be given default values at assembly time.

• They will be initialized at runtime.

Page 5: Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing

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

Page 6: Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing

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

Page 7: Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing

Assembly Code listing of Proc

• BubbleSort:– Push ebp– Mov ebp, esp– Add esp, 0FFFFFFF8h ;add –8 to ESP– Mov esp, ebp– Pop ebp– ret

Page 8: Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing

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

Page 9: Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing

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.

Page 10: Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing

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)

Page 11: Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing

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

Page 12: Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing

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.

Page 13: Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing

Recursion

Page 14: Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing

Stack Frames

• See other slides