assemblypicmplab

subwfb in PIC 18F8722 Assembly


I have a variable temp2 which is assigned 00001101(13) and I had moved 00001010(10) to wreg. (By the way all these registers are checked in MPLAB x Debugger session step by step.) Then, I execute

subwfb temp2

instruction. temp2 should be 00000011(3) but it is 00000010(2). What is the problem here, why temp2 is not as expected? Please, some help!


Solution

  • The subwfb instruction subtracts the W register and the borrow (1 if C is set, 0 otherwise) from register f. Borrow is used when you do chained subtractions, e.g. when performing subtractions on numbers contained in more than one byte. It is produced by the instructions of form subXX and subXXb when an overflow occurs.

    Example:

    ; let num1, num2 be labels of bytes in a 16-bit number
    movlw K
    subwf num1
    movlw L
    subwfb num2
    
    ; num2:num1 now contains: the num2:num1 - L:K where num2 and L are upper bytes of a 16-bit number
    

    To avoid this behavior when not desired, you can use either bsf STATUS, 0 or subwf (notice the absence of the b suffix). Note that using the instruction without b (or c in the case of addition instructions) when not needed may lead to marginally better performance on some models.