Chapter 02 2
Chapter 02 2
n RISC-V instructions
n Encoded as 32-bit instruction words
n Small number of formats encoding operation code
(opcode), register numbers, …
n Regularity!
29
Hexadecimal
n Base 16
n Compact representation of bit strings
n 4 bits per hex digit
30
n Instruction fields
n opcode: operation code
n rd: destination register number
n funct3: 3-bit function code (additional opcode)
n rs1: the first source register number
n rs2: the second source register number
n funct7: 7-bit function code (additional opcode)
31
R-format Example
funct7 rs2 rs1 funct3 rd opcode
7 bits 5 bits 5 bits 3 bits 5 bits 7 bits
add x9,x20,x21
0 21 20 0 9 51
32
33
34
35
Example
If x10 has the base of the array A and x21
corresponds to h, the assignment statement
A[30] = h + A[30] + 1;
is compiled into …
36
40
Check Yourself
n What RISC-V instruction does this
represent?
funct7 rs2 rs1 funct3 rd opcode
32 9 10 0 11 51
41
42
Shift Operations
n Shift left logical
n Shift left and fill with 0 bits
n sll by i bits multiplies by 2i
n Shift right logical
n Shift right and fill with 0 bits
n srl by i bits divides by 2i (unsigned only)
n Shift right arithmetic
n Shift right and fill with copies of old sign bit
n sra
43
AND Operations
n Useful to mask bits in a word
n Select some bits, clear others to 0
and x9,x10,x11
44
OR Operations
n Useful to include bits in a word
n Set some bits to 1, leave others unchanged
or x9,x10,x11
45
XOR Operations
n Differencing operation
n Creates a 0 when bits are the same and a 1
otherwise.
xor x9,x10,x12 // NOT operation
46
Conditional Operations
n Branch to a labeled instruction if a condition is
true
n Otherwise, continue sequentially
47
Compiling If Statements
n C code:
if (i==j) f = g+h;
else f = g-h;
n f, g, … in x19, x20, …
n Compiled RISC-V code:
bne x22, x23, Else
add x19, x20, x21
beq x0,x0,Exit // unconditional
Else: sub x19, x20, x21
Exit: …
Assembler calculates addresses
Chapter 2 — Instructions: Language of the Computer — 48
48
49
Basic Blocks
n A basic block is a sequence of instructions
with
n No embedded branches (except at end)
n No branch targets (except at beginning)
50
51
52
53