Labtask 5
Labtask 5
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.
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,
defining 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 0x00000000.
3. .text specifies the beginning of user code which is stored in the text segment of main
memory which starts from 0x10000000. 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
8
9
2. Provide a screenshot of Memory Address section that depicts the values of input and
resultant matrices. Highlight it in the screenshot.
10
3. Write the values of the mentioned registers in HEX form:
Register Value
ra 0x0000002C
sp 0x7FFFFFDC
gp 0x10000000
s0 0x10000000
s1 0x10000010
s2 0x10000020
11
12
Figure 8: Complete Single-Cycle RISC-V Microarchitecture