Chapter 2 of 8
Give the receiver a damaged signal
A checksum buys you a second opinion about a message. In this receiver the sender and receiver agree that a healthy three-byte message adds up to $E3. You can change the message at the sending end while leaving that agreement in place.
Predict the next PC
Start with your portable receiver. At the bottom, FCC "ORB" lays down three character codes, with no length byte or terminating zero supplied by FCC. **size EQU *-packet is the distance from the packet label to the current assembly position. FCB $E3** supplies an expected value. Changing that value along with the message would spoil the experiment.
In the intact packet the partial sums are $4F, $A1 and $E3. Save a second copy and change only the quoted ORB to ORD. The code for D ($44) is two more than B ($42). Predict A after the third addition. Assemble this copy, set a stop on inspect, and Run. The destination now reads 4F 52 44 00. The expected byte still reads E3.
| With PC at inspect | Intact | Changed |
|---|---|---|
| A before CMPA | $E3 | $E5 |
| Z after stepping CMPA | 1 | 0 |
| PC after stepping BNE | $3F12, success path | $3F18, damaged path |
| Guest output after Continue | ORB | CHECK |
The flag you see on arrival at inspect belongs to the earlier instructions. In particular, the last DECB made B zero on both runs. Step CMPA before interpreting Z as the result of the comparison. CMPA updates the condition codes as if it subtracted the expected byte; A keeps the received sum. BNE branches when Z is clear.
Ask what the sum can tell you
An eight-bit accumulator keeps the low eight bits of the sum. Carry out of the top bit is lost to the total unless a program explicitly uses it. Can this receiver distinguish the letters O, R, B in one order from the same letters in another? Work the sum on paper. Addition gives the same answer in both orders. A checksum like this can catch the edit you made. It cannot prove the message’s order or authorship.
Make two correct comparisons with the same flags
Open Develop → Assembly Quick Reference… and choose the example Compare signed and unsigned. Open its independent example in the workspace. Use Debug from Entry on a disposable guest. Step LDA and CMPA. A holds $FF; the operand was $01. The subtraction used for the flags gives $FE. Z=0, N=1, V=0 and C=0. The comparison leaves A at $FF.
Read the next two branches before stepping them. BLT tests whether N differs from V after the subtraction. They differ here, so BLT goes to signed. The branch there, BHI, tests unsigned “higher”: carry clear and zero clear. It goes to both. Branches use the flags from CMPA; taking the first branch preserved those flags for the second.
How can both be true? Read the same bits as a signed eight-bit number and $FF is −1, less than +1. Read them as an unsigned count and $FF is 255, greater than 1. The author chooses which question to ask by choosing the branch. The example holds at both for inspection. It deliberately loops there; use a fresh BASIC session for the next exercise. The printable receiver instead exits with RTS.