What does the machine do when we read *p?

The C lesson established that a pointer refers to a live object. How does the CPU reach data through that address, where does it keep a result, and where does execution continue after a function returns? Without this bridge, context switches and page faults remain names rather than mechanisms.

The goal is to trace registers, memory and control flow separately, then explain why a load does not necessarily enter the kernel. Allow six hours across three sessions: instructions and data; calls and the stack; address translation and practice. You need pointers, arrays, functions and lifetime. If you cannot distinguish p from *p, revisit section one of the previous lesson.

We use a general CPU model to motivate the problem and RV64I for a precise example. RV64I is the 64-bit base integer portion of RISC-V. Linux remains the destination; RISC-V also prepares us to read small xv6 examples. Do not transfer these register names and instructions to x86-64.

1. Why bring memory data into registers?

Suppose a number in memory must increase by seven. The machine needs somewhere to hold the input, an operation that adds, and a way to save the result. In the base load/store architecture used here, a load brings data into a register, arithmetic operates on registers, and a store writes the result to memory. This motivates a design rather than claiming every historical CPU developed in this sequence.

A register is a small named part of the CPU's architectural state. A name such as a0 identifies a logical data location. Architectural state means what the instruction contract makes observable to software; the processor's internal circuitry need not literally match this simple picture. RAM is much larger working storage. Register contents and RAM contents are distinct: adding in a register does not automatically update RAM.

The PC, or Program Counter, identifies the instruction execution path. In this sequential model it addresses the current instruction; a successfully completed ordinary instruction advances to the next. Branches and jumps can choose another destination. A C loop ultimately requires such control-flow behavior; the CPU need not know C variable names or the meaning of while.

Every instruction in our trace is four bytes, with compressed instructions excluded. Most steps therefore advance PC by four. This is not a rule for every RISC-V program or architecture. The register named zero is special: reading returns zero, and writes do not change its observable value. See the base instruction model.

2. ISA versus internal implementation

The ISA, Instruction Set Architecture, specifies instructions and their observable effects. Two CPUs can run the same program while differing in caches, pipelines and speed. Fetch, decode and execute is useful for reasoning about effects; five table rows do not imply five clock cycles. Real CPUs may overlap work and speculate. This lesson is not a timing model or benchmark.

Assembly gives textual names to instructions and operands. A compiler translates higher-level code; an assembler converts assembly to machine code; a linker combines pieces and resolves references. How many instructions implement one C expression depends on types, optimization and target. The example below is handwritten teaching assembly, not reported compiler output.

3. Worked example: load, add, store and return

Consider this corresponding C function:

#include <stdint.h>
uint64_t add_to(uint64_t *p, uint64_t delta)
{
    *p += delta;
    return *p;
}

Contract: p addresses a live, writable, properly aligned uint64_t object, with no concurrent access in this example. Unsigned addition is defined modulo two to the power of 64. This function performs no syscall and is not atomic.

For our RV64 model and usual integer calling convention, assume a0=0x4000 holds p, a1=7 holds delta, ra=0x1040 is the return address, and the eight-byte value at virtual address 0x4000 is 5. Code starts at 0x2000. Pages are present and access is permitted.

ld   t0, 0(a0)
add  t0, t0, a1
sd   t0, 0(a0)
addi a0, t0, 0
jalr zero, 0(ra)

0(a0) forms an effective address by adding offset zero to a0; it does not mean the numeric value zero inside a0. ld reads eight bytes and sd writes eight. See RV64I instructions.

Instruction PC Main effect a0 afterward t0 afterward Value at 0x4000 Next PC
0x2000: ld Read object 0x4000 5 5 0x2004
0x2004: add Add in register 0x4000 12 5 0x2008
0x2008: sd Store result 0x4000 12 12 0x200c
0x200c: addi Put result in a0 12 12 12 0x2010
0x2010: jalr Return through ra 12 12 12 0x1040

The causal detail matters: a0 retains p until after the store. Overwriting it with 12 before sd would store at address 12, no longer updating the intended object. A fault is possible, but a guaranteed crash is not a valid prediction. Conversely, removing the store can return 12 while leaving memory at 5. Return value and side effect are separate outputs.

jalr zero, 0(ra) takes its return destination from ra and reaches 0x1040 in this model. Ordinary return requires no privilege change. Calling another function also requires preserving the return route; this example is a leaf, meaning it calls no other function.

4. Why an ABI, and what does the stack solve?

Separately compiled functions must agree on argument and result locations. The calling convention is this part of the ABI. In the standard integer convention used here, a0 and a1 carry this function's arguments and a0 carries its result. t0 is a temporary whose preservation across calls is not guaranteed. sp tracks the active stack top. See the RISC-V convention.

Now let f call g and need its earlier data afterward. A nested call can overwrite the return-address register. f must preserve necessary information somewhere safe, such as its stack frame. A stack frame is one call's temporary portion of stack memory and can hold a return address, saved registers or locals. Our simple function needs no such frame; every function does not necessarily consume stack space.

In the standard convention used for this example, the stack grows toward lower addresses and requires sixteen-byte alignment; do not generalize this to all ABIs. Restoring sp after a call need not zero the bytes, yet accessing an expired automatic local is still invalid. This connects machine behavior to the C lifetime lesson. The psABI page was a development document when checked; we use relevant integer rules, not all experimental sections.

5. A pointer usually is not a physical RAM address

Address 0x4000 in the example is a virtual address: a name in the program's execution address space. The MMU, Memory Management Unit, translates it into a physical address according to active mappings and permissions. The kernel manages these mappings. An ordinary successful translation does not require a syscall for every load.

A page is a mapping unit. Our illustrative page size is 4096, or 0x1000, bytes. Virtual address 0x4128 has page number 4 and within-page offset 0x128. If virtual page 4 maps to physical frame 9, the physical address is 0x9000+0x128, or 0x9128. The offset is unchanged; the page base changes. Page size and page-table organization are not universal. See the Linux overview.

Two different jobs: translating an address and keeping data

This is a conceptual path, not a CPU timing or wiring diagram. The TLB caches translations; the data cache holds data. Actual lookup order and overlap depend on the microarchitecture.

A TLB, Translation Lookaside Buffer, reuses recent translations. A TLB miss means the appropriate translation was absent from that cache, not necessarily a page fault. In RISC-V's hardware-paging model, a page-table walk can find a valid mapping and continue without entering a kernel handler. If the required mapping or permission is absent, a fault can reach the kernel. Depending on its cause, the kernel may repair it or stop the program; a page fault therefore need not be fatal. See the RISC-V supervisor rules.

6. Cache misses, protection faults and crashes differ

A data cache retains frequently used data to reduce access cost. An ordinary data-cache miss can be served from a lower memory layer without a kernel handler. A TLB miss concerns translation. A permission fault concerns authorization. These answer different questions: Is the data here? Is the translation here? Is this access allowed?

Two processes printing 0x4000 do not establish a shared physical object without knowing their address spaces and mappings. Conversely, deliberate sharing can connect different virtual addresses to shared data. Data coherence and TLB translation consistency are also distinct; changing mappings may require appropriate invalidation. The memory lesson will develop this. See Linux on caches and TLBs.

Suggested experiment and evidence limits

Compile the user-space C example where a C compiler is available. Predict returned and value first; both should be 12 on the normal path. To inspect assembly for that compiler's target, these commands can be used in Linux:

cc -std=c11 -Wall -Wextra -O0 add_to.c -o add_to
./add_to
cc -std=c11 -O0 -S add_to.c -o add_to-O0.s
cc -std=c11 -O2 -S add_to.c -o add_to-O2.s

If the compiler targets x86-64, its output is not RISC-V. Register names, instruction counts and stack use may differ from the table. The compiler may inline the function or fold constants, eliminating even the expected load. Compare semantic effects, not line-for-line identity. Running a host program is not evidence of executing RV64 instructions. Here the C code has not been compiled, and the assembly has not run on hardware or an emulator; the table is a manual analysis under stated assumptions. Numerical calculations are checked separately.

Practice and continuation criterion

  1. Why does memory remain 5 after add in the table? What changes if sd is removed?
  2. Rebuild the table without notes for initial value 9 and delta 4.
  3. If a0 becomes the result before sd, where does the store go? Why is a definite crash prediction unjustified?
  4. Why can a leaf function run without a stack frame? Suggest a change requiring information preservation.
  5. With page size 0x1000, virtual address 0x5abc, and virtual page 5 mapped to frame 2, what is the physical address?
  6. How does a TLB miss with a valid mapping differ from a permission fault?
  7. Do equal pointer numbers in two processes prove equal data?
  8. Must the five instructions take exactly five clock cycles? Why?

Answers and repair guidance. Before continuing, separate load and store effects, solve the address-translation exercise, and avoid equating a TLB miss with mandatory kernel entry. If registers and memory are confusing, rebuild the trace with independent columns; for offset confusion, divide the address into page number and remainder. Tomorrow, reconstruct example two from memory; at the chapter review, also explain alias use after free. Creating these files establishes no personal study progress.

Sources above were opened and checked on 2026-10-17. ISA links identify v20260120; psABI and latest documentation may change. Models, tables and diagrams are original teaching material. Roadmap · Previous · فارسی. Next: reproducible lab setup, still planned.