CA I - Chapter 2 ISA 2 RISC V
CA I - Chapter 2 ISA 2 RISC V
CA I - Chapter 2 ISA 2 RISC V
[Adapted from Computer Organization and Design, RISC-V Edition, Patterson & Hennessy, © 2018, MK]
[Adapted from Great ideas in Computer Architecture (CS 61C) lecture slides, Garcia and Nikolíc, © 2020, UC Berkeley]
1/29/2024 1
RV32 So Far…
• Add/sub
add rd, rs1, rs2
• Branching
sub rd, rs1, rs2 beq rs1, rs2, Label
bne rs1, rs2, Label
• Add immediate bge rs1, rs2, Label
addi rd, rs1, imm blt rs1, rs2, Label
bgeu rs1, rs2, Label
• Load/store bltu rs1, rs2, Label
lw lb j Label
lbu rd,
sw rs1, rs2, imm
sb rs1, rs2, imm
1/29/2024 2
RISC-V Logical Instructions
• Useful to operate on fields of bits within a word
• e.g., characters within a word (8 bits)
• Operations to pack /unpack bits into words
• Called logical operations
C Java RISC-V
Logical operations operators operators instructions
Bit-by-bit AND & & and
Bit-by-bit OR | | or
Bit-by-bit XOR ^ ^ xor
Shift left logical << << sll
Shift right logical >> >> srl
1/29/2024
RISC-V Logical Instructions
• Always two variants
• Register: and x5, x6, x7 # x5 = x6 & x7
• Immediate: andi x5, x6, 3 # x5 = x6 & 3
• Used for ‘masks’
• andi with 0000 00FFhex isolates the least significant byte
• andi with FF00 0000hex isolates the most significant byte
1/29/2024 4
Logical Shifting
• Shift Left Logical (sll) and immediate (slli):
slli x11,x12,2 #x11=x12<<2
• Store in x11 the value from x12 shifted by 2 bits to the left (they
fall off end), inserting 0’s on right; << in C.
• Before: 0000 0002hex
0000 0000 0000 0000 0000 0000 0000 0010two
• After: 0000 0008hex
0000 0000 0000 0000 0000 0000 0000 1000two
• What arithmetic effect does shift left have?
• Shift Right: srl is opposite shift; >>
1/29/2024 5
Arithmetic Shifting
• Shift right arithmetic (sra, srai) moves n bits to the right
(insert high-order sign bit into empty bits)
• For example, if register x10 contained
1111 1111 1111 1111 1111 1111 1110 0111two= -25ten
• If execute srai x10, x10, 4, result is:
1111 1111 1111 1111 1111 1111 1111 1110two= -2ten
• Unfortunately, this is NOT same as dividing by 2n
• Fails for odd negative numbers
• C arithmetic semantics is that division should round towards 0
1/29/2024 6
Assembler to Machine Code (More Later inCourse)
foo.S bar.S Assembler source files (text)
1/29/2024 7
How Program is Stored
Memory
Program
Bytes
One RISC-V Instruction = 32 bits
Data
1/29/2024 8
RISC-V (63)
Program Execution
Processor Memory
▪ PC (program counter)
Control Read is a register internal to
Instruction
the processor that
Program
holds byte address of
Datapath next instruction to be
Program Counter (PC)
Instruction
Address
executed
Bytes
Registers
Data
Arithmetic-Logic
1/29/2024 10
RISC V Function calls
main() {
int i,j,k,m;
... What information mus
i = mult(j,k); ... compiler/programmer
m = mult(i,i); ... keep track of?
/* really dumb mult function */
int mult (int mcand, int mlier){
int product = 0;
while (mlier > 0) { What instructions can
product = product + mcand; accomplish this?
mlier = mlier -1; }
return product;
}
1/29/2024 11
Six Fundamental Steps in Calling a Function
1. Put arguments in a place where function can access
them
2. Transfer control to function
3. Acquire (local) storage resources needed for function
4. Perform desired task of the function
5. Put return value in a place where calling code can
access it and restore any registers you used; release
local storage
6. Return control to point of origin, since a function can
be called from several points in a program
1/29/2024 12
RISC-V Function Call Conventions
• Registers faster than memory, so use them
• a0–a7 (x10-x17): eight argument registers to pass
parameters and two return values (a0-a1)
• ra: one return address register to return to the point of
origin (x1)
• Also s0-s1 (x8-x9) and s2-s11 (x18-x27):
saved registers (more about those later)
1/29/2024 13
Instruction Support for Functions (1/4)
... sum(a,b);... /* a,b:s0,s1 */
}
int sum(int x, int y) {
C
return x+y;
}
address (shown in decimal)
1000
1004
RISC-V
1/29/2024 18
Summary of InstructionSupport
Actually, only two instructions:
▪ jal rd, Label – jump-and-link
▪ jalr rd, rs, imm – jump-and-link register
1/29/2024 19
RISC-V Instruction Representation
High Level Language
Program (e.g., C)
lw x3, 0(x10) Anything can be represented
Assembly Language lw x4, 4(x10) as a number,
sw x4, 0(x10)
Program (e.g., RISC-V) sw x3, 4(x10)
i.e., data or instructions
Assembler
Machine Language
Program (RISC-V)
Reg[rs1]
1
ALU DMEM
alu
pc+4
inst[31:7]
Imm.
Logic Circuit Description Gen
imm[31:0]
20
1/29/2024
Instructions as Numbers(1/2)
▪ Most data we work with is in words (32-bit chunks):
Each register is a word
lw and sw both access memory one word at a time
▪ So how do we represent instructions?
Remember: Computer only understands 1s and 0s, so assembler
string “add x10,x11,x0” is meaningless to hardware
RISC-V seeks simplicity: since data is in words, make instructions be
fixed-size 32-bit words also
Same 32-bit instructions used for RV32, RV64, RV128
1/29/2024 21
Instructions as Numbers(2/2)
▪ One word is 32 bits, so divide instruction word into “fields”
▪ Each field tells processor something about instruction
▪ We could define different fields for each instruction, but RISC-V
seeks simplicity, so define six basic types of instruction formats:
R-format for register-register arithmetic operations
I-format for register-immediate arithmetic operations and loads
S-format for stores
B-format for branches (minor variant of S-format)
U-format for 20-bit upper immediate instructions
J-format for jumps (minor variant of U-format)
1/29/2024 22
R-Format Instruction Layout
Field’s bit positions
31 25 24 20 19 1514 1211 76 0
funct7 rs2 rs1 funct3 rd opcode
7 5 5 3 5 7
Name of field
Number of bits in field
1/29/2024 23
R-Format Instructions opcode/funct Fields
31 25 24 20 19 1514 12 11 76 0
funct7 rs2 rs1 funct3 rd opcode
7 5 5 3 5 7
opcode: partially specifies what instruction it is
Note: This field is equal to 0110011two for allR-Format register-register
arithmetic instructions
funct7+funct3: combined with opcode, these two fields describe
what operation to perform
▪ Question: You have been professing simplicity, so why aren’t opcode
and funct7 and funct3 a single 17- bit field?
We’ll answer this later
1/29/2024 24
R-Format Instructions Register Specifiers
31 25 24 20 19 15 14 12 11 76 0
funct7 rs2 rs1 funct3 rd opcode
7 5 5 3 5 7
1/29/2024 25
R-Format Example
31 25 24 20 19 1514 1211 76 0
funct7 rs2 rs1 funct3 rd opcode
7 5 5 3 5 7
31 25 24 20 19 1514 1211 76 0
0000000 01010 10011 000 10010 0110011
7 5 5 3 5 7
1/29/2024 26
Your Turn
▪ What is correct encoding of add x4, x3, x2 ?
1) 4021 8233hex
2) 0021 82b3hex
3) 4021 82b3hex
4) 0021 8233hex
5) 0021 8234hex
31 2524 2019 15 14 12 11 7 6 0
1/29/2024 29
I-Format Instruction Layout
31 25 24 20 19 15 14 12 11 76 0
imm[11:0] rs1 funct3 rd opcode
12 5 3 5 7
▪ Only one field is different from R-format, rs2 and funct7 replaced by
12-bit signed immediate, imm[11:0]
▪ Remaining fields (rs1, funct3, rd, opcode) same as before
▪ imm[11:0] can hold values in range [-2048ten , +2047ten]
▪ Immediate is always sign-extended to 32-bits before use in an arithmetic
operation
▪ We’ll later see how to handle immediates > 12 bits
1/29/2024 30
I-Format Example
1/29/2024 31
All RV32 I-format ArithmeticInstructions
imm[11:0] rs1 000 rd 0010011 addi
imm[11:0] rs1 010 rd 0010011 slti
imm[11:0] rs1 011 rd 0010011 sltiu
imm[11:0] rs1 100 rd 0010011 xori
imm[11:0] rs1 110 rd 0010011 ori
imm[11:0] rs1 111 rd 0010011 andi
0000000 shamt rs1 001 rd 0010011 slli
0000000 shamt rs1 101 rd 0010011 srli
0100000 shamt rs1 101 rd 0010011 srai
One of the higher-order immediate bits “Shift-by-immediate” instructions only use
is used to distinguish “shift right lower 5 bits of the immediate value for shift
logical” (SRLI) from “shift right amount (can only shift by 0-31 bit positions)
arithmetic” (SRAI)
1/29/2024 32
Load Instructions are also I-Type
31 20 19 1514 1211 76 0
imm[11:0] rs1 funct3 rd opcode
12 5 3 5 7
offset[11:0] base width dest LOAD
1/29/2024 33
I-Format Load Example
▪ RISC-V Assembly Instruction:
lw x14, 8(x2)
31 20 19 1514 1211 76 0
imm[11:0] rs1 funct3 rd opcode
12 5 3 5 7
offset[11:0] base width dest LOAD
1/29/2024 34
All RV32 Load Instructions
imm[11:0] rs1 000 rd 0000011 lb
imm[11:0] rs1 001 rd 0000011 lh
imm[11:0] rs1 010 rd 0000011 lw
imm[11:0] rs1 100 rd 0000011 lbu
imm[11:0] rs1 101 rd 0000011 lhu
funct3 field encodes size and
‘signedness’ of load data
▪ lbu is “load unsigned byte”
▪ lh is “load halfword”, which loads 16 bits (2 bytes) and sign- extends to fill
destination 32-bit register
▪ lhu is “load unsigned halfword”, which zero-extends 16 bits to fill destination 32-bit
register
▪ There is no ‘lwu’ in RV32, because there is no sign/zero extension needed when copying 32
bits from a memory location into a 32- bit register
1/29/2024 35
S-Format Used for Stores
31 25 24 20 19 1514 12 11 76 0
Imm[11:5] rs2 rs1 funct3 imm[4:0] opcode
7 5 5 3 5 7
▪ Store needs to read two registers, rs1 for base memory address,
and rs2 for data to be stored, as well immediate offset!
▪ Can’t have both rs2 and immediate in same place as other
instructions!
▪ Note that stores don’t write a value to the register file, no rd!
▪ RISC-V design decision is to move low 5 bits of immediate to
where rd field was in other instructions – keep rs1/rs2 fields in
same place
Register names more critical than immediate bits in hardware design
1/29/2024 36
S-Format Example
▪ RISC-V Assembly Instruction:
sw x14, 8(x2)
31 25 24 20 19 1514 12 11 76 0
Imm[11:5] rs2 rs1 funct3 imm[4:0] opcode
7 5 5 3 5 7
offset[11:5] offset[4:0]
=0 rs2=14 rs1=2 SW =8 STORE
1/29/2024 38
Translating C code into Machine Language
• If x10 has the base of the array A and x21 corresponds to h, the
assignment statement
A[30] = h + A[30] + 1;
1/29/2024 39
RISC-V Conditional Branches
1/29/2024 40
Branching Instruction Usage
▪ Branches typically used for loops (if-else,
while, for)
Loops are generally small (< 50 instructions)
Function calls and unconditional jumps handled with jump instructions
(J-Format)
▪ Recall: Instructions stored in a localized area of memory
(Code/Text)
Largest branch distance limited by size of code
Address of current instruction stored in the program counter (PC)
1/29/2024 41
PC-Relative Addressing
PC-Relative Addressing: Use the immediate field as a two’s-
complement offset to PC
Branches generally change the PC by a small amount
Can specify ± 211 ‘unit’ addresses from thePC
(We will see in a bit that we can encode 12-bit offsets as
immediates)
▪ Why not use byte as a unit of offset from PC?
Because instructions are 32-bits (4-bytes)
We don’t branch into middle of instruction
1/29/2024 42
Scaling Branch Offset
▪ One idea: To improve the reach of a single branch instruction,
multiply the offset by four bytes before adding to PC
▪ This would allow one branch instruction to reach ± 211 ×
32-bit instructions eitherside of PC
Four times greater reach than using byte offset
1/29/2024 43
Branch Calculation
▪ If we don’t take the branch:
PC = PC + 4 (i.e., next instruction)
▪ If we do take the branch:
PC = PC + immediate*4
▪ Observations:
immediate is number of instructions to jump (remember,
specifies words) either forward (+) or backwards (–)
1/29/2024 44
RISC-V B-Format for Branches
31 30 25 24 2019 15 14 12 11 8 7 6 0
imm[12] imm[10:5] rs2 rs1 funct3 imm[4:1] imm[11] opcode
1 6 5 5 3 4 1 7
1/29/2024 46
Branch Example, Determine Offset
▪ RISC-V Code: Count instructions
from branch
Loop: beq x19,x10,End 1
add x18,x18,x10 2
addi x19,x19,-1 3
j Loop 4
End: # target instruction
▪ Branch offset =
4×32-bit instructions = 16 bytes
▪ (Branch with offset of 0, branches to itself)
1/29/2024 47
Branch Example, Determine Offset
▪ RISC-V Code: Count instructions
from branch
Loop: beq x19,x10,End
add x18,x18,x10 1
2
addi x19,x19,-1
3
j Loop
4
End: # target instruction
1/29/2024 48
Branch Example, Determine Offset
▪ RISC-V Code: Offset = 16 bytes
=8x2
Loop: beq x19,x10,End 1
add x18,x18,x10 2
addi x19,x19,-1 3
j Loop 4
End: # target instruction
01000
??????? 01010 10011 000 ????? 1100011
imm rs2=10 rs1=19 BEQ imm BRANCH
1/29/2024 49
RISC-V Immediate Encoding
Instruction encodings, inst[31:0]
31 30 25 24 20 19 15 14 12 11 8 7 6 0
funct7 rs2 rs1 funct3 rd opcode R-type
imm[11:0] rs1 funct3 rd opcode I-type
imm[11:5] rs2 rs1 funct3 imm[4:0] opcode S-type
imm[12|10:5] rs2 rs1 funct3 imm[4:1|11] opcode B-type
32-bit immediates produced, imm[31:0]
31 25 24 12 11 10 5 4 1 0
-inst[31]- inst[30:25] inst[24:21] inst[20] I-imm.
-inst[31]- inst[30:25] inst[11:8] inst[7] S-imm.
-inst[31]- inst[7] inst[30:25] inst[11:8] 0
B-imm.
Upper bits sign-extended from inst[31] always Only bit 7 of instruction changes role in
1/29/2024
immediate between S and B 50
Branch Example, CompleteEncoding
1/29/2024 51
All RISC-V Branch Instructions
1/29/2024 52
Questions on PC-addressing
▪ Does the value in branch immediate field change if we
move the code?
If moving individual lines of code, then yes
If moving all of code, then no (‘position-independent code’)
▪ What do we do if destination is > 210 instructions
away from branch?
Other instructions save us
1/29/2024 53
Questions on PC-addressing
▪ Does the value in branch immediate field change if we
move the code?
If moving individual lines of code, then yes
If moving all of code, then no (‘position-independent code’)
▪ What do we do if destination is > 210 instructions
away from branch?
Other instructions save us
beq x10,x0,far
# next instr → bne x10,x0,next
j far
next: # next instr
1/29/2024 54
U-Format for “Upper Immediate”Instructions
31 12 11 76 0
imm[31:12] rd opcode
20 5 7
U-immediate[31:12] dest LUI
U-immediate[31:12] dest AUIPC
1/29/2024 56
One Corner Case
How to set 0xDEADBEEF?
lui x10, 0xDEADB # x10 = 0xDEADB000
addi x10, x10, 0xEEF # x10 = 0xDEADAEEF
1/29/2024 57
Solution
How to set 0xDEADBEEF?
LUI x10, 0xDEADC # x10 = 0xDEADC000
1/29/2024 59
J-Format for Jump Instructions
31 30 21 20 19 12 11 76 0
imm[20] imm[10:1] imm[11] imm[19:12] rd opcode
1 10 1 8 5 7
offset[20:1] dest JAL
1/29/2024 61
JALR Instruction (I-Format)
31 20 19 15 14 12 11 76 0
imm[11:0] rs1 func3 rd opcode
12 5 3 5 7
offset[11:0] base 0 dest JALR
1/29/2024 62
Uses of JALR
# ret and jr psuedo-instructions
ret = jr ra = jalr x0, ra, 0
# Call function at any 32-bit absolute
address
lui x1, <hi20bits>
jalr ra, x1, <lo12bits>
# Jump PC-relative with 32-bit offset
auipc x1, <hi20bits>
jalr x0, x1, <lo12bits>
1/29/2024 63
Summary of RISC-V InstructionFormats
31 30 25 24 21 20 19 15 14 12 11 8 7 6 0
funct7 rs2 rs1 funct3 rd opcode R-type
imm[11:0] rs1 funct3 rd opcode I-type
imm[11:5] rs2 rs1 funct3 imm[4:0] opcode S-type
imm[12|10:5] rs2 rs1 funct3 imm[4:1|11] opcode B-type
imm[31:12] rd opcode U-type
imm[20|10:1|11]] imm[19:12] rd opcode
J-type
1/29/2024 64
Your turn
• Consider RISC V assembler code
1/29/2024 65
Complete RV32I ISA
Open Reference Card¨
Base Integer Instructions: RV32I
Not in 61C
Set < Imm Unsigned I SLTIU rd,rs1,imm
FENCE
Branches Branch = B BEQ rs1,rs2,imm Synch Synch thread I
Branch ≠ B BNE rs1,rs2,imm