SUBWF Arithmetic
Subtract W from a file register.
| Syntax | SUBWF f, d |
|---|---|
| Operands | f: register address 0-127 (bank bits come from STATUS); d: 0 or w puts the result in W, 1 or f puts it back in f |
| Operation | (f) - (W) → destination |
| Status bits | C DC Z |
| Words / cycles | 1 word, 1 cycle |
| Encoding | 00 0010 dfff ffff |
How it works
SUBWF computes f minus W (the register comes first). The PIC does this by adding the two's complement of W, so C means "no borrow": C = 1 when f ≥ W, and C = 0 when the result went negative. DC works the same way for the low nibble.
That makes SUBWF the comparison instruction: subtract, then test C to see which value is larger and Z to see whether they were equal.
Example
; SUBWF example from picsimulator.com/instructions/subwf.html
list p=16f84a
include <p16f84a.inc>
org 0
movlw 0x05
movwf 0x20
movlw 0x03
subwf 0x20, f ; 0x20 = 5 - 3 = 2; C = 1 means no borrow
5 is at least 3, so there is no borrow and C is set. Try MOVLW 0x07 instead of 0x03: the result wraps to 0xFE and C clears.
Open this example in the simulator
Watch out for
- C is inverted compared with many other processors: after a subtraction, C = 0 means a borrow happened.
- The order is f - W, not W - f.