Recursion in MIPS Computer Organization I 1 August 2009 ©2006-09 McQuain, Feng & Ribbens Leaf and Non-Leaf Procedures A leaf procedure is one that.

Slides:



Advertisements
Similar presentations
Procedures 2 Intructions Supporting Procedures Making Use of a Stack.
Advertisements

The University of Adelaide, School of Computer Science
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
10/6: Lecture Topics Procedure call Calling conventions The stack
Procedure Calls Prof. Sirer CS 316 Cornell University.
The University of Adelaide, School of Computer Science
Computer Organization CS224
Computer Architecture CSCE 350
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
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.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
MIPS Assembly Language
Intro to Computer Architecture
Procedures I Fall 2005 C functions main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);... } /* really dumb mult function */ int mult (int mcand,
Lecture 5: Procedures. Function call book-keeping in C main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);... } /* really dumb mult function */
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Higher Level languages have adopted a standard Referred to as C-style calling Uses the stack.
Functions and Procedures. Function or Procedure u A separate piece of code u Possibly separately compiled u Located at some address in the memory used.
MIPS function continued. Recursive functions So far, we have seen how to write – A simple function – A simple function that have to use the stack to save.
Procedures 2a MIPS code examples Making Use of a Stack.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Runtime Stack Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens MIPS Memory Organization In addition to memory for static.
MAL 3 - Procedures Lecture 13. MAL procedure call The use of procedures facilitates modular programming. Four steps to transfer to and return from a procedure:
Subroutines, parameters and the stack Bryan Duggan.
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
MIPS Subroutines Subroutine Call – jal subname Saves RA in $31 and jumps to subroutine entry label subname Subroutine Return – jr $31 Loads PC with return.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Computer Architecture & Operations I
CSCI206 - Computer Organization & Programming
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
MIPS Assembly Language Programming
Function Calls in MIPS To call a function: jal func
© Craig Zilles (adapted from slides by Howard Huang)
CSCI206 - Computer Organization & Programming
MIPS Procedures.
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
MIPS Procedures.
MIPS Instructions.
CSCI206 - Computer Organization & Programming
The University of Adelaide, School of Computer Science
MIPS Procedures.
MIPS function continued
Program and memory layout
Procedures and Calling Conventions
Systems Architecture I
MIPS function continued
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
© Craig Zilles (adapted from slides by Howard Huang)
Procedure Support From previous study of high-level languages, we know the basic issues: - declaration: header, body, local variables - call and return.
Basic Guarantees Right off the bat… what I'm going to describe is what almost certainly happens on contemporary machines, but there is absolutely nothing.
MIPS function continued
MIPS R3000 Subroutine Calls and Stack
Presentation transcript:

Recursion in MIPS Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Leaf and Non-Leaf Procedures A leaf procedure is one that doesn't all any other procedures. A non-leaf procedure is one that does call another procedure. Non-leaf procedures pose an additional, but simple, challenge; we make procedure calls by executing a jump-and-link instruction: jal procedure_0 # puts PC+4 into $ra for return But, if procedure_0 also makes a call, say jal procedure_1 # puts PC+4 into $ra for return then the original return address just got overwritten… the effect is fascinating…

Recursion in MIPS Computer Organization I 2 August 2009 © McQuain, Feng & Ribbens Preserving the Return Address Non-leaf procedures must back up the value of their return address before making a call to another procedure: addi $sp, $sp, -4 # make room on stack sw $ra, 0($sp) # save return address And they must restore the return address before they attempt to return: lw $ra, 0($sp) # retrieve return address addi $sp, $sp, 4 # pop it off the stack Failure to do this will almost certainly lead to a catastrophic runtime failure. The safest way to do this is to back up the address immediately when the procedure is entered, and to restore it immediately before the return is executed. Of course, you must keep careful track of the stack pointer during all of this…

Recursion in MIPS Computer Organization I 3 August 2009 © McQuain, Feng & Ribbens Factorial: First Version ################################################################### # Returns factorial of parameter. # # Pre: # $a0 stores N # Post: # $v0 stores N! # # Modifies: $t0, $t1, $v0, $a0 # fac1: li $t0, 1 # check for base case bgt $a0, $t0, recurse li $v0, 1 # if so, set $v0 jr $ra # and return recurse: move $t1, $a0 # save N addi $a0, $a0, -1 # calc N-1 for recursive call jal fac1 # calc (N-1)! mul $v0, $v0, $t1 # multiply that by N jr $ra # and return Unfortunately, fac1 falls into an infinite loop when it's called with any value larger than 1 for $a0.

Recursion in MIPS Computer Organization I 4 August 2009 © McQuain, Feng & Ribbens What went wrong? fac1: li $t0, 1 # check for base case bgt $a0, $t0, recurse li $v0, 1 # if so, set $v0 jr $ra # and return recurse: move $t1, $a0 # save N addi $a0, $a0, -1 # calc N-1 for recursive call jal fac1 # calc (N-1)! mul $v0, $v0, $t1 # multiply that by N jr $ra # and return Making the recursive call overwrites the original return address with the address of what? And the effect of that is….? And the moral of that is….? An infinite loop in a pgm with no loops. Back up $ra before a call in a non-leaf proc.

Recursion in MIPS Computer Organization I 5 August 2009 © McQuain, Feng & Ribbens Factorial: Second Version fac2: li $t0, 1 # check for base case bgt $a0, $t0, recurse li $v0, 1 # if so, set return value jr $ra # and return recurse: move $t1, $a0 # save N addi $a0, $a0, -1 # calc N-1 for recursive call addi $sp, $sp, -4 # save return address on stack sw $ra, ($sp) jal fac2 # calc (N-1)! mul $v0, $v0, $t1 # multiply that by N lw $ra, ($sp) # restore return address addi $sp, $sp, 4 jr $ra # and return Unfortunately, fac2 returns 32 when called with $a0 == 6.

Recursion in MIPS Computer Organization I 6 August 2009 © McQuain, Feng & Ribbens What went wrong? fac2:... recurse: move $t1, $a0 # save N addi $a0, $a0, -1 # calc N-1 for recursive call addi $sp, $sp, -4 # save return address on stack sw $ra, ($sp) jal fac2 # calc (N-1)! mul $v0, $v0, $t1 # multiply that by N lw $ra, ($sp) # restore return address addi $sp, $sp, 4 jr $ra # and return During the recursive call, the previous contents of $t1 and $a0 are overwritten. Moral: before making a call, back up your registers as necessary.

Recursion in MIPS Computer Organization I 7 August 2009 © McQuain, Feng & Ribbens What went wrong: Details fac2:... recurse: move $t1, $a0 addi $a0, $a0, -1 addi $sp, $sp, -4 sw $ra, ($sp) jal fac2 mul $v0, $v0, $t1 lw $ra, ($sp) addi $sp, $sp, 4 jr $ra Let's say we call this with $a0 set to 3: Moral: before making a call, back up your registers as necessary. In the first (nonrecursive) call, fac2 : -puts 3 into $t1 -sets $a0 to 2 In the second call, fac2: -puts 2 into $t1 -sets $a0 to 1 In the third call, fac2: -puts 1 into $v0 -returns fac2: -mult $t1 and $v0 -returns 2 fac2: -mult $t1 and $v0 -returns 4

Recursion in MIPS Computer Organization I 8 August 2009 © McQuain, Feng & Ribbens Factorial: Stack Organization | | old $sp | | 0x7FFFEFFC | saved return address | new $sp | saved N | 0x7FFFEFF In order to fix the execution of the recursive factorial procedure, we need to use the stack to save values that would otherwise be overwritten when a recursive call takes place. Here's one idea for organizing the stack: on entry to proc after saving return address and N

Recursion in MIPS Computer Organization I 9 August 2009 © McQuain, Feng & Ribbens Factorial: Third Version fac3: li $t0, 1 # check for base case bgt $a0, $t0, recurse li $v0, 1 # if so, set return value jr $ra # and return recurse: addi $sp, $sp, -8 # make room on stack for sw $ra, 4($sp) # return address, and sw $a0, 0($sp) # N addi $a0, $a0, -1 # calc N-1 for recursive call jal fac3 # calc (N-1)! lw $t1, 0($sp) # restore N from stack mul $v0, $v0, $t1 # multiply (N-1)! by N lw $ra, 4($sp) # restore return address from # stack addi $sp, $sp, 8 # and restore stack pointer jr $ra # and return

Recursion in MIPS Computer Organization I 10 August 2009 © McQuain, Feng & Ribbens Factorial: Stack Trace | | old $sp | | 0x7FFFEFFC | saved $ra to main | $sp | saved 3 | 0x7FFFEFF Say we call factorial(3): on first call (not recursive) Third call triggers base case and returns with $v0 == 1 on second call (recursive) | | old $sp | | 0x7FFFEFFC | saved $ra to main | | saved 3 | 0x7FFFEFF | saved $ra to 1st call | new $sp | saved 2 | 0x7FFFEFEC Saved value of N (2) is retrieved from stack and multiplied to $v0; 2*1 is returned to from second call. Saved value of N (3) is retrieved from stack and multipled to $v0; 3*2*1 is returned from first call.