From 59b44d333f2525bdddf576517d24bdf9a766cb57 Mon Sep 17 00:00:00 2001 From: Rauni Date: Tue, 30 Jan 2024 23:53:42 -0500 Subject: [PATCH] lab1 --- lab1/lab1.md | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lab1/lab1.s | 33 +++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 lab1/lab1.md create mode 100644 lab1/lab1.s diff --git a/lab1/lab1.md b/lab1/lab1.md new file mode 100644 index 0000000..99df418 --- /dev/null +++ b/lab1/lab1.md @@ -0,0 +1,81 @@ +# Find GCD (Greatest Common Divisor) + +**Deadline:** Sunday, 02/04/2024. + +*If you work on a lab computer or UConn AnyWare Desktop, save your files to +cloud storage like OneDrive. Otherwise, you may lose your files.* + +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 recommend 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 by the deadline. + +## Tips + +* It can be faster to use keyboard shortcuts to assemble/run/reset the program. + +* Use breakpoints and backsteps to debug. diff --git a/lab1/lab1.s b/lab1/lab1.s new file mode 100644 index 0000000..c37d6ea --- /dev/null +++ b/lab1/lab1.s @@ -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