RV32IMAC Cheat Sheet

Embedded below is the pdf summorizing the entire RISC-V instruction set (RV32IMAC), which is useful during most lessons. The pdf is structured as follows:

  • p.1 - Instructions on how to read the tables.
  • p.2 - Registers Table.
  • p.3-5 - Encoder Table (to translate assembly into binary machine code manually).
  • p.6-7 - Decoder Table (to translate binary machine code into assembly manually)

Below the embedded pdf is a set of exercises to train your ability in encoding and deconding instructions manually.

Open PDF in another tab

Encode and Decode Exercises

  1. Decode the following instructions:

    1. 0x01e007ef
    2. 0x0001e7b7
    3. 0x00440113
    4. 0x40445113
    5. 0x403100b3
    6. 0xfea64ae3
    7. 0x0003a583
    8. 0x00b3a023
  2. Encode the following instructions:

    1. j -12
    2. auipc sp 8
    3. andi sp, s0, 4
    4. sltiu sp, s0, 4
    5. or ra, sp, gp
    6. Solo l'istruzione di branch del seguente programma:
      • <LABEL>
      •     lw a1 0(t2)
      •     add a2 a1 a1
      •     sw a1 0(t2)
      •     bgtz sp <LABEL>
    7. lw a7 8(t2)
    8. sw a7 8(t2)

Examples

Encoding instruction 0x01e007ef

First, convert hex into binary:

hex01e007ef
bin00000001111000000000011111101111

Then identify the instruction at hand using the tables from the RV32IMAC Cheat Sheet. Instructions are better decoded reading them from right to left, so that the first thing you find are its quadrant and opcode.

RV32I00000001111000000000011111101111
Meaning<imm> (encoded)rdopcodequadrant
Value<imm>15Jump and Link4th

We therefore obtain jal x15 <imm>, where <imm> is the still to be decoded immediate.

All the instruction fields except <imm> are decodable just from the tables, for this instruction. For <imm> we have to further scramble some bits to decode it.

Identify the subfields of the immediate:

<imm> (encoded)00000001111000000000
Encodingm2imm2m1imm1

Re-order the subfields as written on the tables. Usually the immediate encoding is just below the corresponding instruction's encoding.

Decoding di <imm>-m2-imm1m1imm20
<imm> (bin)00000000000000000000000000011110

We obtain the 2's complement

  • <imm> (bin) = 00000000000000000000000000011110

that is the decimal

  • <imm> (dec) = 30

The complete disassembled instruction is therefore jal x15 30, o jal a5 30 using the ABI register aliases.

Deconding instruction j -12

Find the instruction j on the encoding part of the RV32IMAC Cheat Sheet.

Instruction Encoding j<imm> (encoded)000001101111

We therefore know the least significant bits xxxxxxxxxxxxxxxxxxxx000001101111. The rest is just an immediate, <imm> = 30 , which we now have to encode.

First convert <imm> = -12 to 2's complement <imm> (bin) = 10100, and now sign-extend it to cover all the 32 bits of its width <imm> (bin) = 11111111111111111111111111110100.

Divide this number into its subfields as of RV32IMAC Cheat Sheet, Encoder section.

<imm> (bin)11111111111111111111111111110100
Decoding <imm>-m2-imm1m1imm20

Re-order the fields to encode the immediate:

Encodingm2imm2m1imm1
<imm> (encoded)11111111010111111111

We obtain <imm> (encoded) = 11111111010111111111xxxxxxxxxxxx, that are the missing most significant bits.

The complete assembled instruction is therefore 11111111010111111111000001101111, after having jointed the two half-results above.