Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
lab1
- Loading branch information
Jerry Shi
committed
Jan 20, 2024
1 parent
af667be
commit 61d6570
Showing
2 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Find GCD (Greatest Common Divisor) | ||
|
||
**Deadline:** Friday, 1/26/2023. | ||
|
||
We will keep the submission open until 2/2/2023. | ||
|
||
Write concise comments in code. | ||
|
||
## Learning Objectives | ||
|
||
* Write and run RISC-V programs in RARS. See | ||
[this page](https://github.com/zhijieshi/cse3666/blob/master/misc/rars.md) on | ||
how to install RARS and run RISC-V programs in RARS. | ||
|
||
* Program with RISC-V arithmetic and control flow instructions. | ||
|
||
* Read RISC-V assembly code and learn basic syntax and assembler directives | ||
like .text and .globl. | ||
|
||
* Read and learn from help files and documentations. | ||
|
||
## Description | ||
|
||
In this lab, we write a program in RISC-V assembly language that finds | ||
the GCD of two positive integers. | ||
|
||
The program reads two positive integers, and then computes and prints their | ||
GCD. The algorithm we use is Euclidean algorithm. Although it is common to use | ||
division in the algorithm, we will use subtraction only, as originally | ||
described by Euclid. The pseudocode is provided below. | ||
|
||
``` | ||
# Given two positive integers a, b, repeatedly subtract the smaller one from the larger one | ||
# until the two integers are the same. | ||
WHILE (a != b) | ||
IF (a > b) | ||
a = a - b | ||
ELSE | ||
b = b - a | ||
``` | ||
|
||
Skeleton code in `lab1.s` already has the instructions to read two integer and | ||
save them in `s1` and `s2`. Implement the following steps with RISC-V | ||
instructions to complete the program. | ||
|
||
* Compute the GCD of the two numbers with Euclidean algorithm by subtraction. | ||
We recommand the following two steps. | ||
|
||
1. Write the while loop. | ||
|
||
2. Write the if-else statement inside the loop. | ||
|
||
* Print the GCD in decimal with a system call. | ||
|
||
The list of system calls is in RARS's help file. Select menu "Help/Help" | ||
and then select "Syscalls" tab. Scroll up to the beginning of the page if | ||
necessary. **There is an example before the table.** | ||
|
||
The skeleton code also has examples of system calls, two for reading | ||
integers and one for terminating the program. Check wether you can write | ||
the instructions from the information in the system call table. With | ||
the system call for printing the result, the submitted code has four | ||
system calls. | ||
|
||
### Testing | ||
|
||
The skeleton code includes some GCD examples. Please test your code with more | ||
positive numbers. In test cases 1 - 3 in Gradespce, two integers are positive | ||
and less than 100. In test cases 4 and 5, the integers are less than 10,000. | ||
|
||
## Deliverables | ||
|
||
Please submit `lab1.s` and take lab1-test in HuskyCT by the deadline. Please | ||
check the filename if the system does not grade the submission. | ||
|
||
## Tips | ||
|
||
* It can be faster to use keyboard shortcuts to assemble/run/reset the program. | ||
|
||
* Use breakpoints and backsteps to debug. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# CSE 3666 Lab 1 | ||
|
||
.globl main | ||
|
||
.text | ||
main: | ||
|
||
# GCD examples: | ||
# gcd(11, 121) = 11 | ||
# gcd(24, 60) = 12 | ||
# gcd(192, 270) = 6 | ||
# gcd(14, 97) = 1 | ||
|
||
# read two positive integers from the console and | ||
# save them in s1 and s2 | ||
|
||
# use system call 5 to read integers | ||
addi a7, x0, 5 | ||
ecall | ||
addi s1, a0, 0 # a in s1 | ||
|
||
addi a7, x0, 5 | ||
ecall | ||
addi s2, a0, 0 # b in s2 | ||
|
||
# TODO | ||
# Add you code here | ||
# compute GCD(a, b) and print it | ||
|
||
|
||
# sys call to exit | ||
exit: addi a7, x0, 10 | ||
ecall |