How computers follow instructions
Understand program state, sequence, decisions, repetition, and the difference between an instruction in a language and a machine instruction.
In this chapter9 sections
A program works with information and changes it over time. To follow a program, ask three questions: what do we know now, what happens next, and what changes? We can practise this before learning C punctuation.
1. State is a snapshot of relevant information
Imagine a counter at a library desk. It starts at zero and becomes one after a book is returned. This count is part of the system’s state: information we need to describe its situation now. Real programs have more state, but we will follow just this one count first.
Some operations calculate a result. Others change stored information or interact with the outside world. C uses the term side effect for changes to objects and certain interactions, including output. The word “side” does not mean the change is unimportant. Updating the count may be the whole purpose of a step.
2. Order can change the meaning
Consider these conceptual instructions, written in ordinary language rather than C: start the count at zero; increase it by one; remember its current value; increase it by one again. The final count is two, while the remembered value is one. Remembering an earlier value does not automatically create a permanent connection to every later change.
| After this step | Count | Remembered value |
|---|---|---|
| Start | 0 | Not supplied yet |
| Increase once | 1 | Not supplied yet |
| Remember the count | 1 | 1 |
| Increase again | 2 | 1 |
A trace is a record of selected steps and the state after them. It helps us check our reasoning. Each row describes a logical step, not necessarily one processor instruction. A compiler may combine work while preserving the behavior C requires.
3. A condition chooses a path
Suppose the desk holds at most five returned books. Before accepting one more, test whether the count is less than five. This test is a condition. Its result selects the next action using the information the program examines.
The boundary matters. “Less than five” and “at most five” are different tests. With a count of five, the first is false and the second is true. Many programming errors are disagreements about a boundary rather than failures of arithmetic. Defining the rule before writing its symbols makes that difference easier to notice.
4. How a loop makes progress
A loop repeats work. Our rule could be: while a book is available and the desk has room, accept a book and increase the count. Follow four parts: the starting count, the test, the work in one repetition, and what eventually makes the loop stop.
If nothing changes a true continuation test, the loop may never finish. Some systems intentionally run an event loop while the application is active, so not every long-running repetition is a mistake. The question is whether that behavior matches the specification and whether resources and external events are handled correctly.
5. A source instruction is not a machine instruction
Source languages provide concepts such as functions, conditions, and objects. A CPU executes instructions from its machine-level instruction set, with support from other system components. There is no universal one-to-one mapping between a source statement, an assembly-language line, and a machine instruction. Some source constructs produce many instructions; some produce no work after optimization.
The common fetch–decode–execute model says that a processor obtains instruction information, interprets it, and carries out the operation. Modern implementations can overlap and reorganize internal work. Treat that model as an introduction, not a timing diagram or a promise that every processor completes exactly one instruction before considering another.
Go deeper: the C abstract machine
C gives us a model for reasoning about execution. An implementation can organise its machine work differently if it preserves the required observable behavior. This freedom applies within the language rules. If an operation has undefined behavior, a convincing-looking trace does not make that operation valid.
6. Use a trace where it is useful
A small state table is especially useful for counters, conditions, loops, and value copying. Write down what you know and mark information that has not been established. Do not fill an unknown entry with zero merely because zero would make the example convenient. Later chapters will explain when C supplies zero initialization and when it does not.
The count starts at four. The rule is “accept while count is less than five,” and each acceptance adds one. How many books can be accepted before that test becomes false?
Read the explanation
One. The first successful test allows an acceptance that changes the count to five. Testing again then gives false. The later failed test does not undo the book already accepted.
Primary references and further reading
- WG14 N1570 — public C11 committee draft: Sections 5.1.2.3, 6.5, and 6.8 provide the core rules used in this C17-baseline explanation. This is a public draft, not the final ISO publication.
Before you move on
- State is the information needed to describe a program’s current situation.
- A trace records selected steps and their effects on that state.
- A loop intended to finish needs a reason that it reaches an exit.
Keep these ideas separate
- Source step / Machine instruction
- A source-level step can map to several machine instructions or be optimised away under the language rules.
What would you record to explain a counter that changes from four to five and then stops?
Optional: say it aloud or keep a note. You can continue whenever you are ready.Check your understanding
Optional. Answers are not saved or sent, and reading stays unrestricted.
Read the explanation for every choice
- Yes, line breaks define processor operations.
Source layout does not establish a one-to-one instruction count.
- Yes, comments each require a machine instruction.
Comments are not runtime instructions.
- No. Translation and optimization can produce a different arrangement.
✓ Correct reasoning. Reason from language semantics before assuming a particular machine-level trace.
General
Ready for the next idea?
Mark this chapter as read whenever you feel ready.