Classic Computer Magazine Archive CREATIVE COMPUTING VOL. 9, NO. 12 / DECEMBER 1983 / PAGE 245

The poor man's programmable key. Pat Macaluso.

The Poor Man's Programmable Key

I recently wrote a program of some 300 lines in Microsoft Basic--on paper, of course. I was then faced with the boring prospect of entering all those lines. Worse still, I was risking an attack of key stroke, a malady which affects the forefinger of hunt-and-peck typists like myself.

An obvious solution was to use one of many available programmable-key programs. These allow the substitution of a single key for a word or expression. An example might be the use of SHIFT-R in place of typing in the word RETURN, SHIFT-G for GOSUB and so on.

The problem with that was that I wanted to enter the program in pieces that I could test and save frequently. I also wanted to avoid the extra loading of a utility program each time I turned the computer on. A good solution would be to integrate a very short utility into my program--at least until the program was fully entered.

An examination of the program I had written showed it to contain many GOSUBs, RETURNs, IFs, and THENs. IF was too small a fish to bother with so I threw it back into the typing pool. Next I looked for three characters not used in the program. The symbols %, &, and ! qualified. The short Basic program shown in Listing 1 was the result. The program to be entered uses line numbers below 63999.

Using The Programmed Keys Suppose you want to enter: 100 IF ST = "Y' THEN GOSUB2000:RETURN

You would simply type: 100 IF ST = "Y' !%2000:&

thereby saving 12 key strokes. When you have entered a decent number of lines and are ready to do some testing or saving, type RUN64000. The routine goes through the lines at about one line per second, converting them to their final form.

This sequence of enter, convert, test, and save is continued until the entire program is entered. The lines from 63999 and up can then be deleted.

How It Works

This simple utility is an example of a self-modifying program. This is not a desirable practice in large or complex programs. In a simple case like this, however, there is little danger of confusion, and it offers considerable convenience.

In this case it trades on three neat features of Microsoft Basic. First, the Basic program lines (source code) are always stored in succession in a memory area with a fixed starting address. In the case of the TRS-80 Model I and Model III, this address is stored at a location whose decimal address is 16548 and 16549.

Second, each Basic program line has a fixed structure. It is quite simple. Bytes 1 through 4 form a header with the address of the next line in bytes 1 and 2. The current line number is in bytes 3 and 4. The end of the line is marked by a byte with an ASCII code of zero. All the intervening bytes consist of the ASCII codes for your program. ASCII codes are listed in most manuals on Microsoft Basic.

The third feature is the use of tokens to represent Basic language keywords. Thus the word GOSUB is stored not as five characters but as a single character whose ASCII code is 145. This turns out to be a TRS-80 graphics character. That is quite all right since the interpreter knows you can't use graphic codes in a Basic program except in literals (quotes) or in character strings.

Line 63999 contains and END to keep the main program from accidentally running into the 64000 block. TROFF (trace off) is simply a convenient way of detecting the end of your Basic program. The token code for it is 151. The utility simply loops through the lines, by-passing the headers and looking for TROFF, 0, !% or &. It exits when 151 is encountered. It starts the next iteration on 0 and substitutes the correct tokens for the selected special characters. That is all there is to it.

If you want to check what is going on for yourself, add the lines shown in Listing 2. RUN 64100 will allow you to examine any part of memory after an initial look at line 1.

Table: Listing 1. Example for three programmed keys.

Table: Listing 2. Program to display stored Basic source code.