MIPS instruction encoder

Type a MIPS assembly routine with its start address and watch how each instruction is encoded field by field: the format (R, I or J), which register goes in each field, the immediate in two’s complement, the branch offset and the 26 bits of j, in binary and in hexadecimal. It also works the other way: from a hexadecimal word to its instruction. It runs in your browser, on your phone too.

How to use it

  1. Type the address of the first instruction (for example 0x00400000) and the routine, one instruction per line, with its labels. Or pick an example: the train ticket office, the bill for table 3 or a routine with forward and backward branches.
  2. Press Step to encode the routine instruction by instruction, or tap a row of the table. You see the fields with their bits, their values and the steps of the maths, down to the hexadecimal word.
  3. With Hex → assembly you type hexadecimal words and see the instruction in each one, with the target address of branches.
  4. With Branches you choose the convention: the one in the notes and the exam (the offset of beq and bne counts from PC + 4) or QtSpim’s without delayed branches (it counts from the branch itself, one more).

How it is encoded

R format (op = 0): op, rs, rt, rd, shamt and funct, of 6, 5, 5, 5, 5 and 6 bits; funct says the operation. I format: op, rs, rt and a 16-bit immediate; in lw and sw, rs is the base and the immediate the offset; in beq and bne, the immediate counts instructions: (target − (PC + 4)) ÷ 4. J format: op and 26 bits that are the target without its 2 low bits (always 00) or its 4 high bits, which PC + 4 provides.

Finally the 32 bits are joined and read in groups of 4: each group is a hexadecimal digit. It is the calculation the exam asks for to turn a routine into hexadecimal.

The rules it follows

  • It encodes the real MIPS32 instructions used in IC1 (not pseudo-instructions such as li, la or move, which the assembler turns into others).
  • Registers by name ($t0) or number ($8); immediates in decimal or hexadecimal.
  • Checked against the SPIM assembler, the core of QtSpim, with thousands of random instructions: with QtSpim’s convention it gives the same words.

For the exam

The exam question gives a routine with its start address and asks for its code in hexadecimal; sometimes the other way round too. Do it on paper and compare it here with the notes’ convention; the challenge below gives you new instructions. The theory is in the instruction set topic.