Instruction Encoding and the Immediate Formats
A line of assembly becomes one word of 32 bits. Six formats share those bits, and the register numbers sit in the same places in all of them. This page cuts real words apart.
You Will Be Able To
- Cut an instruction word into its six fields, and name its format from the opcode.
- Explain why the register numbers are in the same bits in every format.
- Build the immediate of an I and of an S instruction by hand, and make it 32 bits wide.
- Lesson
- S2.2
- Time
- 28 min
- Runs on
- Your browser
- Check
- 6 questions
A processor log shows eight hex digits beside each instruction. Beside addi x5, x5, -1 it shows fff28293. The processor never sees the text. It sees only those 32 bits, and must find in them two registers and a number.
Before You Start
You need the groups of RV32I from S2.1: which instructions name three registers, and which name two registers and an immediate.
You need binary and hex numbers. You need to know that negative numbers are stored in two's complement, where the top bit means "negative".
| Question | |
|---|---|
| 1 | Which bits of an instruction name its format? |
| 2 | Where are rd, rs1 and rs2, and why do they never move? |
| 3 | Where is the immediate of an I instruction, and of an S instruction? |
| 4 | How does an immediate of 12 bits become a value of 32 bits? |
| 5 | What goes wrong when the upper bits are filled with zeros? |
The Idea: Fixed Places for the Things Needed First
An instruction is one word of 32 bits. It must say what to do, which registers to use, and often a number. Different instructions need different mixes. An add needs three registers. A load needs two registers and an offset.
So RV32I has a few layouts for the 32 bits. A layout is called an instruction format. There are six, named by letters: R, I, S, B, U and J.
A slice of the word with one meaning is called an instruction field. This page says field for short. The field in bits 6 to 0 is the opcode. It is in every instruction, and it tells the processor which format the rest of the word has.
Here is the central idea. The processor wants to read its registers at once, before it has worked out the format. So the register numbers are in the same bits in every format that has them. The source registers are named rs1 and rs2, and the destination register is rd.
The immediate gets whatever bits are left. That is why its place changes from format to format, and why it is sometimes split in two.
New Words
| Word | Meaning |
|---|---|
| Instruction format | One of the six layouts of the 32 bits of an instruction: R, I, S, B, U or J. |
| Instruction field | A slice of the instruction word that has one meaning. Field for short. |
| Opcode | The field in bits 6 to 0. It names the format and the group of the instruction. |
| rd | The field that holds the number of the destination register: bits 11 to 7. |
| rs1 | The field that holds the number of the first source register: bits 19 to 15. |
| rs2 | The field that holds the number of the second source register: bits 24 to 20. |
| funct3 | Three more bits that pick the instruction inside a group: bits 14 to 12. |
| funct7 | Seven more bits that pick the instruction, used by the R format: bits 31 to 25. |
| Sign bit | The top bit of a number in two's complement. It is 1 for a negative number. |
| Sign extension | Making a number wider by copying its sign bit into every new upper bit. The value stays the same. |
| Zero extension | Making a number wider by filling the new upper bits with 0. A negative number changes its value. |
| Encoding | Turning a line of assembly into its word. Decoding is the way back. |
One Picture: The Word and Its Six Places
- Control / FSM
- Processor
- Bus
- Memory
- The opcode first
Bits 6 to 0 are the opcode in every instruction. It names the format, and so the meaning of every other box.
- R: three registers
An add needs rd, rs1 and rs2. No immediate. The boxes funct3 and funct7 pick the operation: add, sub, and so on.
- I: two registers and an immediate
There is no rs2. Its box and the funct7 box together hold an immediate of 12 bits.
- S: two sources and an immediate
A store has rs1 and rs2, and no rd. So the low 5 bits of the immediate move into the rd box.
- What never moves
In all three formats rs1 is in bits 19 to 15. Where there is an rs2 or an rd, it is in its own box too.
Read the diagram as text
- Bits 31 to 25: seven bits: funct7, or the top of an immediate
- Bits 24 to 20: five bits: rs2, or more of an immediate
- Bits 19 to 15: five bits: rs1
- Bits 14 to 12: three bits: funct3
- Bits 11 to 7: five bits: rd, or the low end of an immediate
- Bits 6 to 0: seven bits: the opcode
- The immediate: made 32 bits wide before it is used
- Bits 31 to 25 → The immediate: bits 11 to 5
- Bits 24 to 20 → The immediate: bits 4 to 0 (I)
- Bits 11 to 7 → The immediate: bits 4 to 0 (S)
- Bits 6 to 0 → Bits 14 to 12: names the format
Three of the six formats. B is a variation of S, and J of U. The next lesson covers U, B and J.
| Format | 31 to 25 | 24 to 20 | 19 to 15 | 14 to 12 | 11 to 7 | 6 to 0 | Used by |
|---|---|---|---|---|---|---|---|
| R | funct7 | rs2 | rs1 | funct3 | rd | opcode | add, sub, and the other eight |
| I | immediate, bits 11 to 5 | immediate, bits 4 to 0 | rs1 | funct3 | rd | opcode | addi and its group, loads, jalr |
| S | immediate, bits 11 to 5 | rs2 | rs1 | funct3 | immediate, bits 4 to 0 | opcode | stores |
| B | immediate, upper part | rs2 | rs1 | funct3 | immediate, lower part | opcode | branches |
| U | immediate | immediate | immediate | immediate | rd | opcode | lui, auipc |
| J | immediate | immediate | immediate | immediate | rd | opcode | jal |
Read down a column. No column holds rd in one row and rs1 or rs2 in another.
| Opcode, bits 6 to 0 | Group | Format |
|---|---|---|
| 0110011 | arithmetic and logic on two registers | R |
| 0010011 | arithmetic and logic with an immediate | I |
| 0000011 | loads | I |
| 1100111 | jalr | I |
| 0100011 | stores | S |
| 1100011 | branches | B |
| 0110111 and 0010111 | lui and auipc | U |
| 1101111 | jal | J |
Every base opcode ends in 11. The three system instructions have opcodes of their own, and come later.
The Smallest Example, Worked by Hand
Encode one line: addi x5, x5, -1. It is an I instruction. Write each field in binary, then put them side by side.
| Field | Bits | Value | In binary |
|---|---|---|---|
| Immediate | 31 to 20 | -1, in 12 bits | 1111 1111 1111 |
| rs1 | 19 to 15 | x5 | 00101 |
| funct3 | 14 to 12 | the code of addi | 000 |
| rd | 11 to 7 | x5 | 00101 |
| Opcode | 6 to 0 | arithmetic with an immediate | 0010011 |
Side by side: 1111 1111 1111 0010 1000 0010 1001 0011. In hex that is fff28293.
Decode one word: 00752423. In binary it is 0000 0000 0111 0101 0010 0100 0010 0011. Start at the right. The opcode 0100011 says: a store, so the S format.
| Bits | In binary | Field | Meaning |
|---|---|---|---|
| 31 to 25 | 0000000 | immediate, bits 11 to 5 | the upper part of the offset |
| 24 to 20 | 00111 | rs2 | x7: the value to store |
| 19 to 15 | 01010 | rs1 | x10: the register with the address |
| 14 to 12 | 010 | funct3 | a word of 32 bits: sw |
| 11 to 7 | 01000 | immediate, bits 4 to 0 | the lower part of the offset |
| 6 to 0 | 0100011 | opcode | a store |
The immediate is 0000000 followed by 01000, which is 8. The line is sw x7, 8(x10).
An immediate of 12 bits is not yet a value that the processor can add. Registers are 32 bits wide. The 12 bits are made 32 bits wide by sign extension. Bit 11 of the immediate is copied into bits 12 to 31.
For 8 the copies are zeros, and the value stays 8. For the -1 above, bit 11 is a 1. All 20 copies are ones, and the 32 bits still mean -1.
The Rules
Rule 1: Bits 6 to 0 are the opcode, in every instruction.
The opcode names the format.
Rule 2: The fields rd, rs1 and rs2 never move.
They are bits 11 to 7, 19 to 15 and 24 to 20, in every format that has them.
Rule 3: The immediate of the I format is bits 31 to 20.
It takes the places of funct7 and rs2, which an I instruction does not need.
Rule 4: The immediate of the S format is split.
Its bits 11 to 5 are in bits 31 to 25. Its bits 4 to 0 are in bits 11 to 7, where rd would be.
Rule 5: Bit 31 of the word is the sign bit of the immediate, in every format.
Sign extension copies it into every upper bit of the 32-bit value.
Rule 6: An immediate of 12 bits reaches from -2048 to 2047.
A larger constant needs two instructions.
Sources for These Rules
Every encoding fact on this page is stated as commonly described. That covers the six formats, the place of each field, the opcodes and the way an immediate is made 32 bits wide. The RISC-V specifications are not in the reference library. So none of these facts is checked against them. Each one was compared with the instruction tables of the assembler of this track. Those tables were written in the layout of the specification.
The plan of this track points to two textbooks for this material. They are Patterson and Hennessy, sections 2.5 and 2.10, and Harris and Harris, section 6.4. Those places are not checked either.
Now Run It
The lab at the end of this page cuts five real words at the six fixed places. The words are from the programs of S2.1, as the assembler of this track made them.
006283b3 | R | 0000000 00110 00101 000 00111 0110011
fff28293 | I | 1111111 11111 00101 000 00101 0010011
00452303 | I | 0000000 00100 01010 010 00110 0000011
00752423 | S | 0000000 00111 01010 010 01000 0100011
fe029ce3 | B | 1111111 00000 00101 001 11001 1100011Rule 1 at work: read the last group of each row. The format in the second column follows from it alone.
Rule 2 at work: row 1 is add x7, x5, x6. Its groups 2, 3 and 5 are 00110, 00101 and 00111: registers 6, 5 and 7.
Rule 3 at work: rows 2 and 3. Groups 1 and 2 together are the immediate. In row 2 they are twelve ones.
Rule 4 at work: row 4 is the store of the hand example. Group 5 holds 01000, the low bits of the offset.
The lab builds no immediate. Here are the immediates of the same five words, from the instruction tables of this track:
006283b3 | add x7, x5, x6 | no immediate
fff28293 | addi x5, x5, -1 | immediate -1 = ffffffff
00452303 | lw x6, 4(x10) | immediate 4 = 00000004
00752423 | sw x7, 8(x10) | immediate 8 = 00000008
fe029ce3 | bne x5, x0, 0x8 | immediate -8 = fffffff8Rule 5 at work: the second and the last row. Bit 31 of each word is 1, and the wide value starts with f digits.
Row 5 is a branch. Its immediate is -8, but its groups do not spell -8 in any plain order. The next lesson explains why.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Register numbers in fixed places | Each format packs its fields as tightly as it can | The registers are read before the format is known. The immediate must be split in the S format. |
| The sign bit always in bit 31 | The sign wherever the immediate ends | Sign extension can start at once, for every format. The bits of some immediates are out of order. |
| An opcode in the same seven bits | Opcodes of different lengths | One small decoder names the format. Seven bits of every word are spent on it. |
| Immediates are sign-extended | Zero extension, and a subtract instruction with an immediate | One addi adds and subtracts. Offsets reach both ways. The range is cut in half each way. |
| Twelve bits of immediate | Sixteen, as in some older instruction sets | Room for three register numbers of 5 bits. Constants over 2047 need a second instruction. |
The Trap: The Counter That Jumped by 4095
A student builds the datapath for addi. The immediate has 12 bits and the adder has 32. The upper 20 bits must be something, and zeros look harmless. Every test with a small positive number passes.
Then a loop runs addi x5, x5, -1 with 3 in x5. Its immediate field is fff. Here is that field, and three others, made wide in both ways:
field 001 | zeros above: 00000001, x5 becomes 4 | sign above: 00000001, x5 becomes 4
field 7ff | zeros above: 000007ff, x5 becomes 2050 | sign above: 000007ff, x5 becomes 2050
field fff | zeros above: 00000fff, x5 becomes 4098 | sign above: ffffffff, x5 becomes 2
field 800 | zeros above: 00000800, x5 becomes 2051 | sign above: fffff800, x5 becomes -2045Rows 1 and 2: the top bit of the field is 0. Both ways agree, and that is why the early tests passed.
Row 3 is the failure. With zeros above, fff means 4095, and x5 jumps from 3 to 4098. With the sign above, it means -1, and x5 becomes 2.
The loop of S2.1 would never end with this bug: its counter climbs and is never 0. The check is rule 5, and a test with the field 800, the most negative value.
At Scale
This section looks ahead. The few lines of this page become real blocks of the processor.
The immediate generator
A processor has one block that builds the 32-bit immediate for every format. You write it after the next lesson.
The control logic
The opcode, funct3 and funct7 feed a second block that decides what the datapath does. A later lesson of this module builds it.
Illegal words
Many of the possible words are no instruction at all. A processor must spot them and trap: S2.17 showed one.
Tools that read words
A tool that shows a program in memory turns words back into assembly. It uses these same tables.
Try It
Run the lab. For each row, find rs1 in the fourth group and turn it into a register number.
Add the word 00100093. Name its format, its two registers and its immediate from the row.
Change one hex digit of a word so that rd becomes x15. Which digits can change rd?
006283b3 | R | 0000000 00110 00101 000 00111 0110011 fff28293 | I | 1111111 11111 00101 000 00101 0010011 00452303 | I | 0000000 00100 01010 010 00110 0000011 00752423 | S | 0000000 00111 01010 010 01000 0100011 fe029ce3 | B | 1111111 00000 00101 001 11001 1100011Check Yourself
Common Mistakes
Wrong Look for rd of a store in bits 11 to 7.
A store has no rd. Those bits hold the low 5 bits of its immediate.
Fix Read the opcode first. It says which fields the word has.
Wrong Fill the upper 20 bits of an immediate with zeros.
A negative immediate becomes a large positive one.
Fix Copy bit 31 of the word into all 20 upper bits.
Wrong Write addi x5, x5, 4000.
Twelve signed bits reach only from -2048 to 2047. The assembler refuses the line.
Fix Build large constants with two instructions, as the next lesson shows.
Remember
- Bits 6 to 0 are the opcode. It names one of six formats: R, I, S, B, U, J.
- rd, rs1 and rs2 are always in bits 11 to 7, 19 to 15 and 24 to 20.
- The I immediate is bits 31 to 20. The S immediate is bits 31 to 25, then bits 11 to 7.
- Bit 31 is always the sign bit. Sign extension copies it into every upper bit.