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

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

  • This is very informative, but what I am looking for is what is it that allows a CPU to recieve and send commands? – user90980 Jul 20 '11 at 16:46
  • 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. – Daniel R Hicks Aug 4 '11 at 2:51
  • 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. – Daniel R Hicks Aug 4 '11 at 2:55
  • Readers may be interested in the question How does a computer work? over on Computer Science. – Raphael Mar 15 '13 at 12:01
up vote 88 down vote accepted

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.

  • 2
    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? – Simon Verbeke Jul 6 '11 at 15:15
  • 4
    @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). – Breakthrough Jul 6 '11 at 15:26
  • 1
    "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. – LawrenceC Jul 6 '11 at 15:53
  • 3
    No, I was saying because of the longevity and time-and-time-again-extension of the x86 architecture, mapping of individual bits in opcodes to micro-operations isn't cut-and-dry neat like it is in MIPS or ARM to a degree (see, for example, Intel opcodes are nothing like this). I don't think there was much notion of "RISC" when Intel introduced x86 CPUs in 1978. – LawrenceC Jul 6 '11 at 16:37
  • 5
    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. – Fake Name Jul 8 '11 at 6:38

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.

  • 5
    Nice analogy with cars break pedal. – rjmunro Jul 7 '11 at 10:28

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.

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.

  • 2
    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. – dmckee Jul 7 '11 at 2:26

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.

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.

  • 7
    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. – LawrenceC Jul 6 '11 at 15:46
  • 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. – n0pe Jul 6 '11 at 15:47

I've given a related answer at, see How do computers work? where I walked briefly over everything from the ground up about how computers goes about interpreting instructions to moving electrons.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.