Chapter - 02 - Reduced Slides
Chapter - 02 - Reduced Slides
RISC-V
2 ed. nd
Chapter 2
Instructions: Language of
the Computer
§2.1 Introduction
Instruction Set
The repertoire of instructions of a
computer
Different computers have different
instruction sets
But with many aspects in common
Early computers had very simple
instruction sets
Simplified implementation
Many modern computers also have simple
instruction sets
Chapter 2 — Instructions: Language of the Computer — 2
The RISC-V Instruction Set
Used as the example throughout the book
Developed at UC Berkeley as open ISA
Now managed by the RISC-V Foundation
(riscv.org)
Typical of many modern ISAs
See RISC-V Reference Data tear-out card
Similar ISAs have a large share of embedded
core market
Applications in consumer electronics, network/storage
equipment, cameras, printers, …
ld x9, 64(x22)
add x9, x21, x9
sd x9, 96(x22)
x x 1111...111 2 1
x 1 x
Example: negate +2
+2 = 0000 0000 … 0010two
–2 = 1111 1111 … 1101two + 1
= 1111 1111 … 1110two
RISC-V instructions
Encoded as 32-bit instruction words
Small number of formats encoding operation code
(opcode), register numbers, …
Regularity!
Instruction fields
opcode: operation code
rd: destination register number
funct3: 3-bit function code (additional opcode)
rs1: the first source register number
rs2: the second source register number
funct7: 7-bit function code (additional opcode)
add x9,x20,x21
0 21 20 0 9 51
e.g., for case/switch statements
Chapter 2 — Instructions: Language of the Computer — 37
Leaf Procedure Example
C code:
long long int leaf_example (
long long int g, long long int h,
long long int i, long long int j) {
long long int f;
f = (g + h) - (i + j);
return f;
}
Arguments g, …, j in x10, …, x13
f in x20
temporaries x5, x6
Argument n in x10
Result in x10
imm[12] imm[11]
PC-relative addressing
Target address = PC + immediate × 2
Example 2: lock
addi x12,x0,1 // copy locked value
again: lr.d x10,(x20) // read lock
bne x10,x0,again // check if it is 0
yet
sc.d x11,(x20),x12 // attempt to store
bne x11,x0,again // branch if fails
Unlock:
sd x0,0(x20) // free lock
Chapter 2 — Instructions: Language of the Computer — 58
Effect of Compiler Optimization
Compiled with gcc for Pentium 4 under Linux
1.5
0.5
0
C/ none C/ O1 C/ O2 C/ O3 J ava/ int J ava/ J IT
1.5
0.5
0
C/ none C/ O1 C/ O2 C/ O3 J ava/ int J ava/ J IT
2000
1500
1000
500
0
C/ none C/ O1 C/ O2 C/ O3 J ava/ int J ava/ J IT