For instance, if you want to subtract 3 from the W register, you might be tempted to write
SUBLW 3but the actual effect of that instruction is to subtract W from 3.
The correct way to subtract a constant from W is to use the ADDLW instruction with an argument that is the two's complement of the desired subtrahend. For the example of subtracting 3 from W, you could use
ADDLW 256-3or
ADDLW 253or (in some assemblers)
ADDLW -3
The "backwardsness" of the SUBLW instruction can sometimes be put to good advantage. For example, to take the two's complement of the W register, you can use
SUBLW 0rather than the perhaps more obvious sequence
XORLW 0ffh ADDLW 1The same technique is useful for other complements, such as taking the 9's complement of a digit in W by
SUBLW 9In general, any time it is necessary to reverse a sequence of numbers from A to B (where A and B are both in the range of 0 to 255), you can use
SUBLW A+Bwhich will work even if the sum A+B is greater than 255.
Last updated July 21, 1995
Copyright 1995 Eric Smith