A 35000 characters string with Commodore 64 BASIC

Commodore 64 BASIC strings are limited to 255 characters. But, that doesn’t mean you can’t put a bigger string on memory with BASIC. Some days ago I have written a small program that actually creates a 35000 characters string. It also prints it. The whole process takes 52 seconds.

I am no magician and it is not possible to assign a 35000 chars string to a single variable. But you can make use of the way BASIC handles assignments.

You can start with a 250 chars string. This will be repeated and repeated on memory to form a 35000 chars one.

If you keep assigning a string to the same variable, a new string will be created. Each time, the new string will be assigned to the variable, and the old one (the previous content of the variable) will become garbage. But once they have been created, all those “dead strings” can be actually “resurrected”. We can change the pointer of a string variable so that we can read the whole content of the string storage area, in chunks of 250 characters. This is just what the following program does.

 

Download: 35000 chars string

 

First of all, the program creates a 250 characters string. It is made up of a pattern of A and B characters. Then, on line 15, the FRE function is used so that garbage strings are deleted (or, more exactly, space taken by garbage strings is reclaimed).

On line 20, A$ is repeatedly assigned to itself. That may seem useless, but it actually “clones” the string in memory. The string storage area becomes populated of garbage A$ copies. This is just what we want.

Line 25 defines starting address and ending address where the 35000 chars string can be found in memory. Of course, those locations are in the string storage area.

Line 30 contains the slow way of showing the string, and it has been disabled by a REM statement.

Line 35 computes the address variables start from (VR).

As the dummy variable D$ was created first, it is fairly easy to locate where it is on memory. It starts from VR. VR+2 contains the lenght of the string that is assigned to the variable. On line 38, we set this lenght to 250 characters.

The loop on lines 40 and 45 assigns to the variable D$ a 250 chars block, which is part of the 35000 characters string in memory. The string pointer of D$ is changed on each iteration. On line 45, the 250 chars block is printed. The process is repeated until the whole string has been printed.

This is yet another simple experiment I did with Commodore 64 BASIC strings. Many concepts can be found inside this little program. And, it shows that the 255 chars limit can be somewhat extended.

The program could be rewritten more easily by using POKE and PEEKS or by using arrays. But either way, it would be much, much slower.

Some BASICs put no limits on strings lenght. For example, it is possible to easily create a 35000 characters string on Sinclair BASIC. And a variable can directly contain it, there is no 255 chars limit. At least, this is the theory.

In fact, I tried to run the following simple program on a ZX Spectrum 128 +2. Sadly, the program runs very slowly, and it ends up with an “Out of memory error” after roughly one hour of execution time. And it is still very far from completion.

 

This program won’t work (Sinclair Spectrum 128 +2). The program runs for one hour, then we get an out of memory error.

 

The out of memory error. It happens when the string is 13797 characters long. Compared to the 41.5 K that are available to BASIC, this does seem odd.

 

The Sinclair BASIC program is quite simple. It is easy to understand and doesn’t require pointers. The Commodore 64 counterpart is very complex in comparison and requires a competent use of pointers.

But, the Commodore 64 program ends in less than one minute. The Sinclair Spectrum program is very slow and goes out of memory after one hour.

On Commodore 64 BASIC, string pointers can be very helpful. For instance, you can swap the content of two variables by swapping their pointers. This can be done with speed by using a machine language routine, and an example is provided in an issue of Compute! Magazine. A machine language sorting routine is used there, providing a fast way of sorting many BASIC strings. This approach completely prevents possible garbage collection issues.

Don’t be too concerned about garbage collection issues on Commodore 64 BASIC, it can be handled. And there’s no need to use utilities. A competent use of pointers is all that is required on most cases. You can find articles dealing with garbage collection in the programming section of this blog.

If you want to sort strings in BASIC, it can be done with no garbage collection concerns. Just use an integer array to hold indexes for string variables, and you are done. Then, you can just sort the integer array, and use its elements as subscripts to show the sorted string array. You get just the result you were looking for, but elements on the string array are actually never moved. This trick provides better performances (as you are sorting integers and not strings) and doesn’t create any garbage.

Previous Entries Line drawing routines programming: a different approach. 6502/6510 assembly implementation on the Commodore 64 Next Entries 6502 assembly math: division - a simple algorithm using powers of two

Leave a Reply

*