Originally posted by: Gord
Okay, first question. How is it possible to run an electric current through a piece of metal (edit for clarity: I realize that a CPU is, in fact, a whole bunch of pieces of metal) and have it perform computations?
Do you understand how to do arithmetic (adding and subtracting numbers) in binary? If not,
read this and post (or private message me) if any of it doesn't make sense.
Once you understand that, and
Boolean logic, you might find
Binary addition using logic gates interesting.
Something to do with the current's variations, or the time it takes to travel to various parts of the CPU, or the energy loss during said travel, or what?
In modern CPUs, values are encoded as either high voltage (~1.5 volts), or low voltage (0 volts). A given wire can be high or low, and data storage elements can record a high or low value.
Second question, how did we ever get said piece of metal (i.e., CPU) to understand human arguments like 'if,' 'else' and 'when'?
A sample program might be:
print "Enter a number."
input num
if num < 10 then
print "Your number was less than ten"
else
print "Your number was more than nine".
What the CPU actually does for the "if" is take your number, and subtract 10 from it. If your number was less than ten, the result will be negative, and as you (hopefully) know from the binary arithmetic page above, a negative number has a 1 in the top bit position. The CPU has a blob of logic that picks the next instruction to execute, so we just send that top bit to that logic, and using a "multiplexor" controlled by the signal, it can pick either the first "print" statement or the second one as the next instruction. (This is a simplified explanation, but I think it's accurate).
Computers don't really "understand" anything - they just take in a stream of instructions and execute them in order until they hit an "if", and then something like what I just described controls which instruction is executed next.
I realize there's probably a glaringly obvious answer to both of these that I'm just too dumb to figure out, so go easy on me.
I couldn't understand it for a few years after initially deciding I wanted to learn about CPUs, and I was wondering from ~6th to 11th grade.... so it took me a while to figure out . At some point, something clicked, and it became incredibly obvious to me. I think the main realization is that computers
don't think, and while they seem complicated, a basic CPU is actually VERY simple.
Originally posted by: TuxDave
There are 'switches' that you can 'open and close'. So to implement a basic logic function, imagine this.
........B
A ----/ ------ Out
The switch closes with B is powered. So the 'out' terminal is powered when both A AND B are powered. Hence.. the 'and' function. The CPU just has a whole bunch of different structures and together they can interpret instructions.
That's not a good way to describe an AND function, because you can't do it that way in a real circuit. In real circuits, every wire has to be connected to either power (high) or ground (low). In your circuit, your output will either be high (if A and B are high), or "floating" (connected to neither). A floating node can end up anywhere - high, low, or in between. The next gate in your circuit could see any possible input. That's a bad thing. A real AND gate might be implemented with a NOT gate and a NAND, as poorly described
here.