0% found this document useful (0 votes)
21 views31 pages

Lecture 9

The document discusses stacks and procedures in computer engineering. It explains that stacks follow the LIFO principle and allow for nested procedure calls through push and pop operations. When a procedure is called, the return address is pushed onto the stack and local variables and arguments are allocated space in a stack frame. Upon return, the address is popped off the stack to return to the caller.

Uploaded by

Kobe Wang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views31 pages

Lecture 9

The document discusses stacks and procedures in computer engineering. It explains that stacks follow the LIFO principle and allow for nested procedure calls through push and pop operations. When a procedure is called, the return address is pushed onto the stack and local variables and arguments are allocated space in a stack frame. Upon return, the address is popped off the stack to return to the caller.

Uploaded by

Kobe Wang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Introduction to Computer

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

• How do we keep track of the program execution in


nested procedure calls?

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.

IA32 Class Stack: Push


• push SRC

8-8
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

IA32 Class Stack: Push


• push SRC
• Fetch value from SRC
• Decrement %esp by 4 (why?)
• store value at address given by
➢%esp

8-9
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

IA32 Class Stack: Pop


• pop SRC
• Load value from address %esp
• write value to Dest
• Increment %esp by 4

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.

Procedure calls: Control flow


• Use stack to support procedure call and return
• Procedure call: call label
• Push return address on stack
• Jump to label

• call label PC=x4395

x123
esp x3213
x4395

8-13
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Procedure calls: Control flow


• Use stack to support procedure call and return
• Procedure return: ret %esp
• Pop return address from stack
• Jump to address set from the pop instruction

• set PC=x4395

x123
x3213
esp x4395

8-14
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Return values convention


• This is a convention to store return values
• this is not necessary
• Done to make the writing programs easier
• IA32 return values are stored in %eax register
• Choice is arbitrary and could have easily been a different register
• Caller must make sure to save this register value before
calling a callee that returns a value
• Callee placed return value in %eax register
• Upon return, caller finds the return value from the %eax
register

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.

Stack frame structure

8-18
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Call Chain Example

8-19
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Call Chain Example

8-20
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Call Chain Example

8-21
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Call Chain Example

8-22
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Call Chain Example

8-23
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Call Chain Example

8-24
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Call Chain Example

8-25
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Call Chain Example

8-26
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Call Chain Example

8-27
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Call Chain Example

8-28
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Call Chain Example

8-29
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Call Chain Example

8-30
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Stack frame structure

8-31

You might also like