Memory and the processor · The zero flag
Counting
down.
Three things left to do. Then two. Then one. This loop subtracts one each time and finishes when the result reaches zero.
Address 3 holds our starting byte. Address 4 is where we will put the result. Values below are decimal.
Address 3 starts at 3; address 4 starts at 0.
Select a location to edit it in the explorer below. The read and write always use addresses 3 and 4.
Register A
0decimal
- Binary
- 0000 0000
- Hexadecimal
- 00
Z starts at 0. The subtraction sets Z to 1 if its result is zero, or 0 otherwise. Load, store, and jump preserve Z.
The program in memory
Addresses and bytes below are hexadecimal.
← Next (PC) marks the next instruction. Shaded bytes were fetched by the last step.
Runs counts how often each instruction has executed since Start again.
010B · End of this program
Each press runs one instruction; nothing runs between presses.
No instruction has run. PC points to the first instruction.
PC path so far: 0100.
Enable JavaScript to run this experiment.
No instruction has run. A starts at 0.
Change the byte at address 3
Edits here change memory immediately. They do not change A or run an instruction.
Enable JavaScript to change this byte. This example shows 3.
See the 8080 instructions
The first instruction is LDA 0003H: load A from memory address 3. Next comes SUI 1: subtract one from A and update the arithmetic flags. Then comes STA 0004H: store A at address 4. The load and store preserve the processor’s flags.
JNZ 0103H follows the store: jump if not zero. With Z = 0 it returns PC to the subtraction; with Z = 1 it continues at 010B. Both paths fetch all three instruction bytes and preserve A, flags, and RAM. The load runs just once.
SUI subtracts the number encoded in the instruction without using incoming carry. It updates sign (S), zero (Z), auxiliary carry (AC), parity (P), and carry (CY). For subtraction, CY records a borrow; the lesson focuses on Z. All flags appear in the instruction record.
This view shows the first eight locations of a 65,536-byte RAM. The instructions live elsewhere, starting at hexadecimal address 0100. The program counter, PC, tells the processor where to find its next instruction.
Current PC: 0100 (hexadecimal). The lesson stops offering steps at 010B, just after the final instruction. This is a lesson boundary; the processor has not executed a halt instruction.
No instruction has run.
The record includes the reads that fetch the instruction bytes, as well as the data read or write. Editing a memory byte in the explorer is a separate learner action; it does not alter the last instruction’s record.
Explore the Intel 8080 guideOne less each time.
The load puts our starting count, 3, into A. The next instruction is SUI 1: subtract one from A. It makes A = 2, and the store writes that result to address 4.
The subtraction also updates the zero flag, Z. Z = 1 means the result was zero. Z = 0 means it was not zero.
Is there anything left?
The jump is JNZ 0103H: jump if not zero. It tests Z, just as the previous lesson’s JNC tested carry.
- Z = 0: the result was not zero, so return to the subtraction at
0103. - Z = 1: the result was zero, so continue after the jump at
010B.
Take the jump and go around again. A becomes 1, which is still not zero. On the third trip, A becomes 0 and Z becomes 1. The last jump falls through.
The stored results are 2, 1, then 0. The lesson stops offering steps at 010B; the CPU has left the loop without executing a halt instruction.
What Z remembers.
In this program, the subtraction updates Z. Loading, storing, and jumping leave it alone. Z records a calculation’s result; it does not continuously check the current contents of A or memory.
That is why A and Z both start at 0 in this experiment: we initialized them that way. No subtraction has run yet.
Choose a count.
Start again, then change address 3 before loading. Try 1 for one trip through the loop, or 5 for five trips. Each trip subtracts one, stores it, and tests Z.
Changing address 3 after the load does not change the countdown already in A. The jump returns to the subtraction, so the load runs only once.
What if the starting count is zero?
This program subtracts before it tests. Loading 0 does not set Z. The first subtraction, 0 − 1, wraps to 255 in a byte, and sets Z to 0 because that result is not zero.
The loop then counts down from 255. It takes 256 trips altogether to reach zero again. You can try the first few steps and use Start again without finishing that long run. Checking before doing the work would require a different program.
For starting counts from 1 to 255, we now know exactly how many times the loop repeats. A small number in memory controls how much work the program does.