Propeller manual pasm division

I am having issues with this code example. I cannot get the remainder and' ing and shifting as Dave at parallax advised. If I divide an even number all is good. An odd number does not work and I get gobbelygook.

Here is their code: page 380 of the propeller manual

' Divide x[31..0] by y[15..0] (y[16] must be 0)
' on exit, quotient is in x[15..0] and remainder is in x[31..16]
'
divide shl y,#15 'get divisor into y[30..15]
mov t,#16 'ready for 16 quotient bits
:loop cmpsub x,y wc 'y =< x? Subtract it, quotient bit in c
rcl x,#1 'rotate c into quotient, shift dividend
djnz t,#:loop 'loop until done
divide_ret ret 'quotient in x[15..0],
'remainder in x[31..16]

Comments

  • 11 Comments sorted by Date Added Votes
  • Search for my faster spin Interpreter. It has an optimised PASM divide IIRC. My signature may have a link too.
    My Prop boards: P8XBlade2, RamBlade, CpuBlade, TriBlade
    Prop OS (also see Sphinx, PropDos, PropCmd, Spinix)
    Website: www.clusos.com
    Prop Tools (Index) , Emulators (Index) , ZiCog (Z80)
  • Clusco,

    It looks like the links in your signature are broken (maybe because of the n-th forum upgrade?).

    Anyway, as an example the Prop Tools (Index) link is currently:
    http://forums.parallax.com/showthread.php?t=106790
    

    ...and might need to be instead:
    https://forums.parallax.com/discussion/106790
    

    Hope you take this as a friendly observation. Always have valued your code, comments, and commitment to the Prop!

    -joe
  • to octetta I will look at what you sent. for Cluso99 broken links.
    thanks to both
  • The divide routine is fine, but you make a mess with the variablepointers and the wrlong parameters.

    Here is a modified version that works for me on a Quickstartboard:
    CON
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000          'QUICKSTART 80 MHZ  NORMAL CRYSTAL
    
    var
       long dividend         'VARIABLE IN THE PAR ADDRESS TO BE PASSED
       long divisor
       long quotient
       long remainder
    
    obj
      pst : "parallax serial terminal"
    
    pub main
       dividend := 21
       divisor := 2
       pst.start(115200)
       waitcnt(clkfreq*5 + cnt) 'hold five sec to open the
                                'serial terminal and enable it
       cognew(@asm,@dividend)   'start cog at the first variable address
       waitcnt(clkfreq + cnt)   'give top object time to catch up to pasm
    
    
       pst.str(string("quotient:"))
       pst.dec(quotient)
          
       pst.newline
       pst.str(string("remainder:"))
       pst.dec(remainder)
          
       pst.newline
    
    
    dat
    
    
    asm       org
    
              mov tempvar, par      'get the par address into the temporary variable
              rdlong x, tempvar     'read the value into the dividend
              add tempvar, #4       'move over to the next long to get the divisor variable
              rdlong y, tempvar     'read the value of the divisor into the variable
              add tempvar, #4       'move over to the next long to get the quotient address
              
    
    ' Divide x[31..0] by y[15..0] (y[16] must be 0)
    ' on exit, quotient is in x[15..0] and remainder is in x[31..16]
    '
    divide    shl y,#15             'get divisor into y[30..15]
              mov t,#16             'ready for 16 quotient bits
    :loop     cmpsub x,y    wc      'y =< x? Subtract it, quotient bit in c
              rcl x,#1    'rotate c into quotient, shift dividend
              djnz t,#:loop         'loop until done
              
    '  quotient in x[15..0],  ;return if used as a subroutine
    '  remainder in x[31..16]
    
              mov    quotientvar,x
              and    quotientvar,andvar2    'isolate lower 16 bits
              wrlong quotientvar,tempvar    'write into Spinvar 'quotient'
    
              mov    remaindervar,x
              shr    remaindervar, #16      'isolate higher 16 bits
              add    tempvar,#4             'incr pointer to remainder address
              wrlong remaindervar,tempvar   'write into Spinvar 'remainder' 
    
    andvar2       long $ffff
    tempvar       res 1
    x             res 1
    y             res 1
    quotientvar   res 1
    remaindervar  res 1
    t             res 1
    

    Andy
  • Thanks for the broken links feedback. I'll fix as soon as I get time on a real computer.
    My Prop boards: P8XBlade2, RamBlade, CpuBlade, TriBlade
    Prop OS (also see Sphinx, PropDos, PropCmd, Spinix)
    Website: www.clusos.com
    Prop Tools (Index) , Emulators (Index) , ZiCog (Z80)
  • No worries. Seems this is an endemic problem, so you're not the only one.

    Any hope the forum moderators could to a global search and replace for these "showthread.php?t=" things?

    I'm not even sure who to reach out for matters like that...
  • octetta wrote: »
    No worries. Seems this is an endemic problem, so you're not the only one.

    Any hope the forum moderators could to a global search and replace for these "showthread.php?t=" things?

    I'm not even sure who to reach out for matters like that...
    Don't bother wasting you time.
    My Prop boards: P8XBlade2, RamBlade, CpuBlade, TriBlade
    Prop OS (also see Sphinx, PropDos, PropCmd, Spinix)
    Website: www.clusos.com
    Prop Tools (Index) , Emulators (Index) , ZiCog (Z80)
  • PublisonPublison Posts: 10,471
    edited November 26 Vote Up0Vote Down
    Unfortunately, the moderators can not perform things like that. Only webmaster@parallax.com can do that.
    Believe me, I would love to have that capability because I HATE broken links.
    Infernal Machine
  • I will try this tonight. I have been writing a tutorial and thank y'all a lot.
    I will post a version of it soon.
  • Hey guys, I have been researching the pasm stuff in the forums. The broken links are too numerous to count. I have mentioned this to parallax, but I don't think that they will work on it. They have changed the website too many times and the links are not a priority.
  • Works great. Thanks for the help.
Sign In or Register to comment.