Professional Documents
Culture Documents
Lecture 9
Lecture 9
Engineering
CS/ECE 252, Spring 2017
Rahul Nayar
Computer Sciences Department
University of Wisconsin – Madison
Stack and
Procedures
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Why Stack?
• Running nested procedure calls in assembly language
• for each procedure calls return address stored in R7
8-3
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Stack:
• A stack is an Abstract Data Type (ADT), commonly used in
most programming languages.
• It follows LIFO principle to access the data values
• LIFO: Last in first out
• Two basic operations:
• PUSH
• POP
8-4
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Stack:
8-5
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Memory layout
local variables and
2N-1 procedure context
new allocated
variables
static variables
literals
8-6
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Memory layout
8-7
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
8-8
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
8-9
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
8-10
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Procedure calls
• Callee must know where to find the args
• Callee must know where to find the “return address”
• Caller must know where to find the return value
• Caller and Callee run on the same CPU.. use the same
registers
• Caller might need to save the register that the Callee might use
• Callee might need to save the registers that the Caller had used
8-11
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Procedure calls
• The convention of where to leave/find things is called
the procedure call linkage
• Details vary between different systems
• What could happen if our program did not follow these
conventions?
8-12
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
x123
esp x3213
x4395
8-13
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• set PC=x4395
x123
x3213
esp x4395
8-14
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
8-15
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Stack Frames
• Contents of a stack frame
➢Local variables
➢Function arguments
➢Return Information
➢Temporary Space
8-16
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Stack Frames
• Management
➢Space allocated when procedure is entered
– Set up code
➢Space deallocated upon return
– Finish code
8-17
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
8-18
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
8-19
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
8-20
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
8-21
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
8-22
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
8-23
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
8-24
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
8-25
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
8-26
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
8-27
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
8-28
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
8-29
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
8-30
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
8-31