FREE MEMORY (Your Computer, Nov'81, p.67)
Paul Brittain, Chelmsford, Essex

IF YOU LOOK at the memory map for your Sinclair computer -- page 171
ZX-81 manual, page 106 ZX-80 manual - you will see that the free
memory is bounded at the upper end by the machine stack, and at the
lower end by the top of the BASIC system. Thus to find the amount of
free memory at any particular moment, you must subtract the relevant
system variable from the SP register.

However, since it is not possible to access the contents of a
register directly from BASIC, a machine-code routine must be used,
such as the one I have written.

Turning to the listing, you can see that the machine code required is
relatively simple, and for the most part self-explanatory. However, I
shall attempt to answer two questions likely to arise.

First, as you may have noticed, the system variable marking the top
of the BASIC system is DF-END (address 16400) in the case of the 4K
ROM (ZX80); or STKEND (address 16412) in the case of the 8K ROM (ZX81).

Secondly, note that the 8K ROM version contains extra instructions to
transfer the result from the HL register into the BC register, and
to satisfy the return conditions. These instructions should be added
to all routines converted from the 4K ROM if they are to function
correctly - especially those which give a numeric result or alter the
IY or IX registers.

Finally, some notes on how to use the program. Since the code is
entirely position independent, it can be placed anywhere convenient
in memory. For this reason I have not included any addresses in the
listing. When storing the machine code, my favourite method is to
POKE it into a REM statement in the first line of a BASIC program,
remembering that the address of the first byte after the REM is 16427
in 4K ROM machines, and 16514 in 8K ROM machines. For further details
on this and other methods of storing machine code, consult the
article by Trevor Sharples in the August/September issue.

Once the routine has been loaded into memory, it is called using:

PRINT USR (address)

where address is the one to which the first byte was POKEd. This will
result in the number of bytes of free memory being displayed on the
screen.




postscript to:
FREE MEMORY (Your Computer, Nov'81, p.67)
Paul Brittain, Chelmsford, Essex
by:
BASIC Truth (Your Computer, Mar'82, p.73)
P.J.Newbury, Croydon, Surrey

Paul Brittain's "Free Memory" machine code program - Software File,
November 1981 - can be entered very simply by using BASIC characters
in place of the decimal machine code instructions. All you do is
enter the follwoing line:
1 REM j,?.?,T,GOSUB,?,0,RND,GOSUB,PI,FAST,AT,CLEAR,5,s,RND,Y,2,GOSUB,?,TAN
Where j means "Inverse J" and s means "Space". The commas are only
for clarity and are not part of the line. To enter keywords use THEN,
the keyword and thirdly edit out THEN using the cursor position keys
followed by RUBOUT.
        After entering the line with NEWLINE, enter the following as
direct commands:
                POKE 16515,103
                POKE 16516,111
                POKE 16519,75
                POKE 16533,71
The program is ready for use at any time by:
		PRINT USR 16514
        It is useful to have this program in the machine whenever you
are writing a program. By saving it on tape, you can enter it before
your program. Just remember not to use line 1 in your program. THe
four POKE commands will have to be entered each time you load it.
        The lack of READ and DATA statements in ZX-81 BASIC can be
most frustrating. The most common use for such commands is to get the
Nth number from a list of numbers. Here is one solution to the problem.
        Each number in the list must have the same number of
characters including the sign and any decimal point. This can be
eachieved easily with a few extra zeroes. The two program lines are:
LET A$="(your list of numbers entered one after the other)"
LET Z=VAL A$((N-1)*X+1 TO (N-1)*X+X)
X is the number of characters in each number. Any additional zeroes
included to make every number the same number of characters are
removed by the VAL statement. N can, of course, be changed to any
other variable convenient to your program, as can Z.
