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".

Five questions this page answers
Question
1Which bits of an instruction name its format?
2Where are rd, rs1 and rs2, and why do they never move?
3Where is the immediate of an I instruction, and of an S instruction?
4How does an immediate of 12 bits become a value of 32 bits?
5What 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

New words on this page
WordMeaning
Instruction formatOne of the six layouts of the 32 bits of an instruction: R, I, S, B, U or J.
Instruction fieldA slice of the instruction word that has one meaning. Field for short.
OpcodeThe field in bits 6 to 0. It names the format and the group of the instruction.
rdThe field that holds the number of the destination register: bits 11 to 7.
rs1The field that holds the number of the first source register: bits 19 to 15.
rs2The field that holds the number of the second source register: bits 24 to 20.
funct3Three more bits that pick the instruction inside a group: bits 14 to 12.
funct7Seven more bits that pick the instruction, used by the R format: bits 31 to 25.
Sign bitThe top bit of a number in two's complement. It is 1 for a negative number.
Sign extensionMaking a number wider by copying its sign bit into every new upper bit. The value stays the same.
Zero extensionMaking a number wider by filling the new upper bits with 0. A negative number changes its value.
EncodingTurning a line of assembly into its word. Decoding is the way back.

One Picture: The Word and Its Six Places

One Word, Cut at the Same Places in Every FormatStep through the picture. The six boxes on the left are the 32 bits of an instruction, from the top bit down. Each step is one format.
  • Control / FSM
  • Processor
  • Bus
  • Memory
One Word, Cut at the Same Places in Every FormatStep through the picture. The six boxes on the left are the 32 bits of an instruction, from the top bit down. Each step is one format. component Bits 31 to 25 seven bits: funct7, orthe top of animmediate component Bits 24 to 20 five bits: rs2, ormore of an immediate component Bits 19 to 15 five bits: rs1 component Bits 14 to 12 three bits: funct3 component Bits 11 to 7 five bits: rd, or thelow end of animmediate component Bits 6 to 0 seven bits: theopcode component The immediate made 32 bits widebefore it is used bits 11 to 5bits 4 to 0 (I)bits 4 to 0 (S)names the format
  1. component Bits 6 to 0seven bits: the opcode
    • names the format → Bits 14 to 12
  2. component Bits 31 to 25seven bits: funct7, or the top of an immediate
    • bits 11 to 5 → The immediate
  3. component Bits 24 to 20five bits: rs2, or more of an immediate
    • bits 4 to 0 (I) → The immediate
  4. component Bits 19 to 15five bits: rs1
  5. component Bits 14 to 12three bits: funct3
  6. component Bits 11 to 7five bits: rd, or the low end of an immediate
    • bits 4 to 0 (S) → The immediate
  7. component The immediatemade 32 bits wide before it is used
  1. 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.

  2. 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.

  3. I: two registers and an immediate

    There is no rs2. Its box and the funct7 box together hold an immediate of 12 bits.

  4. 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.

  5. 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.

The six formats, bit by bit
Format31 to 2524 to 2019 to 1514 to 1211 to 76 to 0Used by
Rfunct7rs2rs1funct3rdopcodeadd, sub, and the other eight
Iimmediate, bits 11 to 5immediate, bits 4 to 0rs1funct3rdopcodeaddi and its group, loads, jalr
Simmediate, bits 11 to 5rs2rs1funct3immediate, bits 4 to 0opcodestores
Bimmediate, upper partrs2rs1funct3immediate, lower partopcodebranches
Uimmediateimmediateimmediateimmediaterdopcodelui, auipc
Jimmediateimmediateimmediateimmediaterdopcodejal

Read down a column. No column holds rd in one row and rs1 or rs2 in another.

The opcodes of the base instructions
Opcode, bits 6 to 0GroupFormat
0110011arithmetic and logic on two registersR
0010011arithmetic and logic with an immediateI
0000011loadsI
1100111jalrI
0100011storesS
1100011branchesB
0110111 and 0010111lui and auipcU
1101111jalJ

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.

From assembly to a word
FieldBitsValueIn binary
Immediate31 to 20-1, in 12 bits1111 1111 1111
rs119 to 15x500101
funct314 to 12the code of addi000
rd11 to 7x500101
Opcode6 to 0arithmetic with an immediate0010011

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.

From a word back to assembly
BitsIn binaryFieldMeaning
31 to 250000000immediate, bits 11 to 5the upper part of the offset
24 to 2000111rs2x7: the value to store
19 to 1501010rs1x10: the register with the address
14 to 12010funct3a word of 32 bits: sw
11 to 701000immediate, bits 4 to 0the lower part of the offset
6 to 00100011opcodea 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

  1. Rule 1: Bits 6 to 0 are the opcode, in every instruction.

    The opcode names the format.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

Printed by the lab on Icarus. After the format come the six groups of bits, from bit 31 down, as in the picture.
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 1100011

Rule 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:

Printed by the recording script from the assembler and its tables. The last column is the immediate after it is made 32 bits wide.
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 = fffffff8

Rule 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 choiceThe alternativeWhat the choice buys, and costs
Register numbers in fixed placesEach format packs its fields as tightly as it canThe registers are read before the format is known. The immediate must be split in the S format.
The sign bit always in bit 31The sign wherever the immediate endsSign extension can start at once, for every format. The bits of some immediates are out of order.
An opcode in the same seven bitsOpcodes of different lengthsOne small decoder names the format. Seven bits of every word are spent on it.
Immediates are sign-extendedZero extension, and a subtract instruction with an immediateOne addi adds and subtracts. Offsets reach both ways. The range is cut in half each way.
Twelve bits of immediateSixteen, as in some older instruction setsRoom 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:

Printed by test code on Icarus. The field is given directly. Register x5 holds 3 before each row.
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 -2045

Rows 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 page3 formats, 2 words by hand
The decoder of a processor6 formats, every instruction, in one clock cycle

This section looks ahead. The few lines of this page become real blocks of the processor.

  1. The immediate generator

    A processor has one block that builds the 32-bit immediate for every format. You write it after the next lesson.

  2. 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.

  3. Illegal words

    Many of the possible words are no instruction at all. A processor must spot them and trap: S2.17 showed one.

  4. 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?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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 1100011

Check Yourself

Predict the outputThis lab cuts the word fec42e23 and prints its format and two register numbers. Its opcode is 0100011. What does it print?
// Cut instruction words at the six places that never move.
module tb;
  // The opcode, bits 6 to 0, names the format.
  function automatic string format_of(input logic [6:0] opcode);
    case (opcode)
      7'b0110011: return "R";                                        // arithmetic and logic on two registers
      7'b0010011, 7'b0000011, 7'b1100111: return "I";                // with an immediate; loads; jalr
      7'b0100011: return "S";                                        // stores
      7'b1100011: return "B";                                        // branches
      7'b0110111, 7'b0010111: return "U";                            // lui and auipc
      7'b1101111: return "J";                                        // jal
      default:    return "?";
    endcase
  endfunction
  task automatic cut(input logic [31:0] w);
    $display("format %s, rs1=%0d, rs2=%0d", format_of(w[6:0]), w[19:15], w[24:20]);
  endtask
  initial begin
    //                    bits 31:25  24:20  19:15  14:12  11:7   6:0
    cut(32'hfec42e23);   // sw x12, -4(x8)
  end
endmodule
ConceptWhich bits of an instruction name its format?
ConceptWhy are rs1 and rs2 in the same bits in every format?
ConceptWhere is the immediate of an S instruction?
ConceptHow does an immediate of 12 bits become 32 bits wide?
ConceptAn adder gets the field fff with zeros above it, and adds it to 3. What is the result?

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.

Practice

Report a problem with this page