Skip to content
Permalink
59b44d333f
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time

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 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.