Shop OBEX P1 Docs P2 Docs Learn Events
16 bit multiply in PASM - another question — Parallax Forums

16 bit multiply in PASM - another question

Chris_DChris_D Posts: 305
edited 2010-03-13 00:05 in Propeller 1
Hello,

I have been working in PASM for a few months now and I really like it.· I can handle the basic stuff pretty good but cannot seem to grasp how to use the example multiplication and division routines referenced in the manual (Appendix B, Math samples and Function tables).

It appears they have to be treated as a subroutine and called which I have not figured out how to do.· Does someone know of an example program that is VERY simple in nature that takes a couple of variables and performs the multiplication on them?·

Ultimately, what I need to do is square two variables.· Both variables will be able to fit in 16 bit vars so it looks like the multiplication routine would be best suited.· However, expecting to do division in PASM at some point in time, I hope to learn how to use both of those routines at the same time.

Thanks

Chris

Post Edited (Chris_D) : 3/12/2010 11:26:14 PM GMT

Comments

  • heaterheater Posts: 3,370
    edited 2010-03-10 14:22
    All you have to do is set up the x and y values that multiply is going to work on and then call it. The result ends up in y.
    Of course you have to define the LONGs to hold your numbers and x, y and the temporary variable t that multiply uses.

    enter
            mov   x, someNumber_1      'Set x parameter of multiply to some number
            mov   y, someNumber_2      'Set y parameter to some other number
            call  #multiply                         'Do the multiply
            mov   result, y                         'Result is in y
    
    '' Multiply x[noparse][[/noparse]15..0] by y[noparse][[/noparse]15..0] (y[noparse][[/noparse]31..16] must be 0)
    ' on exit, product in y[noparse][[/noparse]31..0]
    '
    multiply        shl     x,#16            'get multiplicand into x[noparse][[/noparse]31..16]
                    mov     t,#16            'ready for 16 multiplier bits
                    shr     y,#1      wc     'get initial multiplier bit into c
    :loop if_c      add     y,x       wc     'if c set, add multiplicand to product
                    rcr     y,#1      wc     'put next multiplier in c, shift prod.
                    djnz    t,#:loop         'loop until done
    multiply_ret    ret                      'return with product in y[noparse][[/noparse]31..0]
     
    someNumber_1     long      1234
    someNumber_2     long      5678
    result                        long      0
    
    x             long  0
    y             long  0
    t             long  0
    
    



    If you are multiplying by small constant (< 9 bits) value put it directly in the mov instruction e.g.

    mov someNumber, #42

    Don't forget the example multiply in the manual is for positive numbers only.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • Chris_DChris_D Posts: 305
    edited 2010-03-10 15:27
    Heater,

    Thanks very much for responding.· That is exactly what I needed and more.· I completely forgot about the positive only values.· I suspect that I can work that out in the rest of the code without too much trouble though.

    Thanks again!

    Chris
  • Chris_DChris_D Posts: 305
    edited 2010-03-12 23:23
    I have another question about this multiply routine.· Is there an average number of clock cycles it takes to run?

    If an example value is needed and example would be 3500 x 3500

    Thanks

    Chris
  • Mike GreenMike Green Posts: 23,101
    edited 2010-03-12 23:56
    The subroutine takes 216 system clock cycles (12 * 16 + 24) including the call. The other 3 mov's take another 12 clock cycles. The time is not dependent on the values used.
  • Chris_DChris_D Posts: 305
    edited 2010-03-13 00:05
    Mike,

    Thanks much for answering my question (and the follow up before I even asked it)!

    Knowing that the time is a constant helps a lot as I work through a new program I am writing.· It will be a time-critical program and knowing the times of all the "little things" is of great importance.

    Thanks again

    Chris
Sign In or Register to comment.