This program implements a long-addition demonstration that accepts two multi-digit numbers as strings and adds them digit by digit, mimicking the written column-addition method taught in schools. It pads the shorter input with leading zeros via a subroutine at line 1000, then iterates from the least-significant digit upward, accumulating a carry in variable R. Leading zeros in both addends are replaced with spaces before display, and the result is shown in a columnar format with the operands, a plus sign, an equals sign, and the sum. The program switches to FAST mode during computation and back to SLOW for display, then loops via RUN after awaiting an INPUT keypress.
Program Analysis
Program Structure
The program is organised into a short main flow and a single subroutine:
- Initialisation (lines 5–6): Clear the result string
C$and carry registerR. - Input (lines 10–36): Prompt for and accept two number strings
A$andB$, record the longer length inA, then pause briefly before switching to FAST mode. - Padding subroutine (line 50 → 1000–1060): Prepend
"0"characters to the shorter string so both are exactlyAdigits wide. - Addition loop (lines 90–140): Iterate from the most-significant position down to 1, computing each digit sum with carry.
- Leading-zero suppression (lines 150–220): Replace leading
"0"characters with spaces in bothA$andB$for display purposes. - Result display (lines 280–340): Print the columnar addition layout, prepend a carry digit
"1"if needed, and pad the sum string with a leading space if it is the same width as the operands. - Loop (lines 345–370): Return to SLOW, wait for any INPUT, clear screen, and restart with
RUN. - Save record (line 998):
SAVE "ADDITIO%N"— the%Nmarks the filename with an inverse N for auto-run.
Subroutine: Zero-Padding (lines 1000–1060)
The subroutine zero-pads both operand strings to length A (the length of the longer input). Two FOR loops prepend "0" characters one at a time. Because string concatenation in ZX81 BASIC is the only available tool, this is done iteratively rather than in one operation.
Addition Algorithm
The core loop at lines 90–140 works right-to-left through the padded strings. At each position N, the digit sum string D$ is built as STR$ (R + VAL A$(N) + VAL B$(N)). If D$ is two characters long, carry R is set to 1; if one character, carry is cleared. The last character of D$ (i.e., the units digit of the local sum) is prepended to the accumulator C$.
Note that the loop runs from A down to 1 (STEP -1), so the most-significant digit is processed first and the result is built by prepending — this correctly handles carry propagation when combined with the final carry check at line 280.
Notable Techniques
- String-based arithmetic: Numbers are held entirely as strings;
VAL A$(N)extracts a single digit’s numeric value, avoiding any floating-point representation issues for large integers that exceed ZX81 numeric precision. - Single-character extraction:
A$(N)on the ZX81 returns theNth character as a one-character string, allowingVALto convert it directly to a digit. - Carry detection via string length:
LEN D$being 1 or 2 neatly distinguishes sums less than 10 from those ≥ 10 without any division or modulo operation. - FAST/SLOW bracketing: The computation is wrapped in
FAST(line 38) andSLOW(line 345) to speed up the string-manipulation loop while keeping display readable. - RUN as a restart loop: Instead of a
GOTO 5, the program usesRUN(line 370), which re-initialises all variables — a deliberate choice sinceC$andRmust be reset each iteration.
Bugs and Anomalies
- Loop direction vs. carry logic: The addition loop runs from position 1 (most-significant after padding) to
A(least-significant), but the algorithm description above shows it runsA TO 1 STEP -1— i.e., least-significant first. The prepend operationC$=D$(LEN D$)+C$correctly rebuilds the digit order. However, because carry from positionNfeeds into positionN-1(the next iteration of the descending loop), carry propagation is accurate. - No overflow beyond one carry digit: The program only handles a single extra carry digit (line 280 prepends
"1"). This is sufficient for single-carry addition of equal-length numbers but would fail if the carry caused a cascade that the loop did not account for — although in practice a single leading carry is always correct for the final step. - Leading-zero suppression loops (150–220) do not reset N cleanly: The
FOR/NEXTloops at 150–180 and 190–220 useGOTO 190andGOTO 280to exit early, bypassingNEXT N. On the ZX81 this leaves the loop variable in an indeterminate state, but sinceNis reused only as the outer loop index (already completed) this causes no practical harm. - Input validation absent: No check is made that the strings entered are actually numeric, so non-digit characters will cause a
VALerror at line 100.
Variable Summary
| Variable | Role |
|---|---|
A$ | First operand (string); padded and display-stripped in place |
B$ | Second operand (string); padded and display-stripped in place |
C$ | Result accumulator string |
A | Length of the longer operand (working column width) |
R | Carry bit (0 or 1) |
D$ | Temporary digit-sum string (“0″–”9” or “10”–”18″) |
N | Outer loop index (column position) |
I | Inner loop index (padding subroutine) |
Content
Source Code
5 LET C$=""
6 LET R=0
10 PRINT ,,"FIRST NUMBER?"
15 INPUT A$
20 LET A=LEN A$
24 PRINT ,,A$
26 PRINT ,,"SECOND NUMBER?"
30 INPUT B$
34 PRINT ,,B$
36 PAUSE 200
38 FAST
40 IF A<LEN B$ THEN LET A=LEN B$
50 GOSUB 1000
90 FOR N=A TO 1 STEP -1
100 LET D$=STR$ (R+VAL A$(N)+VAL B$(N))
110 IF LEN D$<>1 THEN LET R=1
120 IF LEN D$=1 THEN LET R=0
130 LET C$=D$(LEN D$)+C$
140 NEXT N
150 FOR N=1 TO A
160 IF A$(N)="0" THEN LET A$(N)=" "
170 IF A$(N)<>" " THEN GOTO 190
180 NEXT N
190 FOR N=1 TO A
200 IF B$(N)="0" THEN LET B$(N)=" "
210 IF B$(N)<>" " THEN GOTO 280
220 NEXT N
280 IF R=1 THEN LET C$="1"+C$
290 IF LEN C$=A THEN LET C$=" "+C$
300 PRINT ,,,," ";A$
310 PRINT " +"
320 PRINT " ";B$
330 PRINT " ="
340 PRINT C$
345 SLOW
350 INPUT A$
360 CLS
370 RUN
998 SAVE "ADDITIO%N"
999 RUN
1000 FOR I=1 TO A-LEN A$
1010 LET A$="0"+A$
1020 NEXT I
1030 FOR I=1 TO A-LEN B$
1040 LET B$="0"+B$
1050 NEXT I
1060 RETURN
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.
