Skip to content

R-Format

Bits

R-format instructions use the following fields with the following name and number of bits:

opcode $rs $rt $rd shamt funct
6 5 5 5 5 6

Each field is an independent 5- or 6-bit unsigned integer:

  • A 5-bit field can represent any number 0-31.
  • A 6-bit field can represent any number 0-63.
Fields Meaning
opcode Partially specifies the instruction. Equal to 0 for all R-format instructions.
$rs Specify register containing first operand.
$rt Specify register containing second operand.
$rd Specify register which will receive the result of computation.
shamt Amount of shift. Set to 0 in all non-shift instructions.
funct Combined with opcode exactly specifies the instruction.

Steps

The steps to assemble R-format instructions can be summarised as follows:

  1. Set opcode to 0.
  2. Find the register number for $rs.
  3. Find the register number for $rt.
  4. Find the register number for $rd.
  5. Compute the number for shamt (0 if non-shift instruction).
  6. Find the value of funct.
  7. Convert the values to binary.
  8. Combine the fields.

Register Ordering

Note the ordering of the registers. In the MIPS instruction, we have the following ordering:

$rd | $rs | $rt

However, in the fields, we have the following ordering:

$rs | $rt | $rd

Funct Values

The values for the funct field is summarised below:

Operation Hexadecimal Decimal
add 20 32
sub 22 34
sll 00 00
srl 02 02
and 24 36
or 25 37
xor 26 38
nor 27 39

Examples

Arithmetic and Logic

The conversion simply follow the steps above. Remember, the opcode is 0 for all R-format instructions. Additionally, since we are not doing a shift operation, the value of shamt is also 0.

Addition

Addition
1
2
add $t0, $t1, $t2
#op $rd, $rs, $rt
Fields Decimal Value Binaries
opcode 0 000000
$rs 9 01001
$rt 10 01010
$rd 8 01000
shamt 0 00000
funct 32 100000

Given the steps, we can now combine the binaries:

000000 01001 01010 01000 00000 100000

or more simply:

00000001001010100100000000100000

We can also convert this into hexadecimal by splitting it into 4-bit groups:

Binary to Hexadecimal
1
2
3
4
   00000001001010100100000000100000
=> 0000 0001 0010 1010 0100 0000 0010 0000
=> 0    1    2    A    4    0    2    0
=> 012A4020

0x012A4020

Exercise

Convert the following instruction to hexadecimal:

Question
1
add $t2, $a3, $a1

See the steps to get the following binaries:

000000 00111 00101 01010 00000 100000

or more simply:

00000000111001010101000000100000

Convert this into hexadecimal by splitting it into 4-bit groups:

Binary to Hexadecimal
1
2
3
4
   00000000111001010101000000100000
=> 0000 0000 1110 0101 0101 0000 0010 0000
=> 0    0    E    5    5    0    2    0
=> 00E55020

0x00E55020

Fields Decimal Value Binaries
opcode 0 000000
$rs 7 00111
$rt 5 00101
$rd 10 01010
shamt 0 00000
funct 32 100000

Shift

The opcode is still 0 for shift operation. However, the shamt will now be used. On the other hand, the value of $rs will always be zero because if you recap the summary, the shift instruction are of the following format:

Shift
1
2
sll $rd, $rt, C5
srl $rd, $rt, C5

Notice the lack of $rs in the instruction.

Shift Left Logical

Shift Left Logical
1
2
sll $t0, $t1, 4
#op $rd, $rt, shamt
Fields Decimal Value Binaries
opcode 0 000000
$rs 0 00000
$rt 9 01001
$rd 8 01000
shamt 4 00100
funct 0 000000

Given the steps, we can now combine the binaries:

000000 00000 01001 01000 00100 000000

or more simply:

00000000000010010100000100000000

We can also convert this into hexadecimal by splitting it into 4-bit groups:

Binary to Hexadecimal
1
2
3
4
   00000000000010010100000100000000
=> 0000 0000 0000 1001 0100 0001 0000 0000
=> 0    0    0    9    4    1    0    0
=> 00094100

0x00094100