Doh! adding A2 twice does not multiply by 2! Thanks for spotting that one. >-92 = 0xa4 > and 0xa4 << 1 is 0x48 yes, but 0x48 or (72 + x) mod 256 is (x-184) mod 256 or in other words, adding 72 and wrapping around 256 is the same as subtracting 184. I think I'm right on that one. I'm as puzzled as you are about the comf thing. I'm trying to find the book I learned binary math from. Thanks for your time, I know it's in great demand. --- James Newton mailto:jamesnewton@geocities.com 1-619-652-0593 http://techref.massmind.org NEW! FINALLY A REAL NAME! Members can add private/public comments/pages ($0 TANSTAAFL web hosting) -----Original Message----- From: pic microcontroller discussion list [mailto:PICLIST@MITVMA.MIT.EDU]On Behalf Of Scott Dattalo Sent: Friday, February 11, 2000 14:35 To: PICLIST@MITVMA.MIT.EDU Subject: Re: PIC Math FAQ page Importance: Low On Fri, 11 Feb 2000, James Newton wrote: > 4. I'm worried that the comments I've added (of the form W= and A2=) on each > line of John Payson's 16 bit Binary to BCD conversion code may be incorrect. > Specifically: Am I getting the subtraction by addition idea correct? How can > comf do a negate when it does a ones complement vice a twos complement? Math > is not my strongest point. see: > http://techref.massmind.org/microchip/math/radix/b2bu-16b5d.htm Hi James, after you sent me the (private) message the other day, I did spend a few minutes running through the comments. movf NumH,w ;w = A3*16+A2 andlw $0F ;w = A2 addwf Hund,f ;B2 = A3-46 + A2 = A3+A2-46 >>> addwf Hund,f ;B2 = 2*(A3+A2-46) = 2A3+2A2-92 addwf Ones,f ;B0 = A3+4 + A2 = A3+A2+4 should be: addwf Hund,f ;B2 = 2*A2+A3-46 And >>> rlf Tens,f ;B1 = 2*(4A2+A1-92) = 8A2+2A1-184 >>> rlf Ones,f ;B0 = 2*(A3+A2+A1+4) = 2A3+2A2+2A1+8 comf Ones,f ;B0 = -2A3-2A2-2A1-8 rlf Ones,f ;B0 = 2*(-2A3-2A2-2A1-8) = -4A3-4A2-4A1-16 -92 << 1 does not equal -184 -92 = 0xa4 and 0xa4 << 1 is 0x48 Then the next line is going to pick up the carry. So these two lines should read instead: >>> rlf Tens,f ;B1 = 2*(4A2+A1+0xa4) = 8A2+2A1+0x48 >>> rlf Ones,f ;B0 = 2*(A3+A2+A1+4)+1 = 2A3+2A2+2A1+9 These are the only two errors that immediately popped out. However, since the results of these calculations are used further down in the code, the errors are propogated. There may be a bug silently lurking in this code - I hadn't noticed the C bit thing before... Scott