Scott Dattalo Sent: 2006 Feb 23, Thu 13:55 > > James Newton, Host wrote: > >>I want a 8 bit number divide by 10 (ten). > >>I need the quotient and the remain. > >> > >>Which is the fastest ASM code for pic 16f877? > > > > > > Multiply by 1/10 > > > > What about the remainder? Peter's solution has a similar problem. > Oops. Yep, the OP did say remainder was needed. Looks like your solution is optimal. The only problem with it is that I can't understand how it works. The comments stop around the point where the comments are really starting to get necessary... Just for the record, here is Scott's solution again: > -----Original Message----- > From: piclist-bounces@mit.edu > [mailto:piclist-bounces@mit.edu] On Behalf Of Scott Dattalo > Sent: 2006 Feb 23, Thu 08:08 > To: Microcontroller discussion list - Public. > Subject: Re: [PIC] Divide by 10 > > ...18 cycles. > > SWAPF bin, W ;w = A0*16+A1 > ADDWF bin, W ;w = A0+A1 ;ok, that added the nibbles together in W > ANDLW 00001111b ;w = A0+A1 % 16 > SKPNDC ;if A0+A1 > 16 > ADDLW 0x16 ; w += 16 > SKPNDC ;if w % 16 > 10 > ADDLW 0x06 ; w += 6 > ADDLW 0x06 ;w += 6 > SKPDC ;if w < 10 > ADDLW -0x06 ; w -= 6 ;that converted the Binary to BCD for that nibble. > BTFSC bin,4 > ADDLW 0x16 - 1 + 0x6 ;That was checking for the special case were bit 4 is set ; but I don't understand why it doesn't just add 5? > SKPDC > ADDLW -0x06 > > BTFSC bin, 5 > ADDLW 0x30 > > BTFSC bin, 6 > ADDLW 0x60 > ;and I have no idea what that was about. > ; result is in W > > This was adapted from: > http://www.piclist.com/techref/microchip/math/radix/b2bhp-8b3d.htm Which I also don't understand... > And the algorithm is described: > http://www.piclist.com/techref/method/math/radixs.htm Which I will paste in here for completeness: Notes from Scott Dattalo on converting a Byte to BCD quickly: [The routine I have implemented for the PIC uC is] based on binary comparisons. 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 ... 2^n in hex = 0, 2, 4, 8,10,20,40,80, ... 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). For example, the ones' digit for 2^5 = 0x20 is 2 (because 0x20 = 32 decimal). This sum-of-digits trick works even if the binary number is not a perfect power of 2. For example, consider 0xef: 0xef = 239 base 10, the one's digit is '9' 0xe + 0xf = 0x1d A binary to bcd conversion of 0x1d yields 0x29 (because 1d hex is 29 decimal). And the one's digit is '9', just like the one's digit of 0xef. Now, this trick only works if bit 4 is not set. (notice my contrived exampled has every bit set except for that one). It's simple enough to test if bit 4, (and bit 8 for 16-bit conversions) and to subtract 1 and then add 6 (or simply add 5) if it is. The second observation is that the sum of all of the tens' digits of 2^n (for n<8) 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. (What I'm saying is simply that if you look at the eight 2^n numbers 1,2,4,8,0x10,0x20,0x40,0x80 and sum all of the upper nibbles together: 0x10 + 0x20 + 0x40 + 0x80 you get 0xf0 which doesn't cause a carry.) The third observation is that the BCD result is greater than 200 only if the most significant bit is set. Note, that this is necessary but not sufficient condition. e.g. 0x80 has the msb set, but is only 128, but 200 is 0xc8. --- James Newton: PICList webmaster/Admin mailto:jamesnewton@piclist.com 1-619-652-0593 phone http://www.piclist.com/member/JMN-EFP-786 PIC/PICList FAQ: http://www.piclist.com -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist