1

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?

Evan Mosseri
  • 111
  • 2
  • 2
    I am currently reading "Code" by Charles Petzold. You can see a sample chapter on his website (http://www.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. – leancz Mar 04 '13 at 08: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 : http://www.nand2tetris.org/ – Sean McSomething Mar 04 '13 at 20:40

2 Answers2

2

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.

Tony Hopkinson
  • 429
  • 2
  • 4
  • Thanks for the explanation, but how exactly does it "compare"? – Evan Mosseri Mar 03 '13 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!) – Kilian Foth Mar 03 '13 at 15:13
  • Wow, kind of mind boggling. Thanks for being so thorough. – Evan Mosseri Mar 03 '13 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. – Tony Hopkinson Mar 03 '13 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. –  Mar 04 '13 at 11:51
  • BTW - if you want to learn some very simple assembler programming, the Commodore 64, BBC Micro and others used a 6502 (or 6510, which was compatible). This has a very simple instruction set. Grab a [book](http://www.bombjack.org/commodore/books.htm#MACHINE_LANGUAGE_and_ASSEMBLY_LANGUAGE), an [emulator](http://vice-emu.sourceforge.net/) and an [assembler](http://www.aartbik.com/MISC/c64.html) and have a play. –  Mar 04 '13 at 11:59
  • Along with the eight bit Silicon God, Mr Rodnay Zaks, his Z80 and 6809 book are still on my shelf. Never did 6502, I had a proper computer, A spectrum. :) – Tony Hopkinson Mar 04 '13 at 19:18
  • @Tony - I briefly used some Z80 assembler once, but my thinking is that the 6502 instruction set is much simpler. The Z80 instruction set was Intel 8080 compatible, but with as many extra instructions as Zilog could figure out how to pack in. Definitely a faster CPU, both by clock frequency and by having more complex instructions, but also harder to learn. 52 documented instructions total for the 6502 vs. 158 instructions on the Z80. –  Mar 05 '13 at 22:38
  • Most CPUs implement the compare instruction using subtraction. If a borrow occurred during the subtraction operation, the subtrahend is larger than the minuend. If the borrow flag is clear, ORing the bits in the difference will tell you if the minuend is larger (the result of the "OR" operation will be 1) or equal (the result of the "OR" operation will be 0) to the subtrahend. – bit-twiddler Mar 18 '13 at 21:23
0

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.

ipaul
  • 484
  • 2
  • 6