Concepts / Machine Code and Binary: What the CPU Actually Understands

Machine Code and Binary: What the CPU Actually Understands

The CPU is the part of the computer built to obsessively ask 'What is next?' and determine the next operation to execute.

  • Programming

The Question Behind Every Operation

The Central Processing Unit, or CPU, is the part of a computer built to keep asking one question: What is next? Its primary job is to determine the next operation to execute. A program can be understood as a list of instructions for the CPU to follow, while the CPU repeatedly retrieves, interprets, and carries out those instructions.

The CPU is not best understood as performing one large task all at once. It repeatedly determines the next operation and carries it out through the fetch-decode-execute cycle.

Following One Instruction

The CPU follows a repeating three-part process. First, it fetches the next instruction from memory. Next, it decodes the instruction, which means it figures out what the instruction means. Finally, it executes the operation. After execution, the CPU immediately returns to the fetch phase and looks for the next instruction.

instruction retrievedmeaning identifiedoperation completedrepeatFetchRetrieve instructionDecodeDetermine meaningExecutePerform operationNext instruction
How does the CPU repeatedly move from an instruction in memory to an executed operation and then continue to the next instruction?

Tracing the CPU's next decision

Describe what happens when the CPU is ready to process another instruction.

Fetch: The CPU retrieves the next instruction from memory.

Decode: The CPU figures out what the retrieved instruction means.

Execute: The CPU performs the operation described by the instruction.

Continue: The CPU immediately fetches the next instruction and repeats the cycle.

The CPU does not stop after one instruction. It continuously cycles through fetch, decode, and execute.

What Machine Code Means Here

For this lesson, machine code can be treated as the instructions that the CPU fetches, decodes, and executes. The provided material focuses on the CPU's instruction cycle rather than specifying the binary encoding of individual instructions. The key point is that the CPU works directly through instructions and operations, repeating the cycle at very high speed.

decodeexecuteInstructionFetched from memoryMeaningDetermined during decodeOperationPerformed by CPU
How does an instruction move from something stored in memory to an operation the CPU can perform?

The Parts Working Together

The CPU's question is answered by several internal components working together. The Control Unit directs the flow of instructions and coordinates the fetch-decode-execute cycle. The Arithmetic Logic Unit, or ALU, performs calculations and logical operations. Registers are tiny, very fast storage locations that hold data the CPU is currently working with. The cache is a small, very fast memory that stores frequently used data so the CPU does not have to wait for slower main memory.

instructioncoordinateprovide dataworking datadirect operationperformMemoryInstructions and dataControl UnitDirects instruction flowRegistersCurrent working dataCacheFrequently used dataALUCalculations and logicExecuted operation
How do the CPU's main internal components contribute to receiving, interpreting, and carrying out instructions?

Measuring CPU Frequency

CPU speed is measured in Gigahertz, abbreviated GHz. One Gigahertz means one billion cycles per second. Therefore, a 3.0 GHz CPU is described as asking What is next? three billion times per second. The source describes modern CPUs as often operating between 2.0 and 5.0 GHz, although some specialized processors can operate even faster.

CPU cycles repeatthree billion cycles0 seconds3.0 GHzThree billion cycles persecond1 second
How does a CPU frequency of 3.0 GHz relate to repeated CPU cycles during one second?

Interpreting a CPU rating

What does a 3.0 GHz CPU rating mean according to the source?

Read the unit: GHz means Gigahertz, and one Gigahertz means one billion cycles per second.

Apply the number: The number 3.0 indicates three Gigahertz, or three billion cycles per second.

Connect it to the CPU's role: In the source's mental model, the CPU asks What is next? three billion times per second.

A 3.0 GHz CPU is associated with three billion cycles per second.

Why Repetition Matters to Programmers

The CPU can follow instructions at incredible speed, but repeated work still matters. If a program contains a loop that runs a million times and each iteration performs unnecessary work, the CPU repeats that unnecessary work a million times. Efficient code helps the CPU avoid needless operations, while inefficient code can multiply a small problem across many repetitions.

Imagine a program with one unnecessary operation inside a loop that runs one million times. The CPU does not treat that operation as a one-time mistake; it encounters it during every iteration. The source uses this relationship between repetition and unnecessary work to explain why efficient code matters.

  • Thinking of the CPU as completing a whole program in one indivisible action.

    The CPU follows a repeating fetch-decode-execute cycle and determines the next operation continuously.

    Fix: Trace a program as a sequence of instructions that the CPU fetches, decodes, and executes.

  • Treating GHz as a description of the program's entire behavior.

    The source defines GHz as cycles per second and uses it to describe CPU operating frequency.

    Fix: Use GHz to discuss cycles per second, then separately consider what instructions and repeated work the program requires.

  • Ignoring repeated unnecessary work inside a loop.

    The CPU repeats that unnecessary operation during every iteration.

    Fix: Look for work that can be removed or avoided when it is repeated.

  • Assuming this lesson provides exact binary encodings for machine instructions.

    The source explains the CPU's instruction cycle but does not specify individual binary bit patterns.

    Fix: Use this lesson to understand how instructions are fetched, decoded, and executed, without claiming a particular binary encoding.

Check Your Understanding

What do you think happens?

A CPU has fetched an instruction and figured out what it means. What phase comes next?

  • Fetch
  • Decode
  • Execute
  • Stop permanently
Reveal answer

Answer: Execute

The fetch-decode-execute cycle places execution after the CPU decodes the instruction. After executing, the CPU returns to fetch the next instruction.

EASY

Explain the fetch-decode-execute cycle in three ordered steps. Then explain what 2.0 GHz means using the definition of Gigahertz.

Hints
  • Begin with the instruction's movement from memory.
  • Name the phase in which the CPU determines what the instruction means.
  • Remember that one Gigahertz means one billion cycles per second.

Key Takeaways

  1. The CPU is the part of the computer whose central role is determining what operation comes next.
  2. The CPU repeatedly fetches an instruction, decodes its meaning, executes the operation, and fetches the next instruction.
  3. The Control Unit, ALU, registers, and cache work together during instruction execution.
  4. One Gigahertz means one billion cycles per second; a 3.0 GHz CPU is described as carrying out three billion cycles per second.
  5. Because repeated work is repeated at CPU speed, unnecessary operations inside loops can become costly.

Key Takeaways

  • The CPU repeatedly asks What is next? by determining the next operation to execute.
  • Its basic instruction process is fetch, decode, execute, followed by another fetch.
  • Gigahertz measures cycles per second, with one GHz equal to one billion cycles per second.
  • The Control Unit, ALU, registers, and cache cooperate to support instruction execution.
  • Efficient code matters because unnecessary work can be repeated many times at CPU speed.