Self-Modifying Telephone Directory

Type
Utility
Category
Self-modifying program / database
Author
John Anderson
Published in
The Creative Atari (Creative Computing Press, 1983), "Self-Modifying Programs", Figure 2
Year
1983
Language
Atari BASIC
Requirements
Any Atari 8-bit, Atari BASIC; disk drive to save the directory (or change line 270 for cassette)
A text-mode menu boxed in asterisks: Telephone Directory, (L)ist, (A)dd, (D)elete, (S)ave

About the program

Atari BASIC can rewrite its own program lines while it runs. In "Self-Modifying Programs" John Anderson explained the trick: print some program lines on the screen, followed by CONT, move the cursor to the top, switch the screen editor into "forced read" mode and STOP. The editor then "presses RETURN" over every line on the screen by itself. BASIC takes the printed lines in as program lines and then reads CONT, so the program carries on.

His practical example is this telephone directory. Each entry is saved as a REM line numbered 1–100, so when you SAVE the program, your data goes with it. The program becomes its own database.

What you need

Any Atari 8-bit with Atari BASIC, and a disk drive for the (S)AVE option. Cassette users should change line 270 to CSAVE, as its REM says.

Typing it in

Plain ASCII. The banner in lines 140–230 is made of ordinary asterisks and spaces, so keep the spacing if you want a neat box. ? is the abbreviation for PRINT. Don't add any program lines numbered 1–100 yourself, because that range holds the directory entries.

How to use it

RUN it and type a letter at the ? prompt:

  • A (Add): enter a listing number (1–100), a name and a phone number. The screen flickers while the program writes the new line into itself.
  • D (Delete): enter a listing number to remove it.
  • L (List), or any other key: shows all the entries (lines 1–100). Press RETURN to go back to the menu.
  • S (Save): saves the program, entries and all, as D:DIRECTORY. Next time, RUN "D:DIRECTORY" brings everything back.

How it works

  • Location 842 controls the screen editor. POKE 842,13 turns on forced-read mode, in which the editor reads lines from the screen as if RETURN had been pressed on each one. POKE 842,12 turns it off again.
  • For an Add, line 330 prints N REM name number and then CONT on a clean screen. Line 340 puts the cursor at the top, turns on forced read and STOPs. The editor enters the new line, executes CONT, and BASIC continues at line 350, which switches forced read off.
  • A Delete prints just the line number, which deletes that line, the same as typing a bare line number at the keyboard.
  • LIST 1,100 displays the directory, because the entries are simply program lines.
  • The TRAP statements send a bad entry back to the menu or to the prompt.

Variations to try

  • Anderson suggests the same method for deleting set-up code once it has run, to save memory.
  • Keep the data in DATA lines instead of REMs, so that the program can READ, sort and search them.

Listing

110 REM A PRACTICAL DEMONSTRATION
120 DIM A$(10),N$(20),P$(18)
130 GRAPHICS 0
140 ? "*********************************"
150 ? "*                               *"
160 ? "*     TELEPHONE DIRECTORY       *"
170 ? "*                               *"
180 ? "*           (L)IST              *"
190 ? "*           (A)DD               *"
200 ? "*           (D)ELETE            *"
210 ? "*           (S)AVE              *"
220 ? "*                               *"
230 ? "*********************************"
240 INPUT A$:TRAP 130
250 IF A$(1,1)="A" THEN 300
260 IF A$(1,1)="D" THEN 360
270 IF A$(1,1)="S" THEN SAVE "D:DIRECTORY":REM CASSETTE USERS "CSAVE"
280 LIST 1,100
290 ? "PRESS RETURN TO CONTINUE":INPUT A$:GOTO 130
300 ? "WHAT WILL BE THE LISTING NUMEBER";:INPUT N
310 ? "NAME";:INPUT N$:? "PHONE NUMBER";:INPUT P$:TRAP 310
320 GRAPHICS 0:POSITION 2,2
330 ? N;" REM",N$;" ";P$:? "CONT"
340 POSITION 2,0:POKE 842,13:STOP
350 POKE 842,12:GOTO 130
360 ? "LISTING NUMBER TO DELETE";:INPUT N:TRAP 360
370 GRAPHICS 0:POSITION 2,2
380 ? N:? "CONT"
390 POSITION 2,0:POKE 842,13:STOP
400 POKE 842,12:GOTO 130

Credits