Lecture 6 - RISC-V Instruction Set Overview (Notes)
Lecture 6 - RISC-V Instruction Set Overview (Notes)
Peter Cheung
Imperial College London
URL: [Link]/pcheung/teaching/EE2_CAS/
E-mail: [Link]@[Link]
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 1
In this lecture, the instruction set architecture (ISA) of the RISC-V processor will be
introduced. We will only consider the base instruction set for the 32-bit integer
version of the ISA. Focus will be on the six different instruction types with emphasis
each instruction’s functionality and encoding of its machine code.
ISA of a processor does not dictate how the processor is implemented. It only
defines how to the processor is programmed. The actual hardware architecture will
be covered in the next lecture.
1
RISC-V
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 2
RISC-V is by no means the first or the only Reduced Instruction Set Computer that
enjoys widespread adoption. The early RISC processor from Berkeley, the MIPS, was
used for over four decades in industry. The UK’s ARM processor (original stands for
Acorn Risc Machine) is the most manufactured CPU in history with an estimated 200
billions being shipped to date. However, RISC-V is the first widely accepted open-
source RISC processor. Opening the ISA to the public (royalty free) is a new business
model and has captured much attention in the past 5 years. Together with the free
toolchain for development and many open-source design freely available, RISC-V is
expected to pose real competition to x86 and ARM architecture to become at least
one of the dominant play in the sector.
RISC-V was developed by Krste Asanovic and Dave Patterson (and others) in
Berkeley in early 2010’s. The ISA was first published in 2011, and its future
development and ratification are under the control of RISC-V Foudation and RISC-V
International located in Switzerland.
2
Design Principles
H&H p301-303
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 3
As shown in later slides, the instruction set has highly consistent format. The base
instruction set is known as RV32I (RISC-V 32-bit integer only) only has 40
instructions. The ISA has two sources and one destination operands. The format of
the instructions are divided into only six different types.
The second principle is the RISC-V only has commonly used instructions. Yet, it is
Turing Complete, meaning that it can be used to implement any computer
algorithms. This makes the RISC-V implementation both small and fast. Complex
operations are achieved by stringing together multiple instructions.
3
Instructions: Addition & Subtraction
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 4
To understand the RV32I ISA, we start with the add and subtract instructions as
shown in this slide. The diea is very simple – it shows that for add instructions, we
need two source operands (b and c) and one destimation (a).
4
RISC-V Operands
H&H p304
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 5
Where do operands come from and where does the destination operand go?
There are three possibilities. The fastest and most often used operand is from or to
registers on the CPU chip itself. RISC-V RV32I has 32 32-bit registers. They form an
integral part of the CPU design and accessing them is easy and fast. 32 registers
means the instruction must use 3 x 5-bit = 15 bit of the 32-bit instruction to specify a
register-only (R-type) instruction.
The second possibility is from data memory. To access this, the instruction must
specify the data memory address, using a register as a pointer. In RISC-V, the
register content is often specified with an associated offset constant value as part of
the instruction.
The third possibility is from instruction memory, i.e. the operand is a constant within
the instruction itself. In ”RISC-V speak”, this is called an immediate.
5
32-bit RISC-V Instruction Types
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 6
6
RISC-V Registers
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 7
Having x1 to x31 for any general use can be confusing. Common good
practice is included in a guideline where specific registers are used for special
functions. For example x1 is used to store the return address (of a
subroutine) and therefore x1 is also called ra.
The table above shows the various aliases for all 32 registers. You are
recommend o use the given name of these registers to make the program
more readable. For example instead of using x0, you should always refer to it
as zero.
7
RISC-V operand from Registers
a = b + 6; # s0 = a, s1 = b
addi s0, s1, 6
H&H p305
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 8
Consider again the add instructions. ADD is a typical ALU instruction in the class of
arithematic and logic operations. It needs two source operands and one destination
operands to store the results. Shown here is the instruction: add s0, s1, s2
which uses three registers. Consider the encode of this instructions (slide 21).
The operation is specified with the opcode, funct3 and funct7 fields of the instructions.
opcode = 7’h38 (51), funct3 = 3’b0, funct7 = 7’b0.
rd: s0 = x8 = 5’b01000, rs1 = s1 = x9 = 5’b01001, rs2 = s2 = x18 = 5’b10010
If we fill in the fields with these values according the diagram here, we get:
8
Therefore this instruction has a machine code of 32’h00648413.
8
RISC-V operands from memory
• Each 32-bit data word has a unique address
00000004 C D 1 9 A 6 5 B !"#A%0
00000003 4 0 F 3 0 7 8 8 !"#A%,
00000002 0 1 E E 2 8 4 2 !"#A%-
00000001 F 2 F 1 A C 0 7 !"#A%.
00000000 A B C D E F 7 8 !"#A%/
!"#$%&'&(&)*$+,
H&H p307
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 9
9
RISC-V Byte-addressable Memory
13 12 11 10 00000010 C D 1 9 A 6 5 B !"#AB4
F E D C 0000000C 4 0 F 3 0 7 8 8 !"#AB,
B A 9 8 00000008 0 1 E E 2 8 4 2 !"#AB-
7 6 5 4 00000004 F 2 F 1 A C 0 7 !"#AB.
3 2 1 0 00000000 A B C D E F 7 8 !"#ABL
23M 53M
!"#$%&'&(&)*$+,
Based on: “Digital Design and Computer Architecture (RISC-V Edition)”
by Sarah Harris and David Harris (H&H),
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 10
10
Reading Byte-Addressable Memory
13 12 11 10 00000010 C D 1 9 A 6 5 B !"#AB4
F E D C 0000000C 4 0 F 3 0 7 8 8 !"#AB,
B A 9 8 00000008 0 1 E E 2 8 4 2 !"#AB-
7 6 5 4 00000004 F 2 F 1 A C 0 7 !"#AB.
3 2 1 0 00000000 A B C D E F 7 8 !"#ABL
23M 53M
Based on: “Digital Design and Computer Architecture (RISC-V Edition)” !"#$%&'&(&)*$+,
by Sarah Harris and David Harris (H&H),
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 11
11
Writing Byte-Addressable Memory
• Example: store the value held in t7 into memory address 0x10 (16)
– if t7 holds the value 0xAABBCCDD, then after the sw completes,
word 4 (at address 0x10) in memory will contain that value
RISC-V assembly code
sw t7, 0x10(zero) # write t7 into address 16
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 12
12
RISC-V: Operands from Constants
• 12-bit signed constants (immediates) using addi:
C Code RISC-V assembly code
// int is a 32-bit signed word # s0 = a, s1 = b
int a = -372; addi s0, zero, -372
int b = a + 6; addi s1, s0, 6
372 = 12’h174 = 12’b0001_0111_0100
-372 = 12’b1110_1000_1100 = 12’hE8B
• Form 32-bit constant using sign extension
12’hE8B
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 13
13
RISC-V: Operand with 32-bit Constants
H&H p306
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 14
14
RISC-V: 32-bit Constants (bit 11 is 1)
C Code
int a = 0xFEDC8EAB; Note: -341 = 0xEAB
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 15
Fortunately the assembly and compiler for RISC-V take care of this
automatically.
15
RISC-V: Psuedoinstruction
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 16
RISC-V has many instructions missing deliberately to make is small and fast.
More complex operations are accomplished by multiple instructions or by an
instruction that result in the same operation.
For example there is no instruction to load a register with a constant value.
To load s0 with the small constant 6, we use the instruction:
addi s0, zero, 6
This makes the assembly language program of RISC-V much harder to read
and understand. Fortunately, RISC-V assembler understand a number of
pseudo instructions. These instructions do not exist in the RISC-V ISA, but are
translated into equivalent RV32I instructions.
To load a register with a constant of any size constant (up to 32 bits), one can
use the “load immediate” li pseudoinstruction.
li s0, 6
li s0, 0xFEDC9
Slide 29 shows all the pseudo instructions that RISC-V assembler accepts.
16
RISC-V: Addressing Modes
Immediate
• 12-bit signed immediate used as an operand
– Example: addi s4, t5, -73
– Example: ori t3, t7, 0xFF
H&H p340
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 17
17
RISC-V: Base + Offset Addressing
Base Addressing
• Loads and Stores
• Address of operand is:
base address + immediate
– Example: lw s4, 72(zero)
• address = 0 + 72
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 18
Base addressing mode uses one of the registers content as the address into
memory. What stored in the register is not the actual operand, but it stores
the address of the operand. In C++, we call this a pointer - it points to the
place where the operand is stored.
In RISC-V, Base addressing is always used with an offset value which must be
a 12-bit 2’s complement immediate constant. The ”load” and “store”
instrutions use this mode of addressing.
18
RISC-V: PC-relative Addressing
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 19
For example, the above ”branch if not equal” instruction compares s8 and s9
contents. If they are NOT the same, then the PC counter is load with the
address of L1, which is 0x354.
How is the value 0x354 encoded in the instruction? The immediate constant
is calculated with the value of PC for the bne instruction, which ix 0xEB0. The
offset is calculated by 0xEB0 – 0x354 = 0xB5C. Therefore the stored
immediate value is therefore the value -2908.
19
RISC-V: Instruction coding for Branch offset
x24, x25,
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 20
The way that RISC-V encodes the relative offset of -2908 is complicated and
appears illogical. In fact the design decision for this instruction is very clever
and is aimed at making the hardware implementation as simple as possible.
Here are the design constraints that determine how the instruction is
encoded:
1. It uses the same fields for opcode (7 bits), funct3 (3 bits), rs1 and rs2 (5
bits) as other instructions. This means that 20 bits of the 32-bit
instructions are already used. So there are 12 bits left for encoding the
offset.
2. Since the branch destination is ALWAYS an instruction address, and that
RISC-V uses byte-addressable memory, the instructions for RV32I is
ALWAYS aligned to 4. In other words, there is no need to store the bottom
2 bits of the offset – they are always zero. However, there is a variant of
RISC-V ISA which targets microcontroller, where the among of program
memory is limited. The “Compressed” extension of RISC-V ISA includes
16-bit instructions (i.e. packing two instructions into a 32-bit word).
Therefore, the instruction address can be an increment of 2 instead of 4,
meaning that only bit 0 is always 0.
3. It is convenient in hardware that the bits used for encoding B-type
immediate values should be similar to that used for I-type and S-type
instructions. Therefore the locations of bits are the same for imm[4:1],
imm[10:5]. However, the branch immediate is 13 bits instead of 12 bits,
therefore imm[12] now takes the place of imm[11] in other case. They
are both sign bits.
4. Since imm[0] is always 0, there is no need to store it. Instead imm[11] is
20
1. stored here!
20
R-type Instructions: 3 register instructions
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 21
This an the next few slides are summary of ALL the 40 instructions in RISC-V
RV32I ISA.
Here is the R-type instructions that perform arithmetic and logical operations
using three registers. They all share the opcode of 51 decimal (or 0x33). The
funct3 and funct7 fields defines the specific operation.
21
I & S-type Instructions: All involve imm constants
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 22
This group includes two instruction types which both require TWO register
operands and one 12-bit immediate operands.
The S-type instructions does not require a destination register because the
destination is data memory. However they require two source registers, one
contain the value to write to memory, and a second has the base address of
the destination. The 12-bit immediate offset is split into two parts, using the
funct7 field of instr[31:25] and the rd field of instr[11:7], combined to form
imm[11:0].
22
B-type Instructions: PC-relative Branches
H&H p311
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 23
When implementing B-type instruction in hardware, one could use the ALU
to perform the comparison, or create special branch unit which provides
performs ONLY the comparison and no other operations and generates all the
required conditions. The second option makes the design cleaner.
23
U & I -type Instructions: Upper & Jump/Link
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 24
Finally there are four special instructions that are not in the other category.
We have already discussed the lui instruction previously.
The U-type instructions are used to manipulate the upper 20-bit of a register
to handle 32-bit immediate constants.
The J-type instructions are for function or subroutine calls. They will be
discussed in a later lecture.
24
RISC-V Arithmetic instructions
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 25
The next few slides provide a catalogue of all the RISC-V RV32I instructions in
various groups.
25
RISC-V Logic instructions
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 26
26
RISC-V Load/Store instructions
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 27
These instructions perform data memory read and write operations using
pointer address in register and an immediate offset.
27
RISC-V Branch & Jump instructions
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 28
These are the branch and jump instructions involving offset to the Program
Counter.
28
RISC-V Psuedoinstructions
PYKC 28 Oct 2025 EIE2 Instruction Architectures & Compilers Lecture 6 Slide 29
These are all the pseudo instructions accepted by the RISC-V assembler but
are not really RISC-V instructions in the ISA. They are translated by the RISC-
V assembler to one or more RISC-V instructions to make the program more
readable.
29