Lab 2 RISC-V Decision Instructions
1 Objectives
Following completion of this lab you should be able to:
- Write loops in RISC-V programs.
- Understand the limitations of
beq/bne/blt/bge
,jal
, andjalr
. - Properly access memory in RISC-V programs.
- Understand some of the issues surrounding register allocation.
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 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:
-
Solve the problem before coding the solution. Usually this means writing the code in a high-level language or pseudo-code first, then converting it to assembly language.
-
IMPORTANT: Write down (perhaps in a comment in your code) the purpose that you have in mind for each register that you use. Here's an example of how you might document register use in your code.
# x3 = i # x4 = j # x5 = n # x6 = key # x7 = ptr (address of array) # x8 = test # x9 = temp
-
These programs involve many loops, so it will be helpful to set breakpoints. Breakpoints mark positions in the code where execution should pause. After assembling a program, click the Bkpt checkbox to set a breakpoint at that line. Execution will stop before that line is run and you can observe the state of the program. Note: All breakpoints are deleted when you assemble the program!
-
All of these programs require RARS to be configured to assemble all files in the directory.
-
IMPORTANT: Do not use registers 1 and 2 for this exercise.
3 Looping
- Open the
p4.asm
file in RARS. This goal of this program is to sum the integers between 0 and an N. - Q Where are
main
,loop
,done
,N
andSum
located (i.e. what address)? Hint: Assemble and use Settings > Show Labels Window in the Execute view. - 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 (thejal
and thejalr
). Note: Figure 2.1 in Chapter 2 of Patterson and Hennessy has lots of useful information about RISC-V. - 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 'jalr'. What is the final value ofSum
? - Modify
p4.asm
so that it will work correctly ifN
is equal to 0. Be sure to test your modifications to make sure they work. You can do this by changing the values of thetestN
andtest0
variables from 0 to 1. - 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 more than 1 additional instruction, you can do better. - 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
- 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. - Assemble the code and set a breakpoint at address
0x00400004
. This is the start of the program. Set another breakpoint at address0x00400050
. This is the end of your program (should be a jalr). - Q Run
p5.asm
until your second breakpoint. What is the value of max and maxindex at the end of the program? Are they what you expect? - Q Comment out
slli x10, x7, 2
and rerun the program. What happens? Why? - Undo the changes made in Step 4. Modify
p5.asm
so that the largest element ofA
is swapped with the last element ofA
. Be sure to adequately test your modified program. Hint: change the elements and size ofA
, then check your results in the Data segment. - 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 ofA
? - Q Like
p4.asm
this program doesn't work ifN
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
- Open
p6.asm
in RARS. Your goal in this program is to shift the elements in an array. - Q Run
p6.asm
. What is the initial array displayed in the console window? - Modify
p6.asm
to treatV
as a circular array and rotate its contents one position to the right. Rotate the array in place. Specifically, V(i) moves to V((i+1) 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, address0x00400004
will be thejalr
instruction which finishes the program.
- When you assemble the code, you will want to set a breakpoint at address
- 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