TactivergeLite

Lite / Engineering Complex Systems / Week 25

Tracing a program, counting cycles

Two programs give the same answer. One takes 12 clock cycles, the other 33. How do you know before you run them?

About 35 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

Performance engineers predict a program's speed by counting what the hardware must do. Tracing by hand also finds bugs that testing misses: off-by-one loops, a flag set by the wrong instruction, a store to the wrong address.

Predict

In the Nibble 'Count down' program with M0 = 3, how many instructions execute before it halts (counting HALT)?

  1. 5
  2. 8
  3. 11
  4. 15
Guess first, then reveal

LOAD once, then ADD, STORE, JNZ three times (9), then HALT: 1 + 9 + 1 = 11. At 3 cycles each that is 33 cycles. The stepper's cycle counter confirms it.

Discover

Run 'Add two numbers' to the end and record the cycle count and the carry. Then step through 'Count down' one instruction (three clicks) at a time, writing a trace table: line, instruction, A, Z, M0. Check your predicted cycle count.

What the interactive shows (text version)

The stepper runs 'Add two numbers' in 4 instructions (12 cycles), leaving M2 = 1 with C = 1. 'Count down' from 3 executes LOAD, three rounds of ADD 15, STORE 0, JNZ 1, then HALT: 11 instructions, 33 cycles, ending with M0 = 0 and Z = 1.

You can also open the interactive on the full site when you have a faster connection.

Understand

A trace table

List, for each instruction executed: the program counter, the instruction, and every register, flag and memory cell it changes. Only change what the instruction says it changes: STORE writes memory but leaves A and the flags alone. Most tracing mistakes are updating something an instruction doesn't touch.

Flags are how machines decide

Arithmetic sets flags as a side effect: Z (result was zero) and C (a carry fell off the top). A conditional jump like JNZ reads Z. In 'Count down', ADD 15 sets Z when A reaches 0; STORE in between doesn't disturb Z; JNZ then falls through to HALT.

Counting cycles

Cycles = Σ (instructions executed × cycles per instruction). A loop body of b instructions run n times costs b·n plus setup. In the stepper's model every instruction takes 3 cycles; real processors vary by instruction. The executed count, not the program length, decides speed: a 5-line loop can run thousands of instructions.

Explain it another way

Intuitive

Tracing is being the processor with a pencil: read a line, do exactly what it says, update your notes, move to the next line (or jump).

Technical

CPI (cycles per instruction) times instruction count times clock period gives run time: T = IC × CPI × T_clk, the 'iron law' of processor performance.

Mathematical

Count down from M0 = m: instructions = 1 + 3m + 1 = 3m + 2; cycles = 3(3m + 2) = 9m + 6. For m = 3: 33 cycles.

Applied

Embedded engineers count cycles in interrupt handlers to guarantee they finish before the next interrupt; an audio filter that overruns its cycle budget causes audible glitches.

Worked example 1

Trace 'Add two numbers' with M0 = 9, M1 = 8.

LOAD 0: A = 9, Z = 0. ADD 1: A = 17 mod 16 = 1, C = 1, Z = 0. STORE 2: M2 = 1. HALT. 4 instructions × 3 = 12 cycles, and C = 1 warns that the true answer, 17, didn't fit.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

cycles

Practice 2. In the Nibble machine, which instruction does NOT change the Z flag?

cycles

Apply it

Write a Nibble program that adds M0 to itself M1 times without using the multiply answer from the build: instead, explain your loop's trace for M0 = 2, M1 = 3, count its cycles with the 3-cycle model and give a formula for any M1.

Reflect

Which part of your trace table was easiest to get wrong, and how would you check it?

Write your answer on paper or in your own notes. Lite never stores what you write.

← Instructions are just numbersThinking in register transfers →