Sinclair ZX81 User Manual
Hide thumbs Also See for ZX81:

Advertisement

Sinclair ZX81 BASIC Programming
by Steven Vickers
Second Edition 1981
Copyright 1980 Sinclair Research Limited
Front cover illustration by John Harris of Young Artists,
specially comissioned by Sinclair Research Limited.

Advertisement

Table of Contents
loading

Summary of Contents for Sinclair ZX81

  • Page 1 Sinclair ZX81 BASIC Programming by Steven Vickers Second Edition 1981 Copyright 1980 Sinclair Research Limited Front cover illustration by John Harris of Young Artists, specially comissioned by Sinclair Research Limited.
  • Page 2 5. A pair of leads about a foot (30cm) long with 3.5mm jack plugs at both ends. These connect the computer to a tape recorder. You will also need a television - the ZX81 can work without one, but you won't be able to see what it's doing! It must be a UHF television - if it's not built to receive BBC2 (in the UK) then it's no good.
  • Page 3 'What could I type instead?' & try out your replies. The more of your own stuff you write, the better you will understand the ZX81. (This is called unprogrammed learning.) Regardless of what you type in, you cannot damage the computer.
  • Page 4 This system of pressing just one key to get more than one symbol is used a lot on the ZX81. In the rest of this manual, words with their own keys are printed in BOLD TYPE .
  • Page 5 5. Now - and you must always remember this - press NEWLINE , the key . This means 'message complete', or 'all right, computer, lets see some action'. The computer will now read the message, work out what has to be done, and do it.
  • Page 6 The cursor can also be used for correcting mistakes: type ++2, to get PRINT ++2 on the bottom line, Pretty incomprehensible stuff, and when you press NEWLINE you get is the syntax error marker (the syntax is the grammar of the message, saying which are allowed and which are not), and shows that the computer got as far as ' PRINT ', but after that decided that it was not a proper message.
  • Page 7 Summary This chapter has covered how to type messages in for the ZX81, explaining the single keystroke system for words, & cursors, reports, the syntax error marker, & how to correct mistakes using and RUBOUT . The keyboard Here is a picture of the keyboard.
  • Page 8 Chapter 3 - A History lesson The messages that you type in are in a computer language called BASIC (standing for Beginners' All- purpose Symbolic Instruction Code). It actually takes the computer quite a lot of effort to break down the BASIC messages into its own rudumentary operations, but, after all, that's what it's paid to do.
  • Page 9 NEWLINE .) As you would hope, the ZX81 can not only add, but also subtract, multiply using a star * instead of the usual times sign - this is fairly common on computers) & divide (using / instead of ). Try these out.
  • Page 10 PRINT 2.34E15 You will see that after a while the ZX81 also starts using scientific notation. This is because it never takes more than 14 spaces to write a number in. Similarly, try PRINT 2.34E-1 PRINT 2.34E-2...
  • Page 11 & look up the antilog of 0.3010. Why are the answers not exactly equal? 5. The ZX81 uses floating point arithmetic, which means that it keeps separate the digits of a number (its mantissa) and the position of the point (the exponent). This is not always exact, even for whole numbers.
  • Page 12 Mathematically, a function is a rule for giving a number (the result) in exchange for another (the argument, or operand) & so is really a unary operation. The ZX81 has some of these built into it & their names are the words written under the keys.
  • Page 13 0.30103. Try doing multiplication & division using logs, using the ZX81 as a set of log tables in this way (for antilogs, see chapter 2, exercise 3). Check the answer using * and / - which are easier, quicker, more accurate, &...
  • Page 14 -1. Use this & Gauss' law of quadratic reciprocity, to show that 75 is a primitive root modulo 65537. The ZX81 uses p=65537 & a=75, & stores some b -1 in memory. The function RND involves replacing -1 in memory by b -1, &...
  • Page 15 Chapter 6 - Variables 'My pocket calculator,' you will be saying, 'can store a number away & remember it later. Can your ZX81 do that?' Yes. In fact it can store away literally hundreds, using the LET statement. Suppose that eggs cost 58p a dozen, &...
  • Page 16 Note : In some versions of BASIC you are allowed to omit LET & just type in (say) EGGS=58 This is not allowed on the ZX81. In any case, you'd find it rather difficult to type in. Also in some versions, only the first two characters in a name are checked, so that RADIO 3 & RADIO 33 would count as the same name;...
  • Page 17 (B*C) = (A If you've never seen any of this before then don't try to remember it straight away; just remember that = 1/A & = the square root of A (1/2) & maybe when you're familiar with these the rest will begin to make sense. Experiment with all this by telling the computer to print various expressions containing : e.g.
  • Page 18 Chapter 7 - Strings One thing the ZX81 can do that pocket calculators cannot is deal with text. Type PRINT "HI THERE. I AM YOUR ZX81." (" is shifted P.) The chilling greeting inside the quotes is called a string (meaning a string of characters), & can contain any characters you like except the string quote,".
  • Page 19 which is evaluated as Summary Strings Operation: + (for strings) Functions: LEN , VAL , STR$ Exercises 1. Type LET A$="2+2" & then PRINT A$;" = ", VAL A$ Try changing A$ to more complicated things & doing the same, e.g. LET A$="...
  • Page 20 Chapter 8 - Computer programming And now at last you shall write a computer program. Turn the computer off & on, just to make sure that it is clear. Now type 10 LET BUTTER=75 (& NEWLINE ) & the screen will look like this: This is different from what happened with EGGS in chapter 6;...
  • Page 21 & the answer 75 will appear in the top left-hand corner of the screen. At the bottom left-hand corner you will see the report 0/20. 0, as you know, means 'OK, no problems', & 20 is the number of the line where it finished.
  • Page 22 line 15. Last, type LIST 15 You will now see on the screen 15 LET YEAST=40 20 PRINT BUTTER,YEAST Line 10 has vanished from the screen, but it is still in your program - which you can prove by pressing NEWLINE again.
  • Page 23 Chapter 9 - More computer programming Type This will erase any old programs & variables in the ZX81. (This is rather like CLEAR , but CLEAR only erases the variables.) Now carefully type in this program: 10 REM THIS PROGRAM EXTRACTS SQUARE ROOTS...
  • Page 24 For instance, with commas, replace the 30 by 30 PRINT A, & run the program to see how successive PRINT statements can print on the same line but spread out in two columns. With semicolons, on the other hand, with 30 PRINT A;...
  • Page 25 200 GOTO 200 RUN 200 This looks all set to go on for ever unless you pull the plug out; but there is a less drastic remedy. Press the SPACE key, which has 'BREAK' written above it. The program will stop, saying D/200. At the end of every program line, the computer looks to see if this key is pressed;...
  • Page 26 set to 0 to begin with - & ITEM. Input ITEM, add it to TOTAL, print them both, & go round again.) 6. The automatic listings (the ones that are not the result of a LIST statement) may well have you puzzled. If you type in a program with 50 lines, all REM statements, 1 REM 2 REM...
  • Page 28 Chapter 10 - If... All the program we've seen so far have been pretty predictable - they went straight through doing all the instructions, & then maybe went back to the beginning again. This is not all that useful. In practice the computer would be expected to make decisions &...
  • Page 29 <= means 'is less than or equal to', & so on, just as for numbers. Note : In some versions of BASIC - but not on the ZX81 -, the IF statement can have the form IF condition THEN line number...
  • Page 30 IF condition THEN GOTO line number Summary Statements: IF , STOP Operations: =, <, >, <= , >= , <> , AND , OR Function: NOT Exercises 1. <> and = are opposites in the sense that NOT A=B is the same as A <> B &...
  • Page 31 { X if Y is non-zero (counting as X AND Y has the true) value { 0 if Y is zero (counting as false) X OR Y has the { 1 if Y is non-zero value { X if Y is zero NOT X has the { 0 if X is non-zero value...
  • Page 32 For instance ON A GOTO 100, 200, 300, 400, 500 Here, if A has the value 2, then 'GOTO 200' is executed. In ZX81 BASIC this can be replaced by GOTO 100*A In case the line numbers don't go up neatly by hundreds like this, work out how you could use GOTO a conditional expression instead.
  • Page 33 The letters, digits, punctuation marks & so on that can appear in strings are called characters, & they make up the alphabet, or character set, that the ZX81 uses. Most of these characters are single symbols, but there are sone more, called tokens, that represent whole words, such as PRINT , STOP , , &...
  • Page 34 : and (. Of the spurious ones, some are for control characters like , EDIT & NEWLINE , & the rest are for characters that have no special meaning for the ZX81 at all. Summary Functions: CODE , CHR$ Exercises 1.
  • Page 35 If instead of thinking in terms of the usual alphabet of 26 letters we use the extended aphabet of 256 characters, in the same order as their codes, then the principle is exactly the same. For instance, these strings are in ZX81 alphabetical order. " ZACHARY"...
  • Page 36 Chapter 12 - Looping Suppose you want to input five numbers & add them together. One way (don't type this in unless you're feeling dutiful) is to write 10 LET TOTAL=0 20 INPUT A 30 LET TOTAL=TOTAL+A 40 INPUT A 50 LET TOTAL=TOTAL+A 60 INPUT A 70 LET TOTAL=TOTAL+A...
  • Page 37 where the control variable is a single letter, & the initial value, limit & step are all numeric expressions. So, if you replace line 20 in the program by 20 FOR C=1 TO 5 STEP 3/2 then C will run through the values 1, 2.5 & 4. Notice that you don't have to restrict yourself to whole numbers, &...
  • Page 38 (Actually we have cheated slightly here: it should really be GOTO 21 instead of GOTO 30. This will have the same effect in our program.) 3. Run the program, & then type PRINT C Why is the answer 6, & not 5? [Answer: the NEXT statement in line 60 is executed 5 times, and each time 1 is added to C.] What happens if you put STEP 2 in line 20? 4.
  • Page 39 Chapter 13 - Slow & fast The ZX81 can run at two speeds - SLOW and FAST. When first switched on, the computer runs in the SLOW mode and can compute and display information on the screen simultaneously. This mode is ideal for animation displays.
  • Page 40 Chapter 14 - Subroutines Sometimes different parts of your program will have rather similar jobs to do, & you will find yourslef typing the same lines in twice or more; however this is not necessary. You can type the lines in once, in the form known as a subroutine, &...
  • Page 41 1060 LET S= INT (D/12) 1070 LET D=(D-12*S)*E 1080 LET L= INT (S/20)*E 1090 LET S=S*E-20*L 1100 RETURN On its own, this is not much use because there is no program to set up L, S & d beforehand, nor to do anything with them afterwards.
  • Page 42 This works exactly as you'd hope; in fact the line number in a GOSUB (or GOTO or RUN ) statement can be any numerical expression. (Don't expect this to work on computers other than the ZX81, because it is not standard BASIC.) This sort of stuff can work wonders for the clarity of your programs.
  • Page 43 5. A subroutine can have several entry points. For instance, because of the way our main program uses them, with GOSUB 1000 followed immediately by GOSUB 2000, we can replace our two subroutines by one big one that adjusts L, S & D & then prints them. It has two entry points: one at the beginning for the whole subroutine, &...
  • Page 44 Chapter 15 - Making your programs work There is more to the art of programming computers than just knowing which statement does what. You will probably already have found that most of your programs have what are technically known as bugs when you first type them in: maybe just typing errors, or maybe mistakes in your own ideas of what the program should do.
  • Page 45 Conceptually, then, rectangular boxes correspond to GOSUB s; although in practice some boxes - like the one above that inputs L, S & D - are translated directly into BASIC statements, without a subroutine. Anything - like flowcharts, subroutines, & also REM statements - that makes the program clearer gives you a better understanding of it;...
  • Page 46 As was mentioned in chapter 1, & you have no douby found out from experience anyway, when you turn the ZX81 off you lose all the program & variables that were sored inside it. The only way to save these is to have the computer record them onto a cassette tape;...
  • Page 47 Some tape recorders form a feedback loop with the ZX81. This can only happen when the EAR & MIC leads are both in at the same time, so the cure is to SAVE with the EAR lead disconnected.
  • Page 48 BASIC. Your saved programs cannot be loaded into other kinds of computer or the ZX80 with integer BASIC. The ZX80 with ZX81 BASIC, on the other hand is compatible with the ZX81; saved programs from either can be loaded to the other. After loading a ZX80 program, the ZX81 will be in fast mode.
  • Page 49 3. Type in the program "CHARACTERS" again, & then type LET X=7 so that - although it doesn't appear in the program - the computer now contains a variable X with value 7. Now save the program, turn the computer off & on (to make sure there's no cheating), & load the program back again.
  • Page 50 Chapter 17 - Printing with frills You will recall that a PRINT statement has a list of items, each one an expression (or possibly nothing at all), & that they are separated by commas or semicolons. There are two more kinds of PRINT item which are used to tell the computer not what, but where to print.
  • Page 51 Chapter 18 - Graphics Here are some of the most elegant features of the ZX81; they use what are called pixels (picture elements). The screen you can display on has 22 lines & 32 columns, making 22*23 = 704 character positions, &...
  • Page 52 graph). (ii) whereabouts on the screen to put the x-axis (corresponding to 22 in line 20 in the SIN graph program). (iii) how to scale the y-axis of the graph (corrsponding to 20 in line 20 of the SIN graph program). You will find that COS is the easiest - it's just like SIN .
  • Page 53 the N steps D2. Imagine a Monopoly board with M squares round the edge, numbered from 0 to M-1. The square you are on at any time is number S, starting at the corner opposite G0. Each move takes you N squares round the board, & in the straight line on the screen you make either a left-right/ up-down step (if you pass GO on the board), or a diagonal step otherwise.
  • Page 54 Chapter 19 - Time & motion Quite often you will want to make the program take a specified length of time, & for this you will find the PAUSE statement useful (especially in fast mode): PAUSE n stops computing & displays the picture for n frames of the television (at 50 frames per second, or 60 in America).
  • Page 55 10 IF INKEY$<> "" THEN GOTO 10 20 IF INKEY$ ="" THEN GOTO 20 30 LET A$= INKEY$ 40 IF A$= CHR$ 115 THEN GOTO 110 90 PRINT A$; 100 GOTO 10 110 PRINT " "; 120 GOTO 10 Note how we read INKEY$ into A$ in line 30. It would be possible to miss this out & replace A$ by INKEY$ in lines 40 &...
  • Page 56 40 PRINT AT 11,14; " " 50 GOTO 10...
  • Page 57 Chapter 20 - The ZX81 Printer If you have got a ZX81 printer, you will have some operating instructions with it. This chapter covers the BASIC statements needed to make it work. The first two, LPRINT & LLIST , are just kile PRINT and LIST , except that they use the printer instead of the television.
  • Page 58 better albeit less illustrative if replaced by ' AT 0,N'.) 2. Make a printed graph of SIN by running the program in chapter 18 & then using COPY .
  • Page 59 Chapter 21 - Substrings Given a string, a substring of it consists of some consecutive characters from it, taken in sequence. Thus "STRING" is a substring of "BIGGER STRING", but "B STRING" & "BIG REG" are not. There is a notation called slicing for describing substrings, & this can be applied to arbitrary string expressions.
  • Page 60 MID$(A$,N1,N2) gives the substring of A$ consisting of N2 characters starting at the N1th. TL$(A$) gives the substring of A$ consisting of all its characters except the first. How would you write these in ZX81 BASIC? Would your answers work with strings of length 0 or 1? 2. Try this sequence of commands: LET A$="X*+*Y"...
  • Page 61 Whereas B$ contains mere quote image characters (with code 192), A$ contains genuine string quote characters (with code 11). 3. Run this program: 10 LET A$=" LEN "" ABDC "" " 100 PRINT A$;" = "; VAL A$ This will fail because VAL does not treat the quote image "" as a string quote. Insert some extra lines between 10 &...
  • Page 62 Chapter 22 - Arrays Suppose you have a list of numbers, for instance the number of income tax collectors that have died each month in the current financial year. To store them in a computer you could set up a single variable for each month, but you would find this very awkward.
  • Page 63 & you will find that A$ behaves just like a string variable, except that it always has length 10, & assignment to it is always Procrustean. 3. READ, DATA & RESTORE; who needs them? Most BASICs (but not the ZX81 BASIC) have three statements called READ, DATA & RESTORE.
  • Page 64 In theory, you can always replace READ & DATA statements by LET statements; however, one of their major uses is in initializing arrays, as in this program: 5 REM THIS PROGRAM WILL NOT WORK IN ZX81 BASIC 10 DIM M$(12,3)
  • Page 65 Chapter 23 - When the computer gets full The ZX81 has a limited amount of internal storage, and it is not hard to fill it up. The best sign of this happening is usually an error report 4, but other things can happen and some of them are rather strange.
  • Page 66 The ultimate cure for this is to buy a RAM pack, which fits on the back of the computer. The Sinclair 16K RAM pack gives the computer sixteen times as much memory as it has in its unexpanded form.
  • Page 67 Chapter 24 - Counting on your fingers The next chapter digs inside the computer a bit, but before we look at that it would be as well to describe how computers count: they do it using the binary system, which means that they have no fingers - they are all thumbs.
  • Page 68 & fifty five (11111111 binary or FF hex), or alternatively any character in the ZX81 character set. Its value can be written with two hex digits. Two bytes can be grouped together to make what is technically called a word. A word can be written using sixteen bits of hex digits, &...
  • Page 69 2. How would you convert between decimal & hex? (Hint: exercise 1.) When programs on the ZX81 to convert numerical values into the strings giving their hex representation, & vice versa. (This is what STR$ & VAL do with decimal representations.) 3.
  • Page 70 Chapter 25 - How the computer works The illustration overleaf shows the inside of the ZX81 (but don't take it apart yourself because it can be a tricky business putting it together again). As you can see, everything has a three letter abbreviation (TLA).
  • Page 71 The last big chip is the logic chip, or SCL (Sinclair Computer Logic) chip. Again, this was specially designed & made for the ZX81, & it connects the other chips together in rather a clever way to make them do more than they normally would.
  • Page 72 (iii) At the top of the memory. When the ZX81 is switched on, it tests to see how much memory there is and puts the machine stack right at the top so that there is no space for USR routines there. It stores the address of the first non-existant byte (e.g.
  • Page 73 like with. If you then type NEW again it will not affect these twenty bytes. The top of memory is a good place for USR routunes, safe (even from NEW ) and immobile. Its main disadvantage is that it is not saved by SAVE . Summary Functions: USR Statements: NEW...
  • Page 74 Chapter 27 - Organization of memory The memory is sorted into different areas for storing different kinds of information. The areas are only large enough for the information that they actually contain, & if you insert some more at a given point (for instance by adding a program line or variable) space is made by shifting up everything above that point.
  • Page 75 Number whose name is longer than one letter: Array of numbers: The order of the elements is: first, the elements for which the first subscript is 1 next, the elements for which the first subscript is 2 next, the elements for which the first subscript is 3 &...
  • Page 76 Array of characters: The part string at E_LINE contains the line being typed (as a command, a program line, or INPUT data) & also some work space. The calculator is the part of the BASIC system that deals with arithmetic, & the numbers on which it is operating are held mostly in the calculator stack.
  • Page 77 Line number of statement currently being executed. Poking this has no lasting efect except in the last line of the program. 16393 VERSN 0 Identifies ZX81 BASIC in saved programs. 16394 E_PPC Number of current line (with program cursor). 16396 D_FILE See chapter 27.
  • Page 78 16415 Address of area used for calculator's memory. (Usually MEMBOT, but not always.) 16417 not used The number of lines (including one blank line) in the lower part of the 16418 DF_SZ screen. 16419 S_TOP The number of the top program line in automatic listings. 16421 LAST_K Shows which keys pressed.
  • Page 79 Appendix A - The character set This is the complete ZX80 character set, with codes in decimal & hex. If one imagines the codes as being Z80 machine code instructions, then the right hand columns give the corresponding assembly language mnemonics.
  • Page 80 add hl,hl sra c ld hl,(NN) sra d dec hl sra e inc l sra h dec l sra l ld l,N sra (hl) sra a jr nc,DIS ld sp,NN ld (NN),a inc sp inc (hl) dec(hl) ld (hl),N jr c,DIS srl b add hl,sp srl c...
  • Page 81 not used ld e,d bit 3,d adc hl,de not used ld e,e bit 3,e ld de,(NN) not used ld e,h bit 3,h not used ld e,l bit 3,l not used ld e,(hl) bit 3,(hl) im 2 not used ld e,a bit 3,a ld a,r not used...
  • Page 82 inverse " adc a,e res 1,e inverse £ adc a,h res 1,h inverse $ adc a,l res 1,l inverse : adc a,(hl) res 1,(hl) inverse ? adc a,a res 1,a inverse ( sub b res 2,b inverse ) aub c res 2,c inverse >...
  • Page 83 inverse W cp h res 7,h inverse X cp l res 7,l inverse Y cp (hl) res 7,(hl) inverse Z cp a res 7,a "" ret nz set 0,b pop bc set 0,c jp nz,NN set 0,d not used jp NN set 0,e CODE call nz,NN...
  • Page 84 set 5,l GOSUB INPUT xor N set 5,(hl) rst 40 set 5,a LOAD ret p set 6,b LIST pop af set 6,c PAUSE jp p,NN set 6,d set 6,e NEXT call p,NN set 6,h POKE PRINT push af set 6,l or N set 6,(hl) PLOT...
  • Page 85 Appendix B - Report codes This table gives each report code with a general description & a list of the situations where it can occur. In appendix C, a more detailed description of what error reports mean is given under each statement or function.
  • Page 86 Appendix C - The ZX81 for those that understand BASIC General If you already know BASIC then you should not have much trouble using the ZX81; but it has one or two idiosyncrasies. (i) Words are not spelled out, but have keys of their own - this is dealt with in chapter 2 (for keywords &...
  • Page 87 Numbers are stored to an accuracy of 9 or 10 digits. The largest number you can get is about 10 , & the smallest (positive) number is about 4 * 10 A number is stored in the ZX81 in floating point binary with one exponent byte e (1 255), & four e-128 mantissa bytes m (½...
  • Page 88 In (a), suppose the string expression has the value s$. If the slicer is empty, then the result is s$ considered as a substring of itself. If the slicer is a numerical expression with value m, then the result is the mth character of s$ (a substring of length 1).
  • Page 89 number (in radians) Tangent number Calls the machine code subroutine whose starting address is x (rounded to the nearest integer). On return, the result is the contents of the bc register pair. Error B if x is not in the range 0 to 65535. string Evaluates x (without its bounding quotes) as a numerical expression.
  • Page 90 Deletes all variables, freeing the space they occupied. CLEAR (Clear Screen) Clears the display file. See chapter 27 concerning the display file. Suppose p/q was the last report with a non-zero. Then CONT has the effect GOTO q if p CONT 9, GOTO q+1 if p = 9 ( STOP statement) COPY...
  • Page 91 LPRINT ... Like PRINT , but using the printer instead of the television. A line of text is sent to the printer. (i) when printing spills over from one line to the next, (ii) after an LPRINT statement that does not end in a comma or a semicolon, (iii) when a comma or TAB item requires a new line, or (iv) at the end of the program, if there is anything left unprinted.
  • Page 92 PRINT position on at least one place, & after that, however many as are necessary to leave it in column 0 or 16, throwing a new line if necessary. At the end of the PRINT statement, if it does not end in a semicolon or comma, a new line is thrown.

Table of Contents