Starting from:

$24.99

CSE12-02 Lab 3: Looping with RISC-V Assembly Solution


Minimum Submission Requirements
Ensure that your Gradescope submission contains the student completed file:
○ Lab3.asm
Objective
This lab will introduce you to the RISC-V assembly language programming using RARS. You will write a program with nested loops in the provided file Lab3.asm to write a specific pattern to a file by the name of lab3_output.txt (which is generated by running your Lab3.asm file and does not exist prior to this action)
Since this is your very first foray into assembly programming, please read this document thoroughly without skipping any sections!
Resources
Much like how a high-level program has a specific file extension (.c for C, .py for python) RARS based RISC-V programs have an .asm extension.
In the Lab3 folder in the course Google Slide, you will see 7 assembly files. They are meant to read (and understood) in sequence:
1. firstRARSprogram.asm – This program just prints “Hello World” on the output console.
2. add.asm – This program accepts two integers as user inputs and prints their addition result on the output console.
3. multiply.asm – This program accepts two integers as user inputs and prints their multiplication result on the output console. This file specifically shows you how to do loops in assembly
4. fileWriteDemo.asm – This program creates a file for write, and then writes a line of text into the file. Basically, it shows the basics to do a file Write in RARS.
5. fileReadDemo.asm – This program reads the file generated by the above .asm file and reads out its contents on the output console. Basically, it shows the basics to do a file Read in RARS.
6. patternDisplayDemo.asm – This program inputs a number n from user and generates a “* * * ….” Pattern depending on the value of n. This pattern is written into a file. Understanding how this code works will help you in writing the pattern required of this lab assignment into a file as well.
7. Lab3.asm – Some starter code has already been provided in this file which you will need to complete and then submit to your Gradescope autograding portal. The starter code mainly revolves around macros that have been created for your benefit to create and write to the file lab3_output.txt.
Please download these files and make sure to open them in RARS Text editor only. Else the comments and other important code sections won’t be properly highlighted and can be a hindrance to learning assembly language intuitively. Steps on opening, assembling and running an .asm file are provided later in this document.
These 7 files have enough comments in the source code to jumpstart your understanding of RISC-V assembly programming if the lectures have not yet covered certain topics in assembly programming.
For the usage of macros (which are utilized heavily in this lab to generate ecalls), please also refer to the RARS documentation on macros and ecalls as well. For lab3, you don’t even need to know what the inside of a macro block looks like so long you know just what it is supposed to do overall.
Working in RARS
Helpful tip: For lab3 and lab4, it is very useful to create two separate folders in your machine, lab3 and lab4. Make each folder the workspace for your respective lab. So, for the given lab, place the provided three
.asm files in Lab3 folder along with a copy of the .jar RARS application file. This is where you will create your Lab3.asm file as well.

Figure 1 Ideal workspace setup for lab3/lab4
Henceforth, run all .asm files pertinent to Lab3 on this local copy of the .jar RARS application. For reference, see my Lab3 workspace below in Windows
Open the RARS application. You should get the window below.

Figure 2 Opening the RARS application
Let us open firstRARSprogram.asm by clicking File -> Open.
Make sure the comments (which appear in green) are properly indented and haven’t been misaligned when you downloaded the file from the Google Drive. They should appear as shown below:

Figure 3 Opening an asm file on RARS
Make sure to thoroughly read the entire contents of this file in the text editor. Verbose comments have been provided to guide you along in explaining each step in the source code. This will be the norm for the other .asm files in the Lab3 folder in Google Drive as well.
After you have read and understood the source code, it is time to assemble the program. Before you assemble, go to Settings and make sure you have the exact following options checked (or unchecked). For this course, you are allowed to use pseudo instructions. Pseudo instructions are those instructions which are not native to the RISC-V instruction set but the RARS environment has defined these new ones by a combination of actual RISC-V instructions. Permit pseudo instructions (this actually makes coding easier for you) and 64-bit operation. This should be done for every RARS code in CSE12!

Figure 4 RARS setting
Now click on Assemble (the Wrench and screwdriver icon). If correctly assembled, your Messages window should show the following information:

Figure 5 Successful assembly
Now Click on the Run button to Run the program. You will get the following output:

Figure 6 Successful Runtime
Now try running the other .asm files.
One word of caution when your text editor contains multiple opened files is to make sure of assembling the correct assembly file. For example, the window below, multiple files are open and I want to only assemble and run add.asm. Then my tab for add.asm should be highlighted as shown below. Only then can I click Assemble, then Run.

Figure 7 Multiple tabs open
A helpful feature in RARS
RARS has a helpful feature where instead of Running the entire program at once, you can Run One Step At A Time. The corresponding button is beside the Run button. This allows you to sequentially execute each line of code and see how it affects the values of the Registers as they appear to the right of your screen.
Regarding Macros
The file multiply.asm makes extensive use of macros to help create a more readable main program section (Instructions on how to use macros are provided in the file comments). So does the source code in the files fileWriteDemo.asm, fileReadDemo.asm and patternDisplayDemo.asm (we will discuss more on the aspect of file reads and writes that these .asm files do shortly). Based on how we define a macro in the source code, it is tempting to confuse it with a function. However, macros are NOT functions! Whenever you
place multiple instances of the same macro in your code, you are copying the macro’s contents in those code areas for the same number of times.
Naming new files in RARS
When you want to open a new file on RARS, go to File->New. The default file name riscv1.asm shows up on the tab. When you save this file, you MUST make sure that you are explicitly defining the correct extension(.asm) as shown below.

Figure 8 Saving a new file in RARS
About file reads and writes in RARS
File creation and manipulation is a very common part of the learning curve whenever you learn of a new high level programming language, be it C or Python. For lab3, we will be writing the display pattern to a file so that it is more convenient for the auto grader. The auto grader would do a file text equality check between the file generated by your lab3 source code and the one in its deck and accordingly provide you points (or not!).
To give you a demo, we have two reference assembly source code files: fileWriteDemo.asm and fileReadDemo.asm. The former file creates a file with the name fileDemo.txt. The following text is written into fileDemo.txt: “These pretzels are making me thirsty!”. The latter file fileReadDemo.asm contains code to open fileDemo.txt and extract this text to display on the output console of RARS.
The following two images show the results of having run fileWriteDemo.asm and then fileReadDemo.asm.

Figure 9 A new file generated in my workspace after running fileWriteDemo.asm. Note the file size to be shown as 1KB despite us having written only 38 bytes of data into it. That is because a file also contains metadata and a header generated by your OS as well.

Figure 10 RARS output console after running fileReadDemo.asm
Both fileWriteDemo.asm and fileReadDemo.asm use many macros within the source code to make the main
.text section of the code more programmer friendly in terms of writing. For the purposes of lab3, you DO NOT need to understand WHAT these macros are doing within their definition block. It suffices to know simply what the result of executing a macro in your source code simply does. However, understanding the macros does help to build your foundation in RARS programming well.
We will deal with register preservation in lab4 but in lab3, you will only be asked to ensure that you do not use specific registers in your Lab3.asm source code. The list of these taboo registers will be highlighted in the section later on Lab3
Besides the aforementioned 2 files related to file write and read, we also have a 3rd .asm file, patternDisplayDemo.asm. The source code in this file, once run, asks as input an integer n and then prints the pattern “* “ n number of times horizontally.


Figure 11 Output console after running patternDisplayDemo.asm for user input n=3 and7. In both cases, make sure to check the contents of the created file patternDisplay.txt as well
Similar to patternDisplayDemo.asm, Lab3.asm will also make use of loops (nested loops to be precise) to generate a pattern based on the value of n inputted by user. Thus, you should thoroughly read and understand the working of source code in patternDisplayDemo.asm.
Lab3 Programming Assignment
This program will print out a pattern with stars (asterisks, ascii hex code 0x2a) and dollar sign (ascii hex code 0x24) and the character ‘0’(ascii hex code 0x30).
1. It will first prompt for the height of the pattern (i.e., the number of rows in the pattern). If the user enters an invalid input such as a negative number or zero, an error message will be printed and the user will be prompted again. These error messages are listed in the starter code in the Lab3.asm file provided.
2. Then your program should generate the following pattern, a "tattooed right-angled triangle", using stars, dollar,’0’ and newline (ascii hex code 0x0a) characters. Refer to test cases in testCases sub folder in Lab3 folder for examples.
3. This entire pattern generated from the user input of n is written to the file lab3_output.txt.
The actual task of opening the file lab3_output.txt and writing the contents to it is borne by macros used in starter code included in the Lab3.asm file. Consider the screenshot of the Lab3.asm file below:

Figure 12 Lab3.asm screenshot
As you can see, you should write down your code ONLY within the area indicated by the comments.
The way this code works regarding file manipulation is as follows:
In your student code, you can update the file buffer with any character with the macro write_to_buffer. For example, I want to the write the character sequence “*<blank space>**<blank space>* ” to my memory buffer within Lab3.asm’s student code section. Then I would need to write the following student code as shown next:

Figure 13 Modified Lab3.asm screenshot
Run this Lab3.asm file and open the generated lab3_output.txt in a text editor. Specifically, if you are using Notepad++(which is strongly recommended), make sure to apply the setting: View->Show Symbol ->Show All Characters. This will make characters such as null and newline visible.

Figure 14 lab3_output.txt screenshot from running modified Lab3.asm
As you can see, the blank space appears as an orange dot, newline as LF (Line Feed) and null as NUL.
You can see these characters as they reside in the file buffer in memory too on RARS as shown below. If you go to Execute window after running this modified Lab3.asm, selecting 0x1004000 for view and enabling ASCII view, you will get the following screenshot:

Figure 15 “* ** * ” data as it resides in our file buffer
Note that within each individual cell in the Data Segment matrix above, we should read the bytes from right to left.
NOTE: For your student starter code, you MUST NOT use any of the following registers: a0, a1, a7, t6, sp. Using the registers t0 through t5 should be enough for Lab3 assignment.
Example of running the actual correct Lab3.asm source code
The following is a screenshot showing the runtime of the actual solved Lab3.asm code:

Figure 16 Solved Lab3.asm runtime demo
When we open the generated lab3_output.txt file, you should get :

Figure 17 lab3_output.txt screenshot
Your student code MUST display the prompts and error messages in response to user input EXACTLY as shown in Figure 16. Please make use of the provided strings in the .data section of the starter code in Lab3.asm to make sure you do not use any other type of sentence!
NOTE: Although you are not required to print each row in the pattern on your output console, doing so
(as shown in Figure 16 ) will greatly help in the real time code debugging. So, it is strongly advised to do so.
Test Cases
The Lab3 folder in the Google Drive contains some test cases in testCases sub folder for the case when user input was n=1, 3, 6, 9, 30. Make sure your code output generates the exact same alignment of characters as provided there for the corresponding n input in your student code.
Automation
Note that our grading script is automated, so it is imperative that your program’s output matches the specification exactly. The output that deviates from the spec will cause point deduction.
Files to be submitted to your Lab3 gradescope portal
Lab3.asm
-This file contains your pseudocode and assembly code. Include a header comment as indicated in the documentation guidelines here.
You should be doing this assignment completely all by yourself!
Grading Rubric (100 points total)
The following rubric applies provided you have fulfilled all criteria in Minimum Submission Requirements. Failing any criteria listed in that section would result in an automatic grade of zero which cannot be legible for applying for a regrade request.
20 pt Lab3.asm assembles without errors (so even if you submit Lab3.asm having written absolutely no student code, you would still get 20 pts!)
80 pt output in file lab3_output.txt matches the specification:
20 pt error check zero and negative heights using the convention shown in Figure 16
20 pt prompts user until a correct input is entered as shown in Figure 16
20 pt number of rows match user input (i.e., if n=6, the pattern would have 6 row
20 pt correct sequence of stars and dollars and ‘0’s on each row
Legal Notice
In the case of sites such as Chegg.com, we have been able to locate course material shared by a previous quarter student. Chegg cooperated with us by providing the student's contact details, which was sufficient proof of the student's misconduct leading to an automatic failing grade in the course.

More products