Cs61c 2022fa l12 Risc v Formats II
Cs61c 2022fa l12 Risc v Formats II
Great Ideas
UC Berkeley
in UC Berkeley
Teaching Computer Architecture Teaching
Professor (a.k.a. Machine Structures) Professor
Dan Garcia Lisa Yan
Garcia, Yan
cs61c.org
12-RISC-V Instruction Formats, Part II (1)
Disassembling the S-Format (Last
lecture’s
Slide 33)
What is the corresponding assembly
instruction? Store opcode
31 25 24 20 19 15 14 12 11 7 6 0
imm[11:5] rs2 rs1 funct3 imm[4:0] opcode
0000001 01110 00010 010 00100 0100011
rs2 rs1
(“source”) (“base”)
imm (“offset”)
14 2
The two “read”
0000001 00100 reg. fields for S-
+36ten sw rs2, imm(rs1) type and R-type
are at the same bit
sw x14, 36(x2) positions. Garcia, Yan
Program
Address Space +4
Addres Machine PC
s Code Original Assembly Code
0000 0014
0x0c 00A98863 Loop: beq x19,x10,End
0x10 00A90933 add x18,x18,x10 RegFile
0x14 FFF98993 addi x19,x19,-1 x10 0000 0001
x18 0000 0001
0x18 FF5FF06F j Loop x19 0000 0002
0x1c 00A98863 End: … Garcia, Yan
PC + 4
12-RISC-V Instruction Formats, Part II (8)
PC-Relative Addressing
PC-Relative Addressing: Supply a signed offset to update
PC.
▫ PC = PC + byte_offset
0x0c Loop: beq x19,x10,End 0x400 Loop: beq x19,x10,End
▫ “Position-Independent Code”: If all of
Take branch: code moves, relative offsets
0x10 add x18, x18,x10 0x404 add x18, x18,x10
don’t
0x14 change!
addi x19, x19,-1 PC = PC + 16 0x408 addi x19, x19,-1
0x18 j Loop Don’t take branch: 0x40c j Loop
0x1c End: … PC = PC + 4 0x410 End: …
rs2 rs1
10 19 Garcia, Yan
31 25 24 20 19 15 14 12 11 7 6 0
imm[12|10:5] rs2 rs1 funct3 imm[4:1|11] opcode
yxxxxxx wwwwv
B-type imm[12|10:5]
imm[12|10:5] rs2
rs2 rs1
rs1 funct3
funct3 imm[4:1|11]
imm[4:1|11] opcode
opcode
zxxxxxx wwwwy
▫ Instruction Bit 31 is always the Between S and B, only two bits change
sign bit (highest bit to sign extend meaning:
in immediate) Instruction Bit 7 S: Imm Bit 0;
▫ B-type has a 13-bit immediate RISC-V immediateB:bit encoding
Imm Bit 11
encoding b/c of implicit zero in is optimized
Instruction to
Bitreduce
31 S: Imm Bit
Imm bit 0. 11; B: Imm
hardware cost.Bit 12 Garcia, Yan
Garcia, Yan
31 25 24 20 19 15 14 12 11 7 6 0
imm[20|10:5] imm[4:1,11] imm[19:12] rd opcode
7 5 8 5 7
“Destination”
Register
Immediate represents relative offset in increments of 2 gets “return
bytes. address.”
▫ To compute new PC: PC = PC + byte_offset. rd = PC + 4
▫ 20 immediate bits imply ±218 32-bit instructions
reachable:
▫ 1 bit: 2’s complement (allow +/– offset), 1 bit: half-word/16-b
What about jumping
instruction support further ? Garcia, Yan
“Destination”
Register
Immediate represents upper 20 bits of a 32-bit
Saves result of
immediate
operation.
operand imm 31= immed << 12.
12 11 0
imm[31:12] 0x00 00 00
20 12
Long Immediate (“imm“)
Garcia, Yan
Garcia, Yan
Garcia, Yan
31 20 19 15 14 12 11 7 6 0
imm[11:0] rs1 funct3 rd opcode
12 that load 5 3 5 7