> Scott Dattalo's 3 digit BCD code on http://www.piclist.com runs > in about 28 cycles. You would then have to add the ASCII '0' offset > to each value, but the final result would still be very fast and without > using any lookup tables. > > Mike Scott, I've used that routine for a long time to convert numbers for LCDs but never had enough time or the internal wiring to figure it out properly. As you can see, trying to comment it hasn't helped much, and neither did running examples - I just got boggled. I have a vague idea of what's going on - it seems to be working on and testing the magnitude of nybbles. When you're able, could you please offer a brief explanation of how the routine actually works ? Cheers ;note - fixed font is more readable bin2bcd clrf d100 ;hundreds store ;num = A1*16+A0 swapf num,w ;W = A0*16+A1 addwf num,w ;W = A0+A1 ;num = 141 = 8D = 1000 1101, A1 = 8 A0 = D ; ;swapf num,w W = 1101 1000 ;addwf num,w + 1000 1101 ; W = 0110 0101 andlw b'00001111' ;W = (A0+A1) mod 16 ;andlw 00001111 W = 0000 0101 btfsc dc ;check for 4th bit overflow ;dc=0 if A0+A1 =< 16 ;dc=1 if A0+A1 > 16 addlw 0x16 ;else W = W + 16 btfsc dc ;if W mod 16 > 10 addlw 0x06 ;else W = W + 6 addlw 0x06 ;W = W + 6 btfss dc ;if W < 10 addlw -0x06 ;else remove 6 btfsc num,4 ; addlw 0x16 - 1 + 0x6 ;else add 0001 1011 btfss dc addlw -0x06 ;else remove 6 btfsc num,5 addlw 0x30 ;else add 0011 0000 btfsc num,6 addlw 0x60 ;else add 0110 0000 btfsc num,7 addlw 0x20 ;else add 0010 0000 addlw 0x60 ;add 0110 0000 rlcf d100,f ;d100 << d100 btfss d100,w ;test if d100 = 0 addlw -0x60 ;no, remove 0x60 movwf d001 btfsc num,7 ;test incf d100,f ;d100 = d100 + 1 movf d100,w addlw 0x30 ;convert to ASCII movwf d100 swapf d001,w ;separate 10s andlw 0x0f addlw 0x30 ;convert to ASCII movwf d010 movlw 0x0f ;separate 1s andwf d001,w addlw 0x30 ;convert to ASCII movwf d001 -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.