Addition

This file is part of and Synchro-Sette February 1983. Download the collection to get this file.
Date: February 1983
Type: Program
Platform(s): TS 1000

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:

  1. Initialisation (lines 5–6): Clear the result string C$ and carry register R.
  2. Input (lines 10–36): Prompt for and accept two number strings A$ and B$, record the longer length in A, then pause briefly before switching to FAST mode.
  3. Padding subroutine (line 50 → 1000–1060): Prepend "0" characters to the shorter string so both are exactly A digits wide.
  4. Addition loop (lines 90–140): Iterate from the most-significant position down to 1, computing each digit sum with carry.
  5. Leading-zero suppression (lines 150–220): Replace leading "0" characters with spaces in both A$ and B$ for display purposes.
  6. 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.
  7. Loop (lines 345–370): Return to SLOW, wait for any INPUT, clear screen, and restart with RUN.
  8. Save record (line 998): SAVE "ADDITIO%N" — the %N marks 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 the Nth character as a one-character string, allowing VAL to 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) and SLOW (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 uses RUN (line 370), which re-initialises all variables — a deliberate choice since C$ and R must 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 runs A TO 1 STEP -1 — i.e., least-significant first. The prepend operation C$=D$(LEN D$)+C$ correctly rebuilds the digit order. However, because carry from position N feeds into position N-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/NEXT loops at 150–180 and 190–220 use GOTO 190 and GOTO 280 to exit early, bypassing NEXT N. On the ZX81 this leaves the loop variable in an indeterminate state, but since N is 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 VAL error at line 100.

Variable Summary

VariableRole
A$First operand (string); padded and display-stripped in place
B$Second operand (string); padded and display-stripped in place
C$Result accumulator string
ALength of the longer operand (working column width)
RCarry bit (0 or 1)
D$Temporary digit-sum string (“0″–”9” or “10”–”18″)
NOuter loop index (column position)
IInner loop index (padding subroutine)

Content

Appears On

Cassette to accompany the February 1983 issue of Synchro-Sette.

Related Products

Related Articles

Related Content

Image Gallery

Addition

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.

People

No people associated with this content.

Scroll to Top