PIC Microcontoller Bit Math Method

Testing bits in W

Dmitry Kiryashov says:

You can't apply btfss/btfsc operations to [the W register]. You need to put W value to some temp location first and finally check temp with btfss/btfsc commands.

HINTS:

  1. 	and	W, #$80	; will test bit .7 in W, other bits are cleared ;-)
    	and	W, #$40	; test bit .6
    ;...
    

    result of operation in _Z flag (=0 means bit=1 otherwise bit=0 result to _Z=1)

    and W, #is destructive command. You cannot restore W if you didn't save it before.

  2. ;*** WARNING: ADDLW was expanded in three instructions! Check if previous instruction is a skip instruction. 
    ; ADDLW   0x88 ; will test bits .7 and .3
    	mov	Hack, W
    	mov	W, #$88	; will test bits .7 and .3
    	add	W, Hack
    

    results of operation in _C and _DC flags. To restore initial W just ADDLW -0x88 ;-)

  3. You can also apply XORLW value command to check identity W to some value. It is useful command (sometimes) 'cause you can do comparison for alot of values and restore W after that to initial value.
    	xor	W, #A_CONST
    	snb	Z
    	jmp	A_CONST_MATCH
    
    	xor	W, #B_CONST^A_CONST	;compare with B_CONST
    	snb	Z
    	jmp	B_CONST_MATCH
    
    	xor	W, #C_CONST^B_CONST	;compare with C_CONST
    ;....
    
    	xor	W, #LAST_CONST	;last used const value -> restore initial W
    ;.....