IC220 Spring 2013 Project #1: Pernicious Padovan?
This project must
be completed independently (unlike regular assignments).
This means
collaboration between students is not permitted. Online sources (aside
from IC220’s website) are also prohibited. You may consult your notes, textbook, and the
instructor. See the instructor for help or clarification.
Background: You have already had experience with writing simple
MIPS programs and testing them with SPIM.
In this project you will review and integrate your understanding of:
1.
General MIPS programming
2.
Function calls
3.
Recursion
Task: Write a recursive MIPS
program that computes a term in the “Padovan number”
sequence, a famous mathematical sequence.
This sequence of numbers (1, 0, 0, 1, 0, 1, 1, 1, 2, 2, 3, 4, 5, 7, 9,
12, 16, 21, 28, 37, 49, …) can be computed with the
following recursive function:
int padovan
(int N) {
if (N == 0)
return 1;
else if (N == 1)
return 0;
else if (N == 2)
return 0;
else
return padovan(N-2)
+ padovan(N-3);
}
Thus, padovan(0)
= 1, padovan(1) = 0, padovan(2) = 0, padovan(3) = 1, padovan(4) = 0, …. padovan(10) = 3. Be sure you aren’t off by 1
in the sequence!
There are much more efficient ways to compute the Padovan series, but to solidify your understanding of
recursion and the stack you are required to have a recursive solution.
Specifics: Starter code is provided: ProjectStarterCode.asm. (right-click
and use Save As) Start with this!!!! You should do the following with
this code:
1. Modify the main function to:
Print a welcome message that
includes your name
Call the padovan procedure to compute the Padovan term requested by the user
Print the calculated term after it
has been returned to the main function
2. Write a procedure called padovan that:
Recursively
calculates the Nth Padovan term, where N comes from the
argument passed in register $a0.
Returns the value in $v0.
You do not have to write
this function from scratch – translate the C code given above!
3. Test your code with SPIM.
Sample screen shot of completed program:


(more on back)
Tips:
1.
Start early! Recursion can be tricky. Allow time to get help from the instructor if
needed.
2.
But don’t make this harder than it needs to be! Your primary task is to simply translate the C code above to a
MIPS function. While this function is
recursive, really this is just another example of a nested procedure – follow
the rules for nested procedures and recursion will take care of itself.
3.
You
will need to use the stack.
Before you begin, make sure you understand how to do this and what will
need to be saved and restored with the stack.
4.
This
assignment requires you to write only about 30-40 carefully chosen assembly
instructions. If you have many more than that, you probably
are misunderstanding what should be done.
5.
The
following material may be helpful to you:
·
Lecture
notes on MIPS procedure calls, and SPIM
·
Homework,
especially exercises 2-35 thru 2-37. Really spend time to understand these
before you start!
·
Lab
#1, where you first used SPIM
·
SPIM reference material: http://www.usna.edu/Users/cs/lmcdowel/courses/ic220/spim/
6.
Use
Alt-PrintScreen to generate a screenshot.
7.
An
error message like “Can’t add 8 bytes to Lstack”
means you are stuck in infinite recursion.
8.
Use
the starter code!
9. In SPIM,
the value of the registers are displayed by default in
hex. You can change this to decimal via the menu option: “Registers->Decimal"
10. The next page contains another
useful example, demonstrating how to do function calls. Understanding this before you start will save
you time!
Deliverables:
A. Hard-copy (print and staple in the following
order):
1)
A cover sheet with
your feedback. (10 pts)
2)
Screenshot
showing your program computing padovan(15) (10 pts)
3)
Printout
of your correct and recursive assembly code (60
pts)
B. Electronic:
1)
On
Blackboard->IC220->Assignments->Project 1, submit your .asm file. (10
pts)
2)
If
submitting any extra credit, also submit extra .asm
files for those.
Extra credit (up to 5 pts each):
1. Write a new program to calculate the Padovan numbers in a non-recursive manner. Turn in this program and a screenshot showing
the calculation of Padovan(30).
2. Read ahead about floating point registers, then write
a new program to calculate the Padovan numbers using
the floating point registers to keep track of the calculations (you can assume
that you only have to deal with numbers with no fractional part). Why might you want to do this?
Additional
sample program – NOT the same as the provided starter code
# Example code
demonstrating how to call a function as part of
# a complete SPIM program.
.data
str_1: .asciiz
"Enter a number=> "
str_2: .asciiz
"Result=> "
.text
.globl main
main:
# Print the prompt
la $a0, str_1 # 'load address' of string to
print
li $v0, 4 # syscall
#4 = print string
syscall
# Read an
integer from the user
li $v0, 5 # syscall
#5 = read integer
syscall # read int,
result goes in $v0
# Call a function to add that number
together with 13
addi
$a0, $zero, 13 # Set up first
argument to function (13)
move $a1,
$v0 # Set up second
argument (the number entered by user)
jal
doAdd # do add, result now in
$v0
move $s0,
$v0 # save result for
later (b/c v0 clobbered below)
# Print string announcing the result
la $a0, str_2 # 'load address' of string to
print
li $v0, 4
syscall
# Print actual result
li $v0, 1 # syscall
#1 = print integer
move $a0, $s0 # tell syscall
what # to print
syscall
# terminate the program
li
$v0, 10
syscall
# Define the (very simple)
function we use
# Notice that this goes
OUTSIDE of main
doAdd:
add $v0, $a0, $a1 # add two arguments
jr $ra
# return