RISC-V Procedures
CS61C
UC Berkeley�Teaching Professor �Dan Garcia
cs61c.org
Great Ideas
in�Computer Architecture
(a.k.a. Machine Structures)
Garcia, Kao
RISC-V Procedures (1)
Garcia, Kao
C Functions
main() {�int i,j,k,m;�...�i = mult(j,k); ... �m = mult(i,i); ...
}
/* really dumb mult function */
int mult (int mcand, int mlier){�int product = 0;�while (mlier > 0) {� product = product + mcand;� mlier = mlier - 1; }�return product;
}
What information must�compiler/programmer �keep track of?
What instructions can
accomplish this?
RISC-V Procedures (2)
Garcia, Kao
Six Fundamental Steps in Calling a Function
RISC-V Procedures (3)
Garcia, Kao
RISC-V Function Call Conventions
RISC-V Procedures (4)
Garcia, Kao
Instruction Support for Functions (1/4)
... sum(a,b);... /* a,b:s0,s1 */� }� int sum(int x, int y) {� return x+y;� }
address (shown in decimal)� 1000 � 1004 � 1008 � 1012 � 1016 � …� 2000 � 2004
C
RISC-V
In RISC-V, all instructions are 4 bytes, and stored in memory just like data. �So, here we show the addresses of where the programs are stored.
RISC-V Procedures (5)
Garcia, Kao
Instruction Support for Functions (2/4)
... sum(a,b);... /* a,b:s0,s1 */� }� int sum(int x, int y) {� return x+y;� }
address (shown in decimal)� 1000 mv a0,s0 # x = a� 1004 mv a1,s1 # y = b � 1008 addi ra,zero,1016 #ra=1016� 1012 j sum #jump to sum� 1016 … # next inst.
…� 2000 sum: add a0,a0,a1� 2004 jr ra #new instr.“jump reg”
C
RISC-V
RISC-V Procedures (6)
Garcia, Kao
Instruction Support for Functions (3/4)
... sum(a,b);... /* a,b:s0,s1 */� }� int sum(int x, int y) {� return x+y;� }
C
RISC-V
…� 2000 sum: add a0,a0,a1� 2004 jr ra #new instr.“jump reg”
RISC-V Procedures (7)
Garcia, Kao
Instruction Support for Functions (4/4)
RISC-V Procedures (8)
Garcia, Kao
RISC-V Function Call Instructions
(really should be laj “link and jump”)
jal rd, FunctionLabel
RISC-V Procedures (9)
Garcia, Kao
Summary of Instruction Support
Actually, only two instructions:
# rd = pc+4; pc = R[rs1]+imm
j, jr and ret are pseudoinstructions!
RISC-V Procedures (10)
Garcia, Kao
Where Are Old Register Values Saved to Restore Them After Function Call?
RISC-V Procedures (11)
Garcia, Kao
Stack
frame
frame
frame
frame
$sp
0xFFFFFFF0
RISC-V Procedures (12)
Garcia, Kao
RISC-V
Function Call Example
RISC-V Procedures (13)
Garcia, Kao
Function Call Example
int Leaf (int g, int h, int i, int j)
{
int f;
f = (g + h) – (i + j);
return f;
}
RISC-V Procedures (14)
Garcia, Kao
RISC-V Code for Leaf()
Leaf: addi sp,sp,-8 # adjust stack for 2 items
sw s1, 4(sp) # save s1 for use afterwards
sw s0, 0(sp) # save s0 for use afterwards
add s0,a0,a1 # f = g + h
add s1,a2,a3 # s1 = i + j
sub a0,s0,s1 # return value (g + h) – (i + j)
lw s0, 0(sp) # restore register s0 for caller
lw s1, 4(sp) # restore register s1 for caller
addi sp,sp,8 # adjust stack to delete 2 items
jr ra # jump back to calling routine
int Leaf (�int g, �int h, �int i, �int j)
{
int f;
f = (g + h) –� (i + j);
return f;
}
RISC-V Procedures (15)
Garcia, Kao
Stack Before, During, After Function
sp
Before call
sp
Saved s1
During call
Saved s0
sp
After call
Saved s1
Saved s0
RISC-V Procedures (16)
Garcia, Kao
Nested Calls and �Register Conventions
RISC-V Procedures (17)
Garcia, Kao
What If a Function Calls a Function? Recursive Function Calls?
RISC-V Procedures (18)
Garcia, Kao
Nested Procedures
int sumSquare(int x, int y) {� return mult(x,x)+ y;�}
Need to save sumSquare return address before call to mult – again, use stack
RISC-V Procedures (19)
Garcia, Kao
Register Conventions (1/2)
RISC-V Procedures (20)
Garcia, Kao
Register Conventions (2/2)
To reduce expensive loads and stores from spilling and restoring registers, RISC-V function-calling convention divides registers into two categories:
RISC-V Procedures (21)
Garcia, Kao
RISC-V Symbolic Register Names
Register | ABI Name | Description | Saver |
x0 | zero | Hard-wired zero | - |
x1 | ra | Return address | Caller |
x2 | sp | Stack pointer | Callee |
x3 | gp | Global pointer | - |
x4 | tp | Thread pointer | - |
x5 | t0 | Temporary/Alternate link register | Caller |
x6-7 | t1-2 | Temporaries | 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 | Temporaries | Caller |
Human-friendly symbolic names in assembly code
Numbers hardware understands
RISC-V Procedures (22)
Garcia, Kao
And in Conclusion, the RV32 So Far…
add rd, rs1, rs2
sub rd, rs1, rs2
and rd, rs1, rs2
or rd, rs1, rs2
xor rd, rs1, rs2
sll rd, rs1, rs2
srl rd, rs1, rs2
sra rd, rs1, rs2
addi rd, rs1, imm
subi rd, rs1, imm
andi rd, rs1, imm
ori rd, rs1, imm
xori rd, rs1, imm
slli rd, rs1, imm
srli rd, rs1, imm
srai rd, rs1, imm
lw rd, rs1, imm
lb rd, rs1, imm
lbu rd, rs1, imm
sw rs1, rs2, imm
sb rs1, rs2, imm
beq rs1, rs2, Label
bne rs1, rs2, Label� bge rs1, rs2, Label� blt rs1, rs2, Label� bgeu rs1, rs2, Label� bltu rs1, rs2, Label� jal rd, Label� jalr rd, rs, imm
10 RISC-V Procedures (23)
Garcia, Kao
RISC-V Procedures (23)
Garcia, Kao