--- Dwayne Reid wrote: > >I know this must be the worst bin to bcd routine in > C > >How would be the best? > > Its in ASM (sorry), but John Payson wrote the > quickest version I've seen so far. > > ;Takes hex number in NumH:NumL Returns decimal in > ;TenK:Thou:Hund:Tens:Ones > ;written by John Payson > NumH EQU AD3M ;input > NumL EQU AD3L > TenK EQU LOOPER ;share variables > Thou EQU D2 > Hund EQU D1 > Tens EQU R2 > Ones EQU R1 > > swapf NumH,w > iorlw b'11110000' > movwf Thou > addwf Thou,f > addlw .226 > movwf Hund > addlw .50 > movwf Ones > > movfw NumH > andlw b'00001111' > addwf Hund,f > addwf Hund,f > addwf Ones,f > addlw .233 > movwf Tens > addwf Tens,f > addwf Tens,f > > swapf NumL,w > andlw b'00001111' > addwf Tens,f > addwf Ones,f > > rlf Tens,f > rlf Ones,f > comf Ones,f > rlf Ones,f > > movfw NumL > andlw b'00001111' > addwf Ones,f > rlf Thou,f > > movlw 7 > movwf TenK > > ; At this point, the original # == > TenK*10000+Thou*1000+Hund*100+Tens*10+Ones > ; if those entities are regarded as two's compliment > binary. To be precise, > ; all of them are negative except TenK. Now the > number needs to be normalized, > ; but this can all be done with simple byte > arithmetic. > > movlw .10 > Lb1: > addwf Ones,f > decf Tens,f > skpc > goto Lb1 > Lb2: > addwf Tens,f > decf Hund,f > skpc > goto Lb2 > Lb3: > addwf Hund,f > decf Thou,f > skpc > goto Lb3 > Lb4: > addwf Thou,f > decf TenK,f > skpc > goto Lb4 And an equivalent, but untested c-version: void bcd(unsigned binary_number) { char a0,a1,a2,a3; /* binary digits */ char b0,b1,b3,b4,b5; /* bcd digits */ a0 = binary_number & 0xf; a1 = (binary_number >> 4) & 0xf; a2 = (binary_number >> 8) & 0xf; a3 = (binary_number >> 12) & 0xf; b0 = a0 - 4*(a3 + a2 + a1) - 20; b1 = 6*a2 + 2*a1 - 138; b2 = a3 + 2*a2 - 46; b3 = 4*a3 - 64; b4 = 7; while(b0 < 0) { b0 += 10; b1--; } while(b1 < 0) { b1 += 10; b2--; } while(b2 < 0) { b2 += 10; b3--; } while(b4 < 0) { b4 += 10; b5--; } } .lo __________________________________________________ Do You Yahoo!? Thousands of Stores. Millions of Products. All in one place. Yahoo! Shopping: http://shopping.yahoo.com