Faculty of Computer Science © 2006 CMPUT 229 Subroutines - Part 2 Calling Itself.

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Stack Frames: Using LINK. CEG 320/5208: Stack frames and LINK2 Assembling source code One source file: –Use ORG statements for data and code –Assemble.
The University of Adelaide, School of Computer Science
MIPS Function Continued
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
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
Faculty of Computer Science © 2006 CMPUT 229 Why Computer Architecture? An Introduction to CMPUT 229.
Procedure call frame: Hold values passed to a procedure as arguments
EECC250 - Shaaban #1 Lec # 6 Winter Stack-Related Instructions PEA Push Effective Address Calculates an effective address and pushes it.
Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays Differentiating Pointers from Data.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Intro to Computer Architecture
Faculty of Computer Science © 2006 CMPUT 229 Pointers and Arrays (part 2) Differentiating Pointers from Data.
Faculty of Computer Science © 2006 CMPUT 229 Why Computer Architecture? An Introduction to CMPUT 229.
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
EECC250 - Shaaban #1 lec #7 Winter Local workspace of a subroutine: A number of temporary memory locations required by the subroutine for temporary.
Faculty of Computer Science © 2006 CMPUT 229 Subroutines (Part 1) The 68K Stack.
EECC250 - Shaaban #1 lec #8 Winter Recursive Subroutine Calls Example The purpose of this example is to examine how all parameters, local variables,
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
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.
7/23 C Programming in Embedded Systems Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee
Program Compilation and Execution. Today’s Objectives Explain why runtime stack needed for C Explain why runtime stack needed for C Draw logical division.
Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Character Data and 32-bit Constants (Lecture #20) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Passing Parameters using Stack Calling program pushes parameters on the stack one element at a time before calling subroutine. Subroutine Call (jsr, bsr)
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
Functions in Assembly CS 210 Tutorial 7 Functions in Assembly Studwww.cs.auckland.ac.nz/ ~mngu012/public.html/210/7/
1 ECE 372 – Microcontroller Design Assembly Programming HCS12 Assembly Programming Addressing Modes Stack Operations Subroutines.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic5: Linking José Nelson Amaral.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /22/2013 Lecture 12: Character Data Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
C Calling Conventions parameters are passed on the run-time or system stack, SP (or A7) parameters pushed on stack in “right to left” order of call A6.
Computer Architecture & Operations I
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
Computer structure: Procedure Calls
Lecture 5: Procedure Calls
Computer Architecture & Operations I
CSCI206 - Computer Organization & Programming
Subroutines … passing data
Procedures (Functions)
Procedures (Functions)
In this lecture Global variables Local variables System stack
CSCI206 - Computer Organization & Programming
What's wrong with this procedure?
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
The University of Adelaide, School of Computer Science
Reentrant Code a reentrant procedure can have several calls open to it at the same time in an interrupt environment, different ISRs may call the same routine.
10/4: Lecture Topics Overflow and underflow Logical operations
Lecture 6: Assembly Programs
Procedures and Calling Conventions
Systems Architecture I
CSC 497/583 Advanced Topics in Computer Security
Runtime Stack Activation record for hanoi;
Presentation transcript:

Faculty of Computer Science © 2006 CMPUT 229 Subroutines - Part 2 Calling Itself

© 2006 Department of Computing Science CMPUT 229 Calling Itself int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); }

© 2006 Department of Computing Science CMPUT 229 Linking a Recursive Procedure int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC D0 A6 SP

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ Memory SP$8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC D0 $ A6 $ SP $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC D0 $ A6 $ C SP A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC D0 $ A6 $ C SP A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 3 D0 $ A6 $ C SP A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ C SP A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ C SP A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ SP A6 Assuming that call to subroutine fact is at address $100024FC $ $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ SP A6 $ $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ SP A6 $ M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ SP A6 $ $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $ A6 $ SP A6 $ $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS 1 $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $ A6 $ SP A6 $ $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $ A6 $00007FFC SP A6 $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF4 SP A6 $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF4 SP A6 $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF4 SP A6 $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 0 D0 $00007FF8 A6 $00007FF4 SP A6 $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 0 D0 $00007FF8 A6 $00007FF4 SP A6 $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6)Compare with Zero BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 0 D0 $00007FF8 A6 $00007FF0 SP A6 $ $ $7FF8 $7FF4 $7FF0 $ $7FEC $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 0 D0 $00007FEC A6 $00007FE8 SP A6 $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 0 D0 $00007FEC A6 $00007FE8 SP A6 $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FEC A6 $00007FE8 SP A6 $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FEC A6 $00007FE8 SP A6 $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FEC A6 $00007FE8 SP A6 $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF0 SP A6 $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADDQ.L #4,SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF4 SP A6 $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory SP/A6 $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF8 SP $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF0 SP $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 SP/A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $00007FF8 A6 $00007FF0 SP $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 SP/A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $ A6 $00007FFC SP A6 $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory SP $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 1 D0 $ A6 $ SP A6 $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ SP SP/A6 $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ SP SP/A6 $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 SP/A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 SP/A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 SP/A6 $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 SP $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 SP $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $00007FF8 $7FEC 1 $7FE8 SP $1000 3FFBMOVEQ#3,(SP) $1000 3FFCBSRfact $ ….

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,D0 $1000 3FFCBSRfact $ …. $00007FF8 $7FEC 1 $7FE8 A6

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 2 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,D0 $1000 3FFCBSRfact $ …. $00007FF8 $7FEC 1 $7FE8

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,D0 $1000 3FFCBSRfact $ …. $00007FF8 $7FEC 1 $7FE8 SP/A6

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,D0 $1000 3FFCBSRfact $ …. $00007FF8 $7FEC 1 $7FE8 SP/A6

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,D0 $1000 3FFCBSRfact $ …. $00007FF8 $7FEC 1 $7FE8 SP

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,D0 $1000 3FFCBSRfact $ …. $00007FF8 $7FEC 1 $7FE8 SP

© 2006 Department of Computing Science CMPUT 229 Example: fact(3) int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } M68K assembly: fact:LINK A6,#-4 TST.L 8(A6) BGTL6 MOVEQ #1,D0 MOVE.L D0,-4(A6) BRA L5 L6: MOVE.L 8(A6),D0n  1 SUBQ.L #1,D0D0  n-1 MOVE.L D0,(SP) BSR fact ADD.L#4, SP MULS.L 8(A6),D0 MOVE.L D0,-4(A6) L5: MOVE.L -4(A6),D0 UNLK A6 RTS $ $ $ $ $ Memory $8014 $8018 $8010 $800C $8008 $8004 $8000 $7FFC 6 D0 $ A6 $ SP $ $ $7FF8 $7FF4 $7FF0 $ $1000 3FFBMOVEQ#3,D0 $1000 3FFCBSRfact $ …. $00007FF8 $7FEC 1 $7FE8 SP

© 2006 Department of Computing Science CMPUT 229 Other Data Stored in the Stack $sp  High Address  Low Address $fp Before procedure call $sp  High Address  Low Address $fp After procedure call $sp  High Address  Low Address $fp During procedure call Saved argument registers Saved return reg. Saved registers Local arrays and structures Patt.-Hen. pp 139

© 2006 Department of Computing Science CMPUT 229 ASCII Code Patt.-Hen., pp 142

© 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure void strcpy ( char x[ ], char y[ ]) { int i; i = 0; while ((x[i] = y[i]) != 0) i = i + 1; } Parameter Passing Convention base of array x[ ]  A0 base of array y[ ]  A1 Assumption i  D0 M68K assembly: strcpy ADD.L#4, SPRoom in stack for 1 more item MOVED0, (SP) Save D0 into stack MOVEQ#0, D0i  0 L1:MOVE.B(A1,D0), D4D4  y[i] MOVE.B(A0,D0), D5D5  y[i] TST D5if y[I] = 0 BEQL2done ADDQ#1, D0 i  i + 1 BRA L1Repeat L2:MOVE.L(SP), D0Restore D0 ADD.L#-4, SP pop one word off stack RTS return

© 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure save D0 in stack void strcpy ( char x[ ], char y[ ]) { int i; i = 0; while ((x[i] = y[i]) != 0) i = i + 1; } M68K assembly: strcpy ADD.L#4, SPRoom in stack for 1 more item MOVED0, (SP) Save D0 into stack MOVEQ#0, D0i  0 L1:MOVE.B(A1,D0), D4D4  y[i] MOVE.B(A0,D0), D5D5  y[i] TST D5if y[I] = 0 BEQL2done ADDQ#1, D0 i  i + 1 BRA L1Repeat L2:MOVE.L(SP), D0Restore D0 ADD.L#-4, SP pop one word off stack RTS return Parameter Passing Convention base of array x[ ]  A0 base of array y[ ]  A1 Assumption i  D0

© 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure save $s0 in stack i  0 void strcpy ( char x[ ], char y[ ]) { int i; i = 0; while ((x[i] = y[i]) != 0) i = i + 1; } M68K assembly: strcpy ADD.L#4, SPRoom in stack for 1 more item MOVED0, (SP) Save D0 into stack MOVEQ#0, D0i  0 L1:MOVE.B(A1,D0), D4D4  y[i] MOVE.B(A0,D0), D5D5  y[i] TST D5if y[I] = 0 BEQL2done ADDQ#1, D0 i  i + 1 BRA L1Repeat L2:MOVE.L(SP), D0Restore D0 ADD.L#-4, SP pop one word off stack RTS return Parameter Passing Convention base of array x[ ]  A0 base of array y[ ]  A1 Assumption i  D0

© 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure save $s0 in stack i  0 x[i]  y[i] void strcpy ( char x[ ], char y[ ]) { int i; i = 0; while ((x[i] = y[i]) != 0) i = i + 1; } M68K assembly: strcpy ADD.L#4, SPRoom in stack for 1 more item MOVED0, (SP) Save D0 into stack MOVEQ#0, D0i  0 L1:MOVE.B(A1,D0), D4D4  y[i] MOVE.BD4, (A0,D0)x[i]  y[i] TST D5if y[I] = 0 BEQL2done ADDQ#1, D0 i  i + 1 BRA L1Repeat L2:MOVE.L(SP), D0Restore D0 ADD.L#-4, SP pop one word off stack RTS return Parameter Passing Convention base of array x[ ]  A0 base of array y[ ]  A1 Assumption i  D0

© 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure save $s0 in stack y[i] = 0? i  0 x[i]  y[i] void strcpy ( char x[ ], char y[ ]) { int i; i = 0; while ((x[i] = y[i]) != 0) i = i + 1; } M68K assembly: strcpy ADD.L#4, SPRoom in stack for 1 more item MOVED0, (SP) Save D0 into stack MOVEQ#0, D0i  0 L1:MOVE.B(A1,D0), D4D4  y[i] MOVE.BD4, (A0,D0)x[i]  y[i] TST D5if y[I] = 0 BEQL2done ADDQ#1, D0 i  i + 1 BRA L1Repeat L2:MOVE.L(SP), D0Restore D0 ADD.L#-4, SP pop one word off stack RTS return Parameter Passing Convention base of array x[ ]  A0 base of array y[ ]  A1 Assumption i  D0

© 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure i  i + 1 save $s0 in stack no y[i] = 0? i  0 x[i]  y[i] void strcpy ( char x[ ], char y[ ]) { int i; i = 0; while ((x[i] = y[i]) != 0) i = i + 1; } M68K assembly: strcpy ADD.L#4, SPRoom in stack for 1 more item MOVED0, (SP) Save D0 into stack MOVEQ#0, D0i  0 L1:MOVE.B(A1,D0), D4D4  y[i] MOVE.BD4, (A0,D0)x[i]  y[i] TST D5if y[I] = 0 BEQL2done ADDQ#1, D0 i  i + 1 BRA L1Repeat L2:MOVE.L(SP), D0Restore D0 ADD.L#-4, SP pop one word off stack RTS return Parameter Passing Convention base of array x[ ]  A0 base of array y[ ]  A1 Assumption i  D0

© 2006 Department of Computing Science CMPUT 229 A Procedure that Doesn’t Call Another Procedure i  i + 1 save $s0 in stack no y[i] = 0? yes restore $s0 return i  0 x[i]  y[i] void strcpy ( char x[ ], char y[ ]) { int i; i = 0; while ((x[i] = y[i]) != 0) i = i + 1; } M68K assembly: strcpy ADD.L#4, SPRoom in stack for 1 more item MOVED0, (SP) Save D0 into stack MOVEQ#0, D0i  0 L1:MOVE.B(A1,D0), D4D4  y[i] MOVE.BD4, (A0,D0)x[i]  y[i] TST D5if y[I] = 0 BEQL2done ADDQ#1, D0 i  i + 1 BRA L1Repeat L2:MOVE.L(SP), D0Restore D0 ADD.L#-4, SP pop one word off stack RTS return Parameter Passing Convention base of array x[ ]  A0 base of array y[ ]  A1 Assumption i  D0