CS255 Syllabus

### The subtract (SUB) instruction

• The SUB machine instruction

• Subtraction:

 The subtract instruction is similar to the add instruction

• Subtract in a data register:

 ``` SUB.B Src, Dn * Subtract the 8 bits values: Dn = Dn - Src SUB.W Src, Dn * Subtract the 16 bits values: Dn = Dn - Src SUB.L Src, Dn * Subtract the 32 bits values: Dn = Dn - Src ```

• Note:

• The subtract instruction is very similar to the add instruction

• You can use the above examples to learn about the subtract instruction

The examples will work if you replace:

 ``` ADD ---> SUB ```

• Subtract in an address register:

 ``` SUBA.W Src, An * Subtract the 16 bits values: An = An - Src SUBA.L Src, An * Subtract the 32 bits values: An = An - Src ```

• Reminder: When to use data and address registers

• Data registers:

 When you perform calculation: always use data registers

• When you access an array element or a field in a linked list:     use address registers

• That's because the indirect addressing mode used to access an array element or a field in a list:

 ``` m(An) ```

requires an address register An !!!

Example:

 ``` short B[10]; (We assumed array B have been defined) ===> Get the value of B[3] into register D0 In assembler code: /* ========================================================= We want to access B[3]: ==> put base address in address register ========================================================= */ movea.l #B,A0 * A0 = base address of array B * An address is 32 bits, so we use .l !!! /* ========================================================= We put the values B[3] in data register D0 ========================================================= */ move.w 6(A0),D0 * Get short (16 bits) variable B[3] into register D0 * We use an address register A0 because the * indirect addressing mode 6(A0) NEEDS to use * an address register * We put the value into a data register D0 * because we want use the value in B[3] in computations !!! ```