I understand that computers are basically a complex system of electrical signatures that can calculate based on logic boards, and some sort of gate mechanism, but how do computers process something like if the number produced by the keyboard is less than 10, or if a mouse is clicked two times in a certain amount of time it equals a double click?
-
2I am currently reading "Code" by Charles Petzold. You can see a sample chapter on his website (charlespetzold.com/code). The book is fantastic and explains in a well-paced, friendly fashion how a (hypothetical) computer can be built from electrical relays and switches. I think it will help you with answering your question.– leanczCommented Mar 4, 2013 at 8:58
-
There's a lot of levels of abstraction between "tell if keyboard input is a number less than 10" and what's going on at the transistor level - like more than a semester at college worth. Here's a course that tries to explain how a computer works from the bottom up : nand2tetris.org– Sean McSomethingCommented Mar 4, 2013 at 20:40
2 Answers
Not totally sure where you are going with this, and it depends on the hardware The way it all started was.
There's special register in the CPU called Flags Less than is done by a Subtraction. Subtraction is done by addition with two's complement representation. In two's complement negative numbers (bearing in mind overflow) the most significant bit is always 1
So a less than test is Just a - b. The most significant bit of the result is copied to the N bit of the flags register and then that is tested.
There's usually a zero bit to test if A = B and an Overflow bit. A few others as well Carry for istance.
Overflow would be say your number is a signed byte that gives you -128 to + 127 (256 different numbers). so -128 - 1 won't fit and the overflow bit gets set.
There's a load more to this in modern processors, but the basics are still true.
This sort of stuff is a lot easier to pick up in far more basic CPUs than we use now. I learnt it on the Z80 back in 76...
The mouse click one Is store the "time" of the last click. Then on next click subtract the above and then compare it with the interval you stored for how fast you had to click for it to be a double click.
-
Thanks for the explanation, but how exactly does it "compare"? Commented Mar 3, 2013 at 14:59
-
Basically, the arithmetic-logic unit has an intricate network of single-bit data lines that is arranged so that a specific 'less than 0' flag receives voltage exactly in those cases where the first subtraction operand is smaller than the second one. Then a jump instruction then simply checks that flag. So on the lowest level, arithmetic is really just electrical engineering, with the numbers represented by arrays of binary voltage values. (Processor architecture is awesome, you should check it out!) Commented Mar 3, 2013 at 15:13
-
Wow, kind of mind boggling. Thanks for being so thorough. Commented Mar 3, 2013 at 15:28
-
Compare at the level of abstraction we are working at is a - b and then test where the most significant bit is 0 or 1. In terms of logic circuits its a bit more detailed. Could be worse though you could looking at from the point of view of Quantum tunnelling. Commented Mar 3, 2013 at 15:29
-
Tony is right about subtraction - for a lot of processors, there's a compare instruction that's exactly the same as subtraction except the resulting difference is discarded. However, that result was used to set up the flags register - not just the most significant (sign) bit but also was the result zero and a few other things. Then, there's a range of conditional branch instructions that check for a certain subset of those flags having certain values - e.g. a jump-if-zero (or jump-if-equal) will jump if the zero flag was set.– user8709Commented Mar 4, 2013 at 11:51
The CPU is composed of a set of registers. Some are general purpose registers (can be used for computation) and some have dedicated uses (like the instruction pointer which contains the address of the next instruction to fetch). There's a status register (usually) which has several bits such as "overflow." Certain errors are captured by status register flags but its up to the machine code generated or produced from the language you are executing to read the status register and initiate exceptional processing.
Interactive input events are usually handled through interrupts. There's usually an set of interrupt lines into the CPU and when there's a signal on one of those lines that causes the CPU to jump to a new block of code that's called an interrupt handler. Without talking about a particular O/S, the interrupt handler is written as part of the operating system and isn't part of the CPU, although the CPU calls the address provided by the O/S as the interrupt handler. For example, a prototypical O/S, moving the mouse could cause an interrupt, which invokes a serial interrupt handler, which reads the value from the serial port, which then triggers the GUI to redraw, which reads the mouse position and draws the mouse arrow in the right place.
Still other IO is handled through a combination of IO ports and registers and the values are polled periodically for changes.