CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

The University of Adelaide, School of Computer Science
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
10/6: Lecture Topics Procedure call Calling conventions The stack
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Procedure Calls Prof. Sirer CS 316 Cornell University.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
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.
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
Procedure call frame: Hold values passed to a procedure as arguments
MIPS Assembly Language
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
Faculty of Computer Science © 2006 CMPUT 229 Subroutines - Part 2 Calling Itself.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Intro to Computer Architecture
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
CHAPTER 2 ISA Instructions (logical + procedure call)
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
CS224 Fall 2011 Chapter 2b Computer Organization CS224 Fall 2011 Chapter 2 b With thanks to M.J. Irwin, D. Patterson, and J. Hennessy for some lecture.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
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.
MIPS Calling Convention. Procedure Calls Procedure must work the same from any call Procedure uses regs that main was using We need a convention to –pass.
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Lecture 4: MIPS Instruction Set
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic5: Linking José Nelson Amaral.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 8: MIPS Procedures and Recursion.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
Lec 6Systems Architecture1 Systems Architecture Lecture 6: Branching and Procedures in MIPS Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
Lecture 5: Procedure Calls
Lecture 6: Assembly Programs
Computer Architecture Instruction Set Architecture
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
What's wrong with this procedure?
Instructions - Type and Format
MIPS Instructions.
CSCI206 - Computer Organization & Programming
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Logical and Decision Operations
Review.
10/4: Lecture Topics Overflow and underflow Logical operations
Lecture 6: Assembly Programs
Systems Architecture I
Computer Architecture
Presentation transcript:

CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral

CMPUT Computer Organization and Architecture I2 Reading Assignment zChapter 3 of Hennessy and Patterson: ySections 3.6 to 3.8. yPages A-26 to A-29 of Appendix A

CMPUT Computer Organization and Architecture I3 Calling Itself int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); }

CMPUT Computer Organization and Architecture I4 Procedure Call 1Place parameters in a place where the procedure can access them. 2Transfer control to procedure. 3 Acquire the storage resources needed for the procedure. 4Perform the procedure’s task. 5Place the result value in a place where the calling program can access it. 6Return control to the point of origin. Pat.-Hen. pp. 132

CMPUT Computer Organization and Architecture I5 Registers Used for Procedure Calls $a0-$a3: four argument registers in which to pass parameters $v0-$v1: two value registers in which to return values $ra: one return address register to return to the point of origin Pat.-Hen. pp. 132

CMPUT Computer Organization and Architecture I6 Jumping and Returning The jump-and-link instruction (jal) is used to jump to a procedure and link the returning address. jalProcedureAddress stores the value of PC+4 in the register $ra and transfer the execution to the ProcedureAddress When the procedure completes execution, it will jump back to the calling point using: jr$ra Pat.-Hen. pp. 133

CMPUT Computer Organization and Architecture I7 A Procedure that Doesn’t Call Another Procedure int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; } Parameter Passing Convention g  $a0 h  $a1 i  $a2 j  $a3 Assumption f  $s0 MIPS assembly: sub $sp, $sp, 12 # Make room in stack for 3 more items sw $t1, 8($sp)# push $t1 into stack sw $t0, 4($sp)# push $t0 into stack sw $s0, 0($sp)# push $s0 into stack add $t0, $a0, $a1# $t0  g + h add $t1, $a2, $a3# $t1  i + j sub $s0, $t0, $t1# f  $t0 - $t1 add $v0, $s0, $zero# $v0  $s0 lw $s0, 0($sp)# restore $s0 lw $t0, 4($sp)# restore $t0 lw $t1, 8($sp)# restore $t1 add $sp, $sp, 12# adjust stack pointer Pat.-Hen. pp

CMPUT Computer Organization and Architecture I8 Example of lw execution cont($t0) cont($t1) $t0 $t1 g h i $a0 $a1 $a2 j $a3 cont($s0) $s0 $sp MIPS assembly: sub $sp, $sp, 12 # Make room in stack for 3 more items sw $t1, 8($sp)# push $t1 into stack sw $t0, 4($sp)# push $t0 into stack sw $s0, 0($sp)# push $s0 into stack add $t0, $a0, $a1# $t0  g + h add $t1, $a2, $a3# $t1  i + j sub $s0, $t0, $t1# f  $t0 - $t1 add $v0, $s0, $zero# $v0  $s0 lw $s0, 0($sp)# restore $s0 lw $t0, 4($sp)# restore $t0 lw $t1, 8($sp)# restore $t1 add $sp, $sp, 12# adjust stack pointer Memory  High Address  Low Address $v0 Pat.-Hen. pp int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; } Processor

CMPUT Computer Organization and Architecture I9 Example of lw execution cont($t0) cont($t1) $t0 $t1 g h i $a0 $a1 $a2 j $a3 cont($s0) $s0 MIPS assembly: sub $sp, $sp, 12 # Make room in stack for 3 more items sw $t1, 8($sp)# push $t1 into stack sw $t0, 4($sp)# push $t0 into stack sw $s0, 0($sp)# push $s0 into stack add $t0, $a0, $sa1# $t0  g + h add $t1, $a2, $a3# $t1  i + j sub $s0, $t0, $t1# f  $t0 - $t1 add $v0, $s0, $zero# $v0  $s0 lw $s0, 0($sp)# restore $s0 lw $t0, 4($sp)# restore $t0 lw $t1, 8($sp)# restore $t1 add $sp, $sp, 12# adjust stack pointer Memory $sp  High Address  Low Address $v0 Pat.-Hen. pp int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; } Processor

CMPUT Computer Organization and Architecture I10 Example of lw execution cont($t1) cont($t0) cont($t1) $t0 $t1 g h i $a0 $a1 $a2 j $a3 cont($s0) $s0 MIPS assembly: sub $sp, $sp, 12 # Make room in stack for 3 more items sw $t1, 8($sp)# push $t1 into stack sw $t0, 4($sp)# push $t0 into stack sw $s0, 0($sp)# push $s0 into stack add $t0, $a0, $a1# $t0  g + h add $t1, $a2, $a3# $t1  i + j sub $s0, $t0, $t1# f  $t0 - $t1 add $v0, $s0, $zero# $v0  $s0 lw $s0, 0($sp)# restore $s0 lw $t0, 4($sp)# restore $t0 lw $t1, 8($sp)# restore $t1 add $sp, $sp, 12# adjust stack pointer Memory $sp  High Address  Low Address $v0 Pat.-Hen. pp int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; } Processor

CMPUT Computer Organization and Architecture I11 Example of lw execution cont($t1) cont($t0) cont($t1) $t0 $t1 g h i $a0 $a1 $a2 j $a3 cont($s0) $s0 MIPS assembly: sub $sp, $sp, 12 # Make room in stack for 3 more items sw $t1, 8($sp)# push $t1 into stack sw $t0, 4($sp)# push $t0 into stack sw $s0, 0($sp)# push $s0 into stack add $t0, $a0, $a1# $t0  g + h add $t1, $a2, $a3# $t1  i + j sub $s0, $t0, $t1# f  $t0 - $t1 add $v0, $s0, $zero# $v0  $s0 lw $s0, 0($sp)# restore $s0 lw $t0, 4($sp)# restore $t0 lw $t1, 8($sp)# restore $t1 add $sp, $sp, 12# adjust stack pointer Memory $sp  High Address  Low Address $v0 Pat.-Hen. pp int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; } Processor

CMPUT Computer Organization and Architecture I12 Example of lw execution cont($t1) cont($t0) cont($s0) cont($t0) cont($t1) $t0 $t1 g h i $a0 $a1 $a2 j $a3 cont($s0) $s0 MIPS assembly: sub $sp, $sp, 12 # Make room in stack for 3 more items sw $t1, 8($sp)# push $t1 into stack sw $t0, 4($sp)# push $t0 into stack sw $s0, 0($sp)# push $s0 into stack add $t0, $a0, $a1# $t0  g + h add $t1, $a2, $a3# $t1  i + j sub $s0, $t0, $t1# f  $t0 - $t1 add $v0, $s0, $zero# $v0  $s0 lw $s0, 0($sp)# restore $s0 lw $t0, 4($sp)# restore $t0 lw $t1, 8($sp)# restore $t1 add $sp, $sp, 12# adjust stack pointer Memory $sp  High Address  Low Address $v0 Pat.-Hen. pp int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; } Processor

CMPUT Computer Organization and Architecture I13 Example of lw execution cont($t1) cont($t0) cont($s0) g+h cont($t1) $t0 $t1 g h i $a0 $a1 $a2 j $a3 cont($s0) $s0 MIPS assembly: sub $sp, $sp, 12 # Make room in stack for 3 more items sw $t1, 8($sp)# push $t1 into stack sw $t0, 4($sp)# push $t0 into stack sw $s0, 0($sp)# push $s0 into stack add $t0, $a0, $a1# $t0  g + h add $t1, $a2, $a3# $t1  i + j sub $s0, $t0, $t1# f  $t0 - $t1 add $v0, $s0, $zero# $v0  $s0 lw $s0, 0($sp)# restore $s0 lw $t0, 4($sp)# restore $t0 lw $t1, 8($sp)# restore $t1 add $sp, $sp, 12# adjust stack pointer Memory $sp  High Address  Low Address $v0 Pat.-Hen. pp int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; } Processor

CMPUT Computer Organization and Architecture I14 Example of lw execution cont($t1) cont($t0) cont($s0) g+h i+j $t0 $t1 g h i $a0 $a1 $a2 j $a3 cont($s0) $s0 MIPS assembly: sub $sp, $sp, 12 # Make room in stack for 3 more items sw $t1, 8($sp)# push $t1 into stack sw $t0, 4($sp)# push $t0 into stack sw $s0, 0($sp)# push $s0 into stack add $t0, $a0, $a1# $t0  g + h add $t1, $a2, $a3# $t1  i + j sub $s0, $t0, $t1# f  $t0 - $t1 add $v0, $s0, $zero# $v0  $s0 lw $s0, 0($sp)# restore $s0 lw $t0, 4($sp)# restore $t0 lw $t1, 8($sp)# restore $t1 add $sp, $sp, 12# adjust stack pointer Memory $sp  High Address  Low Address $v0 Pat.-Hen. pp int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; } Processor

CMPUT Computer Organization and Architecture I15 Example of lw execution cont($t1) cont($t0) cont($s0) g+h i+j $t0 $t1 g h i $a0 $a1 $a2 j $a3 $t0-$t1 $s0 MIPS assembly: sub $sp, $sp, 12 # Make room in stack for 3 more items sw $t1, 8($sp)# push $t1 into stack sw $t0, 4($sp)# push $t0 into stack sw $s0, 0($sp)# push $s0 into stack add $t0, $a0, $a1# $t0  g + h add $t1, $a2, $a3# $t1  i + j sub $s0, $t0, $t1# f  $t0 - $t1 add $v0, $s0, $zero# $v0  $s0 lw $s0, 0($sp)# restore $s0 lw $t0, 4($sp)# restore $t0 lw $t1, 8($sp)# restore $t1 add $sp, $sp, 12# adjust stack pointer Memory $sp  High Address  Low Address $v0 Pat.-Hen. pp int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; } Processor

CMPUT Computer Organization and Architecture I16 Example of lw execution cont($t1) cont($t0) cont($s0) g+h i+j $t0 $t1 g h i $a0 $a1 $a2 j $a3 $t0-$t1 $s0 MIPS assembly: sub $sp, $sp, 12 # Make room in stack for 3 more items sw $t1, 8($sp)# push $t1 into stack sw $t0, 4($sp)# push $t0 into stack sw $s0, 0($sp)# push $s0 into stack add $t0, $a0, $a1# $t0  g + h add $t1, $a2, $a3# $t1  i + j sub $s0, $t0, $t1# f  $t0 - $t1 add $v0, $s0, $zero# $v0  $s0 lw $s0, 0($sp)# restore $s0 lw $t0, 4($sp)# restore $t0 lw $t1, 8($sp)# restore $t1 add $sp, $sp, 12# adjust stack pointer Memory $sp  High Address  Low Address $t0-$t1 $v0 Pat.-Hen. pp int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; } Processor

CMPUT Computer Organization and Architecture I17 Example of lw execution cont($t1) cont($t0) cont($s0) g+h i+j $t0 $t1 g h i $a0 $a1 $a2 j $a3 cont($s0) $s0 MIPS assembly: sub $sp, $sp, 12 # Make room in stack for 3 more items sw $t1, 8($sp)# push $t1 into stack sw $t0, 4($sp)# push $t0 into stack sw $s0, 0($sp)# push $s0 into stack add $t0, $a0, $a1# $t0  g + h add $t1, $a2, $a3# $t1  i + j sub $s0, $t0, $t1# f  $t0 - $t1 add $v0, $s0, $zero# $v0  $s0 lw $s0, 0($sp)# restore $s0 lw $t0, 4($sp)# restore $t0 lw $t1, 8($sp)# restore $t1 add $sp, $sp, 12# adjust stack pointer Memory $sp  High Address  Low Address $t0-$t1 $v0 Pat.-Hen. pp int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; } Processor

CMPUT Computer Organization and Architecture I18 Example of lw execution cont($t1) cont($t0) cont($s0) cont($t0) i+j $t0 $t1 g h i $a0 $a1 $a2 j $a3 cont($s0) $s0 MIPS assembly: sub $sp, $sp, 12 # Make room in stack for 3 more items sw $t1, 8($sp)# push $t1 into stack sw $t0, 4($sp)# push $t0 into stack sw $s0, 0($sp)# push $s0 into stack add $t0, $a0, $a1# $t0  g + h add $t1, $a2, $a3# $t1  i + j sub $s0, $t0, $t1# f  $t0 - $t1 add $v0, $s0, $zero# $v0  $s0 lw $s0, 0($sp)# restore $s0 lw $t0, 4($sp)# restore $t0 lw $t1, 8($sp)# restore $t1 add $sp, $sp, 12# adjust stack pointer Memory $sp  High Address  Low Address $t0-$t1 $v0 Pat.-Hen. pp int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; } Processor

CMPUT Computer Organization and Architecture I19 Example of lw execution cont($t1) cont($t0) cont($s0) cont($t0) cont($t1) $t0 $t1 g h i $a0 $a1 $a2 j $a3 cont($s0) $s0 MIPS assembly: sub $sp, $sp, 12 # Make room in stack for 3 more items sw $t1, 8($sp)# push $t1 into stack sw $t0, 4($sp)# push $t0 into stack sw $s0, 0($sp)# push $s0 into stack add $t0, $a0, $a1# $t0  g + h add $t1, $a2, $a3# $t1  i + j sub $s0, $t0, $t1# f  $t0 - $t1 add $v0, $s0, $zero# $v0  $s0 lw $s0, 0($sp)# restore $s0 lw $t0, 4($sp)# restore $t0 lw $t1, 8($sp)# restore $t1 add $sp, $sp, 12# adjust stack pointer Memory $sp  High Address  Low Address $t0-$t1 $v0 Pat.-Hen. pp int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; } Processor

CMPUT Computer Organization and Architecture I20 Example of lw execution cont($t1) cont($t0) cont($s0) cont($t0) cont($t1) $t0 $t1 g h i $a0 $a1 $a2 j $a3 cont($s0) $s0 MIPS assembly: sub $sp, $sp, 12 # Make room in stack for 3 more items sw $t1, 8($sp)# push $t1 into stack sw $t0, 4($sp)# push $t0 into stack sw $s0, 0($sp)# push $s0 into stack add $t0, $a0, $a1# $t0  g + h add $t1, $a2, $a3# $t1  i + j sub $s0, $t0, $t1# f  $t0 - $t1 add $v0, $s0, $zero# $v0  $s0 lw $s0, 0($sp)# restore $s0 lw $t0, 4($sp)# restore $t0 lw $t1, 8($sp)# restore $t1 add $sp, $sp, 12# adjust stack pointer Memory Processor  High Address  Low Address $t0-$t1 $v0 $sp Pat.-Hen. pp int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; }

CMPUT Computer Organization and Architecture I21 Register Saving Convention To avoid saving and restoring registers that might not contain any useful value, MIPS adopts the following software convention: Registers $t0-$t9 are not preserved by the callee on a procedure call. Registers $s0-$s7 must be preserved on a procedure call. We also say that registers $t0-$t9 are caller saved, and that registers $s0-s7 are callee saved. These names indicates which procedure (the caller or the callee) is responsible for preserving the value stored in the register at the procedure boundary.

CMPUT Computer Organization and Architecture I22 Linking a Recursive Procedure int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller

CMPUT Computer Organization and Architecture I23 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller $t0 $v0 3 $a0 Processor 0x $sp $ra $sp Memory  High Address  Low Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x …. Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I24 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller $t0 $v0 3 $a0 Processor 0x $sp 0x $ra $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I25 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller $t0 $v0 3 $a0 Processor 0x1000 1FF8 $sp 0x $ra $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I26 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller $t0 $v0 3 $a0 Processor 0x1000 1FF8 $sp 0x $ra 0x $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I27 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 $v0 3 $a0 Processor 0x1000 1FF8 $sp 0x $ra 0x $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I28 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 0 $t0 $v0 3 $a0 Processor 0x1000 1FF8 $sp 0x $ra 0x $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I29 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 0 $t0 $v0 3 $a0 Processor 0x1000 1FF8 $sp 0x $ra 0x $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I30 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 0 $t0 $v0 2 $a0 Processor 0x1000 1FF8 $sp 0x $ra 0x $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I31 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 0 $t0 $v0 2 $a0 Processor 0x1000 1FF8 $sp 0x1000 6FEC $ra 0x $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I32 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 0 $t0 $v0 2 $a0 Processor 0x1000 1FF0 $sp 0x1000 6FEC $ra 0x $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I33 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 0 $t0 $v0 2 $a0 Processor 0x1000 1FF0 $sp 0x1000 6FEC $ra 0x x1000 6FEC 2 $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I34 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 0 $t0 $v0 1 $a0 Processor 0x1000 1FF0 $sp 0x1000 6FEC $ra 0x x1000 6FEC 2 $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I35 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 0 $t0 $v0 1 $a0 Processor 0x1000 1FF0 $sp 0x1000 6FEC $ra 0x x1000 6FEC 2 $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I36 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 0 $t0 $v0 1 $a0 Processor 0x1000 1FE8 $sp 0x1000 6FEC $ra 0x x1000 6FEC 2 $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I37 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 0 $t0 $v0 1 $a0 Processor 0x1000 1FE8 $sp 0x1000 6FEC $ra 0x x1000 6FEC 2 1 $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I38 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 $v0 0 $a0 Processor 0x1000 1FE8 $sp 0x1000 6FEC $ra 0x x1000 6FEC 2 1 $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I39 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 $v0 0 $a0 Processor 0x1000 1FE8 $sp 0x1000 6FEC $ra 0x x1000 6FEC 2 1 $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I40 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 $v0 0 $a0 Processor 0x1000 1FE0 $sp 0x1000 6FEC $ra 0x x1000 6FEC 2 1 Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address 0x1000 6FEC 0 $sp Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I41 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 $v0 0 $a0 Processor 0x1000 1FE0 $sp 0x1000 6FEC $ra 0x x1000 6FEC 2 1 Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address 0x1000 6FEC 0 $sp Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I42 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 1 $v0 0 $a0 Processor 0x1000 1FE0 $sp 0x1000 6FEC $ra 0x x1000 6FEC 2 1 Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address 0x1000 6FEC 0 $sp Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I43 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 1 $v0 0 $a0 Processor 0x1000 1FE8 $sp 0x1000 6FEC $ra 0x x1000 6FEC 2 1 Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address 0x1000 6FEC 0 $sp Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I44 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 1 $v0 0 $a0 Processor 0x1000 1FE8 $sp 0x1000 6FEC $ra 0x x1000 6FEC 2 1 Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address 0x1000 6FEC 0 $sp Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I45 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 1 $v0 1 $a0 Processor 0x1000 1FE8 $sp 0x1000 6FEC $ra 0x x1000 6FEC 2 1 Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address 0x1000 6FEC 0 $sp Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I46 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 1 $v0 1 $a0 Processor 0x1000 1FE8 $sp 0x1000 6FEC $ra 0x x1000 6FEC 2 1 Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address 0x1000 6FEC 0 $sp Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I47 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 1 $v0 1 $a0 Processor 0x1000 1FF0 $sp 0x1000 6FEC $ra 0x x1000 6FEC 2 1 $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address 0x1000 6FEC 0 Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I48 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 1 $v0 1 $a0 Processor 0x1000 1FF0 $sp 0x1000 6FEC $ra 0x x1000 6FEC 2 1 $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address 0x1000 6FEC 0 Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I49 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 1 $v0 1 $a0 Processor 0x1000 1FF0 $sp 0x1000 6FEC $ra 0x x1000 6FEC 2 1 $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address 0x1000 6FEC 0 Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I50 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 1 $v0 2 $a0 Processor 0x1000 1FF0 $sp 0x1000 6FEC $ra 0x x1000 6FEC 2 1 $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address 0x1000 6FEC 0 Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I51 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 1 $v0 2 $a0 Processor 0x1000 1FF8 $sp 0x1000 6FEC $ra 0x x1000 6FEC 2 1 $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address 0x1000 6FEC 0 Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I52 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 2 $v0 2 $a0 Processor 0x1000 1FF8 $sp 0x1000 6FEC $ra 0x x1000 6FEC 2 1 $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address 0x1000 6FEC 0 Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I53 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 2 $v0 2 $a0 Processor 0x1000 1FF8 $sp 0x1000 6FEC $ra 0x x1000 6FEC 2 1 $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address 0x1000 6FEC 0 Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I54 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 2 $v0 3 $a0 Processor 0x1000 1FF8 $sp 0x1000 6FEC $ra 0x x1000 6FEC 2 1 $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address 0x1000 6FEC 0 Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I55 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 2 $v0 3 $a0 Processor 0x1000 1FF8 $sp 0x $ra 0x x1000 6FEC 2 1 $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address 0x1000 6FEC 0 Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I56 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 2 $v0 3 $a0 Processor 0x $sp 0x $ra 0x x1000 6FEC 2 1 $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address 0x1000 6FEC 0 Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I57 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 6 $v0 3 $a0 Processor 0x $sp 0x $ra 0x x1000 6FEC 2 1 $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address 0x1000 6FEC 0 Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I58 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 6 $v0 3 $a0 Processor 0x $sp 0x $ra 0x x1000 6FEC 2 1 $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address 0x1000 6FEC 0 Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I59 Example fact(3) MIPS assembly: fact: sub $sp, $sp, 8 # Make room in stack for 2 more items sw $ra, 4($sp)# save the return address sw $a0, 0($sp)# save the argument n slt $t0, $a0, 1# if ($a0<1) then $t0  1 else $t0  0 beq $t0, $zero, L1# if n  1, go to L1 add $v0, $zero, 1# return 1 add $sp, $sp, 8# pop two items from the stack jr $ra# return to the instruction after jal L1: sub $a0, $a0, 1# subtract 1 from argument jal fact:# call fact(n-1) lw $a0, 0($sp)# just returned from jal: restore n lw $ra, 4($sp)# restore the return address add $sp, $sp, 8# pop two items from the stack mul $v0, $a0, $v0# return n*fact(n-1) jr $ra# return to the caller 1 $t0 6 $v0 3 $a0 Processor 0x $sp 0x $ra 0x x1000 6FEC 2 1 $sp Memory  High Address 0x1000 3FFBadd$a0,$zero,3 0x jalfact 0x ….  Low Address 0x1000 6FEC 0 Pat.-Hen. pp and A-26/A-29

CMPUT Computer Organization and Architecture I60 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

CMPUT Computer Organization and Architecture I61 ASCII Code Patt.-Hen., pp 142

CMPUT Computer Organization and Architecture I62 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  $s0 MIPS assembly: strcpy subi $sp, $sp, 4 # Room in stack for 1 more item sw $s0, 0($sp)# save $s0 into stack add $s0, $zero, $zero# i  0 L1:add $t1, $a1, $s0# $t1  address of y[i] lb $t2, 0($t1)# $t2  y[i] add $t3, $a0, $s0# $t3  address of x[i] sb $t2, 0($t3)# x[i]  y[i] beq $t2, $zero, L2# if y[i] = 0, goto L2 add $s0, $s0, 1# i  i + 1 j L1# go to L1 L2:lw $s0, 0($sp)# restore $s0 addi $sp, $sp, 4# pop one word off stack jr $ra# return Patt.-Hen., pp 143

CMPUT Computer Organization and Architecture I63 A Procedure that Doesn’t Call Another Procedure Parameter Passing Convention base of array x[ ]  $a0 base of array y[ ]  $a1 Assumption i  $s0 MIPS assembly: strcpy subi $sp, $sp, 4 # Room in stack for 1 more item sw $s0, 0($sp)# save $s0 into stack add $s0, $zero, $zero# i  0 L1:add $t1, $a1, $s0# $t1  address of y[i] lb $t2, 0($t1)# $t2  y[i] add $t3, $a0, $s0# $t3  address of x[i] sb $t2, 0($t3)# x[i]  y[i] beq $t2, $zero, L2# if y[i] = 0, goto L2 add $s0, $s0, 1# i  i + 1 j L1# go to L1 L2:lw $s0, 0($sp)# restore $s0 addi $sp, $sp, 4# pop one word off stack jr $ra# return save $s0 in stack void strcpy ( char x[ ], char y[ ]) { int i; i = 0; while ((x[i] = y[i]) != 0) i = i + 1; } Patt.-Hen., pp 143

CMPUT Computer Organization and Architecture I64 A Procedure that Doesn’t Call Another Procedure Parameter Passing Convention base of array x[ ]  $a0 base of array y[ ]  $a1 Assumption i  $s0 MIPS assembly: strcpy subi $sp, $sp, 4 # Room in stack for 1 more item sw $s0, 0($sp)# save $s0 into stack add $s0, $zero, $zero# i  0 L1:add $t1, $a1, $s0# $t1  address of y[i] lb $t2, 0($t1)# $t2  y[i] add $t3, $a0, $s0# $t3  address of x[i] sb $t2, 0($t3)# x[i]  y[i] beq $t2, $zero, L2# if y[i] = 0, goto L2 add $s0, $s0, 1# i  i + 1 j L1# go to L1 L2:lw $s0, 0($sp)# restore $s0 addi $sp, $sp, 4# pop one word off stack jr $ra# return 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; } Patt.-Hen., pp 143

CMPUT Computer Organization and Architecture I65 A Procedure that Doesn’t Call Another Procedure Parameter Passing Convention base of array x[ ]  $a0 base of array y[ ]  $a1 Assumption i  $s0 MIPS assembly: strcpy subi $sp, $sp, 4 # Room in stack for 1 more item sw $s0, 0($sp)# save $s0 into stack add $s0, $zero, $zero# i  0 L1:add $t1, $a1, $s0# $t1  address of y[i] lb $t2, 0($t1)# $t2  y[i] add $t3, $a0, $s0# $t3  address of x[i] sb $t2, 0($t3)# x[i]  y[i] beq $t2, $zero, L2# if y[i] = 0, goto L2 add $s0, $s0, 1# i  i + 1 j L1# go to L1 L2:lw $s0, 0($sp)# restore $s0 addi $sp, $sp, 4# pop one word off stack jr $ra# return 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; } Patt.-Hen., pp 143

CMPUT Computer Organization and Architecture I66 A Procedure that Doesn’t Call Another Procedure Parameter Passing Convention base of array x[ ]  $a0 base of array y[ ]  $a1 Assumption i  $s0 MIPS assembly: strcpy subi $sp, $sp, 4 # Room in stack for 1 more item sw $s0, 0($sp)# save $s0 into stack add $s0, $zero, $zero# i  0 L1:add $t1, $a1, $s0# $t1  address of y[i] lb $t2, 0($t1)# $t2  y[i] add $t3, $a0, $s0# $t3  address of x[i] sb $t2, 0($t3)# x[i]  y[i] beq $t2, $zero, L2# if y[i] = 0, goto L2 add $s0, $s0, 1# i  i + 1 j L1# go to L1 L2:lw $s0, 0($sp)# restore $s0 addi $sp, $sp, 4# pop one word off stack jr $ra# return 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; } Patt.-Hen., pp 143

CMPUT Computer Organization and Architecture I67 A Procedure that Doesn’t Call Another Procedure Parameter Passing Convention base of array x[ ]  $a0 base of array y[ ]  $a1 Assumption i  $s0 MIPS assembly: strcpy subi $sp, $sp, 4 # Room in stack for 1 more item sw $s0, 0($sp)# save $s0 into stack add $s0, $zero, $zero# i  0 L1:add $t1, $a1, $s0# $t1  address of y[i] lb $t2, 0($t1)# $t2  y[i] add $t3, $a0, $s0# $t3  address of x[i] sb $t2, 0($t3)# x[i]  y[i] beq $t2, $zero, L2# if y[i] = 0, goto L2 add $s0, $s0, 1# i  i + 1 j L1# go to L1 L2:lw $s0, 0($sp)# restore $s0 addi $sp, $sp, 4# pop one word off stack jr $ra# return 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; } Patt.-Hen., pp 143

CMPUT Computer Organization and Architecture I68 A Procedure that Doesn’t Call Another Procedure Parameter Passing Convention base of array x[ ]  $a0 base of array y[ ]  $a1 Assumption i  $s0 MIPS assembly: strcpy subi $sp, $sp, 4 # Room in stack for 1 more item sw $s0, 0($sp)# save $s0 into stack add $s0, $zero, $zero# i  0 L1:add $t1, $a1, $s0# $t1  address of y[i] lb $t2, 0($t1)# $t2  y[i] add $t3, $a0, $s0# $t3  address of x[i] sb $t2, 0($t3)# x[i]  y[i] beq $t2, $zero, L2# if y[i] = 0, goto L2 add $s0, $s0, 1# i  i + 1 j L1# go to L1 L2:lw $s0, 0($sp)# restore $s0 addi $sp, $sp, 4# pop one word off stack jr $ra# return 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; } Patt.-Hen., pp 143

CMPUT Computer Organization and Architecture I69 Immediate Operands Often times we need to add a known constant to a register addi$sp, $sp, 4# $sp  $sp OpCodersrtimmediate R29 = $sp Patt.-Hen., pp 145

CMPUT Computer Organization and Architecture I70 Comparing with Non-Zero Constants We also may need to compare the value stored in a register with a non-zero constant. For this we use the set-less-than immediate instruction: slti$t0, $s2, 10# if $s2 < 10 then $t0  1 else $t0  0 Patt.-Hen., pp 146

CMPUT Computer Organization and Architecture I71 Operating with Bigger constants addi$t0, $zero, # $t0  0 + If we want to load a constant into register $t0, we can use the following addi instruction Which is represented by the following binary representation 508 OpCodersrtimmediate sccc cccc cccc cccc What do we do when we want to load a constant whose value needs more than 16 bits to be represented? The constant will be sign extend, thus after this instruction, $t0 will contain ssss ssss ssss ssss sccc cccc cccc cccc Patt.-Hen., pp 147

CMPUT Computer Organization and Architecture I72 Load Upper Immediate For this situation MIPS has the load upper immediate instruction that loads a 16 bit constant into the upper half word of a register: lui$t0, 255# ($t0 is register R8) Which is represented by the following binary representation OpCodersrtimmediate After this lui instruction, the register $t0 constains: upper halflower half Patt.-Hen., pp 147

CMPUT Computer Organization and Architecture I73 Example Write two MIPS instruction that will load the constant 64  = = into register $t0.

CMPUT Computer Organization and Architecture I74 Pseudo-Instruction A pseudo-instruction is an instruction that are accepted by the MIPS assembler, but that does not exist in the MIPS architecture. Example: The MIPS architecture does not have an instruction to move the content of register $t1 into register $t0. But the MIPS assembler accepts the following instruction: move$t0, $t1# $t0  $t1 Whenever the MIPS assembler encounters this instruction in an assembly program, it automatically transforms it into: add$t0, $zero, $t1# $t0  $t1 Patt.-Hen., pp 157

CMPUT Computer Organization and Architecture I75 Pseudo-Instruction Other examples include the bgt, bge, and ble branch instructions. For instance the instruction: blt$s5, $t2, Exit Is converted into the following instructions: slt$at, $s5, $t2 bne$at, $zero, Exit Where $at is an assembler temporary register reserved for the use of the assembler for conversion of pseudo-instructions into real instructions.

Patt-Hen., pp. 152