There is no "add with carry" instruction on PIC 16. But what if I had to add larger data quantity than just a byte? All the Web resources and Microchip application notes I've found suggest something like:
MOVF LSB_A, w
ADDWF LSB_B
MOVF MSB_A, w
BTFSC STATUS, C
INCFSZ MSB_A, w
ADDWF MSB_B
but the code above will just not work. I mean the 16-bit result will be correct, but the carry won't be set properly, so you can't just expand the computational range by repeating the second part of the code above. Am I right so far?
Below is my implementation of addition with carry:
MOVF BYTE1, w
BTFSC STATUS, C
ADDLW 1
RLF TEMP ; save the carry
ADDWF BYTE2
BTFSS STATUS, C
RRF TEMP ; restore the carry (from the first addition)
You can use plain addition for the least significant byte or clear the carry before using a sequence of additions with carry. I guess it works, but are there more effective solutions to this problem?
Microchip's Code Module Library (CML) gives the following snippet for 16-bit addition, which does seem to cope with the overflow situation you describe (too large for a comment so I hope you'll forgive me posting as an answer) :