4.2: Machine Code for the Add Instruction
 Page ID
 27114
This section will translate the following add instruction to machine code.
add $t0, $t1, $t2
The MIPS Greensheet specifies the add instruction as an Rformat instruction and the op code/function for the add as 0/20. The opcode/function field is made up of two numbers, the first is the opcode, and the second is the function. Note that the function is used only for R format instructions. If the instruction has a function, the number to the left of the "/" is the op code, and the number to the right of the "/" is the function. If there is only one number with no "/", it is the opcode, and there is no function.
Both the opcode and the function are 6 bits, divided into a 2 bit number and a 4 bit number. So the first number in the opcode and function is 0..3, and the second is 0..f. Both are generally called hex values, so this text will do so as well. So the 6 bits for the opcode translate to 00 0000, and the 6 bits for the function translate to 10 0000. These are placed into the opcode and function fields of the R format instruction shown in figure 45 below.
Register Rd is $t0
. $t0
is also register $8
, or 01000, so 01000 is placed in the R_{d} field.
Register Rs is $t1
. $t1
is also register $9
, or 01001, so 01001 is placed in the R_{s} field
Register Rt is $t2
. $t2
is also register $10
, or 01010, so 01010 is placed in the R_{t} field.
The shamt is 00000 as there are no bits being shifted.
The result is the following Rformat instruction.
Thus the instruction "add $t0
, $t1
, $t2
" translate into the bit string
00000001001010100100000000100000
This is as hard to type as it is to read. To make it readable, the bits are divided into groups of 4 bits, and these 4 bit values translated into hex. This results in the following instruction.
0000 
0001 
0010 
1010 
0100 
0000 
0010 
0000 

0x 
0 
1 
2 
a 
4 
0 
2 
0 
This can be checked by entering the instruction in MARS, and assembling it to see the resulting machine code. Be careful with the order of the registers when translating into machine code, as the Rd is the last register in machine code.