To divide by any power of 2, you only need to shift right as many times as needed. For example,
Number rmb 2
...
lsr Number
ror Number+1
lsr Number
ror Number+1
will divide a 16-bit number by 4 (two right shifts).
To divide by any 8-bit value, you can use the DIV instruction. For example,
Number rmb 2
...
ldhx #4 ;H=0 (always), X=divisor
lda Number
div
sta Number
lda Number+1
div
sta Number+1
(the example above writes over the original number. You could use the stack or other variable to save the quotient, instead). Note: The divisor in this case does not need to be a power of two, it can be any value from 1 to 255 (with 0 giving an error.)
When done, register H will contain the final remainder of the multi-byte division. You can use this method to divide any byte-lentgh numbers with an 8-bit number.
For larger divisor, you will have to use shift-and-subtract methods.
Message Edited by tonyp on
2007-12-10 12:35 PM