lab04 : MIPS Control Instructions
num | ready? | description | assigned | due |
---|---|---|---|---|
lab04 | true | MIPS Control Instructions | Thu 01/31 08:00AM | Mon 02/05 11:59PM |
Lab 4: MIPS Control Instructions
Due Monday, February 5th
Goals for This Week
By the time you have completed this work, you should be able to:
- Use basic arithmetic and control instructions in MIPS assembly
- Write simple loops in MIPS assembly
Provided files:
Documentation:
Step by Step Instructions
There are three tasks that you need to complete for this week, some with multiple steps. Strictly speaking, you may complete them in any order, though for this lab it is recommended to go in order. The tasks are listed below:
- Task 1: Control Operations
- Task 2: Basic Loops
You may use any MIPS instructions or psuedoinstructions you want in order to implement these tasks. However, you will be somewhat restricted on exams. You may want to read the grading policies regarding MIPS assembly instructions for more information.
The initial step below describes how to get the files you will need into the appropriate place. These files are used for all the different tasks.
Initial Step: Create a Directory for This Lab and Copy Over the Files
After you log in, go into your cs64
directory that you created last time:
cd cs64
Then create a new directory for this lab: lab4
:
mkdir lab4
Then go into that directory.
Now copy over all of the files necessary for this week's tasks:
cp ~zmatni/public_html/cs64w19/labs/4/MedianNumbers.asm ~zmatni/public_html/cs64w19/labs/4/task1_tester.pl ~zmatni/public_html/cs64w19/labs/4/factorial.asm ~zmatni/public_html/cs64w19/labs/4/task2_tester.pl ~zmatni/public_html/cs64w19/labs/4/partner.txt .
Note the use of the trailing .
in the above command, which stipulates that the specified files should be copied into the current directory.
Task 1: Control Operations
This task requires you to use branches in MIPS assembly. To this end, you will write a MIPS program which will ask the user for three numbers and then print out the median. A sample run of this program will look like this, with user input in bold:
Enter the next number: 3 Enter the next number: 5 Enter the next number: -13 Median: 3
Your code should be written in the provided MedianNumbers.asm
template file.
Be sure that your output matches exactly with the output above.
Note that you do not need to use a complex sorting algorithm because you are only comparing exactly 3 numbers, not some arbitrary number of numbers. A good development strategy here is to write a small program in C/C++ that will behave the same way as the program above, and to test it out. From there, it can be ported to assembly, which should be simpler than directly writing in assembly (considering that you are just starting to write in assembly). Try to write as simple of C/C++ code as possible, as ultimately any complexity in C/C++ will almost certainly translate to complexity in assembly.
For your convenience, we have provided a handful of test cases for you. These can be run from the CSIL machines with the following command:
./task1_tester.pl
The provided test cases are hardly complete; you are encouraged to add your own test cases and perform your own testing.
Task 2: Basic Loops
This task requires you to write a basic loop in MIPS assembly. To this end, you will implement the factorial function iteratively (i.e., without using recursion), which will take as an input some number gathered from the user. The factorial function will print out all the partial products as they are encountered, along with the final result. Each of the printed out numbers should be followed by a newline. Pseudocode showing how to do this is shown below:
n = <<user input>> accum = 1 while (n != 0) { accum = accum * n print accum print “\n” n = n - 1 } print accum print “\n”
A sample run of the program on input 1
is shown below, with user input in bold:
Enter the number: 1 1 1
A second run of this program on input 0
is shown below, with user input in bold:
Enter the number: 0 1
A third run of this program on input 5
is shown below, with user input in bold:
Enter the number: 5 5 20 60 120 120 120
You should write your program in the provided factorial.asm
template file.
For full credit, your solution's output must match the above output exactly. While you may write your own algorithm to do this, it must be iterative; that is, you may not use recursion. IF YOU USE RECURSION, YOU WILL GET A ZERO ON THIS PART OF THE ASSIGNMENT! You may assume that the user will only input non-negative numbers, and that no multiplication will lead to overflow. You will be working only with unsigned integers for this task.
For your convenience, we have provided a handful of test cases for you. These can be run from the CSIL machines with the following command:
./task2_tester.pl
The provided test cases are hardly complete; you are encouraged to add your own test cases and perform your own testing.
Turn in Everything Using turnin
If you partnered with someone, record the email address they are using for the class in partner.txt
.
For example, if your partner had the email address foo@bar.com, then the contents of partner.txt
should be the following (and only the following):
Partner: foo@bar.com
If you did not partner with anyone, you do not need to (and should not) edit partner.txt
.
Assuming you are in the cs64/lab4
directory you created at the beginning, you can send in your answers via the following command:
turnin lab4@cs64 MedianNumbers.asm factorial.asm partner.txt
You may turn in the same assignment up to 100 times, which is useful if you are working on it incrementally. Note that only the last version you submitted will be graded.
Even if you did not partner with anyone, you should still turn in partner.txt
, which should not have been modified.
Copyright 2018, Ziad Matni, CS Dept, UC Santa Barbara. Adapted from work by Diana Franklin and Kyle Dewey. Permission to copy for non-commercial, non-profit, educational purposes granted, provided appropriate credit is given; all other rights reserved.