Concepts / Memory and Storage: How Computers Remember

Memory and Storage: How Computers Remember

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

A computer appears to perform many different activities: it calculates, displays information, and runs programs. At the center of these activities is the Central Processing Unit, or CPU. The CPU is the part of the computer built to keep determining what should happen next. Its fundamental question is: What is next?

The CPU is the computer's decision-maker for instruction execution. It repeatedly determines the next operation to perform.

instruction retrievedmeaning determinedoperation completedfetch againFetchRetrieve an instructionDecodeDetermine its meaningExecutePerform the operationNext instructionBegin again
What happens as the CPU determines one operation and proceeds to the next?

Following One Instruction

Tracing a Single CPU Decision

Trace what the CPU does when it is ready to process the next instruction.

Fetch: The CPU retrieves the next instruction from memory.

Decode: The CPU determines what the retrieved instruction means.

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

Repeat: The CPU immediately returns to the beginning and fetches the next instruction.

The CPU does not process a program as one single, undifferentiated event. It repeatedly fetches, interprets, and performs instructions.

The fetch-decode-execute cycle is the basic pattern behind CPU instruction processing. Fetch means retrieving an instruction from memory. Decode means figuring out what that instruction means. Execute means performing the operation. After execution, the CPU fetches the next instruction and repeats the cycle.

How CPU Parts Work Together

The CPU does not answer its question alone. Several internal components contribute to instruction execution. 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, ultra-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.

instructioncoordinatedirectavailable dataworking dataperform calculation or logiccoordinateMemoryInstructions and dataControl UnitDirects instruction flowRegistersCurrent working dataALUCalculations and logicCacheFrequently used dataOperationInstruction executed
How do instructions and data move through the CPU's coordinating components?
CPU componentRole described in the source
Control UnitDirects instruction flow and coordinates the fetch-decode-execute cycle
Arithmetic Logic UnitPerforms calculations and logical operations
RegistersHold data the CPU is currently working with
CacheStores frequently used data in a small, very fast memory

The components that support the CPU's repeated instruction cycle

Reading a Gigahertz Rating

CPU speed is measured in Gigahertz, abbreviated GHz. One Gigahertz means one billion cycles per second.

Interpreting 3.0 GHz

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

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

Apply the number: A rating of 3.0 GHz corresponds to three billion cycles per second.

Connect the rating to the CPU's question: In the source's explanation, the CPU asks What is next? three billion times per second.

A 3.0 GHz CPU is described as operating at three billion cycles per second.

cycle beginsrepeatcontinue repeatingwithinStart0 secondsCPU cycleOne cycleCPU cycleRepeated cycleOne billion cycles1 GigahertzOne secondElapsed time
What does one Gigahertz represent when the CPU repeats its cycle over one second?

The Scale of Modern CPU Activity

The numbers involved are difficult to imagine. The source describes modern CPUs as often running between 2.0 and 5.0 GHz, while some specialized processors can go even faster. At 3.0 GHz, the source presents the CPU as handling three billion decisions, three billion instruction fetches, and three billion execution cycles in one second.

repeatscale upcontinue askingcycle againOne cycleOne decision sequenceRepeated cyclesThe cycle continuesBillions of cyclesWithin one secondNext operationCPU continues
How does a CPU's repeated cycle scale within a single second?

The CPU's speed matters because the same instruction pattern can be repeated an enormous number of times. A tiny amount of unnecessary work can become significant when repeated across many cycles.

Memory, Cache, and Working Data

The source describes memory as the place from which the CPU retrieves instructions. It also identifies registers and cache as important forms of fast working storage inside the CPU. Registers hold data currently being used, while cache keeps frequently used data close to the CPU so it does not have to wait for slower main memory.

instructionworking datafrequently used dataMemoryInstructions retrievedRegistersCurrent working dataCacheFrequently used dataCPUInstruction execution
How do the information-holding roles mentioned in the source differ during CPU work?

Why Repetition Changes Performance

When you write a program, you are giving the CPU a list of instructions to execute in order. The CPU can process those instructions at incredible speed, but that speed also magnifies repetition. If a loop runs a million times and each iteration performs unnecessary work, the CPU repeats that unnecessary work a million times. Efficient code reduces repeated work, helping the program feel instant to the user.

Suppose a program performs the same unnecessary task during every iteration of a loop. The issue is not only the cost of one unnecessary task. The CPU repeats it for every iteration, so the total unnecessary work grows with the number of repetitions.

Understanding the CPU helps programmers judge code beyond whether it produces the correct result. Clear, efficient instructions make better use of the CPU's repeated cycles.

Mistakes Beginners Make

  • Thinking the CPU has only one job: arithmetic

    The source presents the CPU's primary role as determining the next operation. The ALU performs calculations and logical operations, but it is one component working inside the larger CPU process.

    Fix: Describe the CPU as the part of the computer that repeatedly fetches, decodes, and executes instructions.

  • Treating Gigahertz as a count of complete programs per second

    The source defines Gigahertz as one billion cycles per second, not complete programs per second.

    Fix: Say that a 3.0 GHz CPU is described as operating at three billion cycles per second.

  • Stopping after execution

    After execution, the CPU immediately fetches the next instruction and repeats the cycle.

    Fix: Include the repeating return from execution to the next fetch.

  • Ignoring repeated unnecessary work in code

    A loop can repeat the unnecessary work many times, and the source emphasizes that these repetitions can be multiplied across CPU cycles.

    Fix: Look for work that is repeated unnecessarily, especially inside loops.

Check Your Understanding

EASY

Explain the path of one instruction from the moment the CPU retrieves it until the CPU begins working on the following instruction.

Hints
  • Name the three stages in order.
  • State what happens during each stage.
  • Remember what happens immediately after execution.
MEDIUM

A computer is rated at 5.0 GHz. Using the definition in this lesson, explain what the number 5.0 represents and identify what it does not directly tell you.

Hints
  • Start with the meaning of one Gigahertz.
  • Multiply that meaning by 5.0.
  • Do not describe the rating as complete programs per second.
MEDIUM

A loop repeats one million times and performs unnecessary work on every iteration. Why can that inefficiency matter even though the unnecessary work in one iteration seems small?

Hints
  • Focus on repetition.
  • Connect the loop to the CPU's repeated instruction cycles.
  • Use the idea that inefficiencies are multiplied across iterations.

Essential Takeaways

  1. The CPU is the part of the computer that repeatedly determines what operation should happen next.
  2. The CPU follows the fetch-decode-execute cycle: retrieve an instruction, interpret it, perform it, and repeat.
  3. One Gigahertz means one billion cycles per second; a 3.0 GHz CPU is described as operating at three billion cycles per second.
  4. The Control Unit, ALU, registers, and cache work together to support instruction execution.
  5. Repeated unnecessary work can matter because a CPU performs cycles billions of times per second and a program may repeat that work many times.

Key Takeaways

  • The CPU's defining question is What is next?
  • Every instruction follows the repeating pattern of fetch, decode, and execute.
  • Gigahertz measures cycles per second, with one Gigahertz equal to one billion cycles per second.
  • CPU components such as the Control Unit, ALU, registers, and cache cooperate during instruction processing.
  • Efficient code matters because repeated work can be multiplied across millions of program iterations and billions of CPU cycles.