# ECE154 Lab 2: Multiplication and Division in RISC-V Assembly

\$30.00

5/5 - (1 vote)

## Introduction

This project is intended to get you familiar with some of the basic operations and instructions
involved in writing an assembly program in RISC-V. You will be implementing shift-and-add
multiplication and shift-and-subtract division of *unsigned* numbers without using the explicit
multiplication and division instructions.

division algorithms please review lecture viewgraphs (slides 9, 16, 17 in week1
seq&multipliers_2023_updated.pdf slidedeck). The algorithm flowcharts are also provided
below for convenience.

Figure 1. Flowcharts for (left) shift-and-add multiplication and (right) shift-and-subtract division
algorithms.

## Overview

The goal of this project is to write a function that performs integer multiplication and a function
that performs integer division in RISC-V using base integer set of instructions (RV32I), e.g. any
instruction from reference sheet page (Figure 2).

Avoid using pseudoinstruction (that are not
real isntructions and translated to one or several real insructions upon assembly) except for “j”
jump instruction that was discussed in class and can be used instead of “jalr” which will be
discussed later. Just for a reference, see a list of pseudo-instruction (that should be avoided) in
“Assembler Pseudo-instructions” sections at https://michaeljclark.github.io/asm.html .

Note that you cannot just use add or subtract operations repeatedly.
We will use RARS RISC-V emulator https://github.com/TheThirdOne/rars . RARS is distributed
as an executable jar. You will need at least Java 8 to run it. For convenience the latest RARS
executable file is included in the lab package.

Also provided are a skeletons of code for each function. You are to write the body of each
function.

## Multiplication

For the multiplication function, start with skeleton code mult.s. You are provided the two
operands in a0 and a1 which can be up to 8-bits in length. Place the result of the multiplication in
a2. The contents of a0 and a1 must be preserved.
a2 = a0 x a1

## Division

For the division function, start with skeleton code div.s. You are provided the divisor in a0 and
the dividend in a1. Each operand can be up to 8-bits in length. The quotient of the division
should be placed in a2 and the remainder in a3. The contents of a0 and a1 must be preserved.
a2 + reminder a3 = a1/a0

Hints
The code for each function can be greatly simplified through looping. For testing, you can edit
op1 and op2 located at the top of the files. op1 is loaded into a0 and op2 is loaded into a1. Make
sure that your code covers all relevant corner cases, e.g., division by zero, etc.

Lab Report
Your lab report should be a single PDF file, which has all the following items in the following
order and clearly labeled:
1. Please indicate how many hours you spent on this lab. This will be helpful for calibrating
the workload for the next time the course is taught.

4. If you have any feedback on how we might make the lab even better for next quarter, that’s
always welcome. Please submit it in writing at the end of your lab
* For your code files, please copy and paste your code clearly in a monospace font (ex., Courier
New). Provide in the comment the equivalent C code for each added line of the code in the
skeleton.