GERRY COX wrote: >I have a register pair, ADC_H and ADC_L , which contain a 16 bit value >representing an ADC sample. I want to take successive samples and >add them into a 32Bit accumulator. ACC_3, ACC_2, ACC_1, ACC_0. >Speed is essential so code needs to be short. I am having trouble >with the 16bit / 32 bit Add that I attempted to write. It does funny things >for example when I add FFFF successively. I have written the rest of >the loop and the ADC control part but just can't get the accumulate to >work. > >Would someone please give me a clue on how to do the add and how to handle >the >carrys. Or maybe send a snippet of code that will help? > ;******************************************************************* ; Two byte add. Standard PIC instruction set does not have an add ; with carry instruction. ; At exit, if C = 1, result is greater than 2 bytes. ; Usage example: add_word buff, new_data ; Note: Zero flag NOT valid at exit ;******************************************************************* add_word: macro aaa, bbb movf (bbb+1), w addwf (aaa+1), f movf (bbb), w skpnc incfsz (bbb), w addwf (aaa), f endm Below is the companion subtract function. Below that is a multibyte subtract. You can use a similar technique to get a multibyte add. ;******************************************************************* ; Two byte subtract. ; At exit, if C = 1, result is positive. ; Usage example: sub_word new_count, old_count ; Note: Zero flag NOT valid at exit ;******************************************************************* sub_word: macro aaa, bbb movf (bbb+1), w subwf (aaa+1), f movf (bbb), w skpc incfsz (bbb), w subwf (aaa), f endm ;******************************************************************* ;Multi byte subtract. For example, subtract number from total: movf n_1, w ;subtract least significant byte subwf t_1, f ;2nd byte movf n_2, w ;subtract with borrow skpc incfsz n_2, w subwf t_2, f ;3rd byte movf n_3, w skpc incfsz n_3, w subwf t_3, f ;4th byte movf n_4, w skpc incfsz n_4, w subwf t_4, f -- Bob Fehrenbach Wauwatosa, WI bfehrenb@execpc.com