Shop OBEX P1 Docs P2 Docs Learn Events
ASM division — Parallax Forums

ASM division

tom90tom90 Posts: 55
edited 2008-10-14 20:14 in Propeller 1
Hey All,

Some of you helped me some time ago with some simple ASM code for division.· It works just fine when I am dividing by a small number (25), but now I am trying to divide by a larger number (1135) and this code doesn't seem to be working correctly.· The main problem is that I still dont completely understand how this code is working.· Can anybody give any insight into how this code works, and possibly how to get it working with 1135 as the divisor (x).·

                 mov       y,"x"                           'divide by "x" 'get divisor into y[noparse][[/noparse]30..15]  *****I want x = 1135*******
              shl       y,#15      
              mov       divcounter,#16                'ready for 16 quotient bit
                            
:divloop      cmpsub    getsdivided,y   wc            'if y =< x then subtract it, quotient bit into c
              rcl       getsdivided,#1                'rotate c into quotient, shift dividend
              djnz      divcounter,#:divloop          'loop until done 

Thanks
Tom

Comments

  • Ziggy252Ziggy252 Posts: 32
    edited 2008-10-13 22:25
    It looks like you're trying to pass x as a literal. If so, x must be able to fit in 9 bits (ie x<512).
    You'll need to allocate a long to x elsewhere in the cog and preset it to 1135
  • JasonDorieJasonDorie Posts: 1,930
    edited 2008-10-13 22:29
    Basically this code is using repeated comparison and subtraction of your value shifted left by some amount.· If you were to unroll the code, it would look like this:


    if( getsDivided <= (divisor << 15) )
    · getsDivided -= (divisor << 15)
    · result += (1 << 15)

    if( getsDivided <= (divisor << 14) )
    · getsDivided -= (divisor << 14)
    · result += (1 << 14)


    ...and so on, down to 1.· It starts by shifting the divisor up by only 15 bits, which means that you couldn't divide 16 million by 2, because the 2 wouldn't get shifted up enough to start the division properly.

    Another, possibly better way to do it would be to start it by figuring out where the first 'on' bit of the divisor is, like this:

    int initialShift = 31
    while( (divisor OR initialShift) <> initialShift )
    · initialShift--

    Then use the resulting initialShift instead of '15', and change the loop counter to initialShift+1.· Does that make sense?

    Jason
  • Cluso99Cluso99 Posts: 18,069
    edited 2008-10-14 06:58
    Chip published the spin Interpreter and it contains code to do division. See my thread "Spin Interpreter - faster"
  • tom90tom90 Posts: 55
    edited 2008-10-14 19:37
    Thanks for the help everyone

    I just realized that I had my two values switched around when I was trying to calculate it out by hand.

    Thanks Again
    Tom
  • AleAle Posts: 2,363
    edited 2008-10-14 20:14
    You can always use a simulator smile.gif, or have a look at my MATH page at propeller.wikispaces.org/MATH
Sign In or Register to comment.