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.

NOTE: you ARE permitted to discuss solutions to the homework exercises with other students (2-35 thru 2-37 are especially helpful!), but not to discuss any functions from their project or your project.

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:  You will translate a given recursive C++ program into MIPS, and test it thoroughly (in SPIM) to ensure that it is fully correct. The C++ program takes as input two numbers, a “seed” and a “PIN”, then uses a recursive computation to compute a password.  When run, the program might look like this:

To help ensure the security of this super-fancy-sophisticated algorithm, each student will be translating a different C++ program (each one producing different passwords – so your results will probably not match that screenshot above).  Use this link to get your specific assigned program and sample outputs:

(be sure to print this page, for reference and to turn in with your final assignment)

You should NOT modify this code – translate it, as given, into MIPS.  You must keep the recursion.

Specifics: Starter code is provided: ProjectStarterCode.asm. (right-click and use Save As) Start with this!!!! Do this:

1. Read over the starter code (see link above and/or last page).  Make sure you understand where the function is defined and how it is called.

NOTE: The provided doAdd() function is so simple that it does not need to use the stack. Think carefully about your functions – they WILL need the stack!

2. Modify the main function to:

Print a welcome message (follow sample input given to you).

Read the “seed” and “PIN” from the user

Translate this test into MIPS:  if (seed <= 10 && pin <= 10)

If true, call the function specified (see your code), then print the resultant password.

If false, print the error message (see your code).

Exit main (syscall #10).

3. Your given C++ code will have two provided “helper” functions, one that takes one argument and another that takes two arguments.

First write the helper function that uses only one argument, and test it thoroughly!

Next, write the other helper function and test it.

Test the whole program (with main() calling your two helper functions).

You do not have to write these functions from scratch – translate the given C++ code!

4. Make sure all the code you write is commented!

5. Test your code with SPIM.

(more on back)

1.      You must follow the normal procedure call conventions as discussed in class.

2.      Your code must be commented.

3.      You must properly use the stack.  A solution that somehow computes the “right” answer without proper register and stack usage is not correct.

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 to MIPS.  While this involves recursion, 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.
IMPORTANT NOTE: All stack manipulation should be done only by your helper functions. main() should NOT use the stack or modify \$sp in any way.

4.      WARNING WARNING: executing “syscall” may change the value of registers like “a0”, “a1”, etc. and “v0”, “v1” etc. So after a syscall, you must assume that all those registers have changed values.

5.      This assignment requires you to write only about 50-100 carefully chosen assembly instructions.  If you have many more than that, you probably are misunderstanding what should be done.

6.      TESTING: if you are unsure of how your program should behave, start by looking at the sample output provided for your specific program. Test with the smaller values of “seed” and “PIN” first.  Remember, with SPIM you can “single step” through the code to see what is happening!

·         If you need more “test cases”, then you can simply compile the given C++ code and run it for comparison.

7.      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/

8.      Use Alt-PrintScreen to generate a screenshot.

9.      An error message like “Can’t expand stack segment by 12 bytes…” means you are stuck in infinite recursion.

10.  Use the starter code!

11.   In SPIM:

·         The value of the registers might be displayed by default in hex. You can change this to decimal via the menu option:   “Registers->Decimal"

·         If you run your program and nothing happens, try selectin “Window->Console” from the menu to re-display the console.

12.  There is no “subi” instruction – use “addi” with negative constant instead.

1.      Code must well commented – 10 of the points depend on this.

2.      Most of the points earned will come from a solution that actually works. A late submission that works will be worth more than an “on-time” submission that does not!

Deliverables:

A. Hard-copy (print and staple in the following order):

1)            A cover sheet with your feedback (“Project” version, not “Homework” version).           (10 pts)

2)            Screenshot showing your program running (with seed=9, PIN=8)                                    (10 pts)

3)            Output of  http://www.usna.edu/Users/cs/lmcdowel/courses/ic220/spimGen/index.html with your alpha (10 pts)

4)            Printout of your correct, commented, and recursive assembly code                                  (60 pts)

B. Electronic:

1)            On Blackboard->IC220->Assignments->Project 1, submit your .asm file.                       (10 pts)

Starter code (AND example of complete program that includes a function call and definition)

# 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

add \$v0, \$a0, \$a1               # add two arguments

jr \$ra                          # return

Use this example to understand what a function call and definition looks like in SPIM, then start translating your code.