Difference between revisions of "Subtraction"
m (1 revision)
Revision as of 21:23, 16 November 2012
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, or 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 ai %11110110 ; subtract 10
10 in binary is %00001010, so the two's complement would be %11110110. If you want to subtract two arbitrary numbers, however, there is a little more work involved. Take, for instance, the folowing situation:
lr A, 4 ; subtract r5
You want to subtract r5 from r4, and then use the result. Since we don't know the value of r5, we can't use an immediate value. However, we can get the two's complement while running the program, like so:
lr A, 5 com ; get the inverse of the number inc ; add 1 (two's complement) as 4 ; add it to r4
Just remember, you need to get the two's complement of the number you're subtracting, not the one to be subtracted from.