# Subtraction

Subtraction is one of the four basic arithmetic operations, where you take away a number from another number to get the result.

Subtraction, as it applies to the Channel F, doesn't exist as an opcode. However, the result of subtracting a number can be given by using the two's complement of the number, which means the inverse of the number plus one. The side effect of this is that the carry is set; however, since we're using 8-bit numbers, the 8-bit number will be the same as if we subtracted the two values, and the carry can be ignored.

To subtract an immediate value, just add the two's complement:

lr A, 4 ; load A from register 4 (or any other) ai %11110110 ; subtract 10, result is in A

10 in binary is %00001010, so the inverse is %11110101 and then add one and you have the two's complement. If you want to subtract two arbitrary numbers, however, there is a little more work involved. Take, for instance, the following situation:

lr A, 4 ; subtract r5

You want to subtract r5 from r4 (minuend (r4) − subtrahend (r5)) and then use the result. Since we don't know the value of r5, we can't use an immediate value but we can get the two's complement and add the two together. As we need to process the subtrahend - let's start with that:

lr A, 5 com ; get the inverse of the number inc ; add 1 (two's complement) as 4 ; add it to r4, result is saved in r4

Just remember, you need to get the two's complement of the subtrahend. Other methods are possible as well, if the result is needed in r5 instead then two's complement can be saved in r5 and then r4 could be loaded into A and added to register 5 - but it requires more code and is of course slower.