Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Lab 1. Introduction to RISC-V

Due: Sunday, September 13 at 23:59

Your task is to write a program which takes in a number and stores it in a four-element array at a user-specified index, then prints the contents of the array. You will write this program using the RARS RISC-V simulator, which you can download here.

Preliminaries

First, download RARS.

If you don’t already have a GitHub account, make one. As a student, you can get additional GitHub benefits such as unlimited private repositories here, but you don’t need to for this course. All of your repositories will be private to you and course instructors.

You’ll need Git installed in order to download and submit assignments. On macOS, Git is included with Xcode which can be installed from the App Store. On Linux, Git can be installed using your distribution’s package manager. You can install any of the many other Git tools instead, including GitHub Desktop on macOS and Windows.

Click on the assignment link.

Once you have accepted the assignment, you can clone the repository on your computer by following the instruction and begin working.

Program specification

Your task is to change the starter code to read two integer values from the keyboard and store one of the values into the array named arr. Then, you’ll print out every element of the array.

Specifically, read two input values from the keyboard:

  1. a number to enter in the array arr; and
  2. the index into arr where you should store the first number.

Store the first value into the array at the given index.

Finally, print out each element of the array along with its index.

Print out each index of the array, and its contents. It should look like this.

  Please enter a number: 6
  Which element of the array would you like this to be? 1
  0: 0
  1: 6
  2: 0
  3: 0

Or like this.

  Please enter a number: 3
  Which element of the array would you like this to be? 0
  0: 3
  1: 0
  2: 0
  3: 0

You can assume the user will not input any indexes outside of the bounds of the array. You are required to store the user’s value in memory at the specified position, and to load every value in the array from memory into a register to print it out.

Note that you will need to translate between indices, which are doubleword addresses, and values in memory, which are byte addresses.

Make sure to document your programs thoroughly. (This is especially important in assembly language programs, since the code itself is less easily read than high-level language code.) This should include:

  1. A block of comment lines at the beginning of the source file, giving the name and author of the program and a black-box description of what it does.
  2. A few comment lines between major sections of the program, describing the contents of each section.
  3. A comment at the end of most source lines, describing what the instruction on that line does.

Tip

You’ll need to put RARS in 64-bit mode for all of the RISC-V labs in this course.

RARS can be run in a graphical mode and contains a complete integrated development environment (IDE) or it can be run entirely as a command line application that will compile and run your RISC-V program.

You can run in the graphical mode by double clicking on the rars-1.7.jar file or run it from the command line with no additional options:

$ java -jar rars-1.7.jar

To change the setting to 64-bit, click on the Settings menu and then select 64 bit from the menu.

To run in command line mode, you’ll need to specify the argument rv64 as well as the name of the file to compile and run. Like this.

$ java -jar rars-1.7.jar rv64 lab1.s
Please enter a number: 35
Which element of the array would you like this to be? 2
0: 0
1: 0
2: 35
3: 0

Program terminated by calling exit

Helpful resources

  • A guide to the MARS simulator, the predecessor to RARS. MARS is a MIPS simulator rather than RISC-V so you’ll need to do a little translating.
  • A list of MIPS system calls supported by MARS. These same system calls are supported in RARS. The differences are that in RISC-V, the system call number goes in register a7 (rather than MIPS’s $v0 register), the instruction is ecall (rather than MIPS’s syscall), and the returned value (if any) goes in register a0 (rather than MIPS’s $v0). The same list of system calls is available from inside RARS itself by accessing its help and clicking on the system calls tab.

Hints

  1. You will need to use various system calls to print strings, integers, etc. These are all provided on the list of system calls linked above or in RARS itself.
  2. The la instruction loads the address of a label. You can use this to load the addresses of any of the variables in the .data section of your code.
  3. The mul instruction will allow you to multiply registers together. There’s no muli instruction so if you want to multiply a value by a constant, you need to first use li to load the constant into a register.
  4. The starter code contains an example of printing a string and reading an integer. You’ll want to use this code in your own solution.
  5. You should not write any loops. (After you write essentially the same code to print out each element of the array 4 times, I think you’ll agree that writing a loop would be better. We’ll learn how to write loops soon, if we haven’t already.)

Submission

Submit the lab by committing your code and pushing it to your GitHub repository. From the command line, you would enter the commands

$ git commit -m "Finished!" lab1.s
$ git push

from inside your repository’s directory.

The first command instructs Git to take the changes that you made to lab1.s and record those changes on your computer. Usually, when writing software, you would make multiple commits. This allows you to “easily” go back to previous versions. (“Easily” is in quotes because nothing with Git is ever actually easy.)

The second command instructs Git to take all of the changes that you have committed and push them to your repository on GitHub. You can push each time you commit, or you can push after several commits have been made and all commits that haven’t yet been pushed to GitHub will be pushed at that time.

If you don’t push after committing the final version of your code, the graders will not be able to see your changes (because they’ve only been recorded on your local computer).

Rather than use the command line, you can visit the web page for your repository on GitHub and drag and drop the files on the page. I don’t really recommend doing this; it’s better to learn how to use Git properly. But it does work and you’re welcome to do it.