Computers are really just glorified state machines. They aren’t all that complex.
There are basically four states to a computer:
- Fetch
- Decode
- Execute
- Write Back
FETCH:
The first thing a computer does is fetch the instruction to execute. The instruction is going to be a fixed number of bits. One particular bit pattern may be an ADD instruction. Another bit pattern may be SUBTRACT. There are math functions, logic functions, and flow control functions. An example of a flow control function would be “if the negative bit is set, jump to instuction at memory location XYZ”.
DECODE:
A lot of instructions need more data than just what is in the instruction. For example, ADD A + B means you now have to go get A and B. The computer will now do memory fetches to get the necessary data.
EXECUTE:
After the instruction has been fetched and decoded, all of the necessary numbers are now inside the CPU, so if the instruction is an ADD, a piece of the CPU called the arithmetic logic unit (ALU) will add them together.
WRITE BACK:
Once you’ve executed the instruction, you now have the answer sitting inside the CPU. Now you need to write the answer out somewhere, either to an internetal CPU register or out to a memory location. Flow control operations write to a special register called the “program counter” which is the register that also tells the CPU where to fetch its next instruction from during the “fetch” state.
Once the write back is finished, the computer goes back into the fetch state, and the whole thing starts over. It just repeats these states over and over and over and over.
Actual computers are a bit more complicated than that, but that’s the basic idea. Modern computers do a thing called “pipelining” where they actually have dedicated hardware doing each of the four states, and they will process multiple instructions at once. For example, the first instruction is fetched, then when it goes to be decoded, a second instruction is fetched. While the first instruction is executed, the second is decoded, and a third is fetched, etc. Since your decode and fetch stages both want to access the same memory, caches are needed to stop them from stomping on each other. A pentium CPU has a very large instruction cache, a dual execution pipeline (it will execute 2 instructions side by side) and a seperate pipeline for floating point operations.
Here is a page that shows the instruction set for a PC:
http://www.arl.wustl.edu/~lockwood/class/ece291/class-resources/opcodes.html
If you look at it, you’ll see that “ADD” isn’t just one instruction. There are 8 different types of ADD instructions. “Reg” means register, which is one of the internal CPU registers. “Imm” means immediate, which means the number is stored as part of the instruction. “Add 2 to register AL” would be an example of an add immediate to register instruction. “Mem” means memory.
For a much simpler processor, here is the instruction set for the old 6502 CPU (used in the apple II and other computers of the time):
http://www2.asw.cz/~kubecj/aopc.htm