# Lab 5: Branches, Memory, and Arrays in MIPS Assembly

Due Thursday, August 18 at 11:59 PM

## Goals for This Lab

By the time you have completed this work, you should be able to:

• Execute instructions conditionally
• Translate conditionals (`if`/`else`) and loops (`while`, `for`) into MIPS assembly
• Access values in memory with the `lw` instruction
• Store values in memory with the `sw` instruction
• Iterate over arrays in memory
Provided files:

# Step-by-Step Instructions

## Step 1: Edit `factorial.asm`

Open the `factorial.asm` file with a text editor of your choice. Note that word processors (e.g., Microsoft Word, Pages, Google Docs) will probably not work for this purpose, as you must save your file as plain text. You must write MIPS assembly code which, when run under QtSpim, will do the following:

1. Read an integer from the user
2. Compute the factorial of the integer
3. Print out the factorial

An example interaction with this program is shown below:

```5
120
```

`factorial.asm` contains comments which further explain how you can do this. You may assume that the input integer will be non-negative. You may also assume that the computed factorial will fit into a 32-bit unsigned value, so you never need to look at the `HI` register. The Week 5 Materials have some examples which may be helpful.

## Step 2: Edit `sum.asm`

Now move to the `sum.asm` file. This program contains a hard-coded array, along with its length, in the `.data` section of the file. You must iterate through this array, compute the sum of all the elements (i.e., add them all together), and print out the result. Given the existing data in the file, this program should print `28`. Your code should work for ANY `array` and `array_length` values, but you may assume the following:

• `array_length` will always be at least 1, and will be an unsigned integer
• `array` will contain exactly as many `.word`s as `array_length`.
• Each element of the array will be non-negative

Further details are provided in the comments of `sum.asm`. The Week 5 Materials have some examples which may be helpful.

## Step 3: Edit `add_amount.asm`

Now move to `add_amount.asm`. This program works with two arrays of the same length: an input array, and an output array. Additionally, it contains a value to add to each element of the array (`amount_to_add`). For each element of the input array, it should add this amount, putting the result in the output array. A code snippet below illustrates this behavior:

```for (int index = 0; index < array_length; index++) {
}
```

The end of the code (provided) will print each element of the output array. As with the prior problem, this code MUST work for any values of `amount_to_add`, `array_length`, `input_array`, and `output_array`. You may make the following assumptions in your code:

• `array_length` will always be at least 1, and will be an unsigned integer
• `input_array` and `output_array` will always contain exactly `array_length` `.word`s.
• The values of `input_array` will all be non-negative.
• `amount_to_add` will always be an unsigned integer.

The Week 5 Materials have some examples which may be helpful.

## Step 4: Turn in Your Code Using Canvas

Log into Canvas, and go to the COMP 122 class. Click “Assignments” on the left pane, then click “Lab 5”. From here, you can upload your `.asm` files. Specifically, you must turn in the following three files:

• `factorial.asm`
• `sum.asm`
• `add_amount.asm`

In addition, if you collaborated with anyone else, be sure to download `collaborators.txt` and write the names of the people you collaborated with in the file, one per line. Please submit this file along with the other three files.

You can turn in the assignment multiple times, but only the last version you submitted will be graded.

### IMPORTANT: Your Code Must Run Under QtSpim

The code you submit must run under QtSpim without modification.
Code with syntax errors gets an automatic 0.
If you can't get your code to do the right thing, it's better to submit code that runs but does the wrong thing.