A CPU has no built-in loop statement. A loop is built from jumps:
- a backward jump instruction sends the PC back to the start of the body;
- a conditional branch somewhere in the body jumps out when the work is done.
A typical Tiny8 counting loop keeps a counter in memory. Each pass loads it, subtracts 1 (or some step), and stores it back. The SUB sets Z, so JZ can leave the loop when the counter reaches 0. Otherwise a JMP goes back to the top.
When counting instructions or cycles in a loop, be careful with the last pass. It usually leaves through the JZ, so it skips the JMP. A loop that runs n times often executes the JMP only n − 1 times.
A bug to watch for: if the counter never hits exactly 0 (say you subtract 2 from an odd number), Z never becomes 1 and the loop wraps around 256 and keeps going.