Bill Cornutt wrote: > "I can fix that bug in five bytes." Here's a 24-instruction solution with a little looping: Explanation: http://anick.simplenet.com/piclist/Oct97/0369.html Code: http://anick.simplenet.com/piclist/Oct97/0312.html It's based on what Payson affectionately calls his 'wonderful 16-bit binary to BCD algorithm': http://www.iversoft.com/cgi-bin/lwgate/PICLIST/archives/March97/date/article-0.h tml (This link is from the archive and takes a while to download. It's been a year since it's been posted, perhaps it's time again?) Here's the 28-cycle (non-looping) version that I promised yesterday. It's based on binary comparisons. It's one of those routines that is very difficult to comment. So I didn't. However it takes advantage of this little trick to quickly ascertain the ones' digit: If you look at the ones' digit for 2^N you see this pattern: n = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 ... 2^n % 10 = 1, 2, 4, 8, 6, 2, 4, 8, 6, 2, 4, 8 ... If it wasn't for the annoying 6's, you could simply sum the nibbles to get and get the ones' digit (after a relatively simple binary to BCD conversion). However, it's simple enough to test if bit 4, 8,... are high and to subtract 1 and then add 6 (or simply add 5) if they are. The second observation is that the sum of all of the tens' digits is less than 16, and thus can fit into one nibble. This simplifies having to deal with overflow until after all of the digits have been added together. The third observation is that the BCD result is greater than 200 only if the most significant bit is set. ;******************************** ;binary_to_bcd - 8-bits ; ;Input ; bin - 8-bit binary number ;Outputs ; hundreds - the hundreds digit of the BCD conversion ; tens_and_ones - the tens and ones digits of the BCD conversion binary_to_bcd: CLRF hundreds SWAPF bin,W ADDWF bin,W ANDLW 00001111b SKPNDC ADDLW 0x16 SKPNDC ADDLW 0x06 ADDLW 0x06 SKPDC ADDLW -0x06 BTFSC bin,4 ADDLW 0x16 - 1 + 0x6 SKPDC ADDLW -0x06 BTFSC bin,5 ADDLW 0x30 BTFSC bin,6 ADDLW 0x60 BTFSC bin,7 ADDLW 0x20 ADDLW 0x60 RLF hundreds,F BTFSS hundreds,0 ADDLW -0x60 MOVWF tens_and_ones BTFSC bin,7 INCF hundreds,F Scott