2 0 Riscv-Isa-A
2 0 Riscv-Isa-A
2 0 Riscv-Isa-A
Module Outline
●
RISC-V ISA
●
Addressing modes, Instruction classes
●
High level languages, Assembly languages
and object code.
●
Translating and starting a program.
●
Subroutine and subroutine call. Use of stack
for handling subroutine call and return.
Module Outline
●
RISC-V ISA
●
Addressing modes, Instruction classes
●
High level languages, Assembly languages
and object code.
●
Translating and starting a program.
●
Subroutine and subroutine call. Use of stack
for handling subroutine call and return.
Instruction Set Architecture
●
Instruction Set: A vocabulary of commands
understood by an architecture
Instruction Set Architecture
●
Instruction Set: A vocabulary of commands
understood by an architecture
●
ISA includes:
– All available instructions (Instruction Set)
– How are operands specified (Addressing modes)
– What does each instruction look like (Instruction
format)
Instruction Set Architecture
●
Instruction Set: A vocabulary of commands
understood by an architecture
●
ISA includes:
– All available instructions (Instruction Set)
– How are operands specified (Addressing modes)
– What does each instruction look like (Instruction
format)
●
ISA design goal:
– Find a language that makes it easy to build the
hardware and compiler while maximizing
performance and minimizing cost.
Types of Instructions
●
Arithmetic and Logic Instructions
–
●
Data Transfer Instructions
–
–
Types of Instructions
●
Arithmetic and Logic Instructions
– add, subtract, multiply, divide, compare (int/fp)
– or, and, xor, not
– shift (left/right, arithmetic/logical), rotate
●
Data Transfer Instructions
–
–
Types of Instructions
●
Arithmetic and Logic Instructions
– add, subtract, multiply, divide, compare (int/fp)
– or, and, xor, not
– shift (left/right, arithmetic/logical), rotate
●
Data Transfer Instructions
– load (copy data to a register from memory)
– store (copy data to a memory location from a
register)
Types of Instructions
●
Control transfer instructions
–
●
System instructions
–
Types of Instructions
●
Control transfer instructions
– jump, conditional branch, function call, return
●
System instructions
–
Types of Instructions
●
Control transfer instructions
– jump, conditional branch, function call, return
●
System/Privileged instructions
– scall, eret, ...
Types of Instructions
●
Control transfer instructions
– jump, conditional branch, function call, return
●
System/Privileged instructions
– scall, eret, ...
– to access system functionality, might require
privileged access
Types of Instructions
●
Control transfer instructions
– jump, conditional branch, function call, return
●
System/Privileged instructions
– scall, eret, ...
●
Atomic, Counters, FP conversion, ...
RISC-V Instruction Set
●
Pronounced “risk-five”
●
Created from 2010 in UC Berkely
The RISC-V Instruction Set Manual, Volume I: User-Level ISA, v2.2. https://riscv.org/
RISC-V Instruction Set
●
Pronounced “risk-five”
●
Created from 2010 in UC Berkely
●
An open ISA
●
An ISA supporting extensive user-level ISA
extensions and specialized variants.
The RISC-V Instruction Set Manual, Volume I: User-Level ISA, v2.2. https://riscv.org/
RISC-V Instruction Set
●
Pronounced “risk-five”
●
Created from 2010 in UC Berkely
●
An open ISA
●
An ISA supporting extensive user-level ISA
extensions and specialized variants.
●
An ISA with support for highly-parallel multicore
or manycore implementations, including
heterogeneous multiprocessors.
The RISC-V Instruction Set Manual, Volume I: User-Level ISA, v2.2. https://riscv.org/
RISC-V Instruction Set
●
Base Int ISA: RV64I (64b registers)
– RV32I: Int ISA with 32b registers
RISC-V Instruction Set
●
Base Int ISA: RV64I (64b registers)
– RV32I: Int ISA with 32b registers
●
M: Integer multiplication and division extension.
RISC-V Instruction Set
●
Base Int ISA: RV64I (64b registers)
– RV32I: Int ISA with 32b registers
●
M: Integer multiplication and division extension
●
A: Atomic instruction extension.
– Atomically read, modify, and write memory for inter-
processor synchronization.
RISC-V Instruction Set
●
F: Single-precision floating-point extension
– FP registers, SP computational instructions, and SP
loads and stores.
●
D: Double precision
RISC-V Instruction Set
●
Base Int ISA: RV64I (64b registers)
– RV32I: Int ISA with 32b registers
●
G: IMAFD. General purpose Scalar Instruction
set.
– RV64G
RV64I
●
Fixed-length 32 bit instructions
●
RV64I
●
Fixed-length 32 bit instructions
●
31 general-purpose integer registers x1–x31
– Register x0 is hardwired to the constant 0
RV64I
●
Fixed-length 32 bit instructions
●
31 general-purpose integer registers x1–x31
– Register x0 is hardwired to the constant 0
●
Registers are 64 bits wide
– 32 bits wide in RV32I
●
RV64I
●
Fixed-length 32 bit instructions
●
31 general-purpose integer registers x1–x31
– Register x0 is hardwired to the constant 0
●
Registers are 64 bits wide
– 32 bits wide in RV32I
●
Program counter pc holds the address of the
current instruction.
RV64I Logical Instructions
Mnemonics Example Meaning
AND
OR
XOR
RV64I Logical Instructions
Mnemonics Example Meaning
OR OR, ORI
OR OR, ORI
●
NOT a =
RV64I Logical Instructions
Mnemonics Example Meaning
●
NOT a = a XOR (-1)
RV64I Arithmetic Instructions
Mnemonics Example Meaning
●
opcode: Basic operation of the instruction
●
rd: Register destination operand. Gets the result.
●
funcx8: An additional opcode field.
●
rs1: The first register source operand.
●
rs2: The second register source operand.
●
funct7: An additional opcode field.
RV64I Instruction Encoding
●
add x5, x6, x7
●
Instruction Fields
31 0
●
rs1: 6, rs2: 7, rd: 5
RV64I Instruction Encoding
●
add x5, x6, x7
●
Instruction Fields
31 0
●
Word version operates on lower32 bits only
●
Arithmetic shift replicates sign bit
RV64I Shift Instructions
Mnemonics Example Meaning
●
Word version operates on lower32 bits only
●
Arithmetic shift replicates sign bit
RV64I Shift Instructions
Mnemonics Example Meaning
●
Word version operates on lower32 bits only
●
Arithmetic shift replicates sign bit
RV64I – Data Transfer Instructions
Mnemonics Example Meaning
Memory x8
0x100
...
...
...
0x2D8A10EF 0x104
...
RV64I Data Transfer Instructions
Mnemonics Example Meaning
Memory x8
0x100
x7
...
... 0xFFFF FFFF FFFF FFEF
...
0x2D8A10EF 0x104
...
RV64I Data Transfer Instructions
Mnemonics Example Meaning
Memory x8
0x100
x7
...
... 0x0000 0000 0000 00EF
...
0x2D8A10EF 0x104
...
RV64I Data Transfer Instructions
Mnemonics Example Meaning
Memory x8
0x100
x7
...
... 0x0000 0000 0000 00EF
...
0x2D8A10EF 0x104
...
RV64I Data Transfer Instructions
Mnemonics Example Meaning
I Format
Encoding Example
●
From the RV64 ISA Manual
Encoding Example
●
From the RV64 ISA Manual
ld x2, 4(x28)
Encoding Example
●
From the RV64 ISA Manual
ld x2, 4(x28)
ld x2, 4(x28)
op rd, immed(rs1)
ld x2, 4(x28)
op rd, immed(rs1)
●
imm: 12-bit, signed offset in multiples of 2
●
Target address = pc + imm
●
Branch range is ±4 KiB.
– imm range = 0x001 to 0xFFF
0000 0000 0000 1111 1111 1111
RV64I Branch Instructions
Mnemonics Example Meaning
●
imm: 12-bit, signed offset in multiples of 2
●
Target address = pc + imm
●
Branch range is ±4 KiB.
– imm range = 0x001 to 0xFFF
– Branch range = 0x0 to 0x1FFF.
0 0000 0000 0000 1 1111 1111 1110
RV64I Branch Instructions
Mnemonics Example Meaning
●
Eg. Branch forward to the 16th instruction.
What is the immediate value?
●
beq x4, x5, ___
– pc = 0x0100
RV64I Branch Instructions
Mnemonics Example Meaning
●
Eg. Branch forward to the 16th instruction.
What is the immediate value?
●
beq x4, x5, ___ 0x0100 beq x4, x5, ___
add # 1st after beq
or
– pc = 0x0100 ...
...
...
...
xor ... # 16th. Branch here.
RV64I Branch Instructions
Mnemonics Example Meaning
●
Eg. Branch forward to the 16th instruction.
What is the immediate value?
●
beq x4, x5, ___ 0x0100
0x0104
beq x4, x5, ___
add # 1st after beq
or
– pc = 0x0100 ...
...
...
...
0x0140 xor ... # 16th. Branch here.
RV64I Branch Instructions
Mnemonics Example Meaning
●
Eg. Branch forward to the 16th instruction.
What is the immediate value?
●
beq x4, x5, ___ 0x0100
0x0104
beq x4, x5, ___
add # 1st after beq
or
– pc = 0x0100 ...
...
...
...
0x0140 xor ... # 16th. Branch here.
0001 0100 0000
RV64I Branch Instructions
Mnemonics Example Meaning
●
Eg. Branch forward to the 16th instruction. What is the
immediate value?
●
beq x4, x5, ___ 0x0100 beq x4, x5, ___
– pc = 0x0100 0x0104 add # 1st after beq
or
●
(pc – target address) = 0x40 ...
...
...
...
0x0140 xor ... # 16th. Branch here.
0001 0100 0000
RV64I Branch Instructions
Mnemonics Example Meaning
●
Eg. Branch forward to the 16th instruction. What is the
immediate value?
●
beq x4, x5, ___ 0x0100 beq x4, x5, ___
– pc = 0x0100 0x0104 add # 1st after beq
or
●
(pc – target address) = 0x40 ...
...
0100 0000 0010 0000 ...
>>2 ...
0x0140 xor ... # 16th. Branch here.
0001 0100 0000
RV64I Branch Instructions
Mnemonics Example Meaning
●
Eg. Branch forward to the 16th instruction. What is the immediate
value?
●
beq x4, x5, ___
0x0100 beq x4, x5, ___
– pc = 0x0100 0x0104 add # 1st after beq
●
(pc – target address) = 0x40 or
...
●
imm = 0x20 ...
...
beq x4, x5, 32 ...
0x0140 xor ... # 16th. Branch here.
RV64I Branch Instructions
Mnemonics Example Meaning
●
Eg. Branch forward to the 16th instruction.
What is the immediate value?
0x0100 beq x4, x5, loop
● beq x4, x5, 32
0x0104 add # 1st after beq
or
...
...
...
...
0x0140 loop: xor ... # 16th. Branch here.
RV64I Branch Instructions
Mnemonics Example Meaning
●
B-type instruction format
RV64I Jump Instructions
Mnemonics Example Meaning
●
J-immediate encodes a signed offset in multiples of 2 bytes
●
Jumps target a ±1 MiB range
●
JAL stores (pc+4) into rd
●
Calling convention uses x1 as the return address register
RV64I – Load Upper Immediate
Mnemonics Example Meaning
Load lui lui x5, 0xabcde
Upper
lui rd,imm
Immed
RV64I – Load Upper Immediate
Mnemonics Example Meaning
Load lui lui x5, 0xabcde
Upper
lui rd,imm
Immed
20
20bit
bitimmediate
immediate
value
value
RV64I – Load Upper Immediate
Mnemonics Example Meaning
Load lui lui x5, 0xabcde
Upper
lui rd,imm
Immed
●
Used to build 32-bit constants
●
Places the immediate value in the top 20 bits (bits 31–12) of
the destination register, filling in the lowest 12 bits with
zeros.
●
32-bit result is sign-extended to 64 bits
RV64I – Load Upper Immediate
Mnemonics Example Meaning
Load lui lui x5, 0xabcde
Upper
lui rd,imm
Immed
63 31 0
x5 0x1234
0x1234ABCD
ABCD0000
0000789A
789A
RV64I – Load Upper Immediate
Mnemonics Example Meaning
Load lui lui x5, 0xabcde
Upper
lui rd,imm
Immed
63 31 0
0x1234
0x1234ABCD
ABCD0000
0000789A
789A
0x1234
0x1234ABCD
ABCDABCD
ABCDE000
E000
RV64I – Load Upper Immediate
Mnemonics Example Meaning
Load lui lui x5, 0xabcde
Upper
lui rd,imm
Immed
63 31 0
0x1234
0x1234ABCD
ABCD0000
0000789A
789A
0x1234
0x1234ABCD
ABCDABCD
ABCDE000
E000
0xFFFF
0xFFFFFFFF
FFFFABCD
ABCDE000
E000
RV64I – Load Upper Immediate
Mnemonics Example Meaning
Load lui lui x5, 0xabcde
Upper
lui rd,imm
Immed
63 32 0
x5 0001001000110100
00010010001101001010101111001101
10101011110011010000000000000000
00000000000000000111
01111000
10001001
10011010
1010
0x1234
0x1234ABCD
ABCD0000
0000789A
789A
Bit 11
63 0
x5 1111111111111111
11111111111111111111111111111111
11111111111111111010101111001101
10101011110011011110
11100000
00000000
00000000
0000
0xFFFF
0xFFFFFFFF
FFFFABCD
ABCDE000
E000
RV64I – System Instructions
●
SCALL
– Make a request to the operating system
environment.
RV64I – System Instructions
●
SCALL
– Make a request to the operating system
environment.
●
SBREAK
– Used by debuggers to cause control to be
transferred back to the debugging environment.
Arithmetic Overflow
111101
+
111110
Arithmetic Overflow
111101 -3
+ +
111110 -2
Arithmetic Overflow
C 1111
111101 -3
+ +
111110 -2
111011 -5
Arithmetic Overflow
C 1111
111101 -3 000001 1
+ + + +
111110 -2 011111 31
111011 -5
Arithmetic Overflow
C 1111 C 11111
111101 -3 000001 1
+ + + +
111110 -2 011111 31
111011 -5 100000 - 32
Arithmetic Overflow
C 1111 C 11111
111101 -3 000001 1
+ + + +
111110 -2 011111 31
111011 -5 100000 - 32
100000 -32
+ +
111111 -1
Arithmetic Overflow
C 1111 C 11111
111101 -3 000001 1
+ + + +
111110 -2 011111 31
111011 -5 100000 - 32
C 1
100000 -32
+ +
111111 -1
011111 31
Arithmetic Overflow
C 1111 C 11111
111101 -3 000001 1
+ + + +
111110 -2 011111 31
111011 -5 100000 - 32
C 1 ●
MSB0 = 0 && MSB1 =
100000 -32 0 && MSBofResult = 1
+ +
111111 -1 ●
MSB0 = 1 && MSB1 =
011111 31 1 && MSBofResult = 0
Module Outline
●
RISC-V ISA
●
Addressing modes, Instruction classes
●
High level languages, Assembly languages
and object code.
●
Translating and starting a program.
●
Subroutine and subroutine call. Use of stack
for handling subroutine call and return.
Extras
RV64I Compare Instructions
Mnemonics Example Meaning
●
Build pc-relative addresses
●
Forms a 32-bit offset from the 20-bit U-
immediate, fills lowest 12 bits with zeros, adds
to the pc, places result in register rd.
RV64I – LUI and AUIPC Encoding
Mnemonics Example Meaning
Load lui lui x5, 0xabcde
Upper
Immed
Add auipc auipc x5,
upper 0xabcde
immed auipc
to pc rd,imm
RV64I Branch Instructions
Mnemonics Example Meaning