CAO-Fall-2024-Lecture-04-Instruction-Set-Architecture-RISC-V-Machine-Language-Microarchitecture
CAO-Fall-2024-Lecture-04-Instruction-Set-Architecture-RISC-V-Machine-Language-Microarchitecture
Lecture # 04
RISC-V Instruction Set Architecture – Machine Language and
Microarchitecture
Muhammad Imran
[email protected]
Acknowledgement
2
▪ Machine Language
▪ Instructions Encoding and Microarchitecture
▪ Arithmetic, Logical and Shift
▪ Data Transfer & Upper Immediate
▪ Program Control Instructions
▪ Summary of Instruction Formats
Machine Language
Encoding Instructions in Machine Language
5
▪ Overall design philosophy: Split the 32 bits into "chunks" for each
component of an instruction, and try to overlap these chunks as
much as possible to simplify the underlying circuit.
▪ Arithmetic
▪ Example
▪ add x5, x6, x7
▪ Data Transfer
▪ Example
▪ lw x5, 40(x6) # load word
▪ ld x5, 40(x6) # load doubleword (in 64-bit architecture)!
▪ Example
▪ add x9, x20, x21
31 0
0 21 20 0 9 51 Decimal
0000000 10101 10100 000 01001 0110011
Binary
0000 0001 0101 1010 0000 0100 1011 0011
015A04B3 Hex
Arithmetic Instructions – Machine Language
11
-1 2 0 1 19
111111111111 00010 000 00001 0010011
1111 1111 11111 0001 0000 0000 1001 0011
FFF10093
Arithmetic Instructions – Microarchitecture
12
opcode / func3
/ func7
Control
RegWrite = 1 +
rs1
ALU
rs2
Register File
rd
Arithmetic Instructions – Microarchitecture
13
RegWrite = 1
+
rs1
ALU
Register File
Sign Extension
12-bit value to 32-bit value
rd
(preserve signed number)
Example
1111 = -1
11111111 = -1 Sign
immediate[11:0]
Extension
Logical and Shift Instructions – Machine Language
14
R-Type I-Type
opcode /
Control
opcode / func3 func3 / func7
/ func7
Control
rs1
rs1 ALU
ALU
Register File
rs2
Register File rd
rd
Sign
immediate[4:0]
Extension
Load Instruction – Machine Language
16
▪ Example
▪ lw x9, 240(x10)
31 0
240 10 2 9 3
000011110000 01010 010 01001 0000011
0000 1111 0000 0101 0010 0100 1000 0011
0F052483
Load Instruction – Microarchitecture
17
RegWrite = 1 + MemRead = 1
rs1
ALU
Register File
Data Memory
rd
Sign
immediate[11:0]
Extension
Store Instruction – Machine Language
18
▪ Example
▪ sw x9, 240(x3)
▪ Which is rs1 and which is rs2?
▪ rs1 is same as in load (base address register)!
▪ 240 = 000011110000 = [7 16] (in two parts)
31 0
7 9 3 2 16 35
0000111 01001 00011 010 10000 0100011
0000 1110 1001 0001 1010 1000 0010 0011
0E91A823
Store Instruction – Microarchitecture
20
MemRead MemWrite
=1
RegWrite = 0 + =0
rs1
rs2 ALU
Register File
Data Memory
Sign
immediate[11:0]
Extension
Can we have RegWrite = x (don’t care)? Do we need ReadMem?
Conditional Branch Instructions – Machine Language
21
▪ Example
▪ 80012: bne x9, x24, Exit # instruction is at address 80012 in memory!
▪ 80016: add x1, x2, x3
▪ 80020: sub x2, x5, x6
▪ 80024: Exit:
▪ Immediate in encoding bne x9, x24, Exit?
▪ Immediate = 12 (target is 12 (01100) bytes away or 6 (0110) halfwords away)!
Conditional Branch Instructions – Machine Language
25
▪ Example
▪ 80012: bne x9, x24, Exit # instruction is at address 80012 in memory!
▪ …
▪ 80024: Exit:
31 0
0 24 9 1 12 99
0000000 11000 01001 001 01100 1100011
0000 0001 1000 0100 1001 0110 0110 0011
01849663
Conditional Branch Instructions – Microarchitecture
26
4
PC+4
+ + 1 Branch = 1
opcode /
PC+offset Control func3
– RegWrite = 0
Instruction
Instruction
PC: PC Zero? rs1
Memory
Program ALU
Counter
rs2
Register File
Sign
immediate[12:1] <<1
Extension
Unconditional Jump Instructions – Machine Language
27
▪ Example
▪ jal x1, repeat
▪ rd = 1
▪ immediate[20:1] = number of half-words to repeat (target address)!
▪ immediate[20:0] = number of bytes to repeat (target address)!
▪ What is range of offset (bytes) to target address for jal?
▪ -220 to +220-1 bytes away from current instruction’s location!
Unconditional Jump Instructions – Machine Language
28
4
PC+4
+ + 1
Jump = 1 opcode
PC+offset Control
RegWrite = 1
Instruction
Instruction
PC rd
Memory
PC+4
Register File
Sign
immediate[20:1] <<1
Extension
Unconditional Jump Instructions – Microarchitecture
30
4
PC+4
1
+
Jump = 1 opcode /
Control func3
(ra+offset) + RegWrite = 1
Instruction
Instruction
PC rs1
Memory
ALU
Register File
rd
immediate[11:0] Sign
Extension
PC+4
LUI and AUIPC Instructions – Machine Language
31
▪ Note
▪ immediate[31:12] doesn’t mean that upper 20 bits of immediate value (as
specified in instruction) are used
▪ it means 20-bit immediate is placed in upper 20 bits (31:12) of register rd!
▪ Example
▪ lui x19, 976
▪ immediate[31:12] = 976 = 00000000 00111101 0000
▪ x19 = 00000000 00111101 00000000 00000000
LUI and AUIPC Instructions – Microarchitecture
32
▪ For loads and stores, we can add the offset first, then do a 0-offset load
(as with variable offset loads)
How to handle immediates larger than you can store
42
▪ If a jump is:
▪ Within 218 instructions?
▪ Jump normally (ex. j Label)
▪ Greater than 218 instructions?
▪ Do an auipc, then use jalr's immediate to offset the rest:
auipc t0 0x12345
jalr ra t0 0x678