The CS 5 Times Penguin/Pig Gang Fight Brings Violence to Claremont

Slides:



Advertisements
Similar presentations
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Advertisements

Computer Architecture CSCE 350
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
Ch. 8 Functions.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
1 Computer Architecture MIPS Simulator and Assembly language.
EECC250 - Shaaban #1 Lec # 6 Winter Stack-Related Instructions PEA Push Effective Address Calculates an effective address and pushes it.
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
Stacks and HeapsCS-3013 A-term A Short Digression on Stacks and Heaps CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
CS 536 Spring Code generation I Lecture 20.
CS 3013 & CS 502 Summer 2006 Digressions:– Producer-Consumer and Stacks 1 Two Digressions Stacks Producer-Consumer models.
Digression: the “Stack” 1 CS502 Spring 2006 Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n
Code Generation CS 480. Can be complex To do a good job of teaching about code generation I could easily spend ten weeks But, don’t have ten weeks, so.
Recursion and Implementation of Functions
Chapter 10 The Stack Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will describe two uses:
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Discussion of Assignment 9 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Higher Level languages have adopted a standard Referred to as C-style calling Uses the stack.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
CS41B MACHINE David Kauchak CS 52 – Fall Admin  Midterm Thursday  Review question sessions tonight and Wednesday  Assignment 3?  Assignment.
Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook.
Objective At the conclusion of this chapter you will be able to:
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
ARM-7 Assembly: Example Programs 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
CPS110: Implementing threads on a uni-processor Landon Cox January 29, 2008.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.
Computer Science 210 Computer Organization
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Chapter 14 Functions.
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
ECE 3430 – Intro to Microcomputer Systems
© Craig Zilles (adapted from slides by Howard Huang)
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Outline for this evening
CS41B recursion David Kauchak CS 52 – Fall 2015.
Functions and Procedures
Chapter 10 The Stack.
Hmmm Assembly Language
Call Stacks, Arguments and Returns
Sports: HMC CS Professor to coach 2020 U. S
Fundamentals of Programming
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
The University of Adelaide, School of Computer Science
by Richard P. Paul, 2nd edition, 2000.
Functions in Hmmm Assembly
Recursion and Implementation of Functions
Computer Science 210 Computer Organization
ECE 3430 – Intro to Microcomputer Systems
Recursion Taken from notes by Dr. Neil Moore
CISC101 Reminders All assignments are now posted.
EET 2261 Unit 6 The Stack; Subroutines
Computer Organization and Assembly Language
Hmmm Assembly Language
© Craig Zilles (adapted from slides by Howard Huang)
Functions in Hmmm Assembly
Functions in Hmmm Assembly
Chapter 14 Functions.
Implementing Functions: Overview
Presentation transcript:

The CS 5 Times Penguin/Pig Gang Fight Brings Violence to Claremont Claremont (Farm News): Gang activity reached a new low when an angry group of penguins viciously beat a pig, a goose, and a duck in an apparently unprovoked attack. Witnesses said that the gang of birds waddled up to the victims, shouting something about an “invasion” and threatening that they would “make bacon bits” and “have a bit of foie gras.” At first, the farm animals attempted to defend themselves, but they found themselves outnumbered and were forced to retreat into a nearby business, the Claremont Village Grill. The owner of the business, Chef Boy Are We Hungry, welcomed them with open arms. The pig soon escaped through a back door, but the duck and goose have not been seen. Relatives now fear the worst. Victim of attack Worksheet: write down what happens to stack during function calls. Demo: factorial.hmmm

Function Calls… Chew on this… def main(): r1 = input() r13 = emma(r1) r13 = r13 + r1 print(r13) return def emma(r1): r1 = r1 + 1 r13 = sarah(r1) return r13 def sarah(r1): r1 = r1 + 42 r13 = r1 + 1 r1=3 Chew on this… emma(3) r1=3 r1=4 You should be worried! sarah(4) r13=47 This slide has seven animations. The sequence shows how r1 (in particular) has its value preserved across calls, and is local to each function. r13=?? r1=4 r1=46 r13=47 return(47)

Function Calls… Chew on this… def main(): r1 = input() r13 = emma(r1) r13 = r13 + r1 print(r13) return def emma(r1): r1 = r1 + 1 r13 = sarah(r1) return r13 def sarah(r1): r1 = r1 + 42 r13 = r1 + 1 r1=3 Chew on this… emma(3) r1=3 r1=4 sarah(4) r13=47 r13=?? r1=4 r1=46 r13=47 return(47)

Function Calls… def main(): r1 = input() r13 = emma(r1) r13 = r13 + r1 print(r13) return def emma(r1): r1 = r1 + 1 r13 = sarah(r1) return r13 def sarah(r1): r1 = r1 + 42 r13 = r1 + 1 r1=3 emma(3) r13=?? r1=3 r1=4 sarah(4) r13=47 This slide continues the animations showing return values. r13=51 return(51) r1=4 r1=46 r13=47 return(47)

Function Calls… 54 Cool, but how does this work!? def main(): r1 = input() r13 = emma(r1) r13 = r13 + r1 print(r13) return def emma(r1): r1 = r1 + 1 r13 = sarah(r1) return r13 def sarah(r1): r1 = r1 + 42 r13 = r1 + 1 r1=3 emma(3) r13=51 r13=54 54 r1=3 r1=4 sarah(4) r13=47 Cool, but how does this work!? r13=51 return(51) r1=4 r1=46 r13=47 return(47)

The Stack! Insert (“push”) Remove (“pop”) AFLAC, ShmAFLAC! Be careful up there! Insert (“push”) Remove (“pop”)

Function Calls… r1 3 4 4 3 46 r13 47 4 3 def main(): r1 = int(input()) Hmmm code up here! The stack in RAM! def main(): r1 = int(input()) r13 = emma(r1) r13 = r13 + r1 print(r13) return def emma(r1): r1 = r1 + 1 r13 = sarah(r1) return r13 def sarah(r1): r1 = r1 + 42 r13 = r1 + 1 r1=3 r1 3 4 4 3 46 r13 47 This slide has multiple animations showing how the stack works. return address r14 4 3

Function Calls… r1 3 4 46 r13 47 4 3 def main(): r1 = int(input()) Hmmm code up here! The stack in RAM! def main(): r1 = int(input()) r13 = emma(r1) r13 = r13 + r1 print(r13) return def emma(r1): r1 = r1 + 1 r13 = sarah(r1) return r13 def sarah(r1): r1 = r1 + 42 r13 = r1 + 1 r1=3 r1 3 4 46 r13 47 return address r14 4 3

Function Calls… r1 3 3 4 4 46 r13 51 54 47 4 3 def main(): Hmmm code up here! The stack in RAM! def main(): r1 = int(input()) r13 = emma(r1) r13 = r13 + r1 print(r13) return def emma(r1): r1 = r1 + 1 r13 = sarah(r1) return r13 def sarah(r1): r1 = r1 + 42 r13 = r1 + 1 r1 3 3 4 4 46 r13 51 54 47 return address r14 4 3

Function Calls… 3 4 r1 3 4 r13 54 def main(): r1 = int(input()) Hmmm code up here! The stack in RAM! def main(): r1 = int(input()) r13 = emma(r1) r13 = r13 + r1 print(r13) return def emma(r1): r1 = r1 + 1 r13 = sarah(r1) return r13 def sarah(r1): r1 = r1 + 42 r13 = r1 + 1 3 4 r1 3 4 r13 54 return address r14

Implementing Functions or some other large-enough value (1) Use r15 as the stack pointer. setn r15 42 store the return address r14 and the inputs: r1, (r2), (r3) (2) Before the function call, Store all valuable data to the stack—and increment r15 storer r1 r15 addn r15 1 (3) Get r1, (r2), (r3), … ready as function “arguments." (4) Make the function call. line # of the function The result, if any, will be in r13. calln r14 # (5) After the function call, addn r15 -1 loadr r1 r15 for each item stored Load valuable data back from the stack (in reverse order)

Now Without Pigs and Geese! We object to this!!! RAM! def main(): r1 = int(input()) r13 = emma(r1) r13 = r13 + r1 print(r13) return def emma(r1): r1 = r1 + 1 r13 = sarah(r1) return r13 def sarah(r1): r1 = r1 + 42 r13 = r1 + 1 01001001 r1=3 11001011 1 … r1 3 01001011 25 26 r13 return value 27 r14 return address 28 r15 25 stack pointer 29 … 255

Now Without Pigs and Geese! It was better with pigs and geese! 00 setn r15 26 # set stack pointer to 26 01 read r1 # start of main 02 storer r1 r15 # store r1 on the stack 03 addn r15 1 # increment stack pointer 04 calln r14 10 # call emma 05 addn r15 -1 # decrement stack pointer 06 loadr r1 r15 # load r1 from the stack 07 add r13 r13 r1 # r13 = r13 + r1 08 write r13 09 halt 10 addn r1 1 # start of emma! 11 storer r1 r15 # store r1 on the stack 12 addn r15 1 # increment stack pointer 13 storer r14 r15 # save return addr on stack 14 addn r15 1 # increment stack pointer 15 calln r14 22 # call sarah 16 addn r15 -1 # decrement stack pointer 17 loadr r14 r15 # load ret addr from stack 18 addn r15 -1 # decrement stack pointer 19 loadr r1 r15 # load r1 from the stack 20 add r13 r13 r1 # r13 = r13 + r1 21 jumpr r14 # return! 22 addn r1 42 # start of sarah! 23 setn r2 1 # put 1 in a register 24 add r13 r1 r2 25 jumpr r14 # return def main(): r1 = int(input()) r13 = emma(r1) r13 = r13 + r1 print(r13) return def emma(r1): r1 = r1 + 1 r13 = sarah(r1) return r13 def sarah(r1): r1 = r1 + 42 r13 = r1 + 1 save restore save Don't spend much time on this. The point is that the horizontallines represent the different functions, and that there are setup/teardown or scaffolding instructions that serve to guarantee that local variables remain local. We will address this in more detail in the next few slides when we cover recursion. restore

Recursion? fac(5) 5 * fac(4) 4 * fac(3) 3 * fac(2) 2 * fac(1) 1 def fac(N): if N <= 1: return 1 else: return N * fac(N-1) Recursion? fac(5) "The Stack" 5 * fac(4) 4 * fac(3) 3 * fac(2) Remembers all of the individual calls to fac 2 * fac(1) 1

Factorial via Recursion… Python r1 (N) r13(Res) Return value n = int(input()) answer = fac(n) print(n, answer) def fac(n): """recursive factorial!""" if n == 0: return 1 else: res = fac(n-1) return n*res r14 Return address First let’s try N=0 and then N=3 jumpr r14 jumpr r14 This is same as return n*fac(n-1) but done in 2 steps…

Python Hmmm Python Hmmm b r1 n = int(input()) answer = fac(n) match each piece of Python code with the Hmmm assembly code that implements it. Hmmm b r1 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 n = int(input()) answer = fac(n) print(answer) def fac(n): """ recursive factorial! """ if n==0: return 1 else: res = fac(n-1) return n*res Try to align the Python code to the Hmmm code… as shown in the next slide…

Python Hmmm Python Hmmm r13 is the answer r14 is the “return address” “stack pointer” r14 is the “return address” r13 is the answer Python Hmmm 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 n = int(input()) answer = fac(n) print(answer) def fac(n): """ recursive factorial! """ if n==0: return 1 else: res = fac(n-1) return n*res

Python Hmmm Python Hmmm r13 is the answer r14 is the “return address” “stack pointer” r14 is the “return address” r13 is the answer Python Hmmm 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 n = int(input()) answer = fac(n) print(answer) def fac(n): """ recursive factorial! """ if n==0: return 1 else: res = fac(n-1) return n*res

Python Hmmm Python Hmmm r13 is the answer r14 is the “return address” “stack pointer” r14 is the “return address” r13 is the answer Python Hmmm 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 n = int(input()) answer = fac(n) print(answer) def fac(n): """ recursive factorial! """ if n==0: return 1 else: res = fac(n-1) return n*res This is the end of n=0. Next slide is the else case, which is what we do if n=3.

Python Hmmm Python Hmmm r13 is the answer r14 is the “return address” “stack pointer” r14 is the “return address” r13 is the answer Python Hmmm 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 n = int(input()) answer = fac(n) print(answer) def fac(n): """ recursive factorial! """ if n==0: return 1 else: res = fac(n-1) return n*res At this point we're doing n!=0. After the break we'll look at this in more detail. Prepare for function call! All precious belongings must be saved on the stack!

The Alien’s Life Advice Learn people’s names! …and learn them right!

Python Hmmm Python Hmmm r13 is the answer r14 is the “return address” “stack pointer” r14 is the “return address” r13 is the answer Python Hmmm 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 n = int(input()) answer = fac(n) print(answer) def fac(n): """ recursive factorial! """ if n==0: return 1 else: res = fac(n-1) return n*res

Python Hmmm Python Hmmm r13 is the answer r14 is the “return address” “stack pointer” r14 is the “return address” r13 is the answer Python Hmmm 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 n = int(input()) answer = fac(n) print(answer) def fac(n): """ recursive factorial! """ if n==0: return 1 else: res = fac(n-1) return n*res

Python Hmmm Python Hmmm r13 is the answer r14 is the “return address” “stack pointer” r14 is the “return address” r13 is the answer Python Hmmm 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 n = int(input()) answer = fac(n) print(answer) def fac(n): """ recursive factorial! """ if n==0: return 1 else: res = fac(n-1) return n*res Function call over! All precious belongings back into their registers!

Python Hmmm Python Hmmm r13 is the answer r14 is the “return address” “stack pointer” r14 is the “return address” r13 is the answer Python Hmmm 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 n = int(input()) answer = fac(n) print(answer) def fac(n): """ recursive factorial! """ if n==0: return 1 else: res = fac(n-1) return n*res

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 r0 43 argument: n 44 r1 45 result, return value 46 factorial function r13 47 48 return address (line #) r14 49 50 Stack Pointer 51 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

No need to copy this down. The following “movie” will be posted on the course website. Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 r0 43 argument: n 44 r1 45 result, return value 46 factorial function r13 47 48 return address (line #) r14 49 50 Stack Pointer 51 42 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 r0 43 argument: n 44 r1 3 45 result, return value 46 factorial function r13 47 48 return address (line #) r14 49 50 Stack Pointer 51 42 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 r0 43 argument: n 44 r1 3 45 result, return value 46 factorial function r13 47 48 return address (line #) 3 r14 49 50 Stack Pointer 51 42 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 r0 43 argument: n 44 r1 3 45 result, return value 46 factorial function r13 47 48 return address (line #) 3 r14 49 50 Stack Pointer 51 42 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 argument: n 44 r1 3 45 result, return value 46 factorial function r13 47 48 return address (line #) 3 r14 49 50 Stack Pointer 51 42 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 argument: n 44 r1 3 45 result, return value 46 factorial function r13 47 48 return address (line #) 3 r14 49 50 Stack Pointer 51 43 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 argument: n 44 r1 3 45 result, return value 46 factorial function r13 47 48 return address (line #) 3 r14 49 50 Stack Pointer 51 43 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 argument: n 44 r1 3 45 result, return value 46 factorial function r13 47 48 return address (line #) 3 r14 49 50 Stack Pointer 51 44 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 argument: n 44 r1 2 45 result, return value 46 factorial function r13 47 48 return address (line #) 3 r14 49 50 Stack Pointer 51 44 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 argument: n 44 r1 2 45 result, return value 46 factorial function r13 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 44 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 argument: n 44 r1 2 45 result, return value 46 factorial function r13 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 44 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 2 45 result, return value 46 factorial function r13 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 44 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 2 45 result, return value 46 factorial function r13 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 45 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 2 14 45 result, return value 46 factorial function r13 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 45 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 2 14 45 result, return value 46 factorial function r13 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 46 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 1 14 45 result, return value 46 factorial function r13 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 46 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 1 14 45 result, return value 46 factorial function r13 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 46 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 1 14 45 result, return value 46 factorial function r13 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 46 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 1 14 45 1 result, return value 46 factorial function r13 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 46 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 1 14 45 1 result, return value 46 factorial function r13 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 47 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 1 14 45 1 result, return value 46 factorial function r13 14 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 47 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 1 14 45 1 result, return value 46 factorial function r13 14 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 48 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 14 45 1 result, return value 46 factorial function r13 14 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 48 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 14 45 1 result, return value 46 factorial function r13 14 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 48 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 14 45 1 result, return value 46 factorial function r13 14 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 48 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 14 45 1 result, return value 46 factorial function 1 r13 14 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 48 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 14 45 1 result, return value 46 factorial function 1 r13 14 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 48 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 14 45 1 result, return value 46 factorial function 1 r13 14 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 47 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 14 45 1 result, return value 46 factorial function 1 r13 14 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 47 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 14 45 1 result, return value 46 factorial function 1 r13 14 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 46 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 1 14 45 1 result, return value 46 factorial function 1 r13 14 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 46 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 1 14 45 1 result, return value 46 factorial function 1 r13 14 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 46 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 1 14 45 1 result, return value 46 factorial function 1 r13 14 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 46 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 1 14 45 1 result, return value 46 factorial function 1 r13 14 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 45 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 1 14 45 1 result, return value 46 factorial function 1 r13 14 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 45 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 1 14 45 1 result, return value 46 factorial function 1 r13 14 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 44 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 2 14 45 1 result, return value 46 factorial function 1 r13 14 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 44 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 2 14 45 1 result, return value 46 factorial function 2 r13 14 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 44 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 2 14 45 1 result, return value 46 factorial function 2 r13 14 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 44 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 2 14 45 1 result, return value 46 factorial function 2 r13 14 47 48 return address (line #) 14 r14 49 50 Stack Pointer 51 43 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 2 14 45 1 result, return value 46 factorial function 2 r13 14 47 48 return address (line #) 3 r14 49 50 Stack Pointer 51 43 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 2 14 45 1 result, return value 46 factorial function 2 r13 14 47 48 return address (line #) 3 r14 49 50 Stack Pointer 51 42 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 3 14 45 1 result, return value 46 factorial function 2 r13 14 47 48 return address (line #) 3 r14 49 50 Stack Pointer 51 42 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 3 14 45 1 result, return value 46 factorial function 6 r13 14 47 48 return address (line #) 3 r14 49 50 Stack Pointer 51 42 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 3 14 45 1 result, return value 46 factorial function 6 r13 14 47 48 return address (line #) 3 r14 49 50 Stack Pointer 51 42 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 argument: n 44 r1 3 14 45 1 result, return value 46 factorial function 6 r13 14 47 48 return address (line #) 3 r14 49 50 Stack Pointer 51 42 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Worksheet Name: Write down what happens in the registers and memory (the stack) as this program runs… Program (“low part of RAM”) CPU Registers Memory (“high part of RAM”) 00 setn r15 42 01 read r1 02 calln r14 5 03 write r13 04 halt 05 jnezn r1 8 06 setn r13 1 07 jumpr r14 08 storer r1 r15 09 addn r15 1 10 storer r14 r15 11 addn r15 1 12 addn r1 -1 13 calln r14 5 14 addn r15 -1 15 loadr r14 r15 16 addn r15 -1 17 loadr r1 r15 18 mul r13 r13 r1 19 jumpr r14 The input is 3. with labels "the stack" always-0 register 42 3 r0 43 3 2 Input: N 44 r1 3 14 45 1 result, return value 46 factorial function 6 r13 14 47 48 return address (line #) 3 r14 49 50 Stack Pointer 51 42 r15 52 How low could we start the stack? How deep does the stack get? What are the possible values of r14?

Demo Factorial! Have demo terminal window already set up!

Towers of Hanoi hanoi (Disks, From, To) hanoi(3, 1, 3) Peg 1 Peg 2 This puzzle can get Hanoi'ing! Peg 1 Peg 2 Peg 3

Towers of Hanoi hanoi (Disks, From, To) hanoi(3, 1, 3) 1 to 3 Peg 1 This puzzle can get Hanoi'ing! Peg 1 Peg 2 Peg 3

Towers of Hanoi hanoi (Disks, From, To) hanoi(3, 1, 3) 1 to 3 1 to 2 This puzzle can get Hanoi'ing! Peg 1 Peg 2 Peg 3

Towers of Hanoi hanoi (Disks, From, To) hanoi(3, 1, 3) 1 to 3 1 to 2 This puzzle can get Hanoi'ing! Peg 1 Peg 2 Peg 3

Towers of Hanoi hanoi (Disks, From, To) hanoi(3, 1, 3) 1 to 3 1 to 2 This puzzle can get Hanoi'ing! Peg 1 Peg 2 Peg 3

Towers of Hanoi hanoi (Disks, From, To) hanoi(3, 1, 3) 1 to 3 1 to 2 This puzzle can get Hanoi'ing! Peg 1 Peg 2 Peg 3

Towers of Hanoi hanoi (Disks, From, To) hanoi(3, 1, 3) 1 to 3 1 to 2 This puzzle can get Hanoi'ing! Peg 1 Peg 2 Peg 3

Towers of Hanoi hanoi (Disks, From, To) hanoi(3, 1, 3) 1 to 3 1 to 2 This puzzle can get Hanoi'ing! 7 = 23 - 1 moves Peg 1 Peg 2 Peg 3

The Hanoi Legend

Towers of Hanoi Peg 1 Peg 2 Peg 3 hanoi (Disks, From, To) if Disks == 1: print( str(From) + ”,” + str(To) ) return else: # COMPUTE “Other” peg hanoi(Disks-1, From, Other) hanoi(1, From, To) hanoi(Disks-1, Other, To) Peg 1 Peg 2 Peg 3

3 1 3 Peg 1 Peg 2 Peg 3 hanoi (Disks, From, To) if Disks == 1: print( str(From) + ”,” + str(To) ) return else: # COMPUTE “Other” peg hanoi(Disks-1, From, Other) hanoi(1, From, To) hanoi(Disks-1, Other, To) From To hanoi(2, 1, 2) hanoi(1, 1, 3) hanoi(2, 2, 3) Peg 1 Peg 2 Peg 3

3 1 3 Peg 1 Peg 2 Peg 3 hanoi (Disks, From, To) if Disks == 1: print( str(From) + ”,” + str(To) ) return else: # COMPUTE “Other” peg hanoi(Disks-1, From, Other) hanoi(1, From, To) hanoi(Disks-1, Other, To) hanoi(2, 1, 2) hanoi(1, 1, 3) hanoi(1, 1, 2) hanoi(1, 3, 2) hanoi(1, 1, 3) hanoi(2, 2, 3) Peg 1 Peg 2 Peg 3

What’s Next? Cool application areas… Data compression Secret sharing AI and games Object-oriented programs (OOPS!) Limits of computation: Are there things computers cannot do?