Classic Computer Magazine Archive COMPUTE! ISSUE 73 / JUNE 1986 / PAGE 94

Automatic Typist:
Using Apple Exec Files


Mike Miyake

Although it's often overlooked, the EXEC command offers an easy way to extend the power of Applesoft BASIC. EXEC can read and perform commands directly from a disk file, just as if you'd typed the commands on the keyboard yourself. It can also be used as a convenient, built-in merge command for adding frequently used subroutines to Applesoft programs. The example programs below run on any Apple II-series computer; most work with either DOS 3.3 or ProDOS. A disk drive is required.


Have you ever wanted to know the address of a machine language program, or the number code for one of the Apple's 16 low-resolution colors? Are you curious about how a particular Applesoft program uses the computer's memory? In most cases finding the answers to such questions means thumbing through a reference book or typing cumbersome statements like PRINT PEEK (N)+256*PEEK(N+1) to examine memory. And the PEEK statement usually must be typed in immediate mode, since running a short program to get at the information would destroy any program that's already in memory.
    "Automatic Typist" shows you how exec files can solve such problems. An exec file is simply a text file which you activate with an EXEC command from Applesoft BASIC. It executes like an immediate mode statement-something you type directly on the keyboard, without a line number-but it can also be saved to disk and reused over and over, just like a program. In effect, exec files let you control the computer with disk files that act like immediate commands without disturbing a program that's in memory. We'll show how to put both immediate mode commands and program lines in exec files, and provide some useful examples of what exec files can do.

Creating Exec Files
Type in Program 1, then run it once to make sure it works correctly. Run the program and follow the prompts, entering any filename when prompted. Since this is just for practice, it doesn't matter what filename you use. After that's done, exit the program and type CATALOG to view the new file; it should show up as a text (T) file. Now delete the file (it doesn't contain any data, so you're not losing anything of importance).
    Once you're satisfied that Program 1 works correctly, it can be used to create exec files. An exec file ordinarily contains one or more statements in the form of ordinary text. Unfortunately, the Apple II DOS Manual tells you very little about how to create such a file. In most cases the simplest way to do so is within a BASIC program. Program 1 illustrates the basic technique. Once the file has been opened (line 18) and a WRITE statement has executed (line 20), all subsequent PRINT and LIST statements send their output to the disk file instead of to the screen. Other BASIC statements function normally while the output of PRINT and LIST is being diverted. When the file is closed (line 1000), PRINT and LIST resume their normal functions.
    Program 1 provides you with a template program for creating exec files. It lets you choose a filename, opens the file, and prepares it for writing. To use this program, you need only add appropriate PRINT and/or LIST statements in new lines between lines 20 and 1000 of the template.
    Let's try a simple example. Load Program 1, then type in the lines listed in Program 2. The object is to add the lines from Program 2 to the template program. The initial PRINT statements in lines 50-70 write the commands bracketed inside quotes to the disk file. In cases where the exec file itself will contain PRINT statements, it's necessary to write quote characters to the file as well. This is done in line 70 with the variable Q$, which Program 1 defines as CHR$(34) in line 10.
    After you finish adding the lines from Program 2, run the program. Enter the filename CC when asked for a filename, then press the space bar when prompted. The text inside quotes in lines 50-70 is written to a disk file named CC. To execute this file, exit the program and type EXEC CC. It displays all 16 lo-res colors in vertical bars on the screen, with a matching number code directly beneath each color bar.
    If the CC file doesn't work properly, delete it and repeat the process. If you want to use the same filename (the normal case), it's necessary to delete the old version of a text file before writing an updated version of the file to disk. Unlike BASIC program files, which automatically replace an existing file with a new file of the same name, text files simply append new information to the end of the existing file.

A Program-Writing Program
The previous example printed immediate mode statements (commands without line numbers) to the exec file. But you can also print numbered program lines to a file. For instance, reload Program 1 and add this line:

40 PRINT"100 TEXT:HOME"

    Run the program and write the file to disk using the filename HOMER. It creates a text file consisting of the BASIC program line 100 TEXT:HOME (we'll explain below why you might want to create this type of file). Although you can write program lines to a disk file with PRINT, it's often more convenient to use LIST instead. One advantage of doing so is that you can type the lines exactly as they normally appear without having to enclose everything in PRINT statements.
    To illustrate, let's create the HOMER exec file with LIST instead of PRINT. DELETE the HOMER file from your disk, then reload Program 1 and enter these new lines:

21 REM CAPTURE BASIC
22 REM PROGRAM LINES
23 LIST 24,999: GOTO 1000
100 TEXT:HOME

    The LIST command in line 23 writes every program line from 24 to 999 to the disk file. The GOTO command branches around the data (one line in this case) that we're writing to disk. One disadvantage of this technique is that the lines to be written to disk must fall between 24 and 999, inclusive. By renumbering either the template program or the lines to be written, you should be able to overcome this problem in most cases.

Merging Common Subroutines
Since the EXEC command is analogous to typing, an exec file is a good place to save commonly used subroutines for reuse in different programs. This makes it easy to merge the subroutine contained in the exec file with a program already in memory. To bring the lines into memory, simply type EXEC filename. As long as the exec file contains no lines numbered the same as those in the current program, the new lines are added without disturbing the program in memory.
    To illustrate, let's save Program 1 in exec file form. Reload Program 1, then add this line:

25 LIST 1,20:LIST 1000,

    Now run the program, entering C.LINES when prompted for a filename. Exit the program, then type NEW, followed by LIST to confirm that no program is in memory. Type EXEC C.LINES and press RETURN. Several bracket prompts will scroll past as the computer enters each program line automatically. When the cursor reappears, type LIST. Program 1 is back in memory, just as if you had typed each line manually.
    It's not difficult to see how much programming time this method could save, particularly if you build up a library of commonly used subroutines that each use different ranges of line numbers.

Last BLOAD
Program 3 is an exec file that comes in handy in many different situations. Its purpose is to tell you the load address and length of the last file that was BLOADed into memory. Knowing this information lets you run machine language programs immediately with a CALL statement, or copy them without using DOS 3.3's FID utility. Unlike the other examples presented here, this one is for DOS 3.3 only-it does not work with ProDOS.
    The procedure for creating this file should be familiar by now: Reload Program 1, add the lines listed in Program 3, then run the program. This exec file uses pointer locations applicable to a 48K Apple II. If you have a 16K or 32K system, change the pointer locations as shown here:

32K: address=PEEK(27250)+256*PEEK(27251)
     length=PEEK(27232)+256*PEEK(27233)

16K: address=PEEK(10866)+256*PEEK(10867)
     length=PEEK(10848)+256*PEEK(10849)

Memory Map
Program 4 contains the lines to add to Program 1 to create another useful exec file. This one shows the memory locations of the current BASIC program and its strings and variables. To use it, load and run the BASIC program you're curious about, then type EXEC filename in immediate mode. The pointer locations used by this file are discussed on page 140 of the Applesoft II BASIC Reference Manual.

For instructions on entering these listings,
please refer to "COMPUTE!'s Guide to Typing
In Programs" in this issue of COMPUTE!.


Program 1: Exec File Maker

08 10 D$ = CHR$ (4):Q$ = CHR$ (3
      4)
55 11 TEXT : HOME : HTAB 15: PRI
      NT "MAKE FILES:"
60 12 VTAB 6: INPUT "FILENAME: "
      ;N$: IF LEN (N$) = 0 THEN
      12
D2 14 HTAB 1: VTAB 8: CALL - 95B
FE 16 PRINT "INSERT NON-WRITE PR
      OTECTED DISK": PRINT "AND
      PRESS SPACE BAR WHEN READY
      .": PRINT : PRINT "  => ";
      :GET A$
9C 18 PRINT D$"OPEN"N$: PRINT D$
      "CLOSE"
E5 20 PRINT D$"OPEN"N$: PRINT D$
      "WRITE"N$
61 1000 PRINT D$"CLOSE"
B6 1002 VTAB 14: PRINT "DO IT AG
       AIN?";: GET A$
8D 1004 IF A$ = "Y" THEN 14
F2 1006 END


Program 2: Color Chart

49 REM  CC.LINES
50 PRINT "TEXT:GR"
60 PRINT "FORI=1TO15:COLOR=I:VL
   IN20,39 AT2*I:VLIN20,39 AT 2
   *I+1:NEXT"
70 PRINT "PRINT"Q$"0   2   4
   6   8   10  12  14"Q$":PRINT
   "Q$"  1   3   5   7   9   11
     13  15"Q$


Program 3: Last BLOAD

49 REM  LAST BLOAD / 3.3 DOS/48
   K
50 PRINT "TEXT:HOME"
100 PRINT "PRINT"Q$"LAST BLOAD"
    Q$":PRINT:PRINT"Q$"M/L FILE
    : ADDRESS = "Q$"PEEK(-21902
    )+256*PEEK(-21901):HTAB12:P
    RINT"Q$"LENGTH = "Q$"PEEK(-
    21920)+256*PEEK(-21919)"


Program 4: Memory Map

49 REM MEMORY MAP
50 PRINT "TEXT:HOME"
60 PRINT "PRINT"Q$"MEMORY MAP"Q
   $":PRINT:PRINT"Q$"HIMEM"Q$":
   PRINT"Q$"STRINGS (DOWN TO):"
   Q$":PRINT"Q$"(FREE SPACE)"Q$
   ":PRINT"Q$"ARRAYS, POINTERS"
   Q$": PRINT "Q$"& VARIABLES (
   UP TO):"Q$":PRINT"Q$"LOMEM"Q
   $":PRINT"Q$"PROGRAM LINES TO
   :"Q$
70 PRINT "POKE32,22:VTAB3:PRINT
   :PRINT:PRINTPEEK(115)+256*PE
   EK(116):PRINTPEEK(111)+256*P
   EEK(112):PRINT:PRINT:PRINTPE
   EK(109)+256*PEEK(110):PRINTP
   EEK(105)+256*PEEK(106):PRINT
   PEEK(175)+256*PEEK(176)"
80 PRINT "POKE32,0"