# 4.2: Machine Code for the Add Instruction


This section will translate the following add instruction to machine code.

add $t0,$t1, $t2 The MIPS Greensheet specifies the add instruction as an R-format instruction and the op- code/function for the add as 0/20. The op-code/function field is made up of two numbers, the first is the op-code, 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 op-code, and there is no function. Both the op-code and the function are 6 bits, divided into a 2 bit number and a 4 bit number. So the first number in the op-code 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 op-code translate to 00 0000, and the 6 bits for the function translate to 10 0000. These are placed into the op-code and function fields of the R format instruction shown in figure 4-5 below. Register Rd is $t0. $t0 is also register $8, or 01000, so 01000 is placed in the Rd field.

Register Rs is $t1. $t1 is also register $9, or 01001, so 01001 is placed in the Rs field Register Rt is $t2. $t2 is also register $10, or 01010, so 01010 is placed in the Rt field.

The shamt is 00000 as there are no bits being shifted.

The result is the following R-format 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.

This page titled 4.2: Machine Code for the Add Instruction is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Charles W. Kann III.