On Sep 2, 2010, at 12:35 PM, Andres Vahter wrote:

mov.w R12,R13 ; The operand "input" in register R12
           rla.w     R13
           add.w     R12,R13               ; X1=X*2^1+X
           rla.w     R13
           rla.w     R13
           add.w     R12,R13               ; X2=X1*2^2+X
           rla.w     R13
           add.w     R12,R13               ; X3=X2*2^1+X
           rla.w     R13
           add.w     R12,R13               ; X4=X4*2^1+X
           rla.w     R13
           rla.w     R13
           rla.w     R13
           add.w     R12,R13               ; Final Result=X5=X4*2^3+X

It computes  (((X*2 + X)*4 + X)*2 + X)*2 + X)*8 + X
           =     (12X)     + X)*2 + X)*2 + X)*8 + X
           =          26X         + X)*2 + X)*8 + X
           =                54X          + X)*8 + X
           =                     440X           + X
           = 441*X

Which is what it said at the top; multiplies 41 * 441.
It looks to me like a pretty standard multiplication algorithm, only since one argument is a know constant, you get to leave out the steps that would involve adding 0.

I've seen code generators for other microcontrollers that claim to generate the optimal sequence for multiplying a register by any constant. It should be possible to do for MSP430 too. Perhaps the C compiler already does so? (probably not; I've also seen complaints that gcc does a poor job of multiplying by constants.)

BillW


Reply via email to