RANDOM MEMORY

ZX Computing, February 1987



Clyde Bish presents a memory saving "Fastfile" routine.





"All this memory saving for adventurers is all very well, but what

about the rest of us!" I hear ZXC enthusiasts say. Well, here's a

memory saving routine just for you ('though I suspect the adventurers

may find a use for it as well). It's called "Fastfile", and is a

system for holding information, in any form, in a DIMentioned string

of 40,000 characters with a machine code routine, searching at about

50,000 characters per second to extract from it the information

required and print to screen. Without further ado, let's get to work.



Type in the machine code loader, Program 1, RUN it, and enter the

numbers from Table A, [file: FastCODE] reading across the lines as you

enter each number. It will be displayed so you can keep check. Note

any mistakes, and correct them at the end with: POKE address, correct

number



Now NEW the machine - your code is safe above RAMTOP - and type in

Program 2. This is the driver program and must be entered exactly as

printed for reasons that will be obvious later. Note that the "STOP"

in lines 10, 1010 and 2000 is the token, and is entered in symbol

shift mode.



Remember, "Fools rush in where angels fear to tread", so polish up

your halo, and SAVE the program and CODE, just in case of mistakes

with: CLEAR: SAVE "Fastfile" LINE 9000: SAVE "FastCODE" CODE 65263,111

and VERIFY both parts.



Now you're ready to try it out. Type RUN (ENTER) and the menu will

appear. The options are chosen by pressing the appropriate number (if

you get an error message, and you may, as there is a minimum of error

trapping to make as much memory as possible available for the file,

restart with GOTO 100, never RUN). This is what you can do:





1. Entry



This adds an entry to the file, provided there is enough space (You're

told how much space is free each time). The maximum INPUT length is

about half a screen (Remember to restart with GOTO 100 if you get an

Out of Memory message). If you want to save space, but avoid filling

out the ends of lines with spaces to prevent word splitting, use the

PRINT comma trick. For those who missed the earlier articles, this is

what you do. After typing the last character you want on the line get

into E Mode, hold on to the Caps Shift key and press 6, followed by 0.

The cursor will jump to the next line (three of these in succession

would leave a blank line within an entry at a cost of only three bytes!).



Make a file to experiment on using the fore and surnames of your

family. After the last entry pressing just ENTER will return you to

the menu.





2. Search



Select Option 2, and answer the "Key?" prompt by entering the word or

phrase for which you wish to search. The machine code, which

incidentally originated from the good old days of the ZX81, zips

through your file, PRINTing out all entries which include that key.

After each, the prompt "Erase?" will appear. Pressing "y" will erase

that entry, "n" will continue the search. On completion the word END

will be displayed. Pressing just ENTER will return you to the menu.



Try the following with your "names" file:-

a) Enter a forename - only that name will be displayed.

b) Try the surname - all entries with that surname will appear.

c) Try "Bloggs" (assuming that's not your name!) - just the END

   message will appear.

d) Try a single letter that you know is in the file - any entries

   which included that letter will appear as many times as they contain

   the letter. For example, John Jones would appear twice if the key were

   J, o or n, but only once if s were entered. The moral of this exercise

   is that the more specific the key used, the more selective the routine

   becomes. So if you were using the program as an index to magazine

   articles it would be better to reference Spectrum programs as sp1,

   rather than just sp, as in the latter case any entries where an s is

   followed by a p would be displayed.





3. Save



The whole BASIC program and the variables is SAVEd. Why not just the

data array? Because you also need the values held in other variables,

for example the file pointer, n. After VERIFYing you will be returned

to the menu.





4. Load



Use this option to LOAD in an existing file for searching or updating.

Existing files should only be LOADed in this way for interrogation.

Don't be tempted to just LOAD in a SAVEd file directly as it will

probably crash!





5. New



This clears the file by RUNning the program and resetting the arrays.



It would be nice to explain how the machine code operates, but as

usual space precludes that opportunity. Suffice it to say that the

routine compares what is held in a$() in the VARS area with what is in

b$(). It is therefore important that you make no alteration to line 10

until after DIM b$(40002) or these arrays will not be in the correct

places in VARS for the routine to find them. If you alter the length

of the program you will also need to reset v to a new value by PEEKing

the VARS system variable using PRINT PEEK 23627 + 256*PEEK 23628.





Fastfile (Microdrive)



And now, for microdrive owners, a version of "Fastfile" especially for you.



The program is essentially the same as the cassette version except

that the Interface 1 ROM takes over much of the donkey work and, of

course, speeds up the LOAD, SAVE and VERIFY routines. [file: Fastmdrv]



The file array b$() is set to a length of 29000 characters. [It was

only set to 12 characters in the listing. JimG] This length enables

you to hold three files named a, b and c on a clear cartridge, plus

the boot program (which sets everything running) and the machine code,

giving a total storage of 87000 characters. It also allows for a more

user-friendly program with single key-press controls, not to mention

avoiding an encounter with the Infamous Interface 1 ROM bug which

switches on your microdrive permanently! (If this does ever happen do

not power-down. You may lose data. Surprisingly, it is better to pull

out the cartridge first whilst the motor is running!).



Type in the machine code as previously described, then NEW and enter

Program 3. Now to prepare the cartridge. Format the cartridge as

described in the manual, then RUN the program. It will stop with an

error message. Fear not, saith he. All is well. Enter, as a command:

LET n$="a": GOTO 100



The menu will appear. Choose 4, then press "a" in response to the

"Filename?" prompt. The microdrive will run much longer than usual as

it is trying to erase an, at the moment, non-existent file. When the

menu reappears choose 4 again, and this time press "b". The third time

the menu appears press 4 then "c".



Now BREAK out of the program, NEW the machine, and type in the boot,

Program 4. Save the machine code still on board and the boot program

with:

SAVE *"m";1;"run" LINE 10: SAVE *"m";1;"FastCODE" CODE 65263,111

(where "run" is the three letters, not the Keyword). VERIFY both parts

and there you are.



To use the system, RANDOMIZE USR 0 to reset the machine, then press

RUN (Enter). The boot will load, then LOAD in the code and file "a"

(which is DIMentioned but empty).



The menu is essentially the same as before.



The ENTRY option will automatically SAVE a file when it is full, and

invite you to LOAD in a new file to continue.



SEARCH gives a printer option (by altering the stream) as well as to

the screen. The search can be stopped at any time by pressing a

letter. A return is made to the menu by a second press. When the end

of a file is reached there is the option to continue the search with

another file (after SAVEing the existing one if updated).

Alternatively a return can be made to the menu.



Obviously with SAVE the existing a, b or c file is erased before the

update is SAVEd and VERIFYed.



As with save, the filename for LOAD is a single letter.



The DELETE option is now separate, but operates as before. The key is

searched for with "y" to erase, "n" to leave - you may have more than

one entry with that key - and Enter for the menu.



This routine will, of course, search through any information in b$()

to locate a key and so could be used in any situation where fast

retrieval is required. One such use would be vocabulary searching in

an adventure. (You see, I didn't leave you out!). Alternatively it

could be used to search for words understood In an "Elisa"-type

program to experiment with A.I.. I'll show you how it could be done

and then you can improve on it.



Have a look at Program 5 [file: Fastdemo] as you read what follows:-



Line 800: Reset variables and the

arrays you've seen before.



830: R1 we need later. The program will stop If you've said "bye".



840: The user response goes into i$. A suitably cutting response is

printed if this is a null string.



850: The input is printed to screen with a leading capital.



860-861: Spaces in the input string are replaced by STOP tokens, plus

one at the end. The counter n is set to string length + 1.



862: Now to business! The data is RESTOREd. Look at Line 900 for a

moment. You'll notice a sequence of word, phrase, phrase triplets. In

essence the routine takes each response word in turn and searches for

a match in k$. If one is found (i.e. p>0 AND p<n)then one of the

phrases READ along with k$ and held in x$ and y$ is printed. Which, is

determined by R1.



882: If the search falls through the loop, no match has been found as

one of a sequence of general answers is given.



885: If too many general answers

are given then a request is

made to change the subject.



900: This is the first example of many DATA lines. You can produce the

rest yourself but remember three things:-

a) The items must be in threes - keyword, phrase 1, phrase 2.

b) The order of keywords in the data list is most important. Common

   words must be at the end with less likely ones at the beginning or the

   latter will never be found.

c) My original program had a vocabulary of 100 keywords. If you want

   more or less you must change the number ending the FOR statement in

   line 862:2



To end, here's an idea for an April Fool program to catch out an

ardent Arcader (I know it's early, but it'll take you that long to

sort out the DATA!). Have a simple arcade game on board as well, with

a timer line to switch control to your "chat" program after a given

period (Use the FRAMES system variable). You could then have the

machine do a fake "reset crash" (Use PAPER 0, then PAPER 7 with your

own PRINTed copyright notice) followed by a reincarnation and

on-screen communication from the "Spirit of the Machine" offering to

have a chat.



See who you can catch! Good fooling!

