> I'm needing a fast routine to break-down a byte to three digits: > > before: > input = 137; > > after: > output2 = 7; > output1 = 3; > output0 = 1; > > It doesn't have to be small, but it have to be FAST. > > I'm thinking about doing it with a BIG lookup-table. Anyone have a >better idea? Maybe a hybrid routine (table+calculations). A big table would be fastest, but, if saving space is worth a little extra execution time, this hybrid routine comes to mind: (BTW -- I'm assuming a 12-bit core and a base 10 default radix ... also dunno if it works since I just made it up but it may give ideas ... also sorry if its not very good -- I couldn't resist taking a crack at it since I'm tired of the algorithm I'm supposed to be working on right now and needed a break ... I'm a sucker for puzzles) CLRF output0 ;hundreds place MOVLW 100 SUBWF input,F BTFSC STATUS,C INCF output0,F ;input >= 100 BTFSC STATUS,C SUBWF input,F BTFSC STATUS,C INCF output0,F ;input >= 200 BTFSS STATUS,C ADDWF input,F ;subtracted too much .. put it back CALL lookup ;do the tens and ones places via table MOVWF output2 ;ones place MOVWF output1 ;tens place SWAPF output1,F MOVLW 0x0f ANDWF output1,F ANDWF output2,F ;Done ;If you need to preserve 'input' then either ;save and restore it or do this: MOVLW 0 BTFSC output0,0 MOVLW 100 BTFSC output0,1 MOVLW 200 ADDWF input,F ;-------------------------------- ;The lookup table for values 0 .. 99 ;make sure this table doesn't cross a page boundry lookup MOVF input,W ADDWF PCL,F RETLW 0x00 RETLW 0x01 RETLW 0x02 RETLW 0x03 RETLW 0x04 RETLW 0x05 RETLW 0x06 RETLW 0x07 RETLW 0x08 RETLW 0x09 RETLW 0x10 RETLW 0x11 RETLW 0x12 RETLW 0x13 RETLW 0x14 RETLW 0x15 RETLW 0x16 RETLW 0x17 RETLW 0x18 RETLW 0x19 RETLW 0x20 RETLW 0x21 RETLW 0x22 RETLW 0x23 RETLW 0x24 RETLW 0x25 RETLW 0x26 RETLW 0x27 RETLW 0x28 RETLW 0x29 RETLW 0x30 RETLW 0x31 RETLW 0x32 RETLW 0x33 RETLW 0x34 RETLW 0x35 RETLW 0x36 RETLW 0x37 RETLW 0x38 RETLW 0x39 RETLW 0x40 RETLW 0x41 RETLW 0x42 RETLW 0x43 RETLW 0x44 RETLW 0x45 RETLW 0x46 RETLW 0x47 RETLW 0x48 RETLW 0x49 RETLW 0x50 RETLW 0x51 RETLW 0x52 RETLW 0x53 RETLW 0x54 RETLW 0x55 RETLW 0x56 RETLW 0x57 RETLW 0x58 RETLW 0x59 RETLW 0x60 RETLW 0x61 RETLW 0x62 RETLW 0x63 RETLW 0x64 RETLW 0x65 RETLW 0x66 RETLW 0x67 RETLW 0x68 RETLW 0x69 RETLW 0x70 RETLW 0x71 RETLW 0x72 RETLW 0x73 RETLW 0x74 RETLW 0x75 RETLW 0x76 RETLW 0x77 RETLW 0x78 RETLW 0x79 RETLW 0x80 RETLW 0x81 RETLW 0x82 RETLW 0x83 RETLW 0x84 RETLW 0x85 RETLW 0x86 RETLW 0x87 RETLW 0x88 RETLW 0x89 RETLW 0x90 RETLW 0x91 RETLW 0x92 RETLW 0x93 RETLW 0x94 RETLW 0x95 RETLW 0x96 RETLW 0x97 RETLW 0x98 RETLW 0x99 END ... Ok .. I take back that comment about not knowing if it works .. I couldn't resist the temptation to run it through MPSIM .. works like a charm! (but still slower than just using BIG tables) Ken ken@webster.org (no PIC stuff, ... yet)