The USR Command

See all articles from QZX v2 n12
Author(s): Alex Burr (K5XY)
Publication: QZX
Pub Details: QZX v2 n12 (Volume 2, Issue 12)
Date: November 1984
Pages: 2-3

Almost everybody knows that our Timex Sinclair computers come with two languages – BASIC and Machine. Most people use BASIC almost exclusively. It is much more like English and is much easier to use. Machine language consists only of a series of 0’s and 1’s. However, it is much faster than BASIC, particularly on our computers.

The best way to write programs is to use BASIC when you can and to reserve Machine language for parts of the program which are often used or must for some other reason run as fast as possible.

It is the USR command which permits both languages to be used in the same program. In principle this command is simple. The form of the command is USR n, where n is the address (in decimal for our computers). For example, you might see in a BASIC program USR 980. The computer would go to memory location 980 and execute whatever instruction you had put there. It would then go on to the instruction in location 981 and continue in that manner until it encountered an instruction to return to the basic program; whereupon it would take up where it left off in the BASIC part of the program.

The odd REM statements you often see at the start of programs in QZX are put there so that the proper machine code instructions will be in the proper memory locations when the computer needs them.

But I have never seen the USR command in a line by itself, and in different programs it is associated with different action instructions. This has always puzzled me, but recently things have been getting a bit clearer.

It turns out that USR is really a function, not a command. Hence, it must be associated with some action instruction: which one depends on what you want the machine language to do when it returns to the BASIC program.

Two of the instructions most often associated with USR are LET and PRINT. Consider the following lines.

10 LET M = USR 980
20 PRINT USR 980

In the first line when the Machine program returns to the BASIC program, it places the number that it calculated into the BASIC variable called M. In the second line the Machine program printed on the screen the number which it calculated. Sometimes you are not concerned with anything that the Machine program calculated because you had it perform some action. In that case many people use a line something like

30 RANDOMIZE USR 980

Here people use RANDOMIZE, not because they want to have anything to do with random numbers; but because the Machine code has already done what you want it to do, and the RANDOMIZE instruction does nothing more to your program.

Of course, in both of these last two cases you could have preceded differently. To print to the screen, you could have used line 10 and then

20 PRINT M

In the last case, where you did not want the Machine program to do anything after it was finished, you could have used line 10 and then just ignored the variable M for the rest of the program. Line 30 does save a little memory however.

Of course, you can use USR n anyplace a BASIC variable can be used. As for example in the following IF statement.

40 IF USR 16514 < 50 THEN STOP

In summary, remember that USR is a function which must be used with another command. Many people use it with LET if they will use the number returned by the Machine program elsewhere in the BASIC program, with PRINT if they want the result to appear on the screen, and with RANDOMIZE if the desired Machine code action has already taken place.