Memory and the processor · Comparing values
Choosing a
stopping point.
We can count down to zero. But what if we want to count up to 44? The processor can compare two numbers without changing either one.
Address 3 holds our starting byte. Address 4 is where we will put the result. Values below are decimal.
Address 3 starts at 41; 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. Compare sets Z to 1 when A matches the target, or 0 otherwise. Add also updates Z. Load, store, and jump preserve it.
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.
Choose 0–255 before the first step. This changes just the byte at 0109. Start again restores 2C and unlocks this field.
Address 0109 contains 2C in hexadecimal (44 in decimal).
010D · 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 41.
See the 8080 instructions
The first instruction is LDA 0003H: load A from memory address 3. Next comes ADI 1: add one to A and update the arithmetic flags. Then comes STA 0004H: store A at address 4. The load and store preserve the processor’s flags.
CPI follows the store: compare immediate. It subtracts the target byte from A to set the arithmetic flags, but discards the result, leaving A and RAM unchanged. JNZ 0103H then tests Z: with Z = 0 it returns PC to the addition; with Z = 1 it continues at 010D. Both paths fetch all three jump bytes and preserve A, flags, and RAM. The load runs just once.
ADI adds the number encoded in the instruction, regardless of which memory byte is selected. It does not add the incoming carry. Unlike INR A, which preserves carry, ADI updates CY. It also updates sign (S), zero (Z), auxiliary carry (AC), and parity (P); their before/after values appear in the 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 010D, 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 guideHave we reached 44?
The first three instructions are familiar: load 41 into A, add one, and store the result at address 4. A and address 4 now hold 42.
The next instruction is new: CPI 44, meaning compare A with 44. It asks whether the two numbers are equal. The answer appears in the zero flag, Z.
The familiar JNZ 0103H follows. With Z = 0 it returns to the addition. The next trip produces 43, which still differs from 44, so the loop repeats again.
Zero, without making A zero.
On the third trip, adding one makes A = 44. The store writes 44 to address 4. The comparison now sets Z to 1, and JNZ continues past the loop at 010D.
Why the zero flag? Comparing calculates A minus the target just to set the flags. When the numbers match, the difference is zero: 44 − 44 = 0. The processor discards that difference. A keeps 44.
The load runs once; add, store, compare, and jump each run three times. That is thirteen instructions, storing 42, 43, then 44. The lesson stops offering steps after the last jump; the CPU has not executed a halt instruction.
Choose your target.
Start again and change Compare A with this target to 43. The byte at 0109 changes from 2C to 2B: hexadecimal for 44 and 43. The instruction now says CPI 43.
CPI means compare immediate. Like the number in ADI, its target is a byte in the instruction itself. Editing it changes the program in memory; it does not execute the comparison.
The target accepts 0–255 and locks after the first step. Start again restores the starting byte to 41, the target to 44, and all the records and counts to their initial state.
What if the target is 41, or less?
This program adds before it compares. A target of 41 therefore needs a whole trip around the byte’s range: 256 additions to reach 41 again. A lower target also requires passing through 255 and wrapping to 0.
For a short experiment, start again, set the target to 0, and change the byte at address 3 to 254 before loading. Two trips store 255, then 0. Each addition still adds exactly one.
An answer the jump can use.
Z remembers the last instruction that updated it. Both addition and comparison can change Z here. The comparison comes after the addition, so its answer is the one JNZ uses.
We can now make a loop finish at a chosen number while keeping that number in A. Comparison supplies the answer; the jump chooses the path.