Lab04 CA
Lab04 CA
CS Program
Habib University
Spring 2020
1 Introduction
In this lab we will learn about:
1. RISC-V Jump and Return instructions
2 Function calls
Functions, or procedures as they are sometimes called, are an important part of programming.
They help avoid repeating the same code all over our programs. A function call would take
the program flow to the start of the function code. That code will be executed and a return-
from-function instruction would take the program flow back to where originally the function was
called from and continue the execution from the next instruction after the function call.
Like all instruction sets, RISC-V provides special instructions to enable function calls and
return from functions.
Listing 1 lists the jal and jalr instructions used for function calls and returns from functions
respectively in RISC-V.
Executing jal instruction would make the program flow jump to func name label in the code
while at the same time storing the address of the next instruction PC+4 in the register x1. The
return address needs to be stored so that the program flow can be able to go back to where the
function call was made in order to continue execution from there onwards once it has finished
executing the function code. Since a function can be called from multiple locations in a program,
without saving the return address we will not have a clue as to where to return to.
The jalr instruction is used to return from functions. The jump target address is provided
in a register. The example shown in Listing 1, it will jump to the address stored in the register
x1. Combining this with jal instruction we can see how the two instructions work in tandem
to provided the functionality of calling a function and returning from it.
1
RISC-V Registers
Register ABI Description Saved by
Name
x0 zero zero register(hardwired zero)
x1 ra return address Caller
x2 sp stack pointer Callee
x3 gp global pointer
x4 tp thread pointer
x5-7 t0-2 temporary registers Caller
x8 s0 / fp saved register / frame pointer Callee
x9 s1 saved register Callee
x10-11 a0-1 function arguments / return values Caller
x12-17 a2-7 function arguments Caller
x18-27 s2-11 saved registers Callee
x28-31 t3-6 temporary registers Caller
Another issue associated with function calls is passing the parameters and returning the re-
sult. As can be seen from Table 1, the RISC-V convention reserves the registers x10-x17 for
passing parameters to a function when calling it. The caller code would fill in these registers the
parameters needed by a function before calling it. Once the function is called, it will access its
parameters from these registers, use them, and then return the result in x10 before calling the
jalr instruction which would take the control flow to the caller who can then access the result
of the function call by reading x10.
In case the registers x10-x17 contains some values which will still be used by the caller code
after the function call, the caller code needs to store these values as well as its own return address
x1 on the stack before overwriting these values with the ones needed by the callee. See section
2.8 in the book ??.
3 Environment Calls
The Venus simulator supports some system calls, which it calls Environment Calls, as well. See
the url https://github.com/kvakil/venus/wiki/Environmental-Calls. To execute an en-
vironment call we have to put its number in the register a0(x10) , any argument it might need
in a1(x11) and then call the ecall instruction. The simulator will perform the system call
specified in the a0(x10) register.
So far we have not had any use for the Console Output window in the simulator. Here it
comes.
The RISC-V environment calls can be used to print values from registers or memory in the
console window. For example, one of the supported system calls is print int whose number
is 1 and which will print any value stored in the register a1(x11) in the console output as an
integer.
2
The code in listing 2, taken from the above url, would print the integer 42 on the console
output.
4 Task 1
1 i n t sum ( i n t a , i n t b ) {
2 r e t u r n a+b ;
3 }
Listing 3: Sum
Write a code for the function sum shown in listing 3. It takes two integers a and b as arguments
and returns their sum. You should also write code that calls this function i.e. it sets up the two
arguments a and b in registers x10 and x11 and then calls sum(). After calling sum(), it then
retrieves the the returned result from the register x10. It can display the result using the ecall
instruction.
5 Task 2:
1
2 v o i d b ub bl e ( i n t ∗a , u n s i g n e d i n t l e n ) {
3
4 i f ( a==NULL | | l e n ==0)
5 return ;
6
7 f o r ( i n t i =0; i <l e n ; i ++){
8 f o r ( i n t j=i ; j <l e n ; j ++){
9 i f (a [ i ] < a [ j ]) {
10 i n t temp = a [ i ] ;
11 a[ i ] = a[ j ];
12 a [ j ] = temp ;
13 }
14 }
15 }
16
17 return ;
18 }
Listing 4: Bubble sort
Write the equivalent RISC-V assembly code for Listing 4. a and len get passed in x10 and
x11 respectively.
6 Task 3
The nth triangular number is like a factorial but instead of a product we use a summation:
https://en.wikipedia.org/wiki/Triangular_number.
1 i n t n t r i ( i n t num) {
2 i f (num<=1)
3 return 1;
4
5 r e t u r n num + n t r i (num−1) ;
6 }
Listing 5: Nth Triangular number
Translate the above C function to calculate the Nth Triangular number into RISC-V assem-
bly. You should also write the wrapper code, i.e., which calls this function with an argument
and then displays the result returned by this function.
3
Since there’s recursion going on, you will need to adjust the stack so that you store the return
address as well as any useful variables of the the caller on the stack before calling the callee.
4
Assessment Rubric
Computer Architecture Lab
Lab 04
Introduction to RISC V Assembly (Jumps and Returns)
Name: Student ID:
Points Distribution
LR 2 LR 5 AR 4
Task No.
Code Results Report Submission
Task 1 /10 /5
For description of different levels of the mapped rubrics, please refer the provided Lab Evaluation Assessment
Rubrics.
# Assessment Elements Level 1: Unsatisfactory Level 2: Developing Level 3: Good Level 4: Exemplary
Points 0-1 Points 2 Points 3 Points 4
LR5 Results & Plots Figures/ graphs / tables Figures, graphs and tables All figures, graphs, tables Figures / graphs / tables
are not developed or are are drawn but contain are correctly drawn but are correctly drawn and
poorly constructed with errors. Titles, captions, contain minor errors or appropriate titles/captions
erroneous results. Titles, units are not accurate. some of the details are and proper units are
captions, units are not Data presentation is not missing. mentioned. Data
mentioned. Data is too clear. presentation is
presented in an obscure systematic.
manner.
AR4 Report Submission Late submission after 1 week Late submission after 2 days Late submission after the lab Timely submission of the
and in between 2 weeks. and within a week. timing and within 2 days of report and in the lab time.
the due date.