Do you have a question? Post it now! No Registration Necessary
Subject
- Posted on
- Warren Thai
September 29, 2006, 8:37 pm

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?

Re: pic 18f4331 assembly code addition subtraction

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:
http://www.piclist.com/techref/microchip/math/basic.htm
Perhaps the information presented there may be of some help to you.
Regards, Peter

Re: pic 18f4331 assembly code addition subtraction

and
hi
borrow.
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
Site Timeline
- » Can Bluetooth headsets communicate with each other?
- — Next thread in » Electronics Down Under
-
- » bluetooth 1.1 and 2
- — Previous thread in » Electronics Down Under
-
- » Jaycar.
- — Newest thread in » Electronics Down Under
-
- » Obudowy z JLCPCB
- — The site's Newest Thread. Posted in » Electronics (Polish)
-
- » WTB: A6725ELW or TB62705CFN LED Drivers
- — The site's Last Updated Thread. Posted in » Electronic Components
-