Chapter 02 RISC V
Chapter 02 RISC V
RISC-V
Edition
The Hardware/Software Interface
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)
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 — 38
Leaf Procedure Example
C code:
int leaf_example (
int g, int h,
int i, int j)
{
int f;
f = (g + h) - (i + j);
return f;
}
Arguments g, …, j in x10, …, x13
f in x20
temporaries x5, x6
Need to save x5, x6, x20 on stack
Chapter 2 — Instructions: Language of the Computer — 39
Leaf Procedure Example
RISC-V code:
leaf_example:
addi sp,sp,-12 Save x5, x6, x20 on stack
sw x5,8(sp)
sw x6,4(sp)
sw x20,0(sp
add x5,x10,x11 x5 = g + h
add x6,x12,x1 x6 = i + j
sub x20,x5,x6 f = x5 – x6
addi x10,x20,0 copy f to return register
lw x20,0(sp) Resore x5, x6, x20 from stack
lw x6,4(sp)
lw x5,12(sp)
addi sp,sp,12
jalr x0,0(x1) Return to caller
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 — 59
§2.12 Translating and Starting a Program
Translation and Startup
Static linking
Indirection table
Linker/loader code
Dynamically
mapped code
Simple portable
instruction set for
the JVM
Compiles
Interprets
bytecodes of
bytecodes
“hot” methods
into native
code for host
machine
li x19,0 // i = 0
for1tst:
bge x19,x11,exit1 // go to exit1 if x19 ≥ x11 (i≥n)
addi x19,x19,1 // i += 1
j for1tst // branch to test of outer loop
exit1:
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