The Ghost In the Machine
Created | Updated Jan 28, 2002
How the CPU Reads a Program
In order to make sense of how a line of code is processed, we need to understand first how that instruction comes to the computer's immediate attention. When you click on an icon or type a command line and hit Enter, you are invoking a pre-written task, a sequence of instructions.
In order to obey your command, the computer has to know where the instructions are, so it's built into your operating system, that the beginning of a program always starts at a specific street address in RAM. In other words, the first thing the computer will do after that mouse-click is to load the program (or a part of it) into a part of memory that is reserved for task images1, and send the first instruction of the program to the CPU for consideration.
Each instruction has a particular format, which tells the CPU the following facts:
And so, one might imagine a dialogue something like this, if we wrote it in H2G2 role-playing conventions:
[Instruction #1, fresh off the data bus2]: First instruction reporting for processing! I am eight bytes long and you are to add the data in this place to the data in that place and put the results in this third place. (each place being a RAM address)
[CPU]: Ah, so. If you are eight bytes long then Instruction #2 must start at the ninth byte in the task image. I will send to that address and have #2 summoned forthwith. And the data in this place *reaching out and fetching the value at that address, and putting it into a register3* is added to that? *fetches the second datum4 and adds it to the datum already in the register* Very good. *turns to data bus* Take the contents of this register and deliver it to the address he said.
[Data bus]: Beep Beep.
[Instruction #2]: Reporting for processing!
The sequence of instructions continues until there are no more, which event is signalled by the final instruction. On hearing about this, the CPU returns control back to the operating system.
This is the simplest case, of course: in a modern multi-tasking system, the CPU may be handling many programs (open windows on your screen) at the same time. But when a program has the CPU's attention, the above dialogue is a fair metaphor for what happens to individual instructions.
It should also be said that the instructions being processed by the CPU are in microcode. Regardless of which language you originally wrote your program, the compiler or interpreter must distill your high-level instructions into much simpler commands. Even Assembly languages are complex and a simple ADD in assembly may deconstruct into dozens of microcode instructions.