Friday 3 December 2010

Electronics for Dogs 2 - The very fast idiot

So moving on after those initial thoughts lets jump into what that processor at the heart of every computer is. I mentioned it before, the very fast idiot.
So imagine you have an employee working for you. He's as thick as two short planks but he's eager. Let's assume that he always follows instructions given to him, but don't ask him to make any judgement calls himself because he'll normally get it wrong. So what do you do, this guy is so stupid he can't even use a calculator to add up numbers and struggles to answer the phone correctly.
Never mind you are a thoughtful employer so you decide to write out long lists of instructions for him. It goes something like:

if the phone rings then lift the receiver. Then place the bit with the cord coming out of it next to your mouth and the other end of the receiver next to your right ear.

Ask the person at the other end who they are and who they want to speak to. Write this down on a piece of paper you find here using a pen here. Then replace the paper there and the pen there so you'll be able to find them again. Next consult this diagram for where you might find that that person. Follow the instructions on that diagram then give it to that person.
If that person is nor available consult this diagram provided here to work out who else to ask about it. Once you have done all that wait for the phone to ring again and start from the beginning.

This may sound over the top, but your processor at the heart of your computer is even more stupid than this. Hearing something and writing it down is still after decades of work still a problem for computers. Why? Because they are monumentally stupid (in the traditional numan sense) but they are very fast and very very reliable. It's often said that the only time a computer genuinely makes a mistake is because someone has told it to make that mistake.
So let me strip this down further the average processor can do the following things:

Add one number to another
Copy a value from one place to another.
Test if one value is equal, greater than or smaller to another number. Then "jump" to a different place in the instructions depending on the result.
And, Xor, Or two numbers together (for those of you thinking in decimal this doesn't make much sense. For the following discussion lets assume that it can do a number select that is if I asked for the 3rd digit in 5467 I would report the number 4)
Bit shifts (basically multiply or divide by the base. So in decimal terms the ability to multiply or divide by 10. i.e. take the number and add a zero to it or remove the first digit. Wow 56 * 10 is 560 - clever computer...)

And that's about it. Everything your computer does can be accomplished with those few basic abilities.
Let's go back to our very reliable idiot. You give him a whiteboard with 8 boxes on it labelled A to H. You've given up trying to teach him how to multiply numbers so instead you're going to write him some instructions. You're going to teach him long multiplication, but not tell him you're doing that.
The instructions might go something like this:

When writing a number in a box always replace the value there with the new one.
Write 0 into box H
Write the first number in box A
Write the second number in box B
Write the number 1 in box C
Write the number 1 in box D
* Using the number in box C look at the value in box A and select the number correlating to that
Put this value in box E
Using the number in box D look at the value in box B and select the number correlating to that
Put this value in box F
In the Table go across E rows and down F rows Select that number and place it in box G
add one less than the number of zeroes indicated in box C (this is the bit shift we talked about before)
add this to the number in box H and place the result in box H
Is the number in box C the same as the number of digits (Yes a computer can't do this, but it's a small cheat.)
If it is then you're finished
If it isn't then
add 1 to the number in box C
add 1 to the number in box D
Go back to *

Ok there's a small cheat there but you get the idea of how to build something like multiplication up from smaller steps. If we go back to the first lesson you'll see the opposite of this where we tried to break down something into simple steps. At some point we meet in the middle and where we have to have things like displays and keyboards and mice. But that is a subject for the next post.
What I'm trying to get at here is that if your tame idiot was fast enough he'd be able to multiply perfectly fine just using very simple operations.

Let me just sneak in an extra concept here while you're not looking. Now that I have instructions on how to multiply two numbers I never have to tell the idiot how to do it again. If for example later on I was writing instructions on how to do a balance sheet then I'd just say multiply these two numbers using these instruction. To use computing parlance that would be a subroutine or a function call. But that's a bit unwieldy I'd rather say something like multiply number A by number B. If I was getting really generic and lazy I might prefer to write multiply(A, B). A suitably instructed idiot would know to look for a set of instructions labeled multiply and then apply those instructions to A and B. This way of expression has the advantage that it works for square root, sqrt(A); divide (A, B), or anything else I might want to do with two numbers, print (A); perhaps I'm writing a computer game and I need to draw a circle of diameter A of colour B at location on the screen X and Y, draw_circle (A, B, X, Y)

But that really is a topic for the next post. I keep saying all a computer knows is numbers but how can everything a computer does be just numbers? Another one of those things that's really simple once you get it, but if you don't then it all seems so impenetrable. So lets get that out of the way next time.

No comments: