Checkbox
Checkbox
Status
Done
Exercise 1: Familiarizing yourself with Venus
Getting started:
- Open
ex1.s
into the Venus editor. If you were unable to mount the filesystem in Exercise 0, then you can copy/pasteex1.s
from your local machine into the Venus editor directly.
- Click the “Simulator” tab and click the “Assemble & Simulate from Editor” button. This will prepare the code you wrote for execution. If you click back to the “Editor” tab, your simulation will be reset.
- In the simulator, to execute the next instruction, click the “step” button.
- To undo an instruction, click the “prev” button. Note that undo may or may not undo operations performed by
ecall
, such as exiting the program or printing to console.
- To run the program to completion, click the “run” button.
- To reset the program from the start, click the “reset” button.
- The contents of all 32 registers are on the right-hand side, and the console output is at the bottom.
- To view the contents of memory, click the “Memory” tab on the right. You can navigate to different portions of your memory using the dropdown menu at the bottom.
Action Item
Open
ex1.s
in Venus and record your answers to the following questions. Some of the questions will require you to run the RISC-V code using Venus’s simulator tab.- What do the
.data
,.word
,.text
directives mean (i.e. what do you use them for)? Hint: think about the 4 sections of memory.
.data
means that the contents below are data;
.word
means that the following are bytes might be used;
.text
means that the following are instructions.- Run the program to completion. What number did the program output? What does this number represent?
34.
The 9th fib number;
- At what address is
n
stored in memory? Hint: Look at the contents of the registers.
0x10000010
- Without actually editing the code (i.e. without going into the “Editor” tab), have the program calculate the 13th fib number (0-indexed) by manually modifying the value of a register. You may find it helpful to first step through the code. If you prefer to look at decimal values, change the “Display Settings” option at the bottom.
- 通过观察,可以发现
n
的地址被 load 到了t3
中; - 然后,再根据
n
的地址,将其的值仍然存储到t3
中; - 那么我们直接将这个值改为13,也就是十六进制的D就可以。
Exercise 2: Translating from C to RISC-V
Open the files
ex2.c
and ex2.s
. The assembly code provided (.s file) is a translation of the given C program into RISC-V.In addition to opening a file in the “Editor” tab and then running in the “Simulator” tab as described above, you can also run
ex2.s
directly within the Venus terminal by cd
ing into the appropriate folder, then running run ex2.s
or ./ex2.s
. Typing vdb ex2.s
will also assemble the file and take you to the “Simulator” tab directly.Action Item
Find/explain the following components of this assembly file.
- The register representing the variable
k
.
t0
represents the variable k
in C file;- The register representing the variable
sum
.
s0
represents the variable sum
in C file;- The registers acting as pointers to the
source
anddest
arrays.
s1
acting as the pointer to source
array;
s2
acting as the pointer to dest
array;- The assembly code for the loop found in the C code.
- How the pointers are manipulated in the assembly code.
By load the address of the array to some register, then access each item by adding an offset, or shift left logic or shift right logic;
Exercise 3: Factorial
In this exercise, you will be implementing the
factorial
function in RISC-V. This function takes in a single integer parameter n
and returns n!
. A stub of this function can be found in the file factorial.s
.You will only need to add instructions under the
factorial
label, and the argument that is passed into the function is configured to be located at the label n
. You may solve this problem using either recursion or iteration. You may also assume that the factorial
function will only be called on positive values with results that won’t overflow a 32-bit two’s complement integer.Testing
As a sanity check, you should make sure your function properly returns that
3! = 6
, 7! = 5040
and 8! = 40320
.You have the option to test this using the online version of Venus, but we’ve provided a
.jar
for you to test locally! We’ll be using the .jar
in the autograder
so make sure to update your factorial.s
file and run the following command before you submit to verify that the output is correct. Note that you will need to have java installed to run this command.