Meet the Altair · Recognizing an ending
Where does
a message end?
Last time, B told the program how many bytes to print. This time, a byte in memory marks the end. The program reads, checks, and only then sends.
Begin here: The message is still HELLO and a line feed. One extra byte, 00, follows it. Step through the first four instructions: set HL, read H, compare it with zero, then decide whether to finish. Nothing has reached the display yet.
Loaded message program · reference card All addresses and bytes here are hexadecimal
| Address | Reference | RAM | Check |
|---|
0000–0002: 21 00 01 puts 0100 into HL.
0003: 7E reads the byte at HL into A.
0004–0005: FE 00 compares A with zero.
0006–0008: CA 0F 00 jumps to 000F if Z = 1.
0009–000A: D3 01 sends A to the display.
000B: 23 advances HL.
000C–000E: C3 03 00 jumps back to 0003.
000F: 76 halts the CPU.
The lesson checks all sixteen program bytes before executing. Data at 0100–0106 starts as 48 45 4C 4C 4F 0A 00: HELLO, line feed, and a zero terminator. Data can be changed while stopped; RAM elsewhere starts at zero.
This lesson connects an 8080, 65,536 bytes of RAM, and a teaching output device at port 1. The program at 0000 reads text at 0100 until it finds zero. Start again restores the program and data, clears registers, flags, halted state, output, counts, and history, and stops at PC 0000. It is not the hardware RESET switch.
Give the message an ending.
The seven bytes at 0100–0106 are 48 45 4C 4C 4F 0A 00. The first six are familiar. The last byte is zero, labelled NUL in ASCII. This program uses it as a terminator: a value that marks where the message ends.
RAM simply holds bytes. It does not know which ones are letters, where a message begins, or where it ends. The program gives zero this job by checking for it. There is no length in B this time; B stays unused.
Check before sending.
LXI H,0100H sets the pointer, and MOV A,M reads through it. With the original message, A now holds 72, the ASCII byte for H. Neither instruction prints anything.
CPI 0 compares A with zero. Because 72 differs from zero, Z becomes 0. A keeps 72. The following JZ 000FH does not jump, so execution reaches OUT 01H and H appears.
INX H advances the pointer. JMP 0003H returns to the read. Every trip asks the same question about a new byte: is this zero?
Zero, “0”, and a new line.
These three bytes have different values and different jobs. The memory window shows hexadecimal bytes; A shows decimal.
| Byte · hex | Decimal | Meaning here |
|---|---|---|
| 00 | 0 | NUL: the program finishes without sending it. |
| 30 | 48 | The printed character “0”: send it and continue. |
| 0A | 10 | LF: send it to start a new line, then continue. |
After LF, HL advances to 0106. MOV reads zero into A, but Z is still 0 from comparing LF. CPI then sets Z to 1, and JZ jumps straight to HLT. The final zero is read and checked, but never sent. HL stays on its row.
The output device has no special rule for ending this message. The CPU avoids OUT on the terminating path. In the previous lesson, a zero among the six counted bytes would have been sent and displayed as ⟨NUL⟩.
How does the complete run finish?
One setup instruction, six trips of six instructions, then MOV, CPI, JZ, and HLT: 41 instructions. Final A is 0, HL is 0106, Z is 1, and PC is 0010. The display received six writes: HELLO and a line feed.
Move the ending without changing the program.
Use the panel while stopped to change the message. EXAMINE changes PC, so return to 0000 before running from the beginning.
Try stopping after HE
- Start this lesson again.
- Set the switches to 0102 and press EXAMINE. This is the first L.
- Lower all switches and press DEPOSIT to replace it with 00.
- With the switches still at zero, press EXAMINE to return PC to 0000.
- RUN. Predict what happens to the remaining bytes.
The display prints HE and then stops. The later L, O, and LF remain in RAM; the CPU never reads them on this run. The pointer finishes at 0102.
Try the character “0” instead
Start again, EXAMINE 0102, then set the switches to 0030 and DEPOSIT. Lower the switches and EXAMINE 0000 before running. The display prints HE0LO and a line feed: 30 hex is not a terminator.
Try an empty message
Start again and deposit 00 at 0100. EXAMINE 0000 and run. Five instructions set the pointer, read zero, compare, jump, and halt. No OUT executes, so the display stays empty.
Changing RAM after MOV does not change the byte already in A. Likewise, changing RAM after CPI does not change the comparison stored in Z. The next instruction uses the processor’s current state.
A convention needs a matching byte.
The loop has no built-in message length. If you replace the zero at 0106 with a nonzero byte, it sends that byte and keeps reading beyond the window. In this lesson, the next location, 0107, starts at zero, so it would finish there instead. The window reports when HL is outside the displayed range.
A program using this convention needs a reachable zero to finish. It does not stop because it has reached the edge of our picture. STOP can still pause execution between instructions; starting the lesson again restores all the original bytes.