Showing posts with label instructions. Show all posts
Showing posts with label instructions. Show all posts

Tuesday, July 19, 2011

Divide to Conquer

Division has been a concern of mine for quite some time now. Being able to divide numbers is an important part of every day calculations, after all. You have fifty dollars. How many ten dollar items can you buy? That's the sort of thing you'd want to be able to figure out using a computer and could reasonably expect the system to be capable of. Yet, if you don't have a computer with a DIV instruction, you can't directly figure that particular class of problem out.

Multiplication presents a similar sort of problem. The M1 contains logic for doing a multiply instruction intelligently, however. If you use a ripple carry adder architecture and do some clever bit fiddling, you magically get a product from two inputs. The same can be done for division, but I've not yet figured it out.

I've decided to take the grade-school math route until I figure out more advanced methods that I seek and take advantage of the fact that division is functionally just a loop of subtraction operations. Keep track of how many times you were able to subtract the divisor from the dividend and you end up with the quotient. Whatever is left over that's too small for the divisor is your remainder.

There are obvious tradeoffs to this approach. First of all, the time to complete it is wholly dependent upon how large the quotient ends up being. The quotient interestingly ends up being a measure of system cycles consumed by the bulk of the operation. 40 / 5 would take 100 nanoseconds at the M1's current clock speed while 2,000,000 divided by 1,000 would take 25 microseconds (25,000 ns).

While not an ideal solution, it's important enough for this instruction to be there that I'll include this suboptimal method of obtaining quotients and remainders pending the development of a better method.