1K DISASSEMBLER (1K ZX81)
by Mike Biddell
from ZX Computing 2, August/September 1982, pages 66-67

Mike Bidden has produced a disassembler which just squeezes into 1K on the
ZX81. The main aim of the program is to allow you, to unlock the secrets
contained in the ROM, so that you can gain a working understanding of the
routines inside it.

If you have 16K, it will allow you to load Z80 code from various sources and
disassemble the code into understandable form. When he was working on the
program, Mike soon discovered that 1K would not allow a full disassembler to be
written, which is not surprising. Therefore, the program given here does not
print mnemonics, but does group the code into separate instructions as follows:

Address  Code      Mnemonic (not printed)
13D      CD 52 00  CALL 0052
140      3E FF     LD A,FFH
142      C9        RET

The address of each separate instruction is converted to Hex and the code for
one complete instruction is then printed. This renders the code into an easily
understood form.

Program Design

The Z80 OP. Codes were grouped into tables, according to whether they were two,
three or four byte codes, the remainder being one byte codes. The tables so
produced were as follows:

   Table 1          Table 2          Table 3
2 byte codes     3 byte codes     4 byte codes
06 OE 10 16      01 11 21 22      DD ED FD
18 1E 20 26      2A 31 32 3A
28 2E 30 36      C2 C3 C4 CA
38 3E C6 CB      CC CD D2 D4
CE D3 D6 DB      DA DC E2 E4
DE E6 EE F6      EA EC F2 F4
FE               FA FC

These tables were then written into the USR routine. The op.code byte to be
decoded is POKEd into address 16514. The USR routine then examines this byte
and compares it with each look up table in turn using the powerful Z80 CPIR
instruction. If a true comparison in Table 1 (two byte codes) is found, the BC
register pair is loaded with the value 2 (Hex or decimal) and the machine code
routine returns to BASIC. With the ZX81, the value of USR is the contents of
the BC register pair and therefore if the program RETs at this stage, USR is
assigned the value 2. If no match is found in table one, the code jumps to
table 2 (three byte codes).

If a match is found, the program Returns with BC, and hence USR, assigned the
value 3. Again, if no match with the byte under scrutiny is found, table 3 is
examined, where USR is assigned a value of 4. Finally, if no match is found,
the byte must be a one byte instruction and USR is unity.

The BASIC program calls the machine code routine and carries out the necessary
decimal to hexadecimal conversions. Variable W holds the USR value and prints
out 'W' bytes of the code to be disassembled. In this 'disassembled' form the
code is quickly and easily understood.

To test the program, start at 16520 and with each press of Newline, the program
will disassemble itself. The program disassembles data tables as though they
were program and there is some ambiguity surrounding the four byte codes.
However, for the most part, the program works extremely well, rendering
meaningless code into understandable form.

The disassembler tends to be self aligning, ie if you jump into code at a data
byte, rather than an op.byte, it tends to sort out the programming logic after
about three presses of Newline and 'tune in' to the op.bytes.

The disassembler has unravelled vast chunks of the ZX81 ROM for the author and
should be an invaluable programming aid for machine code writers using only the
1K machine and for whom, available 4K or so disassemblers are not a practical
proposition.




AUTO-LISSAJOUS AND LISSAJOUS SKETCHPAD
by Frazer Melton
from ZX Computing August 1982, page 58

Weaving a tangled web

Jules Antoin Lissajous, a French physicist who lived from 1822 to
1880, made a Study of the movement of particles under the action of
periodic motions, acting at right angles to each other. He discovered
that bodies moving in this way trace intricate patterns as their dance
around each other. This program by Frazer Melton of North Kelsey shows
you what Lissajous discovered.

The PLOTted point in this program traces our the path of the sum of
these periodic motions The figure can be used in a number or ways. One
way is to compare two frequencies. If they are the same, the program
will draw a Circle. If they are different, the number of points where
the curve touches either the vertical or horizontal edge is the ratio
of the two frequencies. If f1 is the known frequency, then the unknown
frequency (f2) is equal to the number of times the curve touches the
vertical edge of the confining box multiplied by the known frequency.

We have two versions of the program here The first one (program one)
allows you to enter your own choice of STEP and X and Y frequencies.
STEPS should be around 50 or more if the X and Y frequencies are in
single figures, and correspondingly more as the size of the frequencies
increases, The higher the step number, the greater the resolution of the
final figure.

Try these sample values:

     X         Y
STEP FREQUENCY FREQUENCY
50   1           1
60   2           1
60   3           4
60   5           2
80   1           2.5
600  13          26.5
300  7           4

You can change the 30 in line 100 if you find you are running our of memory.

The second program (PROGRAM TWO) chooses the two frequencies at random,
between one and 20. The step size is set at 500 which, although fairly
slow, ensures that a detailed curve is drawn

If you don't have a printer or you don't want to save any of the figures
on paper, omit lines 115 and 117 in each program. You may like to add a
delay loop (such as for J equals 1 to 300 NEXT J) before the program RUNS
again.




CHEQUE BOOK BALANCER (16K ZX81)
by Tim Hartnell
from ZX Computing 2, August/September 1982, page 109

This 16K program should help make sure you do not become overdrawn - and if you
do, it will tell you.

The program leads you through entering information regarding your account,
gives you the option of altering a particular entry if needed, and (at the end
of the run) prints out information on all the cheques written (who it was made
out to, and why, and the amount). The program then prints out the final balance
and, if necessary, gives you the good news that you are overdrawn.





GETTING PRIMED (1K ZX-81)
from ZX Computing 2 (August-September 1982), page 73

Our final 1K program in this section is a way of getting your ZX81 to earn its
living generating prime numbers. When you run the program you'll get a prompt.
This is the number of prime numbers you want the computer to generate for you.
It will then proceed to do so for you, printing them out as it works them out.
If you want a permanent record of your computer in its prime, change line 160
to read LPRINT D.

We modified the program slightly to count the number of primes it had
generated, and after running it over five hours had only got to prime number
6030 (see printout below). The ZX81 was getting pretty hot by then so we
stopped the process. A pity, as we'd love to find out what the 10,000th prime is.

6030            59651
6031            59659
6032            59663
6033            59669
6034            59671
6035            59693
6036            59699
6037            59707
6038            59723
6039            59729
6040            59743
6041            59747
6042            59753
6043            59771
6044            59779
6045            59791
6046            59797
6047            59809
6048            59833
6049            59863
6050            59879
6051            59887




LINING UP NUMBERS (1K-16K ZX-81)
by Nick Godwin
from ZX Computing 2 (August-September 1982), page 74

There is something irritating about a list of numbers display in a tatty and
irregular format. Nick Godwin from Eyemouth, Berwicks decided to do something
about it.

Consider the following versions of the same sum:

99.089            99.09
679.0734         679.07
-2                -2.00
679              679.00
-186            -186.00
46.009            46.01
-269.087        -269.09
- 12             -12.00
148              148.00
981.08           981.08

2163.1644     2163.1644

The version on the left was produced by the following routine:

   100 LET B=0
   110 FOR J=1 TO 10
   120 INPUT A
   130 LET B=B+A
   140 PRINT A
   160 NEXT J
   170 PRINT
   190 PRINT B
   199 STOP

into which, of course, I entered the values which I wanted to be summated.

This is very untidy.

To start dealing with the problem, modify the program by adding or changing
certain lines, as follows:

   105 LET T=16
   140 LET X=A
   150 GOSUB 1000
   180 LET X=B
   190 GOSUB 1000

The value of T can be adjusted to change the lateral print position, but be
sure to allow sufficient room on the left of the screen for the longest number
you want to enter.

The following subroutine, applied to the above, is suitable if you only wish
to enter positive integers:

   1010 LET X$=STR$ X
   1020 PRINT TAB T-LEN X$;X$
   1029 RETURN

If you wish to enter decimal numbers, but only want its nearest integer
printed in each case, add the following lines:

   1000 IF X=.5 THEN LET X=6
   1005 LET X=INT (X +.5)

You may want to be able to enter either integer or non-integer values, and to
have these printed in full, in which case substitute the following for the
whole of subroutine 1000 above.

   1010 LET X$=STR$ X
   1020 IF X$(1) = "." THEN LET X$="0"+X$
   1030 FOR K=1 TO LEN X$
   1040 IF X$(K)="." THEN GOTO 1070
   1050 NEXT K
   1060 LET X$=X$+".0"
   1080 PRINT TAB T-K;X$
   1089 RETURN

You may wish to be able to enter negative values, in which case add the
following line:

   1015 IF X<0 THEN IF X$(2)="." THEN LET X$=X$(1)+"0"+X$(2 TO)

You may wish to print only the first n decimal places. For example, the
addition of the following lines would be suitable for cash (ie. two decimal
places):

   1002 IF X=.005 THEN LET X=.006
   1003 IF X=-.005 THEN LET X=-.006
   1005 LET X=INT (100X+.5)/100
   1070 IF X$(LEN X$-1)="." THEN LET X$=X$+"0"

You may wish to put, as I have done in my opening example, the total in
complete form, in which case add the following line:

   1006 IF J=11 THEN LET X=B

Another improvement to presentation consists of the addition of the
following line:

   1000 SCROLL

You must also amend any PRINT in the main body of the program to SCROLL (ie.
line 170).

Finally, the program based on the preceding segments of program which accepts
five numbers, adds then together, and - as can be seen from the print out below
- prints them out attractively.

     123.654
        .009
       2.06
      12.60
   23333.80

   23472.123




MORSE TRAINER (16K ZX-81)
by John Knight
from ZX Computing 2 (August-September 1982), page 38

Master Morse code with the help of this 16K ZX81 program from John Knight of
Cheshire.

One of the conditions for getting an amateur radio licence (Class A UK) is a
degree of proficiency in Morse code. This program may help you attain the
required level of skill.

When you run the program a menu will appear giving you the option of entering
an English message, and having it reprinted in Morse, of having the program
generate a Morse symbol at random and give you three tries at entering its
English equivalent.

Notice the use of the initialisation subroutine starting at line 9000, which
goes into FAST, then strips A$ down to elements of C$. To simplify later
processing, C$(38) is the equivalent of CHR$(38), ie. the letter "A". The
program tells you (line 2190) which letter a particular symbol represents if
you don't guess it within the three guesses allowed.




PERMUTATING (1K ZX-81)
by Said Hassan
from ZX Computing 2 (August-September 1982), page 73

Our third program, another one by Said Hassan, calculates combinations and
permutations. You are first asked which calculation you want to perform.

Lines 300 to 400 check that the input figures are numerical and lie within the
machine's capabilities. The permutation of taking n different items r at a time
is given by the formula:

   nPr = n!/(n-r)!

(n! is n factorial)

For example, consider five different playing cards that have to be arranged
in groups of three

   n=5, r=3, and 5P3=60.

The combination of taking n items r at a time is given by the formula

   nCr = n!/(n-r)!r!

How many ways can three book titles be selected from five book titles?

   5C3=10.




PIG LATIN GENERATOR
from ZX Computing, August/September 1982

Teach your ZX8I to speak "Pig Latin" with this
amusing program by Hans Beerbernon.

This 1K program uses the ZX81's "slicing" technique on strings to turn
English text, which you enter one word at a time, into "Pig Latin". Once
you've run it a few times, try to write a "Pig Latin translator" to decipher
the Pig Latin messages given here back into English. Note that line 80 starts
a new print line at random, to stop words wrapping around.




READ/DATA ROUTINE (1K/16K ZX81)
by Martin Frobisher
from ZX Computing 2, August/September 1982, page 114

Chalfont St Giles in Buckinghamshire is the home of Martin Frobisher who
feeling lost on his ZX81 without the use of READ and DATA, decided to create a
routine for it.

Martin writes to ZX Computing: "Having felt lost on the ZX81 without READ/DATA
statements, I set about writing a subroutine to solve this problem. When using
this subroutine, it is necessary to use GOSUB 9950 in place of READ A. The
subroutine is written for numbers only, but can be converted easily to deal
with string information. The string which holds the data is placed at the
beginning of the program, and the subroutine at the end. Note that there must
be a comma at the very end of the 'DATA' within the quote marks of the string
in line 10."

The READ/DATA routine comes with a simple demonstration program.




DICE ROLLER (1K ZX81)
by Martin Frobisher, Chalfont St Giles, Buckinghamshire
from ZX Computing 2, August/September 1982, page 115

Martin's second program prints out a table and graph showing the possible rolls
of three dice. The first column on the table shows the total of the three dice,
the second shows the number of possible combinations that can make this total,
and the third gives the odds of this total appearing. The graph printed
alongside the table is the normal distribution curve, and shows the probability
of the dice falling with a particular total. The program is best run in FAST,
although it does not contains such a statement due to lack of memory.





SORTING IT OUT (1K ZX-81)
by Ann Marshall
from ZX Computing 2 (August-September 1982), page 73

This utility program, from Ann Marshall of Coventry, sorts a series of numbers
(positive, negative or mixed) into order. When you run the program, you'll get
a prompt as the computer waits for you to enter the number of items you want
sorted. Enter this number, then press NEWLINE/RETURN, and then enter the items
of data one by one. Once they are all in, the computer will sort them, then
print them in order, numbering each one as it prints them out. As it is now,
the computer sorts the numbers into descending order. If you want them in
ascending order, then reverse the 'greater than or equal to' sign in line 130.




SPY TIME (1K-16K ZX-81)
from ZX Computing 2 (August-September 1982), page 74

If you ever decide to take part-time employment as a spy, your ZX81 could help
you get messages to and from enemy territory. The following program for the
ZX81 has been adapted from a ZX80 program in the book 'Stretching Your ZX81 or
ZX80 to its Limits' (published by Computer Publications). Here is a sample
message produced by entering a key number of 193.

Message:
THIS IS A TEST TO TRY IT OUT

Encoded Message:
IF INPUT LOAD RAND AT LOAD RAND AT SCROLL AT IF FOR RAND IF AT IF PRINT AT
IF SAVE COPY AT LOAD IF AT PRINT CLS IF

[Enter the message at the first input prompt and the encoded message will be
displayed. At the second input prompt enter the encoded message to decode it
back to the original message. You will need to enter the encoded message by
pressing THEN followed by the keyword, then cursor back and delete the THEN,
then cursor forwards and repeat for the process for the subsequent keywords.]




STRING SORT (16K ZX-81)
by Graham Charlton
from ZX Computing 2 (August-September 1982), page 36

The title should give away what this program does. You are asked how many words
you wish to enter, and the maximum length of the words. This sets up a two
dimensional string array.

You then enter the words, the ZX81 switches into FAST and sorts the words into
alphabetical order, switches into SLOW, and prints out the list. To print the
list onto paper, delete line 250 and change line 260 to LPRINT A$(A).




TELEPHONE DIRECTORY (16K ZX-81)
by Graham Charlton
from ZX Computing 2 (August-September 1982), page 36

When you run this program, you'll be given three options - update, search or
save. Pressing 1 (update), enables you to add to your directory. It asks for
the name of the person you wish to enter, and then the number. This is
converted (see line 1080) to a 32 character length string. It is then placed
into your growing directory in alphabetical order (1190-1160). The program then
requests another name. Simply pressing NEWLINE returns you to the three options.

Entering "2" (search) allows you to search for the number required. Enter the
name of the person whose number you want to find, and it will search for this
name, and print it out. You can have two or more entries for one person, ie.
home and work numbers, the program will print out all of them. If you enter "A"
then all the names and numbers of the people whose name begins with A in your
directory will be printed out. If you enter BA you'll get all the names
starting with BA, and so on. Entering a null string will print out the whole
directory in alphabetical order. Pressing "3" (save) saves the enlarged
database.




USER-DEFINABLE GRAPHICS
from ZX Computing, August/September 1982

Thirteen-year-old Chris Callender from Cove, Helensburgh, has devised
a great program to allow you to define your own characters for dumping
to the ZX printer.

This program will work with a ZX81 or an 8K ROM ZX80. It needs a printer, and
16K RAM. The first thing you must do before typing in the program, or
LOADing it from cassette, is to type:
	POKE 16389,124
	NEW
This will alter RAMTOP to make space for the machine code subroutine. Next,
type in the program as listed, and press RUN.

Now, to design, say, an arrow like the one in figure one, type:
"   .    ";NEWLINE
"  ...   ";NEWLINE
" . . .  ";NEWLINE
".  .  . ";NEWLINE
"   .    ";NEWLINE
"   .    ";NEWLINE
"   .    ";NEWLINE
"   .    ";NEWLINE
The program will then be ready for the next character on that line. If you
are finished, type: "PLOT". There will be a delay of about 10 seconds and
then there should be a buzz from the printer and there will be your character.

To design a character, draw an eight by eight grid and make up your character
by filling in the segments of the grid. Then type in your character line by
line. Everytime you come up against a blob that should be filled in, enter a
fullstop. You make spaces by, obviously enough, typing a space.

How it works:
The program works by using a machine code routine copied from the ROM at
address 2161. This is copied above RAMTOP by lines 5-9. Then the user enters
a line of his character as B$. Lines 50-120 convert this line to a byte of
information in array A$. If the user types PLOT, the program goes to line
9988 which is the start of the print routine.

LINE NUMBER:EFFECT:
   1        Checks it memory above RAMTOP has been reserved
   2        Gives error code if not
   5-9      Copy and adapt print routine in ROM and set up above RAMTOP
  12        Sets up array A$ to store characters
  30        Displays current line
  40        Input line of the character
  50-120    Sets C to the right code for array A$
 130        Sets array A$ to C
9988-9999   Sinclairs high resolution printing program




WALLPAPER (16K ZX-81)
by Mark Charlton
from ZX Computing 2 (August-September 1982), page 37

From Mark Charlton comes a program which expects you to enter a name, some
words, or a design, and then from the string you enter attempts to create
wallpaper'. Sample runs follow the program, using the words 'MARK CHARLTON',
'CLIVE SINCLAIR' and 'ZX COMPUTING'. Mark suggests you could try it just by
pressing NEWLINE, without entering anything, which still produces a fine
design, or just use a few graphics symbols and spaces.




WORD PROCESSOR (16K ZX81)
by N Goodwin and T Hartnell
from ZX Computing 2, August/September 1982, pages 112-113

This word processor program will make text neat and tidy before you print it -
and gives you the chance to correct mistakes, using a free-moving cursor.

The sample runs show how the program operates. You enter your text (up to 17
lines deep) as a single string, X$. When you have the text in, you press
NEWLINE, and the computer will shuffle the words to ensure that none of them
are split at the end of a line.

A menu appears with three options: 1 - correct the text; 2 - LPRINT the text;
and 3 - to start again. If you decide to correct the text, it will reappear on
the screen, with the words "ENTER 1 TO RETURN TO MENU" above it. You use the 5,
6, 7 and 8 keys to move your cursor in the direction indicated by the arrows on
those keys, and the cursor moves along the line of text, inverting the letter
it is passing over. Once you find a letter which is wrong, such as the "E" in
the word WERKS in the sample run, you press "A" and the words ENTER LETTER TO
BE SUBSTITUTED appear at the bottom of the screen. You enter your letter, and
press NEWLINE, and the inverse incorrect letter will be altered to the letter
you've chosen. Pressing "1" at any time will return you from the 'correction
phase' to the original menu, and from this menu you can choose "2" to LPRINT
the text.

If you want the text printed, the computer searches through the whole of the
string, turning any inverse letters back to their non-inverse equivalents.
After LPRINTing, you are shown a further menu, which allows you to run the
whole program again from scratch, or to terminate the run. Although this
program allows you to correct wrongly spelled words, there is no provision to
insert text. You may well wish to add this option.


[The listing the magazine suffers from two bugs, which are corrected with
the following lines:

2050 IF INKEY$="6" AND A<LEN X$-32 THEN LET A=A+32
2102 IF A>LEN X$-32 THEN GOTO 2035
]





