IT3030E CA Chap3 Instruction Set Architecture
IT3030E CA Chap3 Instruction Set Architecture
[with materials from COD, RISC-V 2nd Edition, Patterson & Hennessy 2021,
M.J. Irwin’s presentation, PSU 2008,
The RISC-V Instruction Set Manual, Volume I, ver. 2.2]
IT3030E, Fall 2024 2
Content
❑ Introduction
❑ RISC-V Instruction Set Architecture
l Operands
l Instruction set (basic RV32I variant)
l RISC-V instruction formats
l Other RISC-V instructions
add a, b, c #a b + c
❑ Equivalent C code
f = (g + h) – (i + j)
❑ RISC-V operands
l Registers
l Memory
l Constant/Immediate
Byte =Byte
8 bits
Halfword= 2 bytes
Halfword
Word =Word
4 bytes
Doubleword = 8 bytes
Doubleword
Register File
32 bits
5 32 src1
Read ports src1 addr
5 data
addresses src2 addr 32 Read ports
5 locations
dst addr data
Write ports
32 src2
address and 32
write data data
data
write control
IT3030E, Fall 2024 9
RISC-V Register Convention
❑ RISC-V: load/store machine
❑ Data processing done on registers inside CPU
Byte 0x0f
❑ Byte addressable
address 0x0e
Word 3 ❑ Words are accessed
(32 bit) 0x0d
0x0c via byte address
0x0b
❑ Only accessible via
0x0a load/store instructions
Word 2
0x09
0x08
0x07
0x06
Word 1 Data alignment
0x05
0x04 word address = 4 * word
0x03
number
0x02
Word 0 RISC-V does not require
0x01
data alignment, but it is
0x00
strongly recommended.
➔ handled by compiler
Byte 0x0f
Is this optimized to
address 0x0e
Word 3 declare a struct in C like
(32 bit) 0x0d
this?
0x0c
0x0b Struct data
0x0a {
Word 2
0x09 char x;
0x08 short y;
0x07 int z;
0x06 }
Word 1
0x05
0x04 Aligned Data
0x03 • Primitive data type requires K
0x02 bytes
Word 0 • Address must be multiple of K
0x01
0x00
address value
X 68
X+1 1B
X+2 5D
X+3 FA
❑ 6 instruction formats
Fig. 2.1
Ex: if (i==j)
h = i + j;
start:
addi s0, zero, 2 #load value for s0
addi s1, zero, 2
addi s3, zero, 0
beq s0, s1, Exit
add s3, s2, s1
Exit: add s2, s3, s1
.end start
Solution
slti t0,s1,5 # i<5? (inverse condition)
beq t0,zero,else # if i>=5 goto else part
addi t1,zero,3 # X = 3
j endif # skip the else part
else: addi t1,zero,10 # X = 10
endif:...
❑ Examples
0 21 20 0 9 51
❑ Examples
❑ Examples
bne t0, zero, else How can CPU jump from here
to the “else” label?
addi t1, zero, 3
j endif
endif:...
Solution
Loop:
slli x10, x22, 2 #i*4
add x10, x10, x25 #A[i] address
lw x9, 0(x10) #A[i] value
bne x9, x24, Exit #break if != k
addi x22, x22, 1 #next element
beq x0, x0, Loop
Exit: …
Lo
var
Sa
reg
$sp c
b Frame for
a current
.. procedure
.
$fp
Before calling
IT3030E, Fall 2024 51
Stack structure
❑ Return address:
l Address of the next instruction right after call
main
Prepare
to call
PC jal proc
Prepare
to continue proc
Save, etc.
Restore
jrx0, $ra
jalr 0(x1)
Question: how can the CPU resume the main program execution?
int Q(int i)
{
int t = 3*i;
int v[10];
•
•
return v[t];
}
Stack Frame
❑ Current Stack Frame (“Top” to
Bottom)
l “Argument build:”
Parameters for function about to call
l Local variables Caller
If can’t keep in registers Frame
Arguments
l Saved register context 9+
l Old frame pointer (optional) Frame pointer Return Addr
fp Old fp
(Optional)
Saved
❑ Caller Stack Frame Registers
+
l Return address Local
- Pushed by jal instruction Variables
l Arguments for this call
Argument
Stack pointer Build
sp (Optional)
IT3030E, Fall 2024 60
Six Steps in the Execution of a Procedure
1. Main routine (caller) places parameters in a place
where the procedure (callee) can access them
l a0 – a7 (x10 – x17): 8 argument registers
leaf_example:
addi sp, sp, -12 # room for 3 items
sw t1, 8(sp) # save t1
sw t0, 4(sp) # save t0
sw s0, 0(sp) # save s0
add t0, a0, a1 # t0 = g+h
add t1, a2, a3 # t1 = i+j
sub s0, t0, t1 # s0 = (g+h)-(i+j)
add a0, s0, zero # return value in a0
lw s0, 0(sp) # restore s0
lw t0, 4(sp) # restore t0
lw t1, 8(sp) # restore t1
addi sp, sp, 12 # deallocate
jalr zero, 0(ra) # return to caller
❑ How about:
l Other instruction word length?
l Data other than integers?
l Additional operations: multiplication, division…?