16-bit value multiplied by 128

Discussion in 'Embedded Systems and Microcontrollers' started by atferrari, Apr 17, 2011.

  1. atferrari

    Thread Starter AAC Fanatic!

    Jan 6, 2004
    2,648
    762
    PIC 18F family.

    Three registers ACC_U, ACC_H, ACC_L

    To multiply the 16-bit value stored in ACC_H, ACC_L by 128, I am currently repeating the snipet below, 7 times

    Code ( (Unknown Language)):
    1. BCF STATUS,C               ;ensure that LSB will get a 0 into b0
    2. RLCF ACC_L,F                ;rotate to left and retain new value.
    3. RLCF ACC_H,F                ;rotate to left and retain new value.
    4. RLCF ACC_U,F                ;rotate to left and retain new value.
    which, no surprise, takes 28 lines of code.

    Do you know of any trick to do it faster?
     
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    It can be done this way:

    Code ( (Unknown Language)):
    1. ; ACC = ACC * 128
    2. ; Temp = TEMP
    3. ; ACC size = 16 bits
    4. ; Error = 0.5 %
    5. ; Bytes order = little endian
    6. ; Round = no
    7.  
    8. ; ALGORITHM:
    9. ; Clear accumulator
    10. ; Add input * 128 to accumulator
    11. ; Move accumulator to result
    12. ;
    13. ; Approximated constant: 128, Error: 0 %
    14.  
    15. ;     Input: ACC0 .. ACC1, 16 bits
    16. ;    Output: ACC0 .. ACC2, 23 bits
    17. ; Code size: 7 instructions
    18.  
    19.     cblock
    20.     ACC0
    21.     ACC1
    22.     endc
    23.  
    24. ;shift accumulator left 7 times
    25.     clrc
    26.     rrf ACC1, w
    27.     movwf   ACC2
    28.     rrf ACC0, w
    29.     movwf   ACC1
    30.     clrf    ACC0
    31.     rrf ACC0, f
    32.  
    33.  
    34. ; Generated by [URL="http://www.piclist.com/cgi-bin/constdivmul.exe"]www.piclist.com/cgi-bin/constdivmul.exe[/URL] (1-May-2002 version)
    35. ; Sun Apr 17 18:03:56 2011  GMT
     
  3. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    Surely multiplying by 128 in an 8-bit machine is better done by dividing by 2?

    Right-shift ACC_H and ACC_L. Hold onto the Cy bit that shifted out of ACC_L. Then move ACC_H to ACC_U, ACC_L to ACC_H, clear ACC_L and put the value of Cy into its top bit.

    Done.
     
  4. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    I don't think you posted your question on enough forums Agustín (lol)...

    Regards, Mike
     
Loading...