Shop OBEX P1 Docs P2 Docs Learn Events
help with pasm subroutines — Parallax Forums

help with pasm subroutines

I have been trying to get the math from the book to work as a subroutine as advertised. Tried several combinations and I cannot get them to work. Would someone please look at this as a second set of eyes.
Thanks.

It appears that it should be simple. I have a incrementing count program working but these will not work.

Again thanks

Comments

  • AribaAriba Posts: 2,682
    You still don't stop the cog at the end of the code. And you call the writer-subroutine at a point that never shoud be reached (between the subroutines).

    Also the unnecessary "mov x_var, temp_var" lines are still there. This will confuse a reader of your tutorial.

    Here is your 2 subroutine code with better indentions and the above issues corrected. I also renamed the temp_var to varptr, because that describes it better.
    {{Multiplication based on the propeller manual page 380}}
    
    CON
       _clkmode = xtal1 + pll16x
       '_xinfreq = 6_250_000   'MY BOARD AT 1OOMHZ  DIFFERENT CRYSTAL
       _xinfreq = 5_000_000  'QUICKSTART 80 MHZ  NORMAL CRYSTAL
    
    var
       'Spin variables
       long x             'we pass this address in par at cognew
       long y
       long product
       
    obj
       pst : "parallax serial terminal"
    
    pub main
       x := 3
       y := 9
       pst.start(115000)
       waitcnt(clkfreq*5 +cnt)      'hold five sec to open the
       'serial terminal and enable it
       cognew(@asm,@x)              'start cog at the first variable address
       waitcnt(clkfreq*2 +cnt)      'give pasm time to do the work
       
       pst.str(string("product:"))  'show a string on terminal
       pst.dec(product~)            'show decimal value of product on terminal
       pst.newline
         
    
    dat
    '
    asm                  org
    
                         mov varptr, par            'move par to the local varptr (points to x-var)
                         rdlong x_var, varptr       'read in the x value from Spin vars
                         add varptr, #4             'incr varptr to next long which is the y-var address
                         rdlong y_var, varptr       'read in the y value
                         add varptr, #4             'inc varptr to the product variable address
    
                         call #multiply             'multiply x with y
                         call #writer               'and write result to product var
    
                         cogid   t                  'stop the cog
                         cogstop t
    
    ' Subroutines
    
    multiply             shl x_var,#16              'get multiplicand into x[31..16]
                         mov t,#16                  'ready for 16 multiplier bits
                         shr y_var,#1 wc            'get initial multiplier bit into c
    :loop          if_c  add y_var,x_var wc         'if bit set, add multiplicand to product
                         rcr y_var,#1 wc            'put next multiplier in c, shift prod.
                         djnz t,#:loop              'loop until done                     
    multiply_ret         ret                        'return with product in y[31..0] 'this would be a subroutine
                         
    writer               wrlong y_var, varptr       'write the product from y[31..0] to the
    writer_ret           ret                        'product variable for the top object
    
    
    varptr               res   1                    'reserve space for cogregisters
    x_var                res   1
    y_var                res   1
    t                    res   1
    

    Andy
  • Thanks for the help in cleaning it up. I will look at your suggestions. I will study it to see what I missed.
  • BTW I get the cogid and cogstop to not leave the cog open.
Sign In or Register to comment.