Maker Pro
Maker Pro

pic 18f4331 assembly code addition subtraction

W

Warren Thai

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?
 
P

Pete

Warren said:
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.

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.
with subtraction, you subtract hi first and then lo with borrow.
is this correct?

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:

http://www.piclist.com/techref/microchip/math/basic.htm

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

Regards, Peter
 
J

Johnny Boy

Warren Thai said:
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?

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
 
Top