Personal Computer News


Protecting Your Programs

 
Published in Personal Computer News #093

The last thing you want to happen with your commercial software is for someone else to break into the listing and copy it. Using Stuart Nicholls' routine will ensure that your Spectrum listings are safe.

Breaking And Entering

The last thing you want to happen with your commercial software is for someone else to break into the listing and copy it. Using Stuart Nicholls' routine will ensure that your Spectrum listings are safe

Programmers wishing to write commercial software in Basic often find that there is no real protection against anyone breaking into the program, examining the listings and copying any special routines that they may have produced.

Many methods have been put forward to protect Basic programs. The most successful involve some routine to disable the break keys, but they all have the fault of causing a crash if break is used, which can be annoying if the keys are hit accidentally.

To overcome this I developed a routine that makes Basic programs unbreakable, unmergeable and thus unlistable and, what is more, requires no knowledge of machine language although it does use a five-byte machine code routine. It also ignores many error reports, continuing with the program if they are found.

Error Handling

To explain how this is achieved, we must first examine the Spectrum ROM to find just how and where the break keys are read during the running of a Basic program. This happens after the correct interpretation of a statement has been made at address 1B76h and takes the form:

1B76H	CALL 1F54H
	JR C,1B7DH (cont)
	RST 8
	DEFB 14H
1B7DH	(cont)

The CALL 1F54H instruction checks the break keys and, if there is no carry, calls the error handling routine RST 8.

The error handling routine resets the stack and so on, and then jumps to the routine whose start address is indicated by the system variable ERR-SP 23613/4, normally 1303h. This prints the error/code etc and sets up the continue parameters. 1303h is placed in the address held in the system variable ERR-SP at the start of running a Basic program, and is, in fact, the first address on the machine stack, but ERR-SP address can move around in memory with the build up of the GOSUB stack.

It is, however, possible to alter this to send the error routine elsewhere, and this is what most programmers use to send the error handling routine to an address in ROM which will cause a crash.

If you are a bit more selective, you can send the error routine back to address 1B7Dh and continue with the program as though nothing had happened. This is not a simple case of putting 1B7Dh into the address held in ERR-SP, as on return from the error handling routine the stack will be incorrectly set up without 1B7Dh as the ERR-SP return address. In fact, it will end up with 1B76h as the ERR-SP return address and the GOSUB stack corrupted.

Reset The Stack

What you need is a very small machine code routine to reset the stack each time break is pressed, with address 1B7Dh as the error return address as follows:

LOOP CALL 1B7DH
JR LOOP

and set up ERR-SP with the address loop. Machine code programmers will see that this will put address LOOP+3 as the future return address from error routine (that is the instruction JR LOOP) ensuring that the stack is correctly set up for further break key presses.

This five-byte routine can be placed anywhere in memory, and the address held in ERR-SP, poked with the start address of the routine. As an example, we can use a LINE 1 REM statement and assume the start address to be 23760d, with no Microdrive(s) connected. So the Basic program will start with Listing 1.

Line 1 holds the machine code poked in by the direct commands and Line 20 sets up the address in ERR-SP to hold 23760d.

As an example of an unbreakable Basic program add Listing 2.

When run, this program will be unbreakable, and even the out-of-screen error reports will be ignored when part circles only are drawn. To make it unmergeable, you must save it as a Code program, so add Listing 3.

Other points to watch for are that line 100 retains the ERR-SP address; line 110 pokes ERR-SP with a value that will cause a crash if break is used while loading the program, while line 120 finds the end of the Basic program.

Line 130 saves the Basic with the system variables so that it will auto run. The correct address for ERR-SP is reset in line 140, so that line 10 will be correct. To save the program enter GOTO 100 as a direct command and to load the program you must now use LOAD "BASIC" CODE or LOAD "" CODE.

There is one final point to watch for - the use of stop in inputs. If this is used in normal numeric or string variable inputs, the program will stop with no way of restarting. This happens because the break will have occurred at address 21D0h and not 1B76h. Any other illegal input will be ignored.

This is not a problem because anyone using stop as an input wants to break into your program in order to list or copy it and must suffer the consequence of having to switch off the Spectrum and start again. No point feeling guilty about this little trick.

Finally, input line should be avoided as there is no error trapping of inputs in the ROM and as such this command may lead to the program 'hanging up'.

Easier Word Count

Following on Mike Lewis' article on adding a word count to Tasword 2 (issue 88) the following routine may be of interest. It does away with the need to PEEK and POKE initial and final values, and can be placed anywhere in free memory.

Once you've either entered the assembly listing (below) or poked the values in the decimal dump into memory, all you need do is return to Basic from Tasword then enter PRINT USER start address. An accurate word count should be produced in a couple of seconds.

Stuart Nicholls