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

Need simple ASM division algorithm

KyeKye Posts: 2,200
edited 2010-07-31 16:43 in Propeller 1
Like the title says.

I tried making my own but I messed it up.

                        mov     mathResult,      #0                         ' Setup.
                        shl     mathArgument,    #16                        '
 
divideLoop if_nz        shr     mathArgument,    #1                         ' Preform division.
                        cmpsub  sampleBuffer,    mathArgument wc            '
                        rcl     mathResult,      #1                         '
                        tjnz    mathArgument,    #divideLoop                '
 
sampleBuffer            res     1
mathArugment            res     1
mathResult              res     1


This code would work if it knew when to exit but it always·shifts in·extra junk after it accumulates the answer. I was trying to avoid a counter but I see that's not really possible.

What I'm aiming for is the least amount of ASM instructions used. Not the fastest execution time. I would also prefer that the result appear in the mathArgument variable.

Also, this algorithm doesn't really do what I want. I need to be able to divide two unsigned·32 bit numbers by each other. The results of those divisions will be in the 16 bit to 8 bit area.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,

Comments

  • JavalinJavalin Posts: 892
    edited 2010-07-31 16:43
    this works for me:
    asmdivide32bit          mov     t1,#0                   ' no idea
                            mov     t2,#32                  ' multiply/divide
                            
    :mdiv                   shr     y,#1            wc,wz   ' divide
                            rcr     t1,#1
                if_nz       djnz    t2,#:mdiv
    :mdiv2                  cmpsub  x,t1            wc
                            rcl     y,#1
                            shr     t1,#1
                            djnz    t2,#:mdiv2
    asmdivide32bit_ret      ret  
    

    called like:

                            ' divide SUM by SIZE_OF_ROLLING_AVG
                            mov     x,sum                                           ' x
                            mov     y,RollingAvgSize                                ' y
                            call    #asmdivide32bit                                 ' y = x/y
                            mov     sum, y
    

    James
Sign In or Register to comment.