This is an extreme example of trading off code space for execution time It occupies 1280 locations and executes in 9 cycles ; Assume that initially your value is in W ; And that the answer is in ACC0 and ACC1 ; Assume that initially your value is in W ; And that the answer is in ACC0 and ACC1 ADDWF PCL ; Computed GOTO GOTO P0 GOTO P1 .. .. GOTO P225 P0 MOVLW 0 MOVWF ACC0 MOVLW 0 GOTO DUN .. ... P23 MOVLW 0xDC MOVWF ACC0 MOVLW 0x0B GOTO DUN ... .. P255 MOVLW 0x7C MOVWF ACC0 MOVLW 0x83 DUN MOVWF ACC1 ; *Advantages* ; Executes quickly ; Can be used for any multiplier up to 255 *; Disadvantages* ; Occupies large amount of code space ; Needs care over page boundries ; All multiplcand have to be pre computed and loaded - scope for error ________________________________________ David C Brown 43 Bings Road Whaley Bridge High Peak Phone: 01663 733236 Derbyshire eMail: dcb.home@gmail.com SK23 7ND web: www.bings-knowle.co.uk/dcb *Sent from my etch-a-sketch* On Sat, 18 May 2019 at 23:25, Dwayne Reid wrote: > Hi there, David. > > I'm not exactly sure what you are suggesting. I **think** that you > are suggesting that I create two lookup tables (one each for HI & LO > bytes), each with 256 entries. With overhead, that's about 520 code > spaces. Execution time for each table is about 10 cycles, plus the > overhead of putting the returned value into the proper > location. Lets say around 24 or 26 cycles. > > If that isn't what you are suggesting, could you enlighten me please? > > Many thanks! > > dwayne > > > At 02:44 AM 5/16/2019, David C Brown wrote: > >Look up tables? > >__________________________________________ > >David C Brown > >43 Bings Road > >Whaley Bridge > >High Peak Phone: 01663 733236 > >Derbyshire eMail: dcb.home@gmail.com > >SK23 7ND web: www.bings-knowle.co.uk/dcb > > > > > > > > > >*Sent from my etch-a-sketch* > > > > > >On Thu, 16 May 2019 at 08:38, Mike Rigby-Jones > >wrote: > > > > > We've not had a coding challenge in ages, I really miss those days! > I've > > > not used PIC assembler for a long time so I'm pretty rusty, so there > may be > > > a bonehead mistake in this: > > > > > > ; Input in ACC0 > > > ; 12 cycles, no temp variables > > > > > > clrf ACC1 ; Clear MSB > > > movf ACC0,w ; save ACC0 in w > > > clrc > > > rlf ACC0,f > > > rlf ACC1,f > > > rlf ACC0,f > > > rlf ACC1,f > > > rlf ACC0,f > > > rlf ACC1,f ; ACC1:ACC0 =3D 8 * input > > > addwf ACC1,f ; C + ACC1:ACC0 =3D 264 * input > > > rrf ACC1,f > > > rrf ACC0,f ; ACC1:ACC0 =3D 132 * input > > > > > > Regards > > > > > > Mike > > > > > > On Thu, 16 May 2019 at 00:45, Dwayne Reid > wrote: > > > > > > > Good day to all. > > > > > > > > I'm working on a project where I need to scale an 8-bit value to th= e > > > > range of (about) 0..34,000 or so. This is to feed the period > > > > register of a PWM generator - actual largest value is not > > > > critical. This routine needs to execute very millisecond and syste= m > > > > clock frequency is 4 MHz (1us instruction rate), so I'm opting to g= o > > > > with a simple integer-based fixed multiply routine. > > > > > > > > As always, I turn to Nikolai Golovchenko's wonderful ConstDivMul > > > > calculator whenever I need to scale some quantity by a fixed-value > > > > constant. It consistently generates code that is at least as tight > > > > and small as anything that I can create myself. > > > > > > > > This time, however, the routine I came up with seems to be much > > > > shorter / faster than Nilolai's version. I'm assuming that I someh= ow > > > > made a mistake but I'm not seeing it. I'm hoping that some of the > > > > math wizzes still on the list can give an opinion. > > > > > > > > Input quantity is a single byte (0..255) and is to multiplied by a > > > > fixed constant of 132 > > > > > > > > ; ACC =3D ACC * 132.000000 > > > > ; > > > > ; ALGORITHM: > > > > ; Clear accumulator > > > > ; Add input * 128 to accumulator > > > > ; Add input * 4 to accumulator > > > > ; Move accumulator to result > > > > ; > > > > ; Error in constant approximation : 0.000000, % > > > > > > > > > > > > ; Input: ACC0 (8 bits) > > > > ; Output: ACC0 .. ACC1 (16 bits) > > > > > > > > cblock > > > > ACC0 > > > > ACC1 > > > > TEMP0 > > > > TEMP1 > > > > endc > > > > > > > > clrc > > > > rlf ACC0, f > > > > clrf ACC1 > > > > rlf ACC1, f > > > > rlf ACC0, f > > > > rlf ACC1, f > > > > movf ACC0, w > > > > movwf TEMP0 > > > > movf ACC1, w > > > > movwf TEMP1 > > > > swapf TEMP1, f > > > > swapf TEMP0, w > > > > andlw 15 > > > > iorwf TEMP1, f > > > > swapf TEMP0, w > > > > andlw -16 > > > > movwf TEMP0 > > > > clrc > > > > rlf TEMP0, f > > > > rlf TEMP1, f > > > > movf TEMP0, w > > > > addwf ACC0, f > > > > movf TEMP1, w > > > > skpnc > > > > incfsz TEMP1, w > > > > addwf ACC1, f > > > > > > > > > > > > My version: > > > > > > > > ; Clear accumulator > > > > ; Add input * 128 to accumulator > > > > ; Add input * 4 to accumulator > > > > ; Move accumulator to result > > > > > > > > clrf TEMP0 > > > > clrf ACC1 > > > > clrc ;* 128 > > > > rrf ACC0,W ;b0 -> C > > > > movwf TEMP1 ; > > > > rrf TEMP0,F ;C -> b7 > > > > > > > > clrc ;* 4 > > > > rlf ACC0,F > > > > rlf ACC1,F > > > > rlf ACC0,F > > > > rlf ACC1,F > > > > > > > > movfw TEMP0 ;ACC1,0 + TEMP1,0 -> ACC1,0 > > > > addwf ACC0,F > > > > movfw TEMP1 > > > > skpnc > > > > incfsz TEMP1,W > > > > addwf ACC1,F > > > > > > > > > > > > Question: did I make some silly mistake or is my version reasonable= ? > > > > > > > > Anyone have any other optimizations to offer? > > > > > > > > Many thanks! > > > > > > > > dwayne > > > > > > > > -- > > > > Dwayne Reid > > > > Trinity Electronics Systems Ltd Edmonton, AB, CANADA > > > > 780-489-3199 voice 780-487-6397 fax 888-489-3199 Toll Free > > > > www.trinity-electronics.com > > > > Custom Electronics Design and Manufacturing > > > > > > > > -- > > > > http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive > > > > View/change your membership options at > > > > http://mailman.mit.edu/mailman/listinfo/piclist > > > > > > > -- > > > http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive > > > View/change your membership options at > > > http://mailman.mit.edu/mailman/listinfo/piclist > > > > >-- > >http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive > >View/change your membership options at > >http://mailman.mit.edu/mailman/listinfo/piclist > > > -- > Dwayne Reid > Trinity Electronics Systems Ltd Edmonton, AB, CANADA > 780-489-3199 voice 780-487-6397 fax 888-489-3199 Toll Free > www.trinity-electronics.com > Custom Electronics Design and Manufacturing > > -- > http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .