Shop OBEX P1 Docs P2 Docs Learn Events
PASM & Multiplication and Division.... — Parallax Forums

PASM & Multiplication and Division....

Chris_DChris_D Posts: 305
edited 2009-10-20 11:58 in Propeller 1
Hi folks,

I have a routine in SPIN that I strongly suspect will run better in PASM.· I have good success converting many tasks to PASM from SPIN but multiplication and division stumped me completely.· I found a few references (wikipedia and Dsilvas texts) on routines but can't make sense of them or how to use them.·

Here is what I am trying to do ....

Var_1 ' can be a value from 0 to 200000
Var_2 ' can be a value from 0 to 10000
Var_3 ' can be a value from 100 to 5000

Var_V := ((Var_1 * Var_1) - (Var_2 * Var_2)) / (2 * Var_3)

The above statement is in SPIN of coure.

From reading through the examples and references, there are limits to the max value for the result so I felt it important to show the ranges of data I am trying to work with.

I suspect this might be a BIG help for all of us PASM newbies if we could get a clear explanation of how to do this.

Thanks in advance

Chris
·

Comments

  • LeonLeon Posts: 7,620
    edited 2009-10-18 19:28
    This might help:

    http://forums.parallax.com/showthread.php?p=828096

    Leon

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Amateur radio callsign: G1HSM
    Suzuki SV1000S motorcycle
  • ericballericball Posts: 774
    edited 2009-10-18 20:33
    In SPIN, all calculations are done as 32 bit signed values or +/-2,147,483,647, so it's important to keep all intermediate values in that range.
    Chris_D said...
    Var_1 ' can be a value from 0 to 200,000
    Var_2 ' can be a value from 0 to 10,000
    Var_3 ' can be a value from 100 to 5,000
    Var_V := ((Var_1 * Var_1) - (Var_2 * Var_2)) / (2 * Var_3)
    Var_1 is an 18 bit value, Var_2 is a 14 bit value. Var_3 is a 13 bit value
    Var_1 ^2 is therefore a 36 bit value (overflow!), Var_2 ^2 is a 28 bit value, and 2 * Var_3 is a 14 bit value. So you're going to have a problem in SPIN with overflow. You can probably do it using * and **, but I'm getting a headache trying to work out the code.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Composite NTSC sprite driver: Forum
    NTSC & PAL driver templates: ObEx Forum
    OnePinTVText driver: ObEx Forum
  • localrogerlocalroger Posts: 3,452
    edited 2009-10-18 21:23
    Chris, I have extracted the multiply routine from the Spin interpreter which was published some time ago;·it's not very long and give you basically the same capability in PASM you have in Spin.· The original source is here:

    http://forums.parallax.com/showthread.php?p=711064

    And this is how I extracted it...

    ' 32 to 64 bit signed multiply asm_x by asm_y
    ' asm_y absvaled, result in t1:asm_x
    '
    multiply
                  abs       asm_x,asm_x  wc
                  muxc      asm_n,#1
                  abs       asm_y,asm_y  wc,wz 
            if_c  xor       asm_n,#1
                  mov       t1,#0
                  mov       t2,#32
                  shr       asm_x,#1     wc
                  
    :mloop  if_c  add       t1,asm_y     wc
                  rcr       t1,#1        wc
                  rcr       asm_x,#1     wc
                  djnz      t2,#:mloop
                  test      asm_n,#1     wz
            if_nz neg       t1,t1
            if_nz neg       asm_x,asm_x  wz
            if_nz sub       t1,#1
                  
    multiply_ret  ret
                                  
                  
    asm_x                   long                    $0                              ' should typically equal 0
    asm_y                   long                    $0
    asm_n                   long                    $0
    t1                      long                    $0
    t2                      long                    $0
    
    

    I thus far haven't had a need for DIV but I know where to go when I do.
  • Chris_DChris_D Posts: 305
    edited 2009-10-19 16:36
    Thanks guys.· That one link to the SPIN source code might be very helpful.

    Chris
  • DroneDrone Posts: 433
    edited 2009-10-20 11:58
    @localroger,

    Your code comment says, "32 to 64 bit signed multiply". Does this really do 64 bits? If so perhaps you can provide an example; a little wrapper that makes this an application perhaps? I'm new to PASM.

    Thanks, David
Sign In or Register to comment.