Lab 3

Computer Architecture I ShanghaiTech University

Objectives

Intro to Assembly with RISC-V Simulator

So far, we have been dealing with C program files (.c file extension), and have been using the gcc compiler to execute these higher-level language programs. Now, we are learning about the RISC-V assembly language, which is a lower-level language much closer to machine code. For context, gcc takes the C code we write, first compiles this down to assembly code (gcc uses a more complex assembly language than RISC-V), and then assembles this down to machine code/binary.

In this lab, we will deal with several RISC-V assembly program files, each of which have a .s file extension. To run these, we will need to use a RISC-V simulator. The simulator we will use was developed by Keyhan Vakil and improved by Stephan Kaminsky. The simulator is called Venus and can be found online here. We have deployed Venus on our Autolab server (link).

Assembly/Venus Basics

For the following exercises, please save your completed code in a file on your local machine. This is crucial for the checkoff portion to work.

Exercise 1: Familiarizing yourself with Venus

Getting started:

  1. Paste the contents of lab2_ex1.s into the editor.
  2. Click the “Simulator” tab. This will prepare the code you wrote for execution.
  3. In the simulator, click “Assemble & Simulate from Editor”
  4. In the simulator, to execute the next instruction, click the “step” button.
  5. To undo an instruction, click the “prev” button.
  6. To run the program to completion, click the “run” button.
  7. To reset the program from the start, click the “reset” button.
  8. The contents of all 32 registers are on the right-hand side, and the console output is at the bottom
  9. 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

Record your answers to the following questions in a text file. Some of the questions will require you to run the RISC-V code using Venus’ simulator tab.

  1. What do the .data, .word, .text directives mean (i.e. what do you use them for)? Hint: think about the 4 sections of memory.
  2. Run the program to completion. What number did the program output? What does this number represent?
  3. At what address is n stored in memory? Hint: Look at the contents of the registers.
  4. Without using the “Edit” 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.

Check-off

Show your TA that you are able to run through the above steps and provide answers to the questions.

Exercise 2: Translating from C to RISC-V

Open the files lab2_ex2_c.c and lab2_ex2_assembly.s. The assembly code provided (.s file) is a translation of the given C program into RISC-V.

Action Item

Find/explain the following components of the assembly file and put your answers in a text file.

After you’ve answered explained the above components, edit lab2_ex2_assembly.s so that it dest satisfies the following conditions.

Hint: This can be done by adding one line of code and modifying another (in other words, you only need to make 2 changes). Look at the initial values of dest; how does this help you implement this modification?

Verify that your changes work for the given source and dest arrays by running your code in a new Venus tab and check that the output looks like:

3 1 4 1 5 9
6 1 8 1 10 1

Check-off

Show lab2_ex2_assembly.s to your TA, and run it in Venus, which should give the correct result.

Exercise 3: Factorial

In this exercise, you will be implementing a function factorial in RISC-V that has 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 arguments that are passed into the function are defined at the top of the file. You may solve this problem using either recursion or iteration.

Action Item

Implement factorial and make sure that the program correctly outputs 3!, 4!, 8!, and 9!.

Check-off

Show factorial.s to your TA, and run it in Venus, which should give the correct result.