Classic Computer Magazine Archive COMPUTE! ISSUE 26 / JULY 1982 / PAGE 159

Machine Language Compactor

David L. Evans
Caldwell, ID

This compactor analyzes and compresses BASIC at lightning speed. For PET/CBM, Upgrade and 4.0 BASIC, with disk drive.

When the programs "Compactor" and "UnCompactor" by Robert Baker appeared in the Sept./Oct., 1980 and May, 1981, issues, respectively, of COMPUTE! I immediately typed them in and tried them out on several of my programs.

To my dismay, compacting a 16K program required approximately 48-52 minutes. I am a college student and spend a limited amount of time on my computer (a 16K 4.0 PET) due to study time and my job. That compacting time ate heavily into the time I spend on my PET and I decided to do something about it. This program is the result.

I tried several different BASIC versions of the program, but could come up with no noticeable increase in speed. I then decided to give machine language a try. The result? A 947% increase in speed! Even this phenomenal increase in speed can be improved a bit further by eliminating the error checking in my input subroutine.

The program consists of five different sections. The input section prints the directions and inputs the input and output file names. The scanning section scans the BASIC program for target line numbers and prints them for you to see, while also storing them for use by the compacting section. The compacting section compacts the program (where possible) and prints "DONE" when finished.

A following subroutine section contains an input subroutine, a line number checking routine, and a disk error routine. Following these four sections is the data and work storage area. This program was designed to run on both Upgrade and 4.0 ROMs. It will automatically adjust itself to work on either ROM set, so all the user has to do is to LOAD the PROGRAM and type RUN.

Running the program is fairly simple. Load the program into memory with the LOAD or DLOAD command, depending upon which computer you have. If you do not have DOS 2.0 or higher, make sure that both drives are initialized. Type RUN, and the computer will print a short greeting along with some brief directions.

Typing In Compactor

Below are the step-by-step instructions for entering and SAVEing Compactor. COMPUTE! generally provides BASIC loaders so that readers who don't know machine language can easily type in and use machine language subroutines and programs. Though it could be done, we felt that creating a BASIC loader for Compactor would be too complicated to be worthwhile: Compactor sits in memory starting at address 1024 (like a BASIC program). A loader would involve an awkward process of writing over itself and checksums would be equally difficult to use.

Instead, Compactor is presented as hex dump. To enter it into the computer:

  1. Type: SYS 1024 (to enter the machine language monitor).
  2. When prompted by the period (.), type M 0400 0460 and the screen will reveal (in hexadecimal numbers) the contents of the memory cells between address 0400 and 0460. You can type over these numbers and hit RETURN on each line, thereby entering the new numbers into memory. So, for the first line, you type: 00 0B 04 FF FF 9E 31 30 (RETURN). Continue on until you've typed and entered line 0460 as it appears in the magazine listing.
  3. Then do the next block of memory. Type: M 0468 0500 and continue on, replacing the values on screen with Compactor's numbers as printed. When you've typed in the last line, (0B70), you are done.
  4. Now Compactor is in your computer's memory and it must be SAVEd onto a disk. Type: S "0:COMPACTOR", 08,0400,0B78 (RETURN) and it will now be available for use as a program called "COMPACTOR" on the disk in Drive zero.
  5. If the power went off just before you finished (and you lost everything, including your temper), you might prefer to take up the author's offer (at the end of the article) to make a copy for you.

After reading the directions, type in your input file name preceded by the drive number the file is on. Example : 0:COMPACTOR. You will then be asked for the output file name; do the same with it as you did with the input file name. The computer will then scan and compact your program. If any disk error is detected, the program will report it and will return to BASIC. After your program has been compacted, LOAD it into memory and type the CLR command; this will fix all the line links. Be sure to reSAVE the corrected copy.

Clearly, machine language lends itself to input/output programs more readily than BASIC does. Once the basics of how to open files from machine language are learned (see COMPUTE!, April, 1981, #11), it is easy to generate input/output programs in machine language which will run at a phenomenal speed.

For those of you who do not want to type this in, I will make you a copy of it if you will send me $3.00 and a self-addressed, stamped mailer and a blank tape or disk. For those who send a disk, I have DOS 2.0, so all disks will be written in DOS 2.0.

David Evans
2202 Ellis Ave.
Caldwell, ID 83605