ZX Programming

See all articles from Sinc-Link v5 n1
Author(s): Ray Elder
Publication: Sinc-Link
Pub Details: Sinc-Link v5 n1 (Volume 5, Issue 1)
Date: January/February 1987
Pages: 4

Ray Elder presents a forum of news, hints, tips and advice exclusively for ZX81 users.

There is something about this machine which makes us users resist the Spectrum’s lure of sound and colour or the QL’s memory and microdrives.

It doesn’t take long before the limitations of Basic cause us to give up or progress into machine code. What I hope to do is give a series of machine code routines which can be used by expert and novice alike to improve the 81’s speed and performance.

What I have is a loader program with a massive REM at the start, this Rem contains a suite of programs which perform such essential tasks as a variety of Imputs, Prints, Pauses, Scrolls and screen effects. By keeping careful notes of register values, I then use those which apply to whatever program I am writing.

I hope to give you a similar utility which perhaps other readers may like to contribute to, so building into a program which even QLers will envy!

To simplify for new 81ers, Machine code is operating the computer processor directly by giving it instructions in its own language — numbers.

We have to store these numbers somewhere and the most convenient place is in the first line of the program which is made a REM line. This means that because it is a REM the computer ignores all the characters which follow it until directed there by a specific USR instructions.

The USR function needs to be followed by the memory address where the processor will find the first machine code instruction and this will be an address in the REM.

We know from the manual that the first address that follows the line number and REM code is 16514 and this is often the address at which we begin our machine code.

Unless you have an assembler you have to input the numbers which are usually given in HEX, a system of counting in groups of 16, and get them into the memory locations in the REM.

For this we use a LOADER program, a very simple one which I use as the basis of my master program is given in FIG 1, type it and save it.

FIG 1

9000 PRINT "ADDRESS TO START ?";
9010 INPUT A
9020 PRINT A
9030 LET A$=""
9040 LET X=0
9050 IF A$="" THEN INPUT A$
9060 IF A$="S" THEN STOP
9070 IF X=0 THEN SCROLL
9080 IF X=0 THEN PRINT A;": ";
9090 PRINT A$( TO 2);" ";
9100 POKE A,16*CODE A$+CODE A$(2)-476
9110 LET A=A+1
9120 LET A$=A$(3 TO )
9130 LET X=X+1
9140 IF X=6 THEN GOTO 9040
9150 GOTO 9050

Before we can put numbers into the REM memory we have to claim that memory for our use, and we do that by following the 1 REM by as many characters as we need. Problems arise when you need around 1000 characters as I am about to suggest! One heck of a lot of time, especially as the 81’s entry speed slows as the line length increases.

As computers are supposed to make life easy, our first program might as well create our REM for us. We still have to type in a few characters, but typing 1 REM followed by 96 dots is a lot easier than 1000 dots! Add the REM to the loader prog.

Now we can RUN our loader program and it will ask us to enter the start address. This is, surprise, surprise, 16514

The program is now waiting for our numbers, type in each line of FIG 2 one line at a time, pressing ENTER at the end of each line. Note each line consists of 12 characters with no spaces between them.

Finally add the lines in FIG 3 to the loader program and SAVE it. This should become your initial program and will be useful for entering other machine code programs which you may find in these pages or elsewhere.

FIG 2

D60BCD230F01
06002A8240E5
09444D2A2940
09222940210C
403E095E2356
D5EB09EB722B
7323233D2803
D118EEE1E501
E240A7ED4244
4DE1EDB821E3
403600233602
23C103037123
702336EA0B0B
23110100EB19
EB361BEDB036
7534CD2B0FC9

FIG 3

2 REM
4 PRINT "REM LENGTH ?"
5 INPUT N
6 POKE 16514,N-256*INT (N/256)
7 POKE 16515,INT (N/256)
8 RAND USR 16516
9 STOP

Note 2 REM. This is a safety line and MUST be included even though it has no apparent purpose.

Having Saved the program several times (just in case!) we will now RUN it, and the message REM LENGTH? pops up. We want an all purpose REM and 1000 characters seems plenty, so enter 1000.

If all is well, a flicker and on listing, a massive line of dots…

Delete the lines, one at a time by entering their line numbers, 4, 5, 6, 7, 8 and 9. SAVE this on yet another tape for this will become our master utility program with the first ‘real’ routine next time.

Hints

Mark Baldwin of Barnsley has been squeezing beeping sounds out of his 81. Turn the TV volume up, put the 81 into FAST mode and type in:

FAST
RAND USR 821 ( to 829)

The result says Mark is a whole range of different notes.

New Game

Pooter Games have just brought out Pooter Puzzler No 2 a collection of seven teasing puzzles which sells for £2.95. Information from: Pooter Games, 24 Parsloes Avenue, Dagenham, RM9 5NX.


Ray Elder with more advice for the ZX81 user on building machine code routines.

Last month we created a program to create a giant REM to hold our collection of machine code routines which I hope YOU will help me to develop.

Meanwhile I hope experienced users will excuse me as I realised that we often take for granted things which may confuse relative newcomers, so I will recap on a few well known bits and pieces from time to time.

The ZX81 has a special area of RAM memory which it looks at 50 times a second and transfers to the TV screen, this is called the display file and it has one disadvantage and one advantage when compared to the Spectrum (ignoring colour and hi-res that is).

The disadvantage is that it moves around in memory whilst the Spectrum’s always stays where it is, and the advantage is that you can POKE characters directly to it from BASIC or machine code which you can’t on the Spectrum.

To find out where the display file memory is at any particular time Sir Clive supplied us with two addresses which will tell us its start address MINUS one. These are addresses 16396 and 16397 and a simple program to use this might be as follows:

10 LET A=1+PEEK 16396+256×PEEK 16397
20 POKE A,128

And inverse space should appear in the top left corner of the screen, 128 being the code for an inverse space!

Now we’ve found it, we must know how it is laid out and for this I am presuming that 16K is being used, 1K users-hard luck!

Each line consist of 32 characters PLUS a special marker for the “end of line”, the machine was designed like this to maximise the efficiency when only using 1K, and in fact using the built in SCROLL function will almost certainly destroy this pattern due to it dotting end of line codes all over the place.

In general use the CPU manages to make sense of all this and cope, but if we are PEEKing and especially POKEing then a system crash is very likely.

Why? Because if the end of line code is overwritten then the CPE does get confused and pulling the plug is usually the only way out. The end of line character by the way is number 118, the same as the NEWLINE code.

This issue I have given a simple screen manipulating routine and a REM extender. They are both relocatable and can be entered to any address you desire, making sure of course that they do not overlap. I give the length for each with the code.

Those who created our giant REM last month can simply RUN the loader program given, enter the address and type in the HEX CODES which follow the LET X$=” lines, suggested addresses are given and for those who missed last month’s article (shame on you) type in the program as written ADDING a line 1 REM … with at least 100 dots following it. For you program A is essential, for the rest program A may prove of use later on.

PROGRAM A

2 REM PROGRAM A,ADDRESS 16514
3 REM LENGTH 22 BYTES
10 LET X$="2A7F40118340195E235
62B19117F40AFED52227F40C9"
20 REM ADD PROGRAM C HERE....
30
40
50

This is a program designed to extend a REM. To use it type in a line 2 REM … followed by as many dots as you require to ad to your existing line 1 REM. Then RAND USR start_address (16514?). If you forget to add the 2 REM then your next line which may be a program line will be added to your REM and could cause a little confusion!

PROGRAM B

2 REM PROGRAM B,ADDRESS 16550
3 REM LENGTOH 21 BYTES
10 LET X$="01D6022A0C40237EFE7
628FAC680770B78B120F2C9"
20 REM ADD PROGRAM C HERE....
30
40
50

A little fun routine which simply turns everything on the screen to its inverse. Useful for effects or creating a black screen very quickly, try using in a loop such as:

10 FOR I=1 TO 50
20 RAND USR start_address
(16550?)
30 NEXT I

This works by peeking at each screen position in the display file in turn, adding 128 to it and poking it back onto the display file, ignoring end of line markers of course.

2 REM PROGRAM C,LOAD IN CODE
3 REM TO REM,ADD AFTER A OR B
20 PRINT "LOCATION ADDRESS IN
DECIMAL ?"
30 INPUT A
40 FOR I=A TO A+((LEN A$)/2)
50 POKE I,16*CODE A$+CODE A$(2
)-476
60 LET A$=A$(2 TO )
70 NEXT I

Next month I will give a set of four scroll routines left, right, down and an up scroll that doesn’t mess up the display file as Sir C’s does. Meanwhile keep those neat little routines coming in and we’ll start using them very soon!


These two pages are from the ZX COMPUTING MONTHLY magazine. Subscriptions are 18 Pounds (Surface Mail Rate). That’s about $37 (CAN) for 12 issues. Covers all Sinclair computers. Make Postal note payable to Argus Specialist Publications Ltd. Send to: INFONET LTD, Times House, 179 The Marlowes, Hemel Hamstead, Herts. HP1 1BB