Skip to content
Tactiverge

School/□ Systems Engineer/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

1.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.

  • ◇ Trace a short assembly program, tracking registers, flags, memory and the program counter.
  • ◇ Count instructions executed and clock cycles for straight-line code and loops.
  • ◇ Explain how flags let a machine make decisions.

2.Predict

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

Guess first. Being wrong here is how the surprise works.

3.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.

Loading the interactive…

Can't use the interactive? Read a description instead

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.

4.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1

A straight-line program executes 6 instructions on a machine that takes 3 cycles per instruction. How many cycles?

cycles

Practice 2

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

5.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

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).

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

  • A shorter program always runs faster.
    ✓ Speed depends on instructions executed, not lines written: a short loop can run for a long time.
  • Every instruction updates the flags.
    ✓ Only those the ISA says do; in Nibble STORE, JNZ and HALT leave them unchanged.

6.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 3

Using cycles = 9m + 6 for the count-down program, how many cycles does it take from M0 = 7?

cycles

Practice 4 · a fresh version every time

A program spends 2 cycles on setup, then runs a loop 15 times with 8 cycles per pass. A new instruction cuts the loop body to 6 cycles. What is the speed-up (old cycles ÷ new cycles), to 2 decimal places?

Practice 5 · a fresh version every time

Two 8-bit registers start at A = 0, B = 4. On every clock edge both transfers happen at once: A ← B and B ← (A + B) mod 256. What does B hold after 6 clock cycles?

7.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.

Open BUILD 008: Create a Virtual Computer →

8.Reflect

Concepts: Instruction set architecture · Algorithms, searching and complexity