Larry DinwiddieThis convenient Commodore 64 utility "uncrunches" crowded BASIC program lines into separate, easily readable and edited lines. The utility works with either disk or tape, and although it is written in machine language, no machine language expertise is required to use it. It runs on any Commodore 64 or 128 (in 64 mode).
One common programming technique in Commodore BASIC is to "crunch" programs into compact form by combining multiple statements on a single program line. As most programmers soon learn, crunching conserves memory and helps a program run faster. In addition, programs listed in magazines and books are usually crunched to save space.
However, crunching also makes the program more difficult to read and modify. Often, modifying a crunched program involves breaking up a long line into two or more shorter lines. This can be tedious, and it increases the risk of errors.
"64 Uncruncher" automatically uncrunches an entire program for you, making each BASIC statement a separate program line. The resulting program is much easier to modify than the original. And because each statement is a separate line, it is simpler to follow the program's logic as well.
Figures 1 and 2 illustrate a simple BASIC program before and after uncrunching. Both programs are listed with a width of 40 columns so they appear just as they would on your screen. Notice how much easier it is to read the uncrunched version and decipher its logic.
Figure 1: Crunched Program
10 POKE 53281, 15:POKE 53280, 15:POKE 646, 0:PRINT CHR$ (147):FOR J=1 TO 10
20 GOSUB 50:IF INT (K/2)=K/2 THEN PRINT”, AN EVEN NUMBER”:GOTO 40
30 PRINT”, AN ODD NUMBER”
40 NEXT J:PRINT:PRINT “FINISHED”:END
50 READ K:PRINT “K=” K;:RETURN:DATA 123,456,789,987,654,321,123,456,789,111
Figure 2: Uncrunched Program
100 POKE 53281,15
110 POKE 53280,15
120 POKE 646,0
130 PRINT CHR$ (147)
140 FOR J=1 TO 10
150 GOSUB 220
160 IF INT (K/2)=K/2 THEN PRINT “, AN EVEN NUMBER” :GOTO 180
170 PRINT”, AN ODD NUMBER”
180 NEXT J
200 PRINT “FINISHED”
220 READ K
230 PRINT “K=” K
250 DATA 123,456,789,987,654,321,123,456,789,111
Since Uncruncher is written in machine language, you must type it in using the "MLX" machine language entry program listed elsewhere in this issue. Be sure you read and understand the instructions for using MLX before you begin entering the data for Uncruncher.
When you run the MLX program, it asks you for a starting address and an ending address. Here are the addresses for Uncruncher:
Starting address: C000
Ending address: C60F
After you've typed in and saved all of the Uncruncher data, you can test it on any Commodore 64 BASIC program. Follow these steps:
1. Load Uncruncher into memory by typing LOAD "filename", 8,1 for disk or LOAD "filename”,1,1 for tape. Substitute your own filename, of course.
2. Type NEW and press RETURN.
3. Load (but do not run) the BASIC program you want to uncrunch.
4. To start Uncruncher, type SYS 49152 and press RETURN. The screen clears and Uncruncher displays messages informing you of its progress. It takes three passes through the BASIC code to uncrunch the program. When the READY prompt returns, the uncrunching is complete.
You may pause Uncruncher at any time by holding down the f1 function key. Do not interrupt Uncruncher by pressing RUN/STOP-RESTORE; if you do, the program may be left in a garbled, unusable form. No real harm is done, but you'll need to reload the program and restart Uncruncher.
Uniform Line Numbers
Uncruncher begins numbering the new program at line 100 using line increments of 10 (110, 120, and so on). Each BASIC statement is a separate line, except for lines containing IF-THEN statements. Because the THEN portion of such a statement must be on the same line as IF, IF-THEN lines are left unchanged except for renumbering. For any BASIC statement that references a line number (such as GOTO, GOSUB, IF-THEN, RUN, and LIST), the line reference is also renumbered.
During the third pass, the program prints the line numbers it is replacing. If Uncruncher Finds a statement that refers to a nonexistent line number, it prints this error message:
UNREFERENCED BRANCH IN NEW LINE # xxxxx
When you see this message, xxxxx is replaced by the new line number where the nonexistent reference is located. To mark where the error occurred, Uncruncher replaces the meaningless line number with 63999, the highest legal line number. An unreferenced branch error indicates a logic error in the original program, so you should reload the original, correct the error, and then repeat the uncrunching process.
If uncrunching generates a large number of unreferenced line number errors, you may find it useful to divert Uncruncher's output to a printer. To do this, make sure the printer is turned on, then enter this statement in direct mode (without a line number):
OPEN 4,4:CMD 4:SYS 49152
Now everything that would have been printed on the screen is sent to the printer instead. When Uncruncher is finished, type this statement and press RETURN:
DATA statements are uncrunched in a special way. After uncrunching, each DATA line contains approximately 60 characters per line, including the line number and the keyword DATA. However, no individual DATA item will be split across two lines.
After the program has been uncrunched, you may list it, resave it, or modify it as usual. If you have a crunch utility, you may wish to recrunch the program after making modifications.
Because the additional line numbers take up more memory, the uncrunched program is significantly larger than the original, leaving less memory for BASIC variables and arrays. In most cases this should not cause a problem other than slowing program execution somewhat. However, a very large BASIC program or one that requires a great deal of variable space may not run correctly in uncrunched form.
Similar problems may arise if the original program POKEs sprite shapes, custom characters, or other data into a reserved area within BASIC memory. If the uncrunched program text expands into the reserved area, the POKEs may destroy part of the program text. To be on the safe side, you may want to save the uncrunched program immediately before you try to run it.
Please refer to the "MLX" article in this issue before entering the following listing.
C008:8D 8D C5 A5 74 8D 8E C5 83
C010:A5 75 8D 8F C5 A9 4C 85 5F
C018:73 A9 15 85 74 A9 C5 85 14
C020:75 20 3A C5 A9 FF 91 A3 1C
C028:C8 91 A3 A9 9E A0 C5 20 A4
C030:1E AB A9 C0 A0 C5 20 1E 67
C03B:AB A9 EF 8D 28 03 A9 00 71