add : Register2 = Register2 + Register1
sub : Register2 = Register2 - Register1
| Affected Flags | |||
| C | V | N | Z | 
| X | X | X | X | 
The add and sub instructions are used for addition and substraction.The operation is performed between two registers and the result is put in the last one.
rx = source register
ry = destination register
| Form | Effect | 
| add.b ry,rx | rx <- rx + ry | 
| add.w ry,rx | |
| add.l ry,rx | |
| sub.b ry,rx | rx <- rx - ry | 
| sub.w ry,rx | |
| sub.l ry,rx | 
S = Source register number
D = Destination register number
| Instruction | Op Code | Cycles | Encoding | 
| add.b ry,rx | 0E | 6 | 00001110 SSSSDDDD | 
| add.w ry,rx | 4E | 6 | 01001110 SSSSDDDD | 
| add.l ry,rx | 8E | 6 | 01001110 SSSSDDDD | 
| sub.b ry,rx | 0F | 6 | 00001111 SSSSDDDD | 
| sub.w ry,rx | 4F | 6 | 01001111 SSSSDDDD | 
| sub.l ry,rx | 8F | 6 | 10001111 SSSSDDDD | 
Note: This example is for understanding and can be optimized.
// Example : Simple (naive) Checksum of a c-style text string
; r0 = pointer to string (in)
; r0 = result (out)
checksum: pushregs r1-r2
          movex.b #0,r1
loop:     move.b(r0),r2
          beq out
          inc #1,r0
          add.b  r2,r1
          bra loop
out:      move.b r1,r0
          popregs r1-r2
          rts
                             