-2

I have the following instruction set for a simple computer

Instruction set

There is no remainder or modulus command in the above instruction set. So the last part of my project is to check whether the number n I got from previous calculations, which is maximum 16 bits, is divisible by 10. If it is then I must store the given number n into the data memory, if it isn't then the number is invalid, I have to use the instructions in the above list.

I can't get the logic to go about in checking whether n is divisible by 10 or not. I already know how to check whether n is even or odd, but that doesn't solve the divisibility problem.

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • 1
    Gasp... You might actually have to *implement* your own division function with the basic opcodes that are available. – David Hoelzer May 04 '16 at 03:48
  • 1
    [Implement division operator yourself](http://stackoverflow.com/q/5284898/995714), or check if the number is divisible by 2 **and** [by 5](http://stackoverflow.com/q/17113660/995714). [Divide by 10 using bit shifts](http://stackoverflow.com/q/5558492/995714), [divide by 10 in ARM](http://stackoverflow.com/q/16218228/995714), [C++ fast division/mod by 10^x](http://stackoverflow.com/q/2033210/995714) – phuclv May 04 '16 at 07:10

1 Answers1

0

the easiest way to divide by 10 is to repeatedly subtract 10, 'til the remainder is less than 10

result = 0
remains = divident
while (remains >= divisor)
    result++
    remains -= divisor

a more clever way is to use shift, where you repeatedly (the register width determines how often you have to shift) shift result and divident left, shifting the carry into a 2nd register. Whenever this 2nd register is lager or equal the divisor, subtract it from the 2nd, and increase the result by one.
(this similar to how decimals are divided at school, if you remember)

something like this (assuming 32 bit registers)

result=0
temp=0
loop 32 times
   shift left result
   shift left divident
   shift left with carry temp
   if (temp > divisor)
      result ++
      temp -= divisor
Tommylee2k
  • 2,683
  • 1
  • 9
  • 22