0% found this document useful (0 votes)
2 views9 pages

Lab3[1]

The document outlines Lab #3 for a Computer Architecture and Assembly Language course, focusing on converting C language loops into RISC-V assembly code. It details the implementation of for and while loops, as well as tasks for matrix addition and multiplication in RISC-V. The lab aims to enhance understanding of loop structures and memory operations in assembly language programming.

Uploaded by

omarashraf13456
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views9 pages

Lab3[1]

The document outlines Lab #3 for a Computer Architecture and Assembly Language course, focusing on converting C language loops into RISC-V assembly code. It details the implementation of for and while loops, as well as tasks for matrix addition and multiplication in RISC-V. The lab aims to enhance understanding of loop structures and memory operations in assembly language programming.

Uploaded by

omarashraf13456
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

Department of Computer Science

Institute of Business Administration, Karachi

Lab #3: Loops: From C to RISC-V Assembly

Computer Architecture & Assembly Language


January 29, 2024

Course Instructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Salman Zaffar


Lab Instructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Mehwish Zafar
Week Performed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . : Week 1
Room . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . MTL4

1 Introduction
There are two goals to this laboratory work. These are the compilation and simulation of
the following C language’s statements into RISC-V assembly
1. for Loop

2. while Loop

2 for Loop
For loops are a convenient shorthand that combines the initialization, condition check, and
variable change in one place. The high-level code format of the for loop is:

f or(initialization; condition; loopoperation)statement (1)

The initialization code executes before the for loop begins. The condition is tested at the
beginning of each loop iteration. If the condition is not met, the loop exits. If the condition
is met, the statement (or statements) in the loop body are executed. The loop operation
executes at the end of each loop iteration.
For loops are especially useful for accessing large amounts of similar data stored in
memory arrays, which are discussed now. Code Example 6.21 is a grade inflation algorithm
that adds 10 points to each of the scores. The code for initializing the scores array is not

1
shown. Assume that s0 is initially 0x174300A0, the base address of the array. The index
into the array is a variable (i) that increments by 1 for each array element, so we multiply
it by 4 before adding it to the base address.

Figure 1: for Loop

3 while Loops
While loops repeatedly execute a block of code while a condition is met that is until a
condition is not met. The while loop in Code Example 6.18 determines the value of x such
that 2x = 128. It executes seven times, until pow = 128.

2
Figure 2: while Loop

Like if-else statements, the assembly code for while loops tests the opposite condition of
the one in the high-level code. If that opposite condition is TRUE (in this case, s0==128),
the while loop is finished. Otherwise, the branch isn’t taken and the loop body executes.
Code Example 6.18 initializes pow to 1 and x to 0 before the while loop. The while loop
compares pow to 128 and exits the loop if it is equal. Otherwise, it doubles pow (using a
left shift), increments x, and branches back to the start of the while loop.

3
4 Laboratory Tasks
4.1 Individual Task
1. Implement a 2x2 matrix addition with as few lines of RISC-V assembly code as pos-
sible. All the elements of two matrices are to be stored in the data (main) memory as
shown in the figure below

Figure 3: Memory Representation

2. Fill the parts of code below for Matrices Addition.

. data
matrix1 : . word 1 , 2 , 1 , 2
matrix2 : . word 1 , 2 , 1 , 2
result : . word 0 , 0 , 0 , 0

. text
# LOAD THE ADDRESSES OF ALL THREE MATRICES INTO REGISTERS ( USING la )

# INITIALIZE THE STARTING & ENDING VALUE OF INDEX ( using addi )

# STARTING FOR LOOP


for :
# loop breaking condition ( using bge )

# Calculate the value of offset ( using slli )

4
# Add the value of offset in the memory address of all three
matrices

# load the value of matrix1 & matrix2 with the updated offset (
using lw )

# Perform the addition function on matrix1 & matrix2 ( using add )

# Store addition result at the updated offset of result matrix (


using sw )

# Increment the loop by +1

done :

3. Provide a screenshot of Memory addresses along with its values used in the code.

5
4. Write the registers along with their value:

Register Value

6
4.2 Group-Based task
1. Implement a 2x2 matrix multiplication with as few lines of RISC-V assembly code as
possible. You can use any arithmetic instruction other than the base RISC-V ISA. All
the elements of two matrices are to be stored in the data (main) memory as shown in
the figure below:

Figure 4: Memory Representation

2. Fill the parts of code below for Matrices Multiplication.

. data
matrix1 : . word 1 , 2 , 1 , 2
matrix2 : . word 1 , 2 , 3 , 4
result : . word 0 , 0 , 0 , 0

. text
# WRITE THE FULL CODE BELOW :

7
# END OF CODE

3. Provide a screenshot of Memory addresses along with its values used in the code.

4. Write the registers along with their value:

Register Value

8
Figure 5: RISC-V Base ISA

You might also like