Clarification of Subtraction Instructions

Problem:
On page 14, the book says that "It is not possible to subtract anything from the W register". However, this does not allow for the SUBLW register.

Solution:
The SUBLW instruction is the only instruction that will subtract a value from the W register and that subtracts the literal value that is included in the SUBLW instruction from the W register. For example, SUBLW 0x05 will subtract 5 from the value that was in the W register and place the result back in to the W register. However, a SUBWF will always subtract the contents of the W register from the contents of the register. Therefore, if the W register contains 5 and the register contains 2 then the result will be (2-5 =) minus 3 (253). It is not possible to do a 5-2 calculation without storing the W register into another register temporarily or by doing other esoteric things. For example, the following code will reverse the W register and the other register so that whatever was in the W register will now be in the other register and vice versa.
XORWF reg, 0
XORWF reg, 1
XORWF reg, 0
For example, if W contains 60 (00111100) and the register contains 170 (10101010).
XORWF reg, 0 ;00111100 (w) XOR 10101010 (reg) = 10010110, result to W
XORWF reg, 1 ;10010110 (w) XOR 10101010 (reg) = 00111100, result to reg
XORWF reg, 0 ;10010110 (w) XOR 00111100 (reg) = 10101010, result to W
Therefore, W now contains 10101010 (170) and the register contains 00111100 (60)

Further information:
I hope that this page helps you with your projects but if you have found any other problems or if you have any further questions please contact me though the email address of gares-AT-g4aym-DOT-org-DOT-uk.

Sorry about the email address but it seems to be the only way to avoid the spammers



Click here to return to the PIC Basics Main Page

Click here for the Index of Errors and Ommissions