CS41B recursion David Kauchak CS 52 – Fall 2015.

Slides:



Advertisements
Similar presentations
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
Advertisements

Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
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.
CSS 372 Lecture 1 Course Overview: CSS 372 Web page Syllabus Lab Ettiquette Lab Report Format Review of CSS 371: Simple Computer Architecture Traps Interrupts.
CS 536 Spring Run-time organization Lecture 19.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
CS 536 Spring Code generation I Lecture 20.
Intro to Computer Architecture
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Run-time Environment and Program Organization
Digression: the “Stack” 1 CS502 Spring 2006 Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
CMSC 414 Computer and Network Security Lecture 20 Jonathan Katz.
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Runtime Environments Compiler Construction Chapter 7.
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.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CS41B MACHINE David Kauchak CS 52 – Fall Admin  Midterm Thursday  Review question sessions tonight and Wednesday  Assignment 3?  Assignment.
CS41B MACHINE David Kauchak CS 52 – Fall Admin  Assignment 3  due Monday at 11:59pm  one small error in 5b (fast division) that’s been fixed.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Lecture 5 Page 1 CS 111 Online Processes CS 111 On-Line MS Program Operating Systems Peter Reiher.
ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.
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.
Computer Science 210 Computer Organization
David Kauchak CS 52 – Spring 2017
ECE 3430 – Intro to Microcomputer Systems
Functions and the Stack
© Craig Zilles (adapted from slides by Howard Huang)
The Stack.
143A: Principles of Operating Systems Lecture 4: Calling conventions
Run-time organization
David Kauchak CS 52 – Spring 2016
Introduction to Compilers Tim Teitelbaum
Outline for this evening
Chapter 14 Functions.
Stack Frame Linkage.
Fundamentals of Programming
The University of Adelaide, School of Computer Science
More Functions and the Stack
Topic 3-a Calling Convention 1/10/2019.
Program and memory layout
8051 ASSEMBLY LANGUAGE PROGRAMMING
Program and memory layout
Computer Architecture
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
© Craig Zilles (adapted from slides by Howard Huang)
Chapter 14 Functions.
Topic 2b ISA Support for High-Level Languages
Chapter 14 Functions.
Implementing Functions: Overview
Presentation transcript:

CS41B recursion David Kauchak CS 52 – Fall 2015

Admin Midterm back on Thursday Assignments Survey in assignment 4 Assignment 4: due Monday (10/12 at 11:59pm) Assignment 5: due Friday (10/23 at 5pm) Assignment 6: due Monday (11/2 at 11:59pm) Survey in assignment 4 Academic Honesty

Academic Honesty A few rules to follow for this course to keep you out of trouble: If you talk with someone in the class about a problem, you should not take notes. If you understand the material you talked about, you should be able to recreate it on your own. Similarly, if you talk with someone, you must wait 5 minutes before resuming work on the problem. Stretch. Use the restroom. Go for a quick walk. This will ensure that you really understand the material. You may not sit next to (or where you can see the screen of) anyone you are talking with about the assignment. The only time you may look at someone else's screen is if they are asking you for help with a basic programming problem (e.g. syntax error). You should not look at someone else's code to help yourself!

Examples from this lecture http://www.cs.pomona.edu/~dkauchak/classes/cs52/examples/cs41b/

CS41B machine CPU instruction counter (location in memory of the next instruction in memory) processor ic r0 holds the value 0 (read only) r1 general purpose read/write r2 registers r3

Memory layout Code Heap Stack Code Where dynamically allocated program data is stored Heap Where program/function execution information is stored, parameters, and local variables Stack

Stack frame Key unit for keeping track of a function call return address (where to go when we’re done executing) parameters local variables

CS41B function call conventions r1: reserved for the stack pointer r2: contains the return address r3: contains the first parameter additional parameters go on the stack (more on this) the result (i.e. the return value) should go in r3

Structure of a single parameter function fname psh r2 ; save return address on stack ... ; do work using r3 as argument ; put result in r3 pop r2 ; restore return address from stack jmp r2 ; return to caller conventions: argument is in r3 r1 is off-limits since it’s used for the stack pointer return value goes in r3

Our first function call loa r3 r0 ; get variable lcw r2 increment ; call increment cal r2 r2 sto r0 r3 ; write result, hlt ; and halt increment psh r2 ; save the return address on the stack adc r3 r3 1 ; add 1 to the input parameter pop r2 ; get the return address from stack jmp r2 ; go back to where we were called from

Functions with multiple arguments fname psh r2 ; save return address on stack loa r2 r1 4 ; load the second parameter into r2 ... ; do work using r3 and r2 as arguments ; put result in r3 pop r2 ; restore return address from stack jmp r2 ; return to caller conventions: first argument is in r3 r1 is off-limits since it’s used for the stack pointer return value goes in r3

Functions with multiple arguments fname psh r2 ; save return address on stack loa r2 r1 4 ; load the second parameter into r2 ... ; do work using r3 and r2 as arguments ; put result in r3 pop r2 ; restore return address from stack jmp r2 ; return to caller What does this operation do? What is the 4?

Functions with multiple arguments fname psh r2 ; save return address on stack loa r2 r1 4 ; load the second parameter into r2 ... ; do work using r3 and r2 as arguments ; put result in r3 pop r2 ; restore return address from stack jmp r2 ; return to caller r1 is the stack pointer and points at the top (next) slot stacks grow towards smaller memory values

Functions with multiple arguments fname psh r2 ; save return address on stack loa r2 r1 4 ; load the second parameter into r2 ... ; do work using r3 and r2 as arguments ; put result in r3 pop r2 ; restore return address from stack jmp r2 ; return to caller r1 is the stack pointer and points at the top (next) slot stacks grow towards smaller memory values r1+2 is then the top value of the stack r1+4 is the 2nd value of the stack

Multiple arguments What does this code do? max psh r2 loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif pop r2 jmp r2 What does this code do?

Multiple arguments max, as a function! max psh r2 loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif pop r2 jmp r2 max, as a function!

Calling max loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt Anything different?

Calling max For the second argument, push it on the stack loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt For the second argument, push it on the stack

loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt max loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif jmp r2 r2 r3 sp (r1) Stack

loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt max loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif jmp r2 r2 r3 10 sp (r1) Stack

loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt max loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif jmp r2 r2 2 r3 10 sp (r1) Stack

loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt max loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif jmp r2 r2 2 r3 10 sp (r1) 2 Stack

loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt max loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif jmp r2 r2 2 r3 10 sp (r1) 2 Stack

loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt max loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif jmp r2 r2 max r3 10 Notice that we overwrote the value in r2 If we hadn’t save it on the stack, it would have been lost sp (r1) 2 Stack

loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt max loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif jmp r2 r2 max r3 10 sp (r1) 2 Stack

loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt max loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif jmp r2 r2 loc: cal r3 10 sp (r1) 2 Stack

loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt max loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif jmp r2 r2 loc: cal r3 10 sp (r1) 2 Stack

loc: cal 2 r2 loc: cal r3 10 sp (r1) Stack loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt max loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif jmp r2 r2 loc: cal r3 10 sp (r1) loc: cal 2 Stack

loc: cal 2 r2 loc: cal r3 10 sp (r1) Stack loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt max loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif jmp r2 r2 loc: cal r3 10 sp (r1) loc: cal 2 Stack

loc: cal 2 r2 loc: cal r3 10 sp (r1) larger values Stack loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt max loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif jmp r2 r2 loc: cal r3 10 sp (r1) loc: cal +2 larger values 2 +4 Stack

loc: cal 2 r2 2 r3 10 sp (r1) larger values Stack loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt max loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif jmp r2 r2 2 r3 10 sp (r1) loc: cal +2 larger values 2 +4 Stack

loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt max loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif jmp r2 r2 2 r3 10 sp (r1) loc: cal 2 Stack

loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt max loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif jmp r2 r2 2 r3 10 sp (r1) loc: cal 2 Stack

loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt max loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif jmp r2 r2 loc: cal r3 10 sp (r1) 2 Stack

loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt max loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif jmp r2 r2 loc: cal r3 10 sp (r1) 2 Stack

loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt max loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif jmp r2 r2 loc: cal r3 10 sp (r1) 2 Stack

loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt max loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif jmp r2 r2 2 r3 10 sp (r1) Stack

loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt max loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif jmp r2 r2 2 r3 10 sp (r1) Stack

loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt max loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif jmp r2 r2 2 r3 10 10! sp (r1) Stack

loa r3 r0 loa r2 r0 psh r2 lcw r2 max cal r2 r2 pop r2 sto r0 r3 hlt max loa r2 r1 4 bge r3 r2 endif adc r3 r2 0 endif jmp r2 r2 2 r3 10 sp (r1) Stack

Real structure of CS41B program ; great comments at the top! ; lcw r1 stack instruction1 ; comment instruction2 ; comment ... hlt ; stack area: 50 words dat 100 stack end Save address of highest end (highest address) of the stack in r1 Reserve 50 words for the stack

Recursion What does this function do? int mystery(int a, int b){ if( b <= 0 ){ return 0 else return a + mystery(a, b-1) } What does this function do?

Recursion Multiplication… a*b (assuming b is positive) int mystery(int a, int b){ if( b <= 0 ){ return 0 else return a + mystery(a, b-1) } Multiplication… a*b (assuming b is positive) Note to future Dave from past Dave: write the function up on the board 

Function cleanup and return mult psh r2 ; save the return address psh r3 ; save first argument, a, on stack loa r2 r1 6 ; get at the 2nd argument, b ; b = r2, a = r3 blt r0 r2 else ; 0 < r2, i.e. recursive case adc r3 r0 0 ; return 0 brs endif else sbc r2 r2 1 ; r2 = b-1 psh r2 ; add r2 as 2nd argument lcw r2 mult ; call mult recursively cal r2 r2 pop r2 ; pop 2nd argument off stack loa r2 r1 2 ; load a into r2 off of the stack add r3 r3 r2 ; r3 = a + mult(a, b-1) endif pop r0 ; remove first argument from stack pop r2 ; get the return address jmp r2 ; return Function startup Base case Recursive case Recursive call answer calculation Function cleanup and return

Notice symmetry of psh and pop mult psh r2 ; save the return address psh r3 ; save first argument, a, on stack loa r2 r1 6 ; get at the 2nd argument, b ; b = r2, a = r3 blt r0 r2 else ; 0 < r2, i.e. recursive case adc r3 r0 0 ; return 0 brs endif else sbc r2 r2 1 ; r2 = b-1 psh r2 ; add r2 as 2nd argument, r3 shouldn't have changed lcw r2 mult ; call mult recursively cal r2 r2 pop r2 ; pop 2nd argument off stack loa r2 r1 2 ; load a into r2 off of the stack add r3 r3 r2 ; r3 = a + mult(a, b-1) endif pop r0 ; remove first argument from stack pop r2 ; get the return address jmp r2 ; return Notice symmetry of psh and pop

Calling mult r2 r3 sp (r1) Stack loa r3 r0 loa r2 r0 psh r2 lcw r2 mult cal r2 r2 pop r2 sto r0 r3 hlt sp (r1) Stack

Calling mult r2 r3 sp (r1) Stack loa r3 r0 loa r2 r0 psh r2 lcw r2 mult cal r2 r2 pop r2 sto r0 r3 hlt sp (r1) Stack

Calling mult r2 r3 6 sp (r1) Stack loa r3 r0 loa r2 r0 psh r2 lcw r2 mult cal r2 r2 pop r2 sto r0 r3 hlt sp (r1) Stack

Calling mult r2 r3 6 sp (r1) Stack loa r3 r0 loa r2 r0 psh r2 lcw r2 mult cal r2 r2 pop r2 sto r0 r3 hlt sp (r1) Stack

Calling mult r2 2 r3 6 sp (r1) Stack loa r3 r0 loa r2 r0 psh r2 lcw r2 mult cal r2 r2 pop r2 sto r0 r3 hlt sp (r1) Stack

Calling mult r2 2 r3 6 sp (r1) Stack loa r3 r0 loa r2 r0 psh r2 lcw r2 mult cal r2 r2 pop r2 sto r0 r3 hlt sp (r1) Stack

Calling mult r2 2 r3 6 sp (r1) 2 Stack loa r3 r0 loa r2 r0 psh r2 lcw r2 mult cal r2 r2 pop r2 sto r0 r3 hlt sp (r1) 2 Stack

Calling mult r2 2 r3 6 sp (r1) 2 Stack loa r3 r0 loa r2 r0 psh r2 lcw r2 mult cal r2 r2 pop r2 sto r0 r3 hlt sp (r1) 2 Stack

Calling mult r2 loc: mult r3 6 sp (r1) 2 Stack loa r3 r0 loa r2 r0 psh r2 lcw r2 mult cal r2 r2 pop r2 sto r0 r3 hlt sp (r1) 2 Stack

Calling mult r2 loc: mult r3 6 sp (r1) 2 Stack loa r3 r0 loa r2 r0 psh r2 lcw r2 mult cal r2 r2 pop r2 sto r0 r3 hlt sp (r1) 2 Stack

r2 loc: cal0 r3 6 sp (r1) 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal0 r3 6 sp (r1) 2 Stack

r2 loc: cal0 r3 6 sp (r1) 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal0 r3 6 sp (r1) 2 Stack

r2 loc: cal0 r3 6 sp (r1) loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal0 r3 6 sp (r1) loc: cal0 2 Stack

r2 loc: cal0 r3 6 sp (r1) loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal0 r3 6 sp (r1) loc: cal0 2 Stack

r2 loc: cal0 r3 6 Why push r3? sp (r1) 6 loc: cal0 2 Stack mult psh r2 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal0 r3 6 Why push r3? sp (r1) 6 loc: cal0 2 Stack

mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal0 r3 6 Not always required, but allows us to use r3 for computation without worrying about losing first parameter sp (r1) 6 loc: cal0 2 Stack

r2 loc: cal0 r3 6 sp (r1) 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal0 r3 6 sp (r1) 6 loc: cal0 2 Stack

r2 loc: cal0 r3 6 sp (r1) 6 loc: cal0 larger values 2 Stack +2 +4 +6 mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal0 r3 6 sp (r1) 6 +2 loc: cal0 +4 larger values 2 +6 Stack

r2 2 r3 6 sp (r1) 6 loc: cal0 larger values 2 Stack +2 +4 +6 mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 2 r3 6 sp (r1) 6 +2 loc: cal0 +4 larger values 2 +6 Stack

r2 2 r3 6 sp (r1) 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif jmp r2 r2 2 r3 6 sp (r1) 6 loc: cal0 2 Stack

r2 2 r3 6 sp (r1) 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 2 r3 6 sp (r1) 6 loc: cal0 2 Stack

r2 1 r3 6 sp (r1) 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 1 r3 6 sp (r1) 6 loc: cal0 2 Stack

r2 1 r3 6 sp (r1) 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 1 r3 6 sp (r1) 6 loc: cal0 2 Stack

r2 1 r3 6 sp (r1) 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 1 r3 6 sp (r1) 1 6 loc: cal0 2 Stack

r2 1 r3 6 sp (r1) 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 1 r3 6 sp (r1) 1 6 loc: cal0 2 Stack

r2 loc: mult r3 6 sp (r1) 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: mult r3 6 sp (r1) 1 6 loc: cal0 2 Stack

r2 loc: mult r3 6 sp (r1) 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: mult r3 6 sp (r1) 1 6 loc: cal0 2 Stack

r2 loc: cal1 r3 6 sp (r1) 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal1 r3 6 sp (r1) 1 6 loc: cal0 2 Stack

r2 loc: cal1 r3 6 sp (r1) 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal1 r3 6 sp (r1) 1 6 loc: cal0 2 Stack

r2 loc: cal1 r3 6 sp (r1) loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal1 r3 6 sp (r1) loc: cal1 1 6 loc: cal0 2 Stack

r2 loc: cal1 r3 6 sp (r1) loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal1 r3 6 sp (r1) loc: cal1 1 6 loc: cal0 2 Stack

r2 loc: cal1 r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal1 r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 loc: cal1 r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal1 r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 1 r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 1 r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 1 r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 1 r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 1 r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 1 r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 loc: mult r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: mult r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 loc: mult r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: mult r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 loc: cal1 r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal1 r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 loc: cal1 r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal1 r3 6 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 loc: cal1 r3 6 sp (r1) loc: cal1 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal1 r3 6 sp (r1) loc: cal1 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 loc: cal1 r3 6 sp (r1) loc: cal1 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal1 r3 6 sp (r1) loc: cal1 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 loc: cal1 r3 6 sp (r1) 6 loc: cal1 6 loc: cal1 1 6 loc: cal0 2 mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal1 r3 6 sp (r1) 6 loc: cal1 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 loc: cal1 r3 6 sp (r1) 6 loc: cal1 6 loc: cal1 1 6 loc: cal0 2 mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal1 r3 6 sp (r1) 6 loc: cal1 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 r3 6 sp (r1) 6 loc: cal1 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 r3 6 sp (r1) 6 loc: cal1 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 r3 6 sp (r1) 6 loc: cal1 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 r3 6 sp (r1) 6 loc: cal1 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 r3 6 sp (r1) 6 loc: cal1 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 r3 6 sp (r1) 6 loc: cal1 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 r3 sp (r1) 6 loc: cal1 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 r3 sp (r1) 6 loc: cal1 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 r3 sp (r1) 6 loc: cal1 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 r3 sp (r1) 6 loc: cal1 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 r3 Stack frames! sp (r1) 6 loc: cal1 6 loc: cal1 1 6 loc: cal0 2 mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 r3 Stack frames! sp (r1) 6 loc: cal1 mult(6,0) 6 loc: cal1 mult(6,1) 1 6 loc: cal0 mult(6,2) 2 Stack

r2 r3 sp (r1) 6 loc: cal1 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 r3 sp (r1) 6 loc: cal1 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 r3 sp (r1) loc: cal1 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 r3 We don’t actually care about this value, but need to get it off the stack to cleanup… just put it in r0, which throws it away sp (r1) loc: cal1 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 r3 sp (r1) loc: cal1 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 r3 sp (r1) loc: cal1 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 loc: cal1 r3 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal1 r3 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 loc: cal1 r3 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal1 r3 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack

Returning with answer in r3 mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal1 r3 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack Returning with answer in r3

r2 loc: cal1 r3 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal1 r3 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 r3 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 r3 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 r3 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 r3 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 6 r3 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 6 r3 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 6 r3 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 6 r3 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 6 r3 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 6 r3 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 6 r3 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 6 r3 sp (r1) 6 loc: cal1 1 6 loc: cal0 2 Stack

r2 6 r3 sp (r1) loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 6 r3 sp (r1) loc: cal1 1 6 loc: cal0 2 Stack

r2 6 r3 sp (r1) loc: cal1 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 6 r3 sp (r1) loc: cal1 1 6 loc: cal0 2 Stack

r2 loc: cal1 r3 6 sp (r1) 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal1 r3 6 sp (r1) 1 6 loc: cal0 2 Stack

r2 loc: cal1 r3 6 sp (r1) 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal1 r3 6 sp (r1) 1 6 loc: cal0 2 Stack

Returning with answer in r3 mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal1 r3 6 sp (r1) 1 6 loc: cal0 2 Stack Returning with answer in r3

r2 loc: cal1 r3 6 sp (r1) 1 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal1 r3 6 sp (r1) 1 6 loc: cal0 2 Stack

r2 1 r3 6 sp (r1) 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 1 r3 6 sp (r1) 6 loc: cal0 2 Stack

r2 1 r3 6 sp (r1) 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 1 r3 6 sp (r1) 6 loc: cal0 2 Stack

r2 6 r3 sp (r1) 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 6 r3 sp (r1) 6 loc: cal0 2 Stack

r2 6 r3 sp (r1) 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 6 r3 sp (r1) 6 loc: cal0 2 Stack

r2 6 r3 12 sp (r1) 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 6 r3 12 sp (r1) 6 loc: cal0 2 Stack

r2 6 r3 12 sp (r1) 6 loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 6 r3 12 sp (r1) 6 loc: cal0 2 Stack

r2 6 r3 12 sp (r1) loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 6 r3 12 sp (r1) loc: cal0 2 Stack

r2 6 r3 12 sp (r1) loc: cal0 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 6 r3 12 sp (r1) loc: cal0 2 Stack

r2 loc: cal0 r3 12 sp (r1) 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal0 r3 12 sp (r1) 2 Stack

r2 loc: cal0 r3 12 sp (r1) 2 Stack mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal0 r3 12 sp (r1) 2 Stack

Returning with answer in r3 mult psh r2 psh r3 loa r2 r1 6 blt r0 r2 else adc r3 r0 0 brs endif else sbc r2 r2 1 lcw r2 mult cal r2 r2 pop r2 loa r2 r1 2 add r3 r3 r2 endif pop r0 jmp r2 r2 loc: cal0 r3 12 sp (r1) 2 Stack Returning with answer in r3

Calling mult r2 loc: cal0 r3 12 sp (r1) 2 Stack loa r3 r0 loa r2 r0 psh r2 lcw r2 mult cal r2 r2 pop r2 sto r0 r3 hlt sp (r1) 2 Stack

Calling mult r2 2 r3 12 sp (r1) Stack loa r3 r0 loa r2 r0 psh r2 lcw r2 mult cal r2 r2 pop r2 sto r0 r3 hlt sp (r1) Stack

Calling mult r2 2 r3 12 sp (r1) Stack loa r3 r0 loa r2 r0 psh r2 lcw r2 mult cal r2 r2 pop r2 sto r0 r3 hlt sp (r1) Stack

Calling mult r2 2 r3 12 Print the answer: 12! sp (r1) Stack loa r3 r0 psh r2 lcw r2 mult cal r2 r2 pop r2 sto r0 r3 hlt Print the answer: 12! sp (r1) Stack

Calling mult r2 2 r3 12 Print the answer: 12! sp (r1) Stack loa r3 r0 psh r2 lcw r2 mult cal r2 r2 pop r2 sto r0 r3 hlt Print the answer: 12! sp (r1) Stack

Run mult.a41 in simulator

CS41B programming advice Match your push and pops Follow the register conventions Develop code incrementally Debugging: write out stack, registers, etc. on paper and compare against system execution

Examples from this lecture http://www.cs.pomona.edu/~dkauchak/classes/cs52/examples/cs41b/