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:
- Set
opcodeto 0. - Find the register number for
$rs. - Find the register number for
$rt. - Find the register number for
$rd. - Compute the number for
shamt(0 if non-shift instruction). - Find the value of
funct. - Convert the values to binary.
- 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 | |
| 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 | |
0x012A4020
Exercise
Convert the following instruction to hexadecimal:
| Question | |
|---|---|
1 | |
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 | |
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 | |
Notice the lack of $rs in the instruction.
Shift Left Logical
| Shift Left Logical | |
|---|---|
1 2 | |
| 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 | |
0x00094100