Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 8: MIPS Procedures and Recursion.

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
Lecture 9: MIPS Instruction Set
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
©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.
Chapter 2 — Instructions: Language of the Computer — 1 Branching Far Away If branch target is too far to encode with 16-bit offset, assembler rewrites.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
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.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
The University of Adelaide, School of Computer Science
Computer Architecture CSCE 350
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
CS3350B Computer Architecture Winter 2015 Lecture 4
Procedure call frame: Hold values passed to a procedure as arguments
Lecture 8: MIPS Instruction Set
Lec 9Systems Architecture1 Systems Architecture Lecture 9: Assemblers, Linkers, and Loaders Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some.
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
MIPS Assembly Language
MIPS Coding. Exercise – the bubble sort 5/8/2015week04-3.ppt2.
Assembly Language II CPSC 321 Andreas Klappenecker.
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
1 Lecture 5: MIPS Examples Today’s topics:  the compilation process  full example – sort in C Reminder: 2 nd assignment will be posted later today.
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/
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.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
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
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.
1 Lecture 6: Assembly Programs Today’s topics:  Large constants  The compilation process  A full example  Intro to the MARS simulator.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
1 Lecture 2: MIPS Instruction Set Today’s topic:  MIPS instructions Reminder: sign up for the mailing list cs3810 Reminder: set up your CADE accounts.
Function Calls in Assembly MIPS R3000 Language (extensive use of stack) Updated 7/11/2013.
Lecture 5: Procedure Calls
Computer Architecture & Operations I
Lecture 6: Assembly Programs
Lecture 7: MARS, Computer Arithmetic
The University of Adelaide, School of Computer Science
Lecture 4: MIPS Instruction Set
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Instructions - Type and Format
Addressing in Jumps jump j Label go to Label op address 2 address
MIPS Instructions.
The University of Adelaide, School of Computer Science
MIPS Functions.
Lecture 7: Examples, MARS, Arithmetic
Lecture 5: Procedure Calls
Lecture 6: Assembly Programs
Computer Architecture Procedure Calls
Computer Architecture
10/6: Lecture Topics C Brainteaser More on Procedure Call
Where is all the knowledge we lost with information? T. S. Eliot
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 8: MIPS Procedures and Recursion

ECE232: MIPS Procedures 2 Adapted from Computer Organization and Design, Patterson&Hennessy,UCB, Kundu,UMass Koren Recap  The jal instruction is used to jump to the procedure and save the current PC (+4) into the return address register  Arguments are passed in $a0-$a3; return values in $v0-$v1  Since the callee may over-write the caller’s registers, relevant values may have to be copied into memory  Each procedure may also require memory space for local variables  A stack is used to organize the memory needs for each procedure

ECE232: MIPS Procedures 3 Adapted from Computer Organization and Design, Patterson&Hennessy,UCB, Kundu,UMass Koren Stack Dynamic data (heap) Static data (globals) Text (instructions) Memory Organization  The space allocated on stack by a procedure is termed the activation record (includes saved values and data local to the procedure)  Frame pointer points to the start of the record and stack pointer points to the end  Variable addresses are specified relative to $fp as $sp may change during the execution of the procedure

ECE232: MIPS Procedures 4 Adapted from Computer Organization and Design, Patterson&Hennessy,UCB, Kundu,UMass Koren MIPS Assembly - Dealing with Characters  Instructions are also provided to deal with byte-sized and half-word quantities: lb (load-byte), sb, lh, sh  These data types are most useful when dealing with characters, pixel values, etc.  C employs ASCII formats to represent characters – each character is represented with 8 bits and a string ends in the null character (corresponding to the 8-bit number 0)

ECE232: MIPS Procedures 5 Adapted from Computer Organization and Design, Patterson&Hennessy,UCB, Kundu,UMass Koren ASCII - American Standard Code for Information Interchange

ECE232: MIPS Procedures 6 Adapted from Computer Organization and Design, Patterson&Hennessy,UCB, Kundu,UMass Koren Example – string copy Convert to assembly: void strcpy (char x[], char y[]) { int i; i=0; while ((x[i] = y[i]) != `\0’) i += 1; } strcpy: addi $sp, $sp, -4 sw $s0, 0($sp) add $s0, $zero, $zero L1: add $t1, $s0, $a1 lb $t2, 0($t1) add $t3, $s0, $a0 sb $t2, 0($t3) beq $t2, $zero, L2 addi $s0, $s0, 1 j L1 L2: lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra Base addresses for x and y in $a0 and $a1, respectively $s0 is used for i

ECE232: MIPS Procedures 7 Adapted from Computer Organization and Design, Patterson&Hennessy,UCB, Kundu,UMass Koren Running a Program C Program Assembly language program Object: machine language moduleObject: library routine (machine language) Executable: machine language program Memory Compiler Assembler Linker Loader x.c x.s x.o x.a, x.so a.out

ECE232: MIPS Procedures 8 Adapted from Computer Organization and Design, Patterson&Hennessy,UCB, Kundu,UMass Koren Role of Assembler  Convert pseudo-instructions into actual hardware instructions There are many instructions you can use in MIPS assembly language that don’t really exist! They are a convenience for the programmer (or compiler) – just a shorthand notation for specifying some operation(s). examples: “move”, “blt”, 32-bit immediate operands, etc. blt $s0,$s1,foo is really slt $at,$s0,$s1 bne $at,foo li# load immediate la# load address sgt, sle, sge # set if greater than, … bge, bgt, ble, blt # conditional branching  Convert assembly instructions into machine instructions a separate object file (x.o) is created for each Assembly file (x.s) compute the actual values for instruction labels maintain info on external references and debugging information

ECE232: MIPS Procedures 9 Adapted from Computer Organization and Design, Patterson&Hennessy,UCB, Kundu,UMass Koren Role of a Linker  Stitches different object files into a single executable  patch internal and external references determine addresses of data and instruction labels organize code and data modules in memory  Some libraries (DLLs) are dynamically linked – the executable points to dummy routines – these dummy routines call the dynamic linker-loader so they can update the executable to jump to the correct routine

ECE232: MIPS Procedures 10 Adapted from Computer Organization and Design, Patterson&Hennessy,UCB, Kundu,UMass Koren Allocate registers to program variables Produce code for the program body Preserve registers across procedure invocations void sort (int v[], int n) { int i, j; for (i=0; i<n; i+=1) { for (j=i-1; j>=0 && v[j] > v[j+1]; j-=1) { swap (v,j); } void swap (int v[], int k) { int temp; temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; } Another MIPS Example – Sort

ECE232: MIPS Procedures 11 Adapted from Computer Organization and Design, Patterson&Hennessy,UCB, Kundu,UMass Koren Register allocation: $a0 and $a1 for the two arguments, $t0 for the temp variable – no need for saves and restores as we’re not using $s0-$s7 and this is a leaf procedure (won’t need to re-use $a0 and $a1) swap: sll $t1, $a1, 2 add $t1, $a0, $t1 lw $t0, 0($t1) lw $t2, 4($t1) sw $t2, 0($t1) sw $t0, 4($t1) jr $ra The swap Procedure void swap (int v[], int k) { int temp; temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; }

ECE232: MIPS Procedures 12 Adapted from Computer Organization and Design, Patterson&Hennessy,UCB, Kundu,UMass Koren for (i=0; i<n; i+=1) { for (j=i-1; j>=0 && v[j] > v[j+1]; j-=1) { swap (v,j); } The sort Procedure  Register allocation: arguments v and n use $a0 and $a1, i and j use $s0 and $s1; must save $a0 and $a1 before calling the leaf procedure  The outer for loop looks like this: (note the use of pseudo- instructions) addi $s0, $zero, $zero # initialize the loop loopbody1: bge $s0, $a1, exit1 # will eventually use slt and beq … body of inner loop … addi $s0, $s0, 1 j loopbody1 exit1:

ECE232: MIPS Procedures 13 Adapted from Computer Organization and Design, Patterson&Hennessy,UCB, Kundu,UMass Koren move$s0, $zero# i  0 for1test: slt$t0, $s0, $a1# if(i<n) $t0  1 else $t0  0 beq$t0, $zero, exit1 addi$s0, $s0, 1# i  i+1 jfor1test# go back to test of outer loop exit1: The sort procedure: Condition Check addi$s1, $s0, -1# j  i-1 slti$t0, $s1, 0# if(j<0) $t0  1 else $t0  0 bne$t0, $zero, exit2 sll$t1, $s1, 2# $t1  4*j add$t2, $a0, $t1# $t2  = Addr(v[j]) lw$t3, 0($t2)# $t3  v[j] lw$t4, 4($t2)# $t4  v[j+1] slt$t0, $t4, $t3# if (v[j+1]<v[j]) $t0  1 beq$t0, $zero, exit2 add$s1, $s1, 1# j  j+1 jfor2test for2test: exit2: for (i=0 ; i < n ; i=i+1) for (j=i-1; j >= 0 && v[j] > v[j+1]; j=j+1) swap(v,,j);

ECE232: MIPS Procedures 14 Adapted from Computer Organization and Design, Patterson&Hennessy,UCB, Kundu,UMass Koren Saves and Restores  Since we repeatedly call “swap” with $a0 and $a1, we begin “sort” by copying its arguments into $s2 and $s3 – must update the rest of the code in “sort” to use $s2 and $s3 instead of $a0 and $a1  Must save $ra at the start of “sort” because it will get over- written when we call “swap”  Must also save $s0-$s3 so we don’t overwrite something that belongs to the procedure that called “sort”

ECE232: MIPS Procedures 15 Adapted from Computer Organization and Design, Patterson&Hennessy,UCB, Kundu,UMass Koren 9 lines of C code  35 lines of assembly Saves and Restores sort: addi $sp, $sp, -20 sw $ra, 16($sp) sw $s3, 12($sp) sw $s2, 8($sp) sw $s1, 4($sp) sw $s0, 0($sp) move $s2, $a0 move $s3, $a1 … move $a0, $s2 # the inner loop body starts here move $a1, $s1 jal swap … exit1: lw $s0, 0($sp) … addi $sp, $sp, 20 jr $ra

ECE232: MIPS Procedures 16 Adapted from Computer Organization and Design, Patterson&Hennessy,UCB, Kundu,UMass Koren Customs for function calls in MIPS Steps for the code that is calling a function: Caller Before Entry:  Step 1: Pass the arguments: The first four arguments (arg0-arg3) are passed in registers $a0-$a3 Remaining arguments are pushed onto the stack  Step 2: Save caller-saved registers Save registers $s0-$s7 if they contain values you need to remember  Step 3: Save $ra, $fp if necessary  Step 4: Execute a jal instruction Jump and Link will save your return address in the $ra register, so that you can return to where you started After Return:  Step 1: Restore $s0-$s7, $ra, $fp if saved

ECE232: MIPS Procedures 17 Adapted from Computer Organization and Design, Patterson&Hennessy,UCB, Kundu,UMass Koren Customs for function calls in MIPS Steps for the code that is called : Callee On Entry:  Step 1: Save callee-saved registers Save registers $s0-$s7 if they are used in the callee procedure Before Exit:  Step 1: Save return values in $v0-$v1 registers  Step 2: Restore $s0-$s7 registers, if they were saved  Step 3: Execute jr $ra

ECE232: MIPS Procedures 18 Adapted from Computer Organization and Design, Patterson&Hennessy,UCB, Kundu,UMass Koren Software Conventions for MIPS Registers RegisterNamesUsage by Software Convention $0$zeroHardwired to zero $1$atReserved by assembler $2 - $3$v0 - $v1Function return result registers $4 - $7$a0 - $a3Function passing argument value registers $8 - $15$t0 - $t7Temporary registers, caller saved $16 - $23$s0 - $s7Saved registers, callee saved $24 - $25$t8 - $t9Temporary registers, caller saved $26 - $27$k0 - $k1Reserved for OS kernel $28$gpGlobal pointer $29$spStack pointer $30$fpFrame pointer $31$raReturn address (pushed by call instruction) $hi High result register (remainder/div, high word/mult) $lo Low result register (quotient/div, low word/mult)

ECE232: MIPS Procedures 19 Adapted from Computer Organization and Design, Patterson&Hennessy,UCB, Kundu,UMass Koren Recursion  Recursion vs. Iteration - A recursive procedure calls itself  Some applications can be naturally expressed using recursion  From implementation viewpoint Very similar to any other procedure call Activation records are stored on the stack int factorial(int n) {if (n==0) return 1; return (n*factorial(n-1)); }

ECE232: MIPS Procedures 20 Adapted from Computer Organization and Design, Patterson&Hennessy,UCB, Kundu,UMass Koren Fibonacci Number  fib(0) = 0  fib(1) = 1  fib(n) = fib(n-1) + fib(n-2) int fib(int n) { if (n <=1) return n; return fib(n-1) + fib(n-2); }  Caller = Callee  How many arguments? On entry, which register holds value?  When fib(n-1) returns which register holds value?  Need to save the return value ($s0)?  How about $ra?  How about $a0? fib(n) fib(n-1)fib(n-2) fib(n-3)

ECE232: MIPS Procedures 21 Adapted from Computer Organization and Design, Patterson&Hennessy,UCB, Kundu,UMass Koren Example: Fibonacci fib: subi $sp,$sp,12 # save registers on stack sw $a0, 0($sp) # save $a0 = n sw $s0, 4($sp) # save $s0 sw $ra, 8($sp) # save return address $ra bgt $a0,1, gen # if n>1 then goto generic case move $v0,$a0 # output = input if n=0 or n=1 j exit # goto restore registers gen: subi $a0,$a0,1 # param = n-1 jal fib # compute fib(n-1) move $s0,$v0 # save fib(n-1) subi $a0,$a0,1 # set param to n-2 jal fib # and make recursive call add $v0, $v0, $s0 # $v0 = fib(n-2)+fib(n-1) exit: lw $a0, 0($sp) # restore registers from stack lw $s0, 4($sp) # lw $ra, 8($sp) # addi $sp, $sp, 12 # decrease the stack size jr $ra

ECE232: MIPS Procedures 22 Adapted from Computer Organization and Design, Patterson&Hennessy,UCB, Kundu,UMass Koren Example: Fibonacci fib: subi $sp,$sp,12 sw $a0, 0($sp) sw $s0, 4($sp) sw $ra, 8($sp) bgt $a0,1, gen move $v0,$a0 j exit gen: subi $a0,$a0,1 jal fib move $s0,$v0 subi $a0,$a0,1 jal fib add $v0, $v0, $s0 exit: lw $a0, 0($sp) lw $s0, 4($sp) lw $ra, 8($sp) addi $sp, $sp, 12 jr $ra fib(3) fib(2) fib(1) $ra $s0 3 $ra $s0 2 $ra $s0 1 $ra $s0 3 $ra $s0 2 $ra 1 1 fib(1)