5.1.12 · D4Instruction Set Architecture (ISA)

Exercises — Instruction-level semantics and exceptions

3,107 words14 min readBack to topic

Before we start, a tiny recap of the two facts we lean on the most:

Recall Two facts you will reuse constantly

Fault return PC ::: the same instruction (restart it) Trap return PC ::: the next instruction (it already did its job)

We also assume, unless a problem says otherwise, a fixed-length ISA of 4 bytes per instruction, so the default next instruction is always . Addresses are written in hex like .

One symbol we will use in almost every solution below deserves its own plain-words definition first:


Level 1 — Recognition

(Can you name the thing and pick the right box?)

Exercise 1.1 — Classify each event

For each of the following, say whether it is a Fault, Trap, Abort, or Interrupt:

  1. A syscall instruction requesting a file read.
  2. A LW (load word) touching a page that is not in physical memory.
  3. A keyboard key pressed while your program is doing arithmetic.
  4. The memory controller reporting an uncorrectable multi-bit error.
  5. A division DIV R1, R2, R3 where .
Recall Solution
  1. Trap — deliberate, program asked for the service; resume after it.
  2. Fault — correctable, detected before the load writes; restart the same load.
  3. Interrupt — external device, asynchronous, unrelated to the current arithmetic.
  4. Abort — unrecoverable hardware error; the program cannot continue.
  5. Fault — detected before commit; div-by-zero is correctable by the OS (e.g. signal a handler), and it is synchronous (same inputs reproduce it).

Exercise 1.2 — Architectural or microarchitectural?

Label each piece of machine state Architectural (visible, defined by the ISA) or Microarchitectural (invisible implementation detail): General-purpose register R4; the branch predictor's history table; the program counter PC; the reorder buffer; a data cache line; the carry flag.

Recall Solution
  • R4Architectural (programmer-visible register).
  • Branch predictor table — Microarchitectural (pure speed hint, no ISA meaning).
  • PCArchitectural (defined by every instruction's semantics).
  • Reorder buffer — Microarchitectural (bookkeeping for in-order commit).
  • Data cache line — Microarchitectural (a copy of memory; the ISA defines memory, not the cache).
  • Carry flag — Architectural (a status flag the ISA specifies).

The test: Could Intel remove or redesign it without breaking your compiled program? If yes → microarchitectural. If no → architectural. See ISA as a Hardware–Software Contract.


Level 2 — Application

(Plug into the state-transition and return-PC rules.)

Exercise 2.1 — Apply the ADD semantics

Before: , , . Execute ADD R1, R2, R3. Give the new and the new .

Recall Solution

The semantics is .

  • .
  • .

The PC update is part of the instruction's meaning — an instruction that computed but forgot to advance the PC would execute forever.

Exercise 2.2 — Apply the branch semantics

BEQ R1, R2, offset sits at . Here offset counts instructions (each 4 bytes), so the taken target is . Compute the next PC when: (a) and ; (b) , .

Recall Solution

(a) Equal ⇒ taken: . (b) Not equal ⇒ fall through: .

Notice the only thing a branch changes versus ADD is the PC-write rule — same read/compute skeleton, different step 4. See Pipelining and Hazards for why branches are expensive.

Exercise 2.3 — Return PC for a trap

A syscall sits at (4-byte instructions). What EPC does the handler use to return, and why?

Recall Solution

syscall is a trap: it already performed its meaning (it lodged the OS request). We resume at the next instruction, so . Returning to would re-issue the same request twice. See System Calls and Privilege Levels.

Exercise 2.4 — Return PC for a fault

A LW R5, 0(R6) at page-faults. What EPC does the handler use, and what is the value of when the handler starts?

Recall Solution

A page fault is a fault: it is detected before the load commits, so is unchanged (precise-exception rule 2). — the address of the LW itself — because after the OS brings the page in, the same load must run again to actually deliver the data. See Virtual Memory and Page Faults.


Level 3 — Analysis

(Reason about ordering, precision, and which event wins.)

Exercise 3.1 — Which exception is reported?

In program order . During one cycle a wide out-of-order machine detects: has a div-by-zero fault, and has a page fault. Which exception is reported, and what must be true of the other three instructions when the handler starts?

Recall Solution

Report the oldest faulting instruction in program order ⇒ (div-by-zero).

  • must have fully committed (it is older than the reported fault ⇒ precise rule 1).
  • and everything younger () must not have modified architectural state (rule 2).
  • 's page fault is discarded — in the clean sequential world execution never reached , because it should have stopped at . If the OS re-runs and it faults again after the fix, only then will get its chance. See Out-of-Order Execution and the Reorder Buffer.

Exercise 3.2 — Where does precision come from?

An out-of-order CPU physically executes instructions in the wrong order. Explain in one paragraph how the architectural state can still be precise at every exception. Name the stage responsible.

Recall Solution

Instructions may compute out of order, but they commit (retire) — become architecturally visible — strictly in program order, at the commit/retire stage fed by the reorder buffer. A younger instruction's result sits in the buffer, invisible, until every older instruction has retired. When a fault is detected, the hardware squashes the faulting instruction and everything younger (their buffered results are thrown away) and lets older ones retire. So the visible state is always exactly the clean sequential state at some instruction boundary.

Exercise 3.3 — Synchronous vs asynchronous, tested by re-running

You run a program twice with identical input. The first run raises a page fault at instruction #500 and receives a timer interrupt somewhere near instruction #900. Which of these two is guaranteed to occur at the exact same instruction on the second run? Why?

Recall Solution

The page fault is guaranteed identical: exceptions are synchronous — a deterministic function of the instruction stream and data, so the same inputs reproduce it at #500 every time. The timer interrupt is asynchronous — driven by external wall-clock timing, not the program. On the second run it may land at a different instruction, or not fire in that window at all. See Interrupts and I/O.


Level 4 — Synthesis

(Combine multiple rules into a full scenario.)

Exercise 4.1 — Full page-fault timeline

Memory holds, in program order at 4-byte spacing:

Addr Instr
ADD R1, R2, R3
LW R5, 0(R6) (page not present)
SUB R7, R1, R5

Walk the entire event sequence: which instructions commit, what EPC is saved, what the OS does, and where execution resumes. Assume the fault is precise.

Recall Solution

The figure below draws the same three-row timeline described in words here — read the text first, then use the picture to check yourself.

Figure — Instruction-level semantics and exceptions
  1. ADD commits. It is older than the fault, so it fully updates . (Top row, drawn in chalk blue — "commits (older)".)
  2. LW faults. The page is absent. Detected before commit ⇒ is not written. (Middle row, chalk pink — "FAULT (page absent)".)
  3. SUB is squashed. It is younger than the fault; even if it already computed something speculatively, that result is discarded — is untouched. (Bottom row, plain chalk — "squashed (younger)".) Good thing: it depends on , which the fault left stale.
  4. Save state. (the LW's own address, because it is a fault), plus the cause and the faulting virtual address. (The pale-yellow curved arrow in the figure points from the saved state back to .)
  5. OS handler runs. It brings the missing page into physical memory (see Virtual Memory and Page Faults), updates the page table, and returns to .
  6. Resume at . The LW re-executes, this time hits a present page, writes , and execution flows on to SUB at normally.

Net effect: the program never notices anything except a time delay — the promise "execute in order, completely" is kept.

Exercise 4.2 — Trap timeline with a return value

A syscall at requests the current time; the OS writes the result into and returns. Before the call , . After a successful return, give (conceptually) and the PC.

Recall Solution
  • syscall is a trap ⇒ resume at the next instruction: .
  • The OS did the requested work and deliberately wrote the result into the architectural register (this is the trap "completing its meaning"). So now holds the time value the OS supplied.
  • Contrast with a fault: a fault handler does not leave a return value in the faulting register — it fixes the environment and re-runs the same instruction, which then produces the value itself.

Level 5 — Mastery

(Design-level judgement and edge cases.)

Exercise 5.1 — Priority among simultaneous events

In one cycle: instruction raises a fault, and a timer interrupt also becomes pending. Both older instructions have committed. State a defensible policy for what happens, and justify it using the synchronous/asynchronous distinction.

Recall Solution

A clean, defensible policy: finish resolving the synchronous exception's instruction boundary first, then take the interrupt. Concretely — squash and younger, save address of for the fault, and take the fault handler. The interrupt is asynchronous: it is not tied to any particular instruction, so it is fine to service it slightly later (many designs take it at the very next clean commit boundary, which the fault handling just created). The key correctness rule: whichever we take, the saved EPC must point at a precise instruction boundary so both handlers can return cleanly. (Real ISAs specify an exact priority table; the principle — precision preserved either way — is what matters.)

Exercise 5.2 — Degenerate / zero cases

Answer each edge case and give the one-line reason:

  1. An instruction that reads and writes the same register, ADD R1, R1, R1, with . Final ?
  2. A branch with offset = 0. Where does a taken branch go?
  3. A syscall at address is the very last instruction before an unmapped page — that is, the next address lies in a page not present in memory. What EPC does the trap save, and what happens the moment execution returns?
Recall Solution
  1. . Semantics are atomic per instruction: the read of both source operands happens on the old value (), the write happens after (). The sequential model forbids the write from feeding back into this instruction's own read.
  2. Taken target — the same address as falling through. An offset-0 taken branch simply lands on the next instruction; it is a legal but pointless jump.
  3. (it is a trap, resume at next). On return the PC points into the unmapped page, so fetching the instruction at itself page-faults. That is a separate, later fault — an instruction-fetch page fault — handled entirely on its own. The trap's return was still correct; the machine simply raises a fresh, distinct exception immediately. Two distinct events, each resolved at its own precise boundary, one at a time.

Exercise 5.3 — Why the contract matters

In two sentences, explain how the sequential-execution promise lets a chip vendor ship a radically faster CPU that runs your old unmodified binary correctly.

Recall Solution

Software depends only on architectural state behaving as if instructions ran one-by-one in order; the ISA guarantees exactly that. So the vendor can redesign all microarchitecture — deeper pipelines, out-of-order execution, speculation — and as long as commit stays in-order and exceptions stay precise, your old binary sees the identical visible results, just faster. This is the whole point of the ISA as a Hardware–Software Contract.


Recall Final self-check (one line each)

Fault return PC vs trap return PC ::: fault → same instruction; trap → next instruction Which exception is reported among several ::: the oldest in program order What enforces precise exceptions in an out-of-order CPU ::: in-order commit/retire (reorder buffer) Synchronous vs asynchronous ::: exception = caused by instruction stream; interrupt = external timing ADD R1, R1, R1 with R1=6 ::: 12 (read-before-write, atomic) What does EPC hold ::: the return address bookmarked when an exception fires