|CS 354||Computer Organization and Systems||Fall 2017|
Project 10 - Using Subroutines and Recursion in MIPS to compute binomial coefficients
In this lab you will gain familiarity with the MIPS assembly programming related to subroutines (also called functions and methods) and recursion. You will do this by writing a MIPS program to compute binomial coefficients using a recursive formula.
Subroutines in MIPS
Section A.6 of our text describes how one can use subroutines (also called procedures, functions, and methods) in MIPS. Because of the importance of subroutines in modern programming, most hardware designers include mechanisms to help programmers. In a high-level language, like C or Java, most of the details of subroutine calling are hidden from the programmer.
MIPS has special registers to send information to and from a subroutine. The registers $a0, $a1, $a2, $a3 are used to pass arguments (or parameters) into the subroutine. The registers $v0 and $v1 are used to pass arguments (or parameters) back from the subroutine.
The stack (and stack pointer register $sp) is used for a variety of things when using subroutines. The stack is used to pass additional parameters to and from subroutines. It is also used to hold temporary values in memory that a subroutine may need. Most importantly, it is used to save the current state so the subroutine can return back to the caller once it has completed. This include the frame pointer ($fp), the return address register ($ra), and the callee-saved registers ($s0-$s7).
Imagine you would like a program that reads two integers from the user, determine the largest of the two, the prints the maximum value. One way to do this would be to have a subroutine that takes two arguments and returns the larger of the two. The program max.s shown below illustrates one way to do this. Download and run this program, stepping through the program and watch the memory and the register values change.
Here are some important things to note about the above program. Because the stack starts at a high address and grows by growing towards lower addresses, pushing items on the stack require decrementing the corresponding stack pointer address. The $ra register stores the address where a subroutine should return when it is completed; it is set automatically by the jal instruction to be the address of the instruction following the jal instruction. In the subroutine, the jr instruction sets the program counter to be the address located in the $ra register.
Recursion in MIPS
One classical example of recursion is the factorial function. The program factorial.s shown below illustrates one way to do this. This is very similar to the example in section A.6. Download and run this program, stepping through the program and watch the memory and the register values change.
Here are some important things to note about the above program. As in any high-level language, recursion requires some careful thinking about what is happening. Perhaps seeing what happens at the assembly language level will help you understand recursion better.
The of combinations of n things taken m at a time, denoted C(n,m), assuming n >= 0 and m >= 0, is given by
You can use the formulaC(n,m) = 1 if n = m = 1 if m or n = 0 = C(n-1, m) + C(n-1, m-1) otherwise.
to test you function for small values of n and m. Note this function defines Pascal's triangle:C(n,m) = n!/((n-m)! m!)
where each value is the sum of the two values above it. Note C(5,3) = 10, which says given 5 things, you can for 10 possible subsets of 3 elements.1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 . . . . . . . . . . . . . .
Write a MIPS program using a recursive subprogram to prompt a user for two integers, m and n, then compute and print the combination of n things taken m at a time.
Send me an email with the MIPS file containing your program. Comment each line and code segment explaining its function.
Copyright © 2017, David A. Reimann. All rights reserved.