How does a CPU 'know' what commands and instructions actually mean?

48,976

Solution 1

When a computer interprets assembly level instructions, these instructions are turned into their binary equivalents for the CPU to read. When the CPU executes the instructions, it interprets the opcode part of the instruction into individual "microprograms", containing their microcode equivalents. Just so you know, a full assembly instruction consists of an opcode and any applicable data that goes with it, if required (e.g. register names, memory addresses).

Microcode instructions are extremely low-level (more so then assembly), and control the actual digital signals which control the flow of logic in the microprocessor. For example, one microcode instruction could update a condition code register flag with a new value, or connect a CPU register with one of the ALU units. More complex tasks are possible, but this shows you the general idea of what microcode is used for.

The general flow from compilation to execution is as follows. The assembly instructions are assembled (turned into their binary equivalent 0s and 1s, or from now on, logic signals). These logic signals are in-turn interpreted by the CPU, and turned into more low-level logic signals which direct the flow of the CPU to execute the particular instruction. This can take one or more clock cycles, depending on the processor's architecture and design (most processor reference manuals tell you how many clock cycles it takes to execute a particular instruction, like this one for example).

All of this is done with hard-programmed microcode (physically embedded within the processor in some kind of ROM, set during manufacturing), which directs the flow through actual low-level logic gates. This provides an interface between the abstract assembly instructions and the physical electrical logic in the processor.


So, in summary, processor instructions are assembled and loaded by the processor. The processor will then use these instructions to look up the microprogram (in the form of microcode) corresponding to that particular instruction, which is what "actually" executes the instruction. Once the microcodes for the particular instruction have been executed (which can take one or more clock cycles), the processor executes the microcode to fetch the next instruction, and the cycle repeats.

Solution 2

The processor doesn't really 'know' what the commands are. The commands are just binary patterns that cause the processor to do what we interpret the commands to mean.

For example, an ADD-R1-into-R2 operation will cause registers 1 and 2's values to reach the ALU (arithmetic and logic unit), cause the ALU to use the output of the adder instead of the various other stuff, and cause the output of the ALU to replace the value in register 2. There are simple logic circuits to achieve all of these things (multiplexer, adder, counter, ...), although real processors use very complicated optimizations.

It's sortof like you're asking how a car knows to slow down when you press the brakes. The car doesn't know, the brake pedal just happens to indirectly control how hard pads are pressed against the wheels.

Solution 3

Take, for example, the instruction that tells an x86/IA-32 processor to move an immediate 8-bit value into a register. The binary code for this instruction is 10110 followed by a 3-bit identifier for which register to use. The identifier for the AL register is 000, so the following machine code loads the AL register with the data 01100001.

10110000 01100001

This binary computer code can be made more human-readable by expressing it in hexadecimal as follows

B0 61

Here, B0 means 'Move a copy of the following value into AL', and 61 is a hexadecimal representation of the value 01100001, which is 97 in decimal. Intel assembly language provides the mnemonic MOV (an abbreviation of move) for instructions such as this, so the machine code above can be written as follows in assembly language, complete with an explanatory comment if required, after the semicolon. This is much easier to read and to remember.

http://en.wikipedia.org/wiki/Assembler_language

In other words, when you 'assemble' your assembly program, your instructions such as

MOV AL, 61h

are converted to numbers, which the CPU associates a special meaning and then acts accordingly.

Solution 4

Suggested reading:

Also check out the course notes from CS152: Computer Architecture and Engineering at UC Berkeley, a course in which students implement a CPU.

If you google for "home-built cpu" you will find many goodies.

Solution 5

At the extreme most lowest level, all the CPU can do is add. From addition, it can subtract, multiply and divide (seeing as these are just addition in a different manner). The CPU uses this to move data around in memory by applying the additions to memory addresses.

Keep in mind though that this is at the lowest level possible. The CPU does in fact "understand" certain commands, in the form of microcode. See Breakthrough's answer, it's very well written.

Share:
48,976

Related videos on Youtube

Simon Verbeke
Author by

Simon Verbeke

Updated on September 18, 2022

Comments

  • Simon Verbeke
    Simon Verbeke over 1 year

    How does a processor 'know' what the different commands mean?

    I'm thinking of assembly level commands like MOV, PUSH, CALL, etc...

    • Admin
      Admin almost 13 years
      This is very informative, but what I am looking for is what is it that allows a CPU to recieve and send commands?
    • Admin
      Admin almost 13 years
      I don't understand that comment. A CPU "receives" instructions from memory, by asking for them by address. The only commands a CPU "sends" (in the simplistic view, at least) are commands to the memory to deliver data, and commands on I/O buses, to operate I/O devices.
    • Admin
      Admin almost 13 years
      At the heart of any CPU is some logic that is (literally) hardwired to run a simple procedure: Take the value from the instruction address register, send it to memory, retrieve the instruction that memory returns, and then jam it into a rather more complex nest of hardwired logic that "understands" what the instruction means and how to execute it. Oh, and somewhere along there increment the instruction address register.
    • Admin
      Admin about 11 years
      Readers may be interested in the question How does a computer work? over on Computer Science.
  • Simon Verbeke
    Simon Verbeke almost 13 years
    Ok, I get it, I think :) So the command bits toggle "switches" that will make the processor do certain things with the data it receives?
  • Breakthrough
    Breakthrough almost 13 years
    @Simon Verbeke, exactly correct. They just toggle switches to direct the flow of electrical signals in the processor (which can also direct it to re-load more commands!). The switch analogy is good, since everything is digital (either logic 1/0, or true/false). Just so you know, the logic level is an actual voltage. It's up to the engineer to specify what is a 0 or a 1 (for example, a logic 1 could be specified as more then 2 volts).
  • LawrenceC
    LawrenceC almost 13 years
    Logical operations like AND, OR, and NOT, as well as bitshifting are more fundamental than adding. Adding can actually be expressed in terms of these operations. There are actually discrete ICs (the Texas Instruments LS series) that do nothing but perform these operations and it is possible to build a CPU of sorts out of them. Google "Pong schematic" to see how a game, for example, is made, without a CPU.
  • n0pe
    n0pe almost 13 years
    I meant from a more software point of view :) Yes hardware/logic wise you have an insane amount of NAND and NOR gates. Good point.
  • LawrenceC
    LawrenceC almost 13 years
    "Binary equivalent instructions" to which the compiler or assembler boils everything down to are called opcodes. If you take a look at the opcode structure of a RISC architecture like MIPS or ARM, you can see how various bits in the opcode map to specific operations. Intel due to its longevity and tendency to be extended time and time again no longer has a simple mapping structure.
  • Breakthrough
    Breakthrough almost 13 years
    @ultrasawblade thank you, that is correct. I updated the first paragraph in the answer to correctly reflect that (instructions are opcodes and registers/addresses). I would argue, however, that Intel's decision to go with a CISC architecture has little to do with their longevity and more to do with their performance layout (e.g. specific instructions for video encoding dealing with motion vectors).
  • Breakthrough
    Breakthrough almost 13 years
    Ah, sorry for that misinterpretation. By the way, amazing reference site you linked to there, +1.
  • dmckee --- ex-moderator kitten
    dmckee --- ex-moderator kitten almost 13 years
    It is also worth noting that the assignment of the mnemonic symbol "mov" to this particular bit pattern was completely arbitrary. I could in principle write an assembler that called that instruction "oof" and it would work just as well, aside from being harder to remember.
  • rjmunro
    rjmunro almost 13 years
    Nice analogy with cars break pedal.
  • Fake Name
    Fake Name almost 13 years
    It't should be noted that only some CPUs are microcoded. Some (mostly smaller devices) operate directly off the assembly opcodes. It depends on the architecture.
  • Breakthrough
    Breakthrough almost 13 years
    @Fake Name, yes, you are correct - the term for this is a hard-wired CPU (as opposed to a microcoded one as you said). For anyone that wants to learn more, here is a great resource from the amazing FOLDOC.
  • Fake Name
    Fake Name almost 13 years
    I've never heard "Hard-Wired" before. It's always just been microcoded, or not microcoded, e.g. normal. But then, I work with embedded micro-controllers almost exclusively. It's worth noting that a processor that is not microcoded may still take multiple clock cycles to complete each instruction. One of the devices I work with a lot manages one instruction every 4 clock cycles.
  • trogne
    trogne over 5 years
    @Breakthrough , When you say "These logic signals are in-turn interpreted by the CPU", how a CPU is capable of interpreting ? It's like a computer in a computer, a neverending question ! Or when does the binary becomes "logic signals" ?
  • Breakthrough
    Breakthrough over 5 years
    @trogne what I meant by "interpreted" is the signals are used by logic gates to switch on/off parts of the CPU based on what the signals are, thus accomplishing the "interpretation" of the signals. At the end of the day, there is fundamental gates (XOR, NOR, NAND, AND, OR) executing these particular instructions. Good question :)