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