Shop OBEX P1 Docs P2 Docs Learn Events
Assembly Divide Routine — Parallax Forums

Assembly Divide Routine

tekochiptekochip Posts: 56
edited 2007-08-06 21:19 in Propeller 1
I did a search and didn't come up with one, so here's the one I wrote:


Divide
              mov      x, #27                   'Setup initial conditions
              mov      y, #9                    'for x/y
              mov      divCounter, #32          'Number of bits to shift clears the Z flag
              mov      rem, #0                  'Clear the remainder to start

:Loop
              shl      x, #1 wc, nr             'Just test to see if B31 set
              rcl      rem, #1                  'Shift B31 of x into remainder
              cmpsub   rem, y wc                'Subtract if we can and set flag if we did
              rcl      x, #1                    'Rotate subtraction result into x
              djnz     divCounter, #:Loop       'Rinse and repeat

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-06 20:23
    There's a writeup that includes multiplication, division, and square root routines among other things. Search Parallax for "Propeller Guts".
  • deSilvadeSilva Posts: 2,967
    edited 2007-08-06 21:19
    Don't forget to add support for signed operations smile.gif

    It generally makes sense to additionally make a version for 16 bit division (dividing by small numbers is much more common!); if you don't know statically you can check dynamically - it will cut the time needed in halves.. You can extend this idea to 8 bits, as it is just DIVCOUNTER that has to change...
Sign In or Register to comment.