0% found this document useful (0 votes)
2 views

labtask3

The document outlines a laboratory exercise focused on converting C language loops into RISC-V assembly, specifically for 'for' and 'while' loops. It includes tasks for implementing 2x2 matrix addition and multiplication in RISC-V assembly, along with requirements for memory representation and register values. The lab is part of a Computer Architecture and Assembly Language course at the Institute of Business Administration, Karachi.

Uploaded by

omarashraf13456
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

labtask3

The document outlines a laboratory exercise focused on converting C language loops into RISC-V assembly, specifically for 'for' and 'while' loops. It includes tasks for implementing 2x2 matrix addition and multiplication in RISC-V assembly, along with requirements for memory representation and register values. The lab is part of a Computer Architecture and Assembly Language course at the Institute of Business Administration, Karachi.

Uploaded by

omarashraf13456
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

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:

for(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
shown. Assume that s0 is initially 0x174300A0, the base address of the array. The index into

1
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.
4 Laboratory Tasks
4.1 Individual Task
1. Implement a 2x2 matrix addition with as few lines of RISC-V assembly code as possible.
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

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

4
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
T0 268435456

T1 268435472

T2 268435488

T3 4

T4 4

T5 12

T6 2

S0 2

S1 4

S2 268435468

S3 268435484

S4 268435500

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

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

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

9
4. Write the registers along with their value:
Register Value
T0 0x00000002

T1 0x00000002

T2 0x00000008

T3 0x00000002

T4 0x00000004

S0 0x10000000

S1 0x10000010

S2 0x10000020

S3 0x10000008

S4 0x10000014

S5 0x00000002

S6 0x00000004

S7 0x00000002

S8 0x00000008

S9 0x0000000a

S10 0x1000002c

10
Figure 5: RISC-V Base ISA

11

You might also like