1

I have to find log time algorithm (for a virtual machine) using maximally 6 registers (a,b,c,d,e,f) to divide two unsigned integers N1 and N2( [both are >=0] positive or 0) where if divider is 0 than result is 0 and modulo operation.

  • div -> N1/N2
  • mod -> N1 % N2

with commands like

  • RESET a -> a =0
  • ADD a b -> a = a+b
  • SUB a b -> a = max(0,a-b)
  • SHR a -> a=floor(a/2)
  • SHL a -> a=floor(a*2)
  • INC a -> a+=1
  • DEC a -> a=max(0,a-1)
  • JUMP j -> jump to j-th line
  • JZERO x j -> if x is 0 than jump to k+j
  • JODD x j -> if x is odd than jump to k+j

Are there any algorithms that can help me ?

I can only check if value in reg is ODD or ZERO.

Thank you for help.

1 Answer 1

1

Saturating-subtract and jzero allows compare for less-than-or-equal (or greater-than), so you can implement the C version of njuffa's answer on How can I multiply and divide using only bit shifting and adding? which produces quotient and remainder. Since you have non-saturating add, you can implement the wrapping add (and then do manual carry-out detection by checking for wraparound, as Nathan does it in C.)

jodd lets you test the low bit, like if (x&1), which would let you implement the standard multiply algorithm as well. So if you had a division algorithm that only gave you a quotient, you could do remainder = dividend - quotient*divisor with a log-time multiply.


Other binary division Q&As:

1
  • Thank you for help :)!
    – sqoshi
    Dec 2, 2020 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.