Lab5
Lab5
1 Introduction
There are two purposes of this lab. These are
1. to introduce the compilation of recursive function calling (of C language) into RISC-V
assembly.
2. to understand the assembler directives in RISC-V assembly language
1
Figure 1: Recursive Function Call
2
Figure 2: Stack: (a) before, (b) during, and (c) after factorial function call with n = 3
Figure 2(c) shows the stack as the recursively called functions return. When factorial
returns to the caller, the stack pointer is in its original position (0xFF0), none of the contents
of the stack above the pointer have changed, and all of the preserved registers hold their
original values. a0 holds the return value, 6.
3
Figure 3: Complete run-through factorial code in Code Example 6.28 for n=4
4
3 Recursive Functions with Assembler Directives
Assembler directives guide the assembler in allocating and initializing global variables, defin-
ing constants, and differentiating between code and data. Table 6.5 in the book lists common
RISC-V assembler directives. Let us discuss the assembler directives given in Figure 3 now
1. The program begins by making the main label global (.globl main) so that the main
function can be called from outside this file, typically by the OS or bootloader.
2. Next, the program allocates the following global variables A (a 7−element array of
32−byte values), B (an element), and str1 (a null−terminated string). A, B, and str1
are initialized, respectively, to 5, 42, −88, 2, −5033, 720, 314, 0x9, and “RISC−V”
(i.e., 52, 49, 53, 43, 2D, 56, 00. These global variables are stored in data segment of
the memory which starts from 0x00000 000.
3. .text specifies the beginning of user code which is stored in the text segment of main
memory which starts from 0x10000 000. This is actually the instruction memory.
5
Figure 4: Code with Assembler Directives
6
Figure 5: Asembler directives to store global variables in data segment of the main memory
4 Laboratory Tasks
Implement the 2x2 multiplication with all the elements of Matrices A, B and resultant in
the data segment of the main memory. Use Recursive Function Call to implement Matrix
multiplication.
7
1. Fill the parts of code below for Recursive Matrix Multiplication.
. data
matrix1 : . word 1 , 2 , 1 , 2
matrix2 : . word 1 , 2 , 1 , 2
result : . word 0 , 0 , 0 , 0
. text
main :
# LOAD THE ADDRESSES OF ALL THREE MATRICES INTO REGISTERS ( USING la )
# USE s0 , s1 , & s2 registers for loading addresses of matrices .
# INITIALIZE THE STARTING & ENDING INDEX OF FOR LOOP ( using addi )
for :
Recursive_Multiplcation :
8
# Matrix Multipli cation Code
# Increment the sp by 16
# return
done :
9
2. Provide a screenshot of Memory Address section that depicts the values of input and
resultant matrices. Highlight it in the screenshot.
Register Value
ra
sp
gp
s0
s1
s2
10
Figure 7: RISC-V Base ISA
11
12
Figure 8: Complete Single-Cycle RISC-V Microarchitecture