Lab 2 MIPS Decision Instructions

1 Objectives

Following completion of this lab you should be able to:

2 General

This and the remaining labs should be done in pairs - only one Lab should be submitted for your pair.

You will need to answers to the questions contained in the lab guide (marked by a Q below) on the lab question sheet (Word doc version). At the end of this lab you will be uploading a PDF of the question sheet with your answers.

Before each lab, you should get any repository updates from the server. Get the latest version by opening a terminal window in your repository directory and pulling any changes from the server:

git pull

You should find a lab02 directory which contains subdirectories for each part of the lab.

In writing these and other assembly language programs:

3 Looping

  1. Open the p4.asm file in MARS. This goal of this program is to sum the integers between 0 and an \(N\).
  2. Q Where are main, loop, done, N and Sum located (i.e. what address)? Hint: Assemble and use Settings > Show Labels Window in the Execute view.
  3. Before you run the program, calculate the number of instructions that will be executed and the final value of Sum. Only count the instructions that run between the test setup and teardown (the jal and the jr). Note: Appendix A of Patterson and Hennessy has lots of useful information about MIPS.
  4. Q Assemble the code and set a breakpoint at address 0x00400004. Use Run > Go to run the program and it should stop at the breakpoint. Single step to the end of the program. How many instructions were actually executed? Remember, only count the instructions that execute between the 'jal' and the 'jr'. What is the final value of Sum?
  5. Modify p4.asm so that it will work correctly if N is equal to 0. Be sure to test your modifications to make sure they work. You can do this by changing the values of the testN and test0 variables from 0 to 1.
  6. Q How many instructions does your modified program execute when N is equal to 5? Can this number be improved? If so, how? Hint: If your modified program executes 5 additional instructions, you can do better.
  7. Q Will your modified program work if N is less than 0? You do not need to make any additional modifications.

4 Finding the Maximum

  1. Open the p5.asm file. This goal of this program is to find the maximum element in an array and put it at the end of the array.
  2. Assemble the code and set a breakpoint at address 0x00400004. This is the start of the program.
  3. Q Run p5.asm. What is the value of max and maxindex at the end of the program? Are they what you expect?
  4. Q Comment out sll $t5, $t2, 2 and rerun the program. What happens? Why?
  5. Undo the changes made in Step 4. Modify p5.asm so that the largest element of A is swapped with the last element of A. Be sure to adequately test your modified program. Hint: change the elements and size of A, then check your results in the Data segment.
  6. Q If you repeatedly apply your modified program to the subarrays of A from 0 to \( N-i \) where \(i\) is the number of times you've applied your program, what is the final state of A?
  7. Q Like p4.asm this program doesn't work if N is equal to 0. It is brittle in other ways as well. For example, what happens if all of the elements are less than -1? Is there a more robust way to set the maximum value, instead of using the magic constant of -1?

5 Rotating an Array

  1. Open p6.asm in MARS. Your goal in this program is to shift the elements in an array.
  2. Q Run p6.asm. What is the initial array displayed in the console window?
  3. Modify p6.asm to treat V as a circular array and rotate its contents one position to the right. Rotate the array in place. Specifically, \( \textrm{V}(i) \) moves to \( \textrm{V}((i+1)\ \textrm{mod}\ N) \). After performing the rotation, your program should print the string ``The rotated array:'' followed by the elements of the rotated array, each preceded by a single space character.
    • When you assemble the code, you will want to set a breakpoint at address 0x00400004, similar to the other programs. Note that until you write your rotation code, address 0x00400004 will be the jr instruction which finishes the program.
  4. Q Run your modified p6.asm. What is the rotated array displayed in the console?

6 Turning It In

Submit the answers to the questions contained in the lab guide using the question sheet via gradescope, only 1 per team (make sure all team member's names are included). In gradescope you are able add your team members names to the submission, make sure you do so. When you are prompted to indicate where the "code question" is on the answer sheet, just select the first page of the assignment, you DO NOT upload code to gradescope.

Lab code will be submitted to EVERY team member's git repository. You WILL need this code for Lab 3, if you don't have a copy in your repository its gonna be pretty awkward.

Place the modified assembly programs in the lab02 directory in your repository.

Then, add the files to a repository commit. For example, you might issues these commands:

git add lab02/p4/p4.asm
git add lab02/p5/p5.asm
git add lab02/p6/p6.asm

You can now commit the files with:

git commit -m "Done with lab2"

Finally, sync your changes with the server:

git push