A girl and a robot thinking together

Let’s think it through · Start with a story

The girl sees a line of addition code and wonders how it turns into a number on the screen. Let’s follow it through.

Break down the process

Follow the numbered steps, then explain them in your own words
  1. Write codeExpress 3 plus 5
  2. CompileTranslate it into an executable form
  3. ExecuteProcess the data
  4. OutputDisplay 8

The CPU executes machine instructions. C++ code is processed by tools before the computer executes it at runtime.

One line of C++ does not necessarily correspond to one machine instruction; the compiler may even calculate a fixed result ahead of time. This diagram shows the stages, not a frame-by-frame view of instructions.

01What is a line of code we write?

int c = a + b;

This is a line of C++ source code. It describes adding a and b, then saving the result in c.

02Does the CPU read C++ directly?

No. The CPU does not execute C++ text directly. It ultimately executes machine instructions that the target processor can understand.

03From source code to an executable

Preprocessing

Handle #include, macros, and more.

Compilation

Analyze syntax and meaning, then generate lower-level code.

Assembly

Generate machine code in object files.

Linking

Combine object files and libraries into a program.

04How does a program get into memory at runtime?

At startup, the operating system creates a process and maps the required code and data into its virtual address space. Pages can be brought into physical memory on demand when they are accessed.

05What does the CPU actually execute?

Machine code in memory→CPU fetches, decodes, and executes→Results change

06Does one line of code correspond to one machine instruction?

Usually there is no one-to-one correspondence. One line of C++ may generate several machine instructions or be optimized away; multiple lines may also be combined during optimization.

What you have learned

  • The CPU does not execute C++ source code directly.
  • A build commonly involves preprocessing, compilation, assembly, and linking.
  • At runtime, the operating system creates a process and maps code and data as needed.
  • The CPU ultimately executes machine instructions.
  • One source line does not have a fixed one-to-one match with a machine instruction.

Your turn

If you only save the source code without compiling and running it, will an 8 definitely appear on the screen?

Have an answer? Click to see the explanation

No. Saving only writes the code to a file. The program must go through the appropriate build and run steps before its output becomes visible.