The Ghost In the Machine

0 Conversations

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:

  • how long the instruction is (how many bytes)
  • what operation to perform
  • where the data lives that is to be operated upon (what its address is)
  • and, optionally
  • where to put the results of the operation (if not replacing the contents of the original variable)
  • 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.

    1The computer does not read the program instruction by instruction from your hard drive or CD. Not only would that take too long, but the program is encapsuled like a tin of beans on a shelf. Invoking the program opens the can and pours the beans onto a plate where the computer can consume it, bean by bean. That pool of instructions for a particular program is called the task image. Really big programs are broken down into segments, which are loaded into RAM whenever you call upon particular functions outside the scope of the instructions already resident in memory. This accounts for the pauses that can occur while you are using your word processor or 3D graphics game.2There are circuits on the motherboard which are dedicated to sending data to the CPU, and to taking information from the CPU. These have come to be known as busses, although the term 'tram' might have been more strictly accurate, given that the circuit is like trolley rails.3A register is a short chunk of memory integral to the CPU itself for the temporary storage of information. This information may be an address or it may be a value (the contents of a variable). The CPU performs all mathematical and logical operations on data using these registers, rather than operating on RAM, which saves a great number of clock cycles.4Datum: singular form of data.

    Bookmark on your Personal Space


    Conversations About This Entry

    There are no Conversations for this Entry

    Entry

    A581979

    Infinite Improbability Drive

    Infinite Improbability Drive

    Read a random Edited Entry


    Written and Edited by

    Disclaimer

    h2g2 is created by h2g2's users, who are members of the public. The views expressed are theirs and unless specifically stated are not those of the Not Panicking Ltd. Unlike Edited Entries, Entries have not been checked by an Editor. If you consider any Entry to be in breach of the site's House Rules, please register a complaint. For any other comments, please visit the Feedback page.

    Write an Entry

    "The Hitchhiker's Guide to the Galaxy is a wholly remarkable book. It has been compiled and recompiled many times and under many different editorships. It contains contributions from countless numbers of travellers and researchers."

    Write an entry
    Read more