Assembly Divide Routine
tekochip
Posts: 56
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
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...