Is there any way to divide 64-bit integer by 10 on RARS?

433 views Asked by At

I have been trying to divide two 32-bit registers by 10. I know how to divide a register if a dividend is 32-bit like the following code, but I cannot solve if it's 64-bit.

div     s8, s0, a6 # divide a result by 10 (for example, 1234->123)
mul     s6, s8, a6 # multiply s8 by 10 (for example, 123->1230)
sub     s7, s0, s6 # the result - s6 (for example, 1234-1230->4)
beqz    s7, output # if s7 is zero, jump

And I found a tips that

The division is more tricky. You may either implement regular binary division by comapare, shift and subtract or use another method: Think of a 64bit number ab (where a and be are 32-bit numbers) as of a * 2^32 + b. Express 2 ^32 as x * 10 + y. Then ab / 10 = x + (y *a + b) / 10. If you apply this method twice, you may reduce 64-bit division by 10 to a 32-bit one.

But I cannot understand why I can get a 32-bit number if I apply this method twice.

0

There are 0 answers