Carl
all lessons
The 6502, by doing Lesson 4 of 9

Loops and branches

A program that runs once is barely a program. INX adds one to X; BNE jumps back if the result isn't zero. Put a store, an increment, and a branch in a loop and the LEDs become a live binary counter you can watch tick.

6502 assembly loops branches

Real programs repeat. The 6502 loops with two pieces: an instruction that changes something (INX — increment X by one), and a branch that decides whether to go back. BNE target means “branch if not equal to zero” — it jumps to target unless the last result was zero.

Below, X counts 0, 1, 2, … and each value is shown on the LEDs. The inner delay loop does nothing useful on purpose: it spins Y from 0 back to 0 (256 steps) just to slow the count down enough to watch. INY / BNE delay is the same loop shape, smaller.

CodeLab — A binary counter on the LEDs
6502 source — stores to VIA Port B ($B000 = 8 LEDs)
loading…
assembler
VIA Port B — 8 LEDs (bit 7 … 0)
CPU
disassembly

Press Assemble, then Run. The LEDs count in binary — watch the rightmost LED toggle fastest, the leftmost slowest, exactly like a car odometer in base 2. Press Stop, then Step a few times to see one increment crawl through the loop. (This program never parks — it’s meant to run forever, so CodeLab lets it.)

Try this: change JMP loop to count by twos (add a second INX), or make the delay shorter and watch it blur.

Next: instead of counting, walk a single lit LED — and meet the shift instructions.