Thread: Sinclair ZX81
View Single Post
Old 26th Jul 2017, 9:31 pm   #24
cmjones01
Nonode
 
Join Date: Oct 2008
Location: Warsaw, Poland and Cambridge, UK
Posts: 2,679
Default Re: Sinclair ZX81

Quote:
Originally Posted by mole42uk View Post
what is the significance of RAND USR 16514?
The ZX81 was designed to be programmed by the user in BASIC, and could only easily save and load BASIC programs to and from tape. If you wanted to program the ZX81 in machine code rather than BASIC, you had to somehow conceal your machine code in a BASIC program. The common way to do this was to start your program with a REM statement containing as many characters as your machine code needed. The machine code would then be painstakingly POKEed in to memory at that location by manually entering numbers. The REM statement did nothing when the program was run, since it was just a comment, but was obediently saved and loaded to and from tape by the standard SAVE and LOAD commands.

The memory layout of the ZX81 meant that the first character of that REM statement on the first line of a BASIC program was at location 16514. The USR function in BASIC calls a machine code routine at the address it's given. USR returns a value (I can't remember what it meant - something to do with register contents, probably) so has to be given as a parameter to something else. It was common to use RAND for this because its only side effect was seeding the random number generator, which didn't usually disturb anything.

So the command RAND USR 16514 was the most common way of running a machine code program on the ZX81.

Afterthought: my memory is a bit rusty, so it may be that the first line of the program was actually RAND USR 16514 and the second line contained the REM statement, so that running the BASIC program would start the machine code running. You'd have to consult the ZX81 memory map to work it out.

Chris
__________________
What's going on in the workshop? http://martin-jones.com/
cmjones01 is offline