Self-Programming Computers
I have a Commodore 64, and recently while running a program I encountered a ?SYNTAX ERROR IN LINE 580 message. When I listed line 580, there was none. When I ran the program again, I got another ?SYNTAX ERROR, but this time in line 13337. When I checked the original listing, there was no line numbered 13337. When I listed 13337, all that was displayed was gibberish. Even worse, when I attempted to delete 13337, the screen went black, a strange sound came out of the speaker, and the keyboard locked up. What happened?
Neal Hatton
You didn't mention what kind of program it was, or where you obtained your listing, but you have encountered one of the more subtle programming bugs, the self-modifying program.
It's sometimes necessary to protect your BASIC program from the operating system of your computer and from the program itself. The program may have overwritten itself by storing sprite data or character data in the middle of the BASIC program area, or variables may have been stored over the program due to a corruption of the pointers to the start of the BASIC array storage area, addresses 47 and 48 ($2F–$30).
When sprite data and redefined character data are POKEd into a program, you must exercise some caution to prevent overwriting areas of the program you need. This is one of the things we check when testing programs for publication. If variables are causing the program to overwrite and crash, it could seem to function normally for a while before the program is corrupted.
That gibberish you saw on the screen when you tried to list the program was caused by your computer attempting to interpret the data it found in memory as a BASIC line, reading the data as tokens. Many strange things can happen when a program is destroyed this way, and it's usually necessary to turn off your computer to regain control from your program's nervous breakdown.