From the course: Getting Started with RISC-V

A simple coding demo

- [Instructor] Let me quickly show you how to find the exercise files in the GitHub repository. This is a single branch repository so you can find the code related to each video under the source folder, which contains one folder per example. The source folders are named with the chapter number followed by the video number. For example, the code for this video is in 06_04. So to work with this code, you can always download it and run it in your own setting, or you can use GitHub Codespaces, a cloud-based IDE you may use for this course. Although you may create a codespace in the course repository, it will not allow you to push your changes so they would only be saved inside your codespace. Because of this, I recommend that you create your own copy of the repository on your GitHub account so you can keep any changes that you've made even if you later decide to delete a codespace. To create your own copy of the repository, you can click on the fork button and follow the instructions. Once you have your own copy of this repository, you can start a new codespace. All you need to do is click on the code button and under the Codespaces tab, you can start a new codespace or open an existing one. I'll create a new one. Setting up the codespace may take a while. Once started, you will see a cloud version of Visual Studio Code with your repository as an open folder so you can browse to find any of your files. Now let me show you the first code demo. This one is a console application located in the 06_04 folder. Now let me get rid of the bottom and left panes. As you can see in the opening comments, this code starts by asking the user's name and then responds with a message using that name. Now, the purpose of these exercises is only to show you some assembly code applications running on a simulator. That's why I have added comments all over the code. The idea is for you to read through the code and maybe find the logic behind the instructions being used. In line 13, we have the .data directive, which informs the assembler that the following code is to be placed in the data section of volatile memory. Then we have the definition of three strings using the .string directive. These are named "prompt", "response", and "name". Notice that name is just a bunch of spaces. This is where the user's name will be stored. And also notice the \n character. This is the new line character and it causes the same effect as hitting enter in a text editor. In line 19, we have the .text directive. This instructs the assembler to place the following code in the code section of a program memory. So let's see the assembly code. The code starts with the main entry point in line 26. The RISC-V ISA always starts execution at address zero. In line 28, we have the la pseudo instruction to load the address of the name string to register t0. Then starting at line 31, we have an environment call to print the prompt message on the terminal. Notice that first we load the address of prompt to a1, which is the argument for the environment call. Then we load four into a0. This identifies the environment call as "print_string". And lastly, we run the ecall instruction. In line 36, we have a call to the read_str sub-routine. This will get the user's name from the terminal. Next, we print out the response message starting at line 39. Then starting at line 44, we print out the user's name. Now starting at line 49, we have three environment calls to print single ASCII characters. These are one exclamation mark in line 51 and two new line characters in lines 54 and 55. Lastly, we have the exit environment call in line 59. Scrolling down, we have the sub-routine that reads the input from the console. You may want to read the comments if you want to learn the details of its operation. All right, let's see it running. For that, I'll press the run and debug button at the left and at the top we have a dropdown to select a launch configuration. For this example, I will choose to launch the current file. Now I can press the run button. This will open the bottom pane where you can select the terminal. And you will also see the execution buttons at the top of the screen. These buttons will allow you to run the simulation continuously or step by step. I'll press the continue button to run the application freely. Now let me maximize the terminal, and there's the message, "Hi, what's your name?" So let me enter my name and hit enter. And there's the response, "It's nice to meet you, Eduardo!"

Contents