Concepts / How the CPU Executes Instructions

How the CPU Executes Instructions

Main memory is fast and nearly as quick as the CPU, but it is volatile and loses all data when power is cut off.

  • Programming

Speed Before Persistence

When you run a program, the CPU does not work directly with the program file on a hard drive. The program must first be copied into main memory, where the CPU can access it quickly. This creates a fundamental division of responsibility: main memory supports active work, while secondary memory preserves programs and files for the long term.

copy programaccess and executeBrowser programsecondary memoryBrowser programmain memoryCPUexecutes code
How does program information move between secondary memory, main memory, and the CPU during execution?

The CPU works with data in main memory. If data begins in secondary memory, it must be brought into main memory before the CPU can process it.

The Execution Path

Opening a Web Browser

Trace what happens when a user opens a browser, loads a webpage, and then closes the browser without saving the page.

Program loading: The browser application is stored on secondary memory, such as a hard drive. When the browser is opened, the operating system copies the program from secondary memory into main memory.

Program execution: The CPU executes the browser code from main memory rather than working directly with the file on the hard drive.

Web request: After the user enters a web address and presses Enter, the browser sends a request over a network connection.

Webpage arrival: The web server sends webpage data back through the network. The arriving data is placed into main memory.

Processing and display: The CPU processes the webpage data in main memory, and the webpage is displayed on the screen.

Closing without saving: If the browser is closed without saving the page, the webpage data in main memory is lost. The browser application remains on the hard drive and can be loaded again later.

The CPU uses main memory for active execution and processing, while secondary memory keeps the browser application available for future use.

save dataload dataMain memoryfast; needs powerSecondary memoryslower; keeps data
What changes when information moves between fast, volatile main memory and slower, persistent secondary memory?

Main memory is fast and nearly as quick as the CPU, but it is volatile. Volatile memory requires continuous power to retain its data. Secondary memory is much slower, but it is non-volatile: it retains data without power. The computer therefore uses main memory for immediate work and secondary memory for long-term storage.

Volatility at Shutdown

contains working datacontains saved dataloses dataretains dataMain memoryworking data presentPower cutMain memorydata lostSecondary memorysaved data presentSecondary memorydata retained
What happens to data in main memory when power is cut off, and how is that different from persistent storage?

A power cut removes all data from main memory. Data already stored in secondary memory remains available because secondary memory retains data without power. This is why a program can disappear from its active working state while the installed application or previously saved file remains on a drive.

  • Assuming that data in main memory is automatically permanent.

    The edited data may exist only in volatile main memory.

    Fix: Save the data to secondary memory when it must remain available after shutdown.

  • Assuming that the CPU executes a program directly from its hard drive.

    The CPU works with program information in main memory.

    Fix: Understand the path as secondary memory to main memory to CPU.

  • Treating speed and persistence as the same property.

    Main memory is fast but volatile, while secondary memory is slower but non-volatile.

    Fix: Choose main memory for active processing and secondary memory for long-term retention.

Secondary Storage Choices

Secondary memory is used when information must remain available without power. Common examples include hard disk drives, solid-state drives, and flash memory. Flash memory is commonly found in USB sticks and portable music players. These technologies are appropriate for long-term storage because they can retain data indefinitely without power.

supportssupportssupportsHard disk drivelong-term storageData without powerretained indefinitelySolid-state drivelong-term storageFlash memoryUSB sticks; music players
How do common secondary-memory technologies compare in persistence and suitable use?
Memory typeSpeed relationshipPower requirementAppropriate role
Main memoryFast and nearly as quick as the CPURequires power to retain dataActive program execution and data processing
Hard disk driveMuch slower than main memoryRetains data without powerLong-term storage of programs and files
Solid-state driveMuch slower than main memoryRetains data without powerLong-term storage of programs and files
Flash memoryMuch slower than main memoryRetains data without powerPortable long-term storage, including USB sticks

The central trade-off is fast active work versus persistent long-term storage.

When a program needs to work with a large file, explicitly read the file from secondary memory into main memory first. When the program's results must remain after the program finishes, explicitly write those results to secondary memory.

Remote Data over Networks

A network connection can provide access to data stored somewhere else, so it can act as a slow and unreliable form of secondary memory. The data is not immediately available beside the CPU. It must travel between the local computer and a remote system, and the arriving data is then placed into main memory for processing.

send requestcarry requestsend webpage datadeliver dataprocess dataCPUprocesses local dataNetwork connectionslow and unreliableWeb serverremote dataMain memoryreceives webpage data
How does data travel from a local computer to remote storage over a network, and where can delay or unreliability affect the process?
Access methodWhere data is locatedRelative speedMain limitation
Local secondary memoryOn the computer's drive or flash deviceMuch slower than main memoryData must still be loaded into main memory before CPU processing
Network connectionOn a remote systemSlow and unreliableRemote access can delay or prevent timely access

Apply the Memory Model

What do you think happens?

A webpage has arrived over a network connection. Where must the webpage data be placed before the CPU processes it?

  • Directly on the remote web server
  • In main memory
  • Only in the network connection
  • Only in the screen
Reveal answer

Answer: In main memory

The webpage data arrives over the network and is placed into main memory. The CPU then processes the data from main memory.

MEDIUM

For each situation, identify whether the information belongs in main memory, local secondary memory, or remote access over a network. Then explain what happens to the information if power is cut off before it is saved.

Hints
  • Ask whether the CPU is actively processing the information.
  • Ask whether the information must remain available without power.
  • For remote information, consider the additional network connection.
  1. Locate the information's current home: main memory, local secondary memory, or a remote system.
  2. If the CPU must process it, identify the transfer into main memory.
  3. If the information must survive shutdown, identify the transfer into secondary memory.
  4. If the information comes from a network, account for the connection's slowness and unreliability.

The Memory Hierarchy

  1. The CPU executes programs and processes data from main memory, not directly from files on a hard drive.
  2. Main memory is fast and nearly as quick as the CPU, but it is volatile and loses data when power is cut off.
  3. Secondary memory is slower but non-volatile, so hard disk drives, solid-state drives, and flash memory can preserve programs and files without power.
  4. Saving explicitly copies data from main memory to secondary memory so it can persist after shutdown.
  5. Network connections provide remote data access but are slow and unreliable, making them unsuitable for critical, time-sensitive operations.

Key Takeaways

  • The CPU needs program instructions and data in fast main memory to execute and process them.
  • Main memory is volatile, while secondary memory is slower but retains data without power.
  • Saving is the explicit movement of data from main memory to secondary memory.
  • Hard disk drives, solid-state drives, and flash memory are examples of persistent secondary memory.
  • Network connections can provide remote storage access, but their slowness and unreliability limit their use for time-sensitive work.