pic 18f4331 assembly code addition subtraction

with the 18f chip, if i have to files AH,AL to represent one number and BH,BL for another. (H and L are hi and lo obviously), how would i use the instruction set to add and subtract a and b and taking care of the carry and borrow bits. im thinking, with addition, you add the lo first and add the hi with carry. with subtraction, you subtract hi first and then lo with borrow. is this correct?

Reply to
Warren Thai
Loading thread data ...

I can't remember the pic instruction set, but that's basically correct. If the pic doesn't have an add without carry instruction, you'll also have to ensure that the carry is clear before the first add.

Not quite - just like the math you learned at school, you still need to start at the low end first with subtraction.

I'm sure that you'll be able to find plenty of pic code with google - I just did a quick search and found this:

formatting link

Perhaps the information presented there may be of some help to you.

Regards, Peter

Reply to




Don't know if it will help, I've only used the 16F... series, but I do a 2's complement then add. For the 16F... series, I use the following:-

DBL2Comp comf Val2Low,f incf Val2Low,f btfsc STATUS,Z decf Val2High,f comf Val2High,f retlw 0

; Double Precision Addition (Val2 + Val1 -> Val2) DBLAdd movf Val1Low,w addwf Val2Low,f ; Add LSB. btfsc STATUS,C ; Add in carry. incf Val2High,f movf Val1High,w addwf Val2High,f ; Add MSB. retlw 0

; Double Precision Subtraction (Val1 - Val2 -> Val2) DBLSubtract call DBL2Comp call DBLAdd retlw 0

Reply to
Johnny Boy

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.