Self-Modifying Programs

John Anderson

Original Atari Basic has its strong and weak points, as do all computer languages. Because Atari Basic is a somewhat renegade dialect, however (as opposed to the orthodoxy of Microsoft Basic), it is subject to especially intense scrutiny. Those who dislike it tend to detest it; even those who like it tend toward ambivalence. C'est la langue.

There is at least one good reason why Atari Basic is a "splinter language." It was designed in tandem with and in order to squeeze the most from the Atari operating system. And as such, it is capable of some exotic tricks--that much is undeniable.

One of these tricks is the ability to write-code that in turn rewrites itself. Imagine the possibilities.

The Atari has a very open-minded operating system. It will allow the screen editor to operate from sources other than a human at the keyboard. The editor will go so far as to accept data pushed to the screen from Atari Basic, and Atari Basic can then execute commands directly from the screen editor.

Figure 1 is a short example of how this feature can work. First we clear the screen, and position the cursor. Then we straightforwardly print code lines to the screen. These lines will act as if the Atari has automatically pushed the RETURN key over them, thus incorporating them into the program (and eliminating any previous lines with the same line numbers). Notice the inclusion of a CONTinue command. You must print this command at the bottom of any list of modifications or the program will terminate before modification takes place. The program run must actually stop, accept the new data, and start itself again.

The trickiest facet of the technique is placement of the cursor. If you position it incorrectly, you can lose modifications, or get locked into a loop. A bit of experimentation will lead to successful results.

Only now is the potential of this capability being fully explored. Two new programs from Artworx Software make use of the technique: Drawpic saves four color user drawings in graphics modes 3 through 7, by saving modified strings; Player/Missile Editor does the same for player/missile shape tables. You can get more information concerning these programs from Artworx, 150 North Main Street, Fairport, NY 14450.

A hint on how you might simply utilize the technique in your own programs is shown in Figure 2. Here the user is asked to input data, which is then incorporated into the modified program. This program can then be saved, thus saving the input information.

I've used the simplest possible approach in this example, saving up to one hundred phone numbers as REM statements. You might wish to improve radically on this approach.

Another use of the technique would be to delete lines when they are no longer needed. Line numbers devoted to user input of variables, for example, could be deleted after the variable table has been constructed. This would help conserve memory.

Countless other applications await your entry into Atari behavior modification. The limits are set by your imagination only.

Souping Your Machine

Although the Atari does most things well, you can now customize it to do things better. The idea may seem to you akin to putting slicks on a BMW, but let me tell you about a few products we've tested that can make your machine faster and more versatile.

The Fastchip, from Newell Industries, replaces the floating point chip on the operating system board. Floating point routines, which involve mathematical operations with real numbers as well as integers, run extremely slowly on a standard Atari. Newell Industries claims that execution of these routines is boosted to three times the original rate.

I played Hail To the Chief twice on the same machine, once with the original chip, and once with Fastchip. Calculations within the program involve lengthy breaks in the action. Fastchip cut the waiting at least in half, from a maximum of 11 seconds to a maximum of about 5 seconds. It may not sound like much of a difference, but when you're waiting it is.

If you are into floating point routines and don't have a lot of time, Fastchip will help. It lists for $39.95, and installation in an Atari 800 takes less than five minutes. For more information, contact Newell Industries, 3340 Nottingham Lane, Piano. TX 75074.

You should resist with all your strength the temptation to confuse Fastchip with Fast Chip, the disk drive upgrade chip from Binary Corporation. This product will interest Atari owners with original 810 disk drives, as it provides a disk format 30% faster than the original. The company claims that the custom chip is 10% faster than even the new Atari upgrade chip.

It took about a half an hour for me to perform the upgrade, and was a bit more involved than I had initially anticipated. Still, the instructions are clear, and the process is broken into logical steps.

Binary Fast Chip without a doubt provides a faster format for your disks. I found that it cut about twenty seconds off the load time of a 12K file. But the disks are also rather sensitive--cases arose wherein Fast Chip formatted disks took much longer to read or write. This was without exception true with disks for use with Valforth. It might therefore be advisable to wait until you have two drives, then install a Fast Chip in one. You can then choose the format to match the application.

The product lists for $39.95. For more information, contact Binary Corporation, 3237 Woodward Ave., Berkley, MI 48072.

Perhaps you've wondered if ROM cartridges could be copied to disk. Well they can now, with the Block from Protronics. The Block allows you to transfer from a ROM to a binary disk file. Up to ten ROM cartridges can be saved to a single disk. I was unable to find anything that the Block couldn't copy.

Potential pirates should take note: the Block itself is a ROM cartridge, and no cartridge file will run unless the Block is installed.

The Block lists for $99.95. For more information, contact Protronics, 17537 Chatsworth, Granada Hills, CA 91344.

The Library Crows

I remember, in the dim recesses of my mind, a time when information about the Atari was an extremely rare commodity. This was in ancient times, of course: maybe a year and a half ago. Now it seems a new book about the Atari arrives here every week. These are six of the best:

Atari Games and Recreations, by Herb Kohl, Ted Kahn, Len Lindsay, and Pat Cleland, Reston Publishing, Reston, VA 22090. Excellent starter for novices and kids, with an emphasis on fun programs the user can type in, play, and understand. Includes some nifty appendices.

Atari Sound and Graphics, by Herb Moore, Judy Lower, and Bob Albrecht, John Wiley and Sons, 605 Third Avenue, New York, NY 10158. The authors pace the text so that new concepts are introduced at a rate that can be absorbed. Sound and graphics are a motivating force with kids, but many hobbyists will want this one too.

The Atari Assembler, by Don Inman and Kurt Inman, Reston Publishing, Reston, VA 22090. Best beginners machine language book available for Atari owners. Assumes you have Basic and an editor/assembler. Assembly language is tough stuff, but authors manage to keep things fresh with humor and good examples.

Games for the Atari, by S. Roberts, W. Hofacker, 53 Redrock Lane, Pomona, CA 91766. Includes good examples of player/missile movement from Basic, priority detection, and patching from Basic to machine language subroutines. Includes ten games to be typed in, unfortunately without much explanation.

Picture This, by David D. Thornburg, Addison-Wesley, Reading, MA 01867. A kid's introduction to graphics through Atari Pilot. Excellent as a supplement to "Student Pilot," the reference guide supplied with the Pilot cartridge.

Your Atari Computer, by Lon Poole, Martin McNiff, and Steven Cook, Osborne McGraw-Hill, 630 Bancroft Way, Berkeley, CA 94710. It may have taken two and a half years, but there is finally a manual available which thoroughly documents the rudiments, as well as a number of advanced topics, concerning Atari personal computers.

Following a remarkably steady pace, the book progresses through beginning operation, getting started in Basic programming, and includes comprehensive chapters on the program recorder, disk drive, and printers.

The main body of the book deals with advanced Basic programming, and stands to serve the proficient Basic programmer as well as the novice.

Next the book focuses on the goodies. Graphics and sound are given a clear and thorough treatment with a chapter devoted to advanced graphics techniques. Character set animation, display lists and player-missile graphics are explained simply and thoroughly, with examples helping to illuminate the way. Those of you (like myself) who need every last thing spelled out for you, are bound to benefit from this approach.

Later chapters examine sound routines and summarize Basic commands.

The book concludes with nine appendices, each of value to the Atari programmer. Finally we can turn to a single resource for an annotated list of error codes and their meanings, status and keyboard codes, memory usage charts, and a listing of important memory locations.

Your Atari Computer should be packed with each and every unit Atari ships, alongside or in lieu of current documentation. No Atari owner should be without it.

Scuttlebytes

In the November issue, we gave a phone number for a Sunnyvale bulletin board system called TEAM Atari. Begun by an Atari employee, the board is unfortunately no longer in service. Apparently some Atari execs felt it was inappropriate, which is too bad.

You might try Bay Area Atari at (408) 244-6229. Sorry if we caused any inconvenience, but it is tough to compile a BBS list that remains totally accurate for any length of time.

A question many people are asking concerns the new Atari 5200 video system: is it or isn't it a 400 without a keyboard? The answer: well, yes and no. It does have 6502, Antic, GTIA, and Pokey chips. It does run nearly identical ROM software. However, for reasons somewhat difficult to fathom, the 5200 has had enough changes made to ensure incompatibility with Atari computers. The most dramatic evidence of this is a redesigned game controller, which uses an analog input, in addition to a telephone-style keypad.

The advantages to a handheld keypad are obvious: the advantages of an analog joystick perhaps less so. A potentiometer-controlled stick allows for better control in games such as Missile Command, but a digital stick is faster in quick-turning games like Pac-Man. The 5200 controller ports are necessarily redesigned, as are the cartridges themselves. Whether this incompatibility is utterly surmountable remains to be seen, but it certainly would be a formidable task.

Another topic we hear a lot about is the "next generation" Atari. Have you heard about the Atari 600? We have, although we haven't been able to confirm anything. It will be a single board computer, totally compatible with the 400 and 800. It will have RS-232 capability built-in, and a full-stroke keyboard. It will come with 48K standard, and sport programmable function keys. We have even heard about an Atari 1000, with a built-in dual density drive, and CP/ M capability!

Figure 1.

10 REM SELF-MODIFYING EXAMPLE
20 GRAPHICS 0:POSITION 2,5:REM CLEAR SCREEN AND POSITION CURSOR. Y COORDINATE = NUMBER OF LINES TO CHANGE (5)
30 ? 110;" . THESE ARE THE LINES"
40 ? 120;" . THAT WILL REPLACE"
50 ? 130;" . THE LINES AT THE"
60 ? 140;" . END OF THE PROGRAM."
70 ? 150;" . NOTE ABBREVIATIONS REMAIN             ACCEPTABLE."
80 ? "CONT:REM THIS STATEMENT IS CRUCIAL, AND MUST NOT BE NUMBERED."
90 POSITION 2,0:REM REPOSITION CURSOR AT TOP OF SCREEN
100 POKE 842,13:STOP :REM DON'T PUT ADDITIONAL COMMANDS ON THIS LINE
105 POKE 842,12:REM POKES HALT PROGRAM, ENABLE EDITOR, ACCEPT COMMANDS, RETURN TO PROGRAM
110 REM WATCH THESE LINES CHANGE
120 REM TO THE LINES STIPULATED
130 REM IN LINES 30 THROUGH 70.
140 REM DON'T FORGET THE "CONT"
150 REM COMMAND AFTER LINE CHANGES!

Figure 2.

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

John Anderson is an associate editor for Creative Computing magazine.

Table of Contents
Previous Section: The Upstart Atari
Next Section: Super Text Mode