SME
Computer Organization & Architecture
Instruction sets
Use this for “A 32-bit instruction has a 7-bit opcode, two 5-bit register fields and a 3-bit funct. What immediates fit?”
Diagram
imm 12 + rs1 5 + funct 3 + rd 5 + opcode 7 = 32 bits, the whole 32-bit word.
Smallest immediate immₘᵢₙ = −2,048
Largest immediate immₘₐₓ = 2,047
This is an example. Type your own values to start a new problem.
i (bit)
=
w (bit)
−
o (bit)
−
(
k
×
r (bit)
)
−
f (bit)
R
Registers
example
immₘᵢₙ
Smallest immediate
worked out (not typed)
immₘₐₓ
Largest immediate
calculated
Formulas
r
=
log₂(
R
)
5 = log₂(32)
i
=
w
−
o
−
k
×
r
−
f
12 = 32 − 7 − 2 × 5 − 3
immₘᵢₙ
=
−
2
i
− 1
−2,048
=
−
2
12 − 1
immₘₐₓ
=
2
i
− 1
−
1
2,047
=
2
12 − 1
−
1
Assumptions
•
Fields in RISC-V I-type order: imm, rs1, funct, rd, opcode (bit 31 on the left).
•
Each register field names one of R registers, so it needs log₂ R bits.
•
The immediate takes the bits the other fields leave, and is a two’s complement number.
Step-by-step
WE KNOW
w = 32 bit, o = 7 bit, R = 32, k = 2, f = 3 bit
FIND
register field bits (r), immediate bits (i), smallest immediate (immₘᵢₙ), largest immediate (immₘₐₓ)
Step 1 · Find register field bits (r)
Use r = log₂(R)
Each
register
needs
its
own
pattern
of
r
bits,
and
r
bits
make
2ʳ
patterns.
r
=
log₂(32)
log₂
32
=
5,
since
2
5
=
32
r
=
5
bit
Step 2 · Find immediate bits (i)
Use i = w − o − k × r − f
The immediate gets the bits the opcode, register and function fields leave.
i
=
32
−
7
−
2
×
5
−
3
i
=
32
−
7
−
10
−
3
i
=
12
bit
Step 3 · Find smallest immediate (immₘᵢₙ)
Use immₘᵢₙ = −2ⁱ⁻¹
In two’s complement the top bit weighs −2ⁱ⁻¹; with every other bit 0, that is the least value.
immₘᵢₙ
=
−(
2
i
− 1
)
immₘᵢₙ
=
−(
2
12 − 1
)
immₘᵢₙ
=
−
2
11
immₘᵢₙ = −2,048
Step 4 · Find largest immediate (immₘₐₓ)
Use immₘₐₓ = 2ⁱ⁻¹ − 1
The sign bit 0 and every other bit 1 gives the largest value.
immₘₐₓ
=
2
12 − 1
−
1
immₘₐₓ
=
2
11
−
1
immₘₐₓ = 2,048 − 1
immₘₐₓ = 2,047
Check
Put the numbers back in. Both sides should match.
5 = log₂(32) ✓
12 = 32 − 7 − 2 × 5 − 3 ✓
−2,048
=
−
2
12 − 1
✓
2,047
=
2
12 − 1
−
1
✓
Topic check
See what you know: 5 questions on this topic
New questions each time. Pass to mark it complete.