APPENDIX A The character set This is the complete Spectrum character set, with codes in decimal and hex. If one imagines the codes as being Z80 machine code instructions, then the right hand columns give the corresponding assembly language mnemonics. As you are probably aware if you understand these things, certain Z80 instructions are compounds starting with CBh or EDh; the two right hand columns give these. Code Character Hex Z80 Assembler after CB after ED 0 } 00 nop rlc b 1 } 01 ld bc,NN rlc c 2 } Not used 02 ld (bc),a rlc d 3 } 03 inc bc rlc e 4 } 04 inc b rlc h 5 } 05 dec b rlc l 6 PRINT comma 06 ld b,N rlc (hl) 7 EDIT 07 rlca rlc a 8 cursor left 08 ex af,af' rrc b 9 cursor right 09 add hl,bc rrc c 10 cursor down 0A ld a,(bc) rrc d 11 cursor up 08 dec bc rrc e 12 DELETE 0C incc rrch 13 ENTER 0D dec c rrc 14 number 0E ld c,N rrc (hl) 15 not used 0F rrca rrc a 16 INK control 10 djnz DIS rl b 17 PAPER control 11 ld de,NN rlc 18 FLASH control 12 ld (de),a rl d 19 BRIGHT control 13 inc de rl e 20 INVERSE control 14 inc d rl h 21 OVER control 15 dec d rl l 22 AT control 16 ld d,N rl (hl) 23 TAB control 17 rla rl a 24 } 18 jr DIS rr b 25 } 19 add hl,de rr c 26 } 1A ld a,(de) rr d 27 } 1B dec de rr e 28 } 1C inc e rr h 29 } 1D dec e rr l 30 } 1E ld e,N rr (hl) 31 } 1F rra rr a 32 space 20 jr nz, DlS sla b 33 ! 21 ld hl,NN sla c 34 " 22 ld (NN),hl sla d 35 # 23 inc hl sla e 36 $ 24 inc h sla h 37 % 25 dec h sla i 38 & 26 ld h,N sla (hl) 39 , 27 daa sla a 40 ( 28 jr z,DlS sra b 41 ) 29 add hl,hl sra c 42 * 2A ld hl,(NN) sra d 43 + 2B dec hl sra e 44 , 2C inc ll sra h 45 - 2D dec l sra l 46 . 2F ld l,N sra (hl) 47 / 2F cpl sra a 48 0 30 jr nc,DlS 49 1 31 ld sp,NN 50 2 32 ld (NN),a 51 3 33 inc sp 52 4 34 inc (hl) 53 5 35 dec (hl) 54 6 36 ld (hl),N 55 7 37 scf 56 8 38 lr c,DlS srl b 57 9 39 add hl,sp srl c 58 : 3A ld a,(NN) srl d 59 ; 3B dec sp srl e 60 < 3C inc a srl h 61 = 3D dec a srl l 62 > 3E ld a,N srl (hl) 63 ? 3F ccf srl a 64 @ 40 ld b,b bit 0,b in b,(c) 65 A 41 ld b,c bit 0,c out (c),b 66 B 42 ld b,d bit 0,d sbc hl,bc 67 C 43 ld b,e bit 0,e ld (NN),bc 68 D 44 ld b,h bit 0,h neg 69 E 45 ld b,l bit 0,1 retn 70 F 46 ld b,(hl) bit 0,(hl) im 0 71 G 47 ld b,a bit 0,a ld i,a 72 H 48 ld c,b bit 1,b in c,(c) 73 I 49 ld cc bit 1,c out (c),c 74 J 4A ld c,d bit i,d adc hl,bc 75 K 4B ld c,e bit 1,e ld bc,(NN) 76 L 4C ld c,h bit 1,h 77 M 4D ld c,l bit 1,l reti 78 N 4E ld c,(hl) bit 1,(hl) 79 O 4F ld c,a bit 1,a ld r,a 80 P 50 ld d,b bit 2,b in d,(c) 81 Q 51 ld d,c bit 2,c out (c),d 82 R 52 ld d,d bit 2,d sbc hl,de 83 S 53 ld d,e bit 2,e ld (NN),de 84 T 54 ld d,h bit 2,h 85 U 55 ld d,l bit 2,l 86 V 56 ld d,(hl) bit 2,(hl) im 1 87 W 57 ld d,a bit 2,a ld a,i 88 X 58 ld e,b bit 3,b in e,(c) 89 Y 59 ld e,c bit 3,c out (c),e 90 Z 5A ld e,d bit 3,d adc hl,de 91 [ 5B ld e,e bit 3,e ld de,(NN) 92 / 5C ld e,h bit 3,h 93 ] 5D ld e,l bit 3,l 94 ^ 5E ld e,(hl) bit 3,(hl) im 2 95 _ 5F ld e,a bit 3,a ld a,r 96 œ 60 ld h,b bit 4,b in h,(c) 97 a 61 ld h,c bit 4,c out (c),h 98 b 62 ld h,d bit 4,d sbc hl,hl 99 c 63 ld h,e bit 4,e ld (NN),hl 100 d 64 ld h,h bit 4,h 101 e 65 ld h,l bit 4,1 102 f 66 ld h,(hl) bit 4,(hl) 103 g 67 ld h,a bit 4,a rrd 104 h 68 ld l,b bit 5,b in l,(c) 105 i 69 ld l,c bit 5,c out (c),l 106 j 6A ld l,d bit 5,d adc hl,hl 107 k 6B ld l,e bit 5,e ld hl,(NN),sp 108 l 6C ld l,h bit 5,h 109 m 6D ld l,l bit 5,l 110 n 6E ld l,(hl) bit 5,(hl) 111 o 6F ld l,a bit 5,a rld 112 p 70 ld (hl),b bit 6,b in f,(c) 113 q 71 ld (hl),c bit 6,c 114 r 72 ld (hl),d bit 6,d sbc hl,sp 115 s 73 ld (hl),e bit 6,e ld (NN),sp 116 t 74 ld (hl),h bit 6,h 117 u 75 ld (hl),l bit 6,l 118 v 76 halt bit 6,(hl) 119 w 77 ld (hl),a bit 6,a 120 x 78 ld a,b bit 7,b in a,(c) 121 y 79 ld a,c bit 7,c out (c),a 122 z 7A ld a,d bit 7,d adc hl,sp 123 { 7B ld a,e bit 7,e ld sp,(NN) 124 | 7C lda,h bit 7,h 125 } 7D ld al bit 7,l 126 - 7E ld a,(hl) bit 7,(hl) 127 (C) 7F ld a,a bit 7,a 128 80 add a,b res 0,b 129 81 add a,c res 0,c 130 82 add a,d res 0,d 131 83 add a,e res 0,e 132 84 add a,h res 0,h 133 85 add a,l res 0,l 134 86 add a,(hl) res 0,(hl) 135 87 add a,a res 0,a 136 88 adc a,b res 1,b 137 89 adc a,c res 1,c 138 8A adc a,d res 1,d 139 8B adc a,e res 1,e 140 8C adc a,h res 1,h 141 8D adc a,l res 1,i 142 8E adc a,(hl) res 1,(hl) 143 8F adc a,a res 1,a 144 (a) 90 sub b res 2,b 145 (b) 91 sub c res 2,c 146 (c) 92 sub d res 2,d 147 (d) 93 sub e res 2,e 148 (e) 94 sub h res 2,h 149 (f) 95 sub l res 2,l 150 (g) 96 sub (hl) res 2,(hl) 151 (h) 97 sub a res 2,a 152 (i) 98 sbc a,b res 3,b 153 (j) 99 sbc a,c res 3,c 154 (k) user 9A sbc a,d res 3,d 155 (l) graphics 9B sbc a,e res 3,e 156 (m) 9C sbc a,h res 3,h 157 (n) 9D sbc a,l res 3,l 158 (o) 9E sbc a,(hl) res 3,(hl) 159 (p) 9F sbc a,a res 3,a 160 (q) A0 and b res 4,b ld 161 (r) A1 and c res 4,c cpi 162 (s) A2 and d res 4,d ini 163 (t) A3 and e res 4,e outi 164 (u) A4 and h res 4,h 165 RND A5 and l res 4,l 166 INKEY$ A6 and (hl) res 4,(hl) 167 PI A7 and a res 4,a 168 FN A8 xor b res 5,b ldd 169 POINT A9 xor c res 5,c cpd 170 SCREEN$ AA xor d res 5,d ind 171 ATTR AB xor e res 5,e outd 172 AT AC xor h res 5,h 173 TAB AD xor l res 5,i 174 VAL$ AE xor (hl) res 5,(hl) 175 CODE AF xor a res 5,a 176 VAL B0 or b res 6,b ldir 177 LEN B1 or c res 6,c cpir 178 SIN B2 or d res 6,d inir 179 COS B3 or e res 6,e otir 180 TAN B4 or h res 6,h 181 ASN B5 or l res 6,l 182 ACS B6 or (hl) res 6,(hl) 183 ATN B7 or a res 6,a 184 LN B8 cp b res 7,b lddr 185 EXP B9 cp c res 7,c cpdr 186 INT BA cp d res 7,d indr 187 SOR BB cp e res 7,e otdr 188 SGN BC cp h res 7,h 189 ABS BD cp l res 7,l 190 PEEK BE cp (hl) res 7,(hl) 191 IN BF cp a res 7,a 192 USR C0 ret nz set 0,b 193 STR$ C1 pop bc set 0,c 194 CHR$ C2 jp nz,NN set 0,d 195 NOT C3 jp NN set 0,e 196 BIN C4 call nz,NN set 0,h 197 OR C5 push bc set 0,l 198 AND C6 add a,N set 0,(hl) 199 <= C7 rst 0 set 0,a 200 >= C8 ret z set 1,b 201 <> C9 ret set l,c 202 LINE CA jp z,NN set l,d 203 THEN CB set l,e 204 TO CC call z,NN set l,h 205 STEP CD call NN set 1,l 206 DEF FN CE adc a,N set 1,(hl) 207 CAT CF rst B set 1,a 208 FORMAT D0 ret nc set 2,b 209 MOVE D1 pop de set 2,c 210 ERASE D2 jpnc,NN set 2,d 211 OPEN # D3 out (N),a set 2,e 212 CLOSE # D4 call nc,NN set 2,h 213 MERGE D5 push de set 2,l 214 VERIFY D6 sub N set 2,(hl) 215 BEEP D7 rst 16 set 2,a 216 CIRCLE D8 ret c set 3,b 217 INK D9 exx set 3,c 218 PAPER DA jpc,NN set 3,d 219 FLASH DB in a,(N) set 3,e 220 BRIGHT DC call c,NN set 3,h 221 INVERSE DD [prefixes set 3,l instructions using ix] 222 OVER DE sbc a,N set 3,(hl) 223 OUT DF rst 24 set 3,a 224 LPRINT E0 ret po set 4,b 225 LLIST E1 pop hl set 4,c 226 STOP E2 jp po,NN set 4,d 227 READ E3 ex (sp),hl set 4,e 228 DATA E4 call po,NN set 4,h 229 RESTORE E5 push hl set 4,l 230 NEW E6 and N set 4,(hl) 231 BORDER E7 rst 32 set 4,a 232 CONTINUE E8 ret pe set 5,b 233 DIM E9 jp (hl) set 5,c 234 REM EA jp pe,NN set 5,d 235 FOR EB ex de,hl set 5,e 236 GO TO EC call pe,NN set 5,h 237 GO SUB ED set 5,l 238 INPUT EE xor N set 5,(hl) 239 LOAD EF rst 40 set 5,a 240 LIST F0 ret p set 6,b 241 LET Fl pop af set 6,c 242 PAUSE F2 jp p,NN set 6,d 243 NEXT F3 di set 6,e 244 POKE F4 call p,NN set 6,h 245 PRINT F5 push af set 6,l 246 PLOT F6 or N set 6,(hl) 247 RUN F7 rst 48 set 6,a 248 SAVE F8 ret m set 7,b 249 RANDOMIZE F9 ld sp,hl set 7,c 250 IF FA jp m,NN set 7,d 251 CLS FB ei set 7,e 252 DRAW FC call m,NN set 7,h 253 CLEAR FD [prefixes set 7,l instructions using iy] 254 RETURN FE cp N set 7,(hl) 255 COPY FF rst 56 set 7,a APPENDIX B Reports These appear at the bottom of the screen whenever the computer stops executing some BASIC, and explain why it stopped, whether for a natural reason, or because an error occurred. The report has a code number or letter so that you can refer to the table here, a brief message explaining what happened and the line number and statement number within that line where it stopped. (A command is shown as line 0. Within a line, statement 1 is at the beginning, statement 2 comes after the first colon or THEN, and so on.) The behaviour of CONTINUE depends very much on the reports. Normally, CONTINUE goes to the line and statement specified in the last report, but there are exceptions with reports 0, 9 and D (also see Appendix C). Here is a table showing all the reports. It also tells you in what circumstances the report can occur, and this refers you to Appendix C. For instance error A Invalid argument can occur with SQR, IN, ACS and ASN and the entries for these in Appendix C tell you exactly what arguments are invalid. Code Meaning Situations 0 0K Any Successful completion, or jump to a line number bigger than any existing. This report does not change the line and statement jumped to by CONTINUE. 1 NEXT without FOR NEXT The control variable does not exist (it has not been set up by a FOR statement), but there is an ordinary variable with the same name. 2 Variable not found Any For a simple variable this will happen if the variable is used before it has been assigned to in a LET, READ or INPUT statement or loaded from tape or set up in a FOR statement. For a subscripted variable it will happen if the variable is used before it has been dimensioned in a DIM statement or loaded from tape. 3 Subscript wrong Subscripted variables, Substrings A subscript is beyond the dimension of the array, or there are the wrong number of subscripts. If the subscript is negative or bigger than 65535, then error B will result. 4 Out of memory LET, INPUT, FOR, DIM, GO SUB, LOAD, MERGE. Sometimes during expression evaluation. There is not enough room in the computer for what you are trying to do. If the computer really seems to be stuck in this state, you may have to clear out the command line using DELETE and then delete a program line or two (with the intention of putting them back afterwards) to give yourself room to manoeuvre with - say - CLEAR. 5 Out of screen INPUT, PRINT AT An INPUT statement has tried to generate more than 23 lines in the lower half of the screen. Also occurs with PRINT AT 22, . . . 6 Number too big Any arithmetic Calculations have led to a number greater than about 1038. 7 RETURN without GO SUB RETURN There has been one more RETURN than there were GO SUBs. 8 End of file Microdrive, etc, operations [undocumented] 9 STOP statement STOP After this, CONTINUE will not repeat the STOP, but carries on with the statement after. A Invalid argument SQR, LN, ASN, ACS, USR (with string argument) The argument for a function is no good for some reason. B Integer out of range RUN, RANDOMIZE, POKE, DIM, GO TO, GO SUB, LIST, LLIST, PAUSE, PLOT, CHR$, PEEK, USR (with numeric argument), Array access When an integer is required, the floating point argument is rounded to the nearest integer. If this is outside a suitable range then error B results. For array access, see also Error 3. C Nonsense in BASIC VAL, VAL$ The text of the (string) argument does not form a valid expression. D BREAK - CONT repeats LOAD, SAVE, VERIFY, MERGE, LPRINT, LLIST, COPY. Also when the computer asks scroll? and you type N, SPACE or STOP. BREAK was pressed during some peripheral operation. The behaviour of CONTINUE after this report is normal in that it repeats the statement. Compare with report L. E Out of DATA READ You have tried to READ past the end of the DATA list. F Invalid file name SAVE SAVE with name empty or longer than 10 characters . G No room for line Entering a line into the program There is not enough room left in memory to accommodate the new program line. H STOP in INPUT INPUT Some INPUT data started with STOP, or - for INPUT LINE - was pressed. Unlike the case with report 9, after report H CONTINUE will behave normally, by repeating the INPUT statement. I FOR without NEXT FOR There was a FOR loop to be executed no times (e.g. FOR n= 1 TO 0) and the corresponding NEXT statement could not be found. J Invalid I/O device Microdrive, etc, operations [undocumented] K Invalid colour INK, PAPER, BORDER, FLASH, BRIGHT, INVERSE, OVER; also after one of the corresponding control characters. The number specified is not an appropriate value. L BREAK into program Any BREAK pressed, this is detected between two statements. The line and statement number in the report refer to the statement before BREAK was pressed, but CONTINUE goes to the statement after (allowing for any jumps to be done), so it does not repeat any statements. M RAMTOP no good CLEAR; possibly in RUN The number specified for RAMTOP is either too big or too small. N Statement lost RETURN, NEXT, CONTINUE Jump to a statement that no longer exists. O Invalid stream Microdrive,etc, operations [undocumented] P FN without DEF FN User-defined function Q Parameter error FN Wrong number of arguments, or one of them is the wrong type (string instead of number or vice versa). R Tape loading error VERIFY LOAD or MERGE A file on tape was found but for some reason could not be read in, or would not verify. APPENDIX C (Part 1) A description of the ZX Spectrum for reference The first section of this appendix is a repeat of that part of the Introduction concerning the keyboard and screen. The keyboard ZX Spectrum characters comprise not only the single symbols (letters, digits, etc), but also the compound tokens (keywords, function names, etc) and all these are entered trom the keyboard rather than being spelled out. To obtain all these functions and commands some keys have five or more distinct meanings, given partly by shifting the keys (i.e. pressing either the CAPS SHIFT key or the SYMBOL SHIFT key at the same time as the required one) and partly by having the machine in different modes. The mode is indicated by the cursor, a flashing letter that shows where the next character from the keyboard will be inserted. K (for keywords) mode automatically replaces L mode when the machine is expecting a command or program line (rather than INPUT data), and from its position on the line it knows it should expect a line number or a keyword. This is at the beginning of the line, or just after THEN, or just after: (except in a string). If unshifted, the next key will be interpreted as either a keyword (written on the keys), or a digit. L (for letters) mode normally occurs at all other times. If unshifted, the next key will be interpreted as the main symbol on that key, in lower case for letters. In both K and L modes, SYMBOL SHIFT and a key will be interpreted as the subsidiary red character on the key and CAPS SHIFT with a digit key will be interpreted as the control function written in white above the key. CAPS SHIFT with other keys does not affect the keywords in K mode, and in L mode it converts lower case to capitals. C (for capitals) mode is a variant of L mode in which all letters appear as capitals. CAPS LOCK causes a change from L mode to C mode or back again. E (for extended) mode is used for obtaining further characters, mostly tokens. It occurs after both shift keys are pressed together, and lasts for one key depression only. In this mode, a letter gives one character or token (shown in green above it) if unshifted, and another (shown in red below it) if pressed with either shift. A digit key gives a token if pressed with SYMBOL SHIFT; otherwise it gives a colour control sequence. G (for graphics) mode occurs after GRAPHICS (CAPS SHIFT and 9) is pressed, and lasts until it is pressed again. A digit key will give a mosaic graphic, quit GRAPHICS or DELETE, and each of the letter keys apart from V, W, X, Y and Z, will give a user-defined graphic. If any key is held down for more than about 2 or 3 seconds, it will start repeating. Keyboard input appears in the bottom half of the screen as it is typed, each character (single symbol or compound token) being inserted just before the cursor. The cursor can be moved left with CAPS SHIFT and 5, or right with CAPS SHIFT and 8. The character before the cursor can be deleted with DELETE (CAPS SHIFT and 0). (Note: the whole line can be deleted by typing EDIT (CAPS SHIFT and 1) followed by ENTER.) When ENTER is pressed, the line is executed, entered into the program, or used as INPUT data as appropriate, unless it contains a syntax error. In this case a flashing ? appears next to the error. As program lines are entered, a listing is displayed in the top half of the screen. The manner in which the listing is produced is rather complicated, and explained more fully in Chapter 2. The last line entered is called the current line and is indicated by the symbol >, but this can be changed by using the keys ê (CAPS SHIFT and 6) and é (CAPS SHIFT and 7). If EDIT (CAPS SHIFT and 1) is pressed, the current line is brought down to the bottom part of the screen and can be edited. When a command is executed or a program run, output is displayed in the top half of the screen and remains until a program line is entered, or ENTER is pressed with an empty line, or é or ê is pressed. In the bottom part appears a report giving a code (digit or letter) referring you to Appendix B, a brief verbal summary of what Appendix B says, the number of the line containing the last statement executed (or 0 for a command) and the position of the statement within the line. The report remains on the screen until a key is pressed (and indicates K mode). In certain circumstances, CAPS SHIFT with the SPACE key acts as a BREAK, stopping the computer with report D or L. This is recognised (i) at the end of a statement while a program is running, or (ii) while the computer is using the cassette recorder or printer. The television screen This has 24 lines, each 32 characters long, and is divided into two parts. The top part is at most 22 lines and displays either a listing or program output. When printing in the top part has reached the bottom, it all scrolls up one line; if this would involve losing a line that you have not had a chance to see yet, then the computer stops with the message scroll?. Pressing the keys N, SPACE or STOP will make the program stop with report D BREAK- CONT repeats; any other key will let the scrolling continue. The bottom part is used for inputting commands, program lines, and INPUT data, and also for displaying reports. The bottom part starts of as two lines (the upper one blank), but it expands to accommodate whatever is typed in. When it reaches the current print position in the top half, further expansions will make the top half scroll up. Each character position has attributes specifying its paper (background) and ink (foreground) colours, a two-level brightness, and whether it flashes or not. The available colours are black, blue, red, magenta, green, yellow and white. The edge of the screen can be set to any of the colours using the border statement. A character position is divided into 8x8 pixels and high resolution graphics are obtained by setting the pixels individually to show either the ink or paper colour for that character position. The attributes at a character position are adjusted whenever a character is written there or a pixel is plotted The exact manner of the adjustment is determined by the pnnting parameters. of which there are two sets (called permanent and temporary) of six: the PAPER, INK, FLASH, BRIGHT, INVERSE and OVER parameters. Permanent parameters for the top part are set up by PAPER, INK, etc, statements, and last until further notice. (Initially they are black ink on white paper. With normal brightness, no flashing, normal video and no overprinting). Permanent parameters for the bottom part use the border colour as the paper colour, with a black or white contrasting ink colour, normal brightness, no flashing, normal video and no overprinting. Temporary parameters are set up by PAPER, INK, etc, items, which are embedded in PRINT, LPRINT, INPUT, PLOT, DRAW and CIRCLE statements, and also by PAPER, INK, etc control characters when they are printed to the television - they are followed by a further byte to specify the parameter value. Temporary parameters last only to the end of the PRINT (or whatever) statement, or, in INPUT statements, until some INPUT data is needed from the keyboard, when they are replaced by the permanent parameters. PAPER and INK parameters are in the range 0 to 9. Parameters 0 to 7 are the colours used when a character is printed: 0 black 1 blue 2 red 3 magenta 4 green 5 cyan 6 yellow 7 white Parameter 8 ('transparent') specifies that the colour on the screen is to be left unchanged when a character is printed. Parameter 9 ('contrast') specifies that the colour in question (paper or ink) is to be made either white or black to show up against the other colour. FLASH and BRIGHT parameters are 0,1 or 8: 1 means that flashing or brightness is turned on, 0 that it is turned off, and 8 ('transparent') that it is left unchanged at any character position. OVER and INVERSE parameters are 0 or 1: OVER 0 new characters obliterate old ones OVER 1 the bit patterns of the old and new characters are combined using an 'exclusive or' operation (overprinting) INVERSE 0 new characters are printed as ink colour on paper colour (normal video) INVERSE 1 new characters are printed as paper colour on ink colour (inverse video) When a TAB control character is received by the television, two more bytes are expected to specify a tab stop n (less significant byte first). This is reduced modulo 32 to n0 (say), and then sufficient spaces are printed to move the printing position into column n0. When a comma control character is received, then sufficient spaces (at least one) are printed to move the printing position into column 0 or column 16. When an ENTER control character is received, the printing position is moved on to the next line. The printer Output to the ZX printer is via a buffer one line (32 characters) long, and a line is sent to the printer (i) when printing spills over from one line to the next, (ii) when an ENTER character is received, (iii) at the end of the program, if there is anything left unprinted, (iv) when a TAB control or comma control moves the printing position on to a new line. TAB controls and comma controls output spaces in the same way as on the television. The AT control changes the printing position using the column number, and ignores the line number. The printer is affected by INVERSE and OVER controls (and also statements) in the same way as the screen is, but not by PAPER, INK, FLASH or BRIGHT. The printer will stop with error B if BREAK is pressed. If the printer is absent the output will simply be lost. APPENDIX C (Part 2) The BASIC Numbers are stored to an accuracy of 9 or 10 digits. The largest number you can get is about 1038, and the smallest (positive) number is about 4*10-39. A number is stored in the ZX Spectrum in floating point binary with one exponent byte e (l<=e<=255), and four mantissa bytes m (½<=m0 Numeric left operand: A AND B = { 0 if B=0 Numeric right operand: A$ AND B = { A$ if B<>0 { A$ if B=0 ASN number Arcsine in radians. Error A if x not in the range -1 to +1 ATN number Arctangent in radians ATTR two arguments, x and A number whose binary form codes the y, both numbers; attributes of line x, column y on the television. enclosed in brackets Bit 7 (most significant) is 1 for flashing, 0 for not flashing. Bit 6 is 1 for bright, 0 for normal. Bits 5 to 3 are the paper colour. Bits 2 to 0 are the ink colour. Error B unless 0<=x<=23 and 0<=y<=31 BIN This is not really a function, but an alternative notation for numbers: BIN followed by a sequence of 0s and 1s is the number with such a representation in binary. CHR$ number The character whose code is x, rounded to the nearest integer CODE string The code of the first character in x (or 0 if x is the empty string) COS number (in radians) Cosine x EXP number ex FN FN followed by a letter calls up a user-defined function (see DEF). The arguments must be enclosed in brackets; even if there are no arguments the brackets must still be present. IN number The result of inputting at processor level from port x (0<=x<=FFFFh) (loads the bc register pair with x and does the assembly language instruction in a(c)) INKEY$ none Reads the keyboard. The result is the character representing (in L or C mode) the key pressed if there is exactly one, else the empty string. INT number Integer part (always rounds down) LEN string Length LN number Natural logarithm (to base e). Error A if x<=0 NOT number 0 if x<>0, 1 if x=0. NOT has priority 4 OR binary operation, a OR b= { 1 if b<>0 both operands { a if b=0 numbers OR has priority 2 PEEK number The value of the byte in memory whose address is x (rounded to the nearest integer). Error B if x is not in the range 0 to 65535 PI none p (3.14159265 . . . ) POINT Two arguments, x and 1 if the pixel at (x,y) is ink colour. 0 if it is paper y, both numbers; colour. enclosed in brackets Error B unless 0<=x<=255 and 0<=y<= 175 RND none The next pseudorandom number in a sequence generated by taking the powers of 75 modulo 65537, subtracting 1 and dividing by 65536. 0£y< 1 SCREEN$ Two arguments, x and The character that appears, either normally or y, both numbers; inverted, on the television at line x, column y. enclosed in brackets Gives the empty string, if the character is not recognised . Error B unless 0<=x<=23 and 0<=y<=31 SGN number Signum: the sign (-1 for negative, 0 for zero or +1 for positive) of x SIN number (in radians) Sine x SQR number Square root. Error A if x<0 STR$ number The string of characters that would be displayed if x were printed TAN number (in radians) Tangent USR number Calls the machine code subroutine whose starting address is x. On return, the result is the contents of the bc register pair USR string The address of the bit pattern for the user- defined graphic corresponding to x. Error A if x is not a single letter between a and u, or a user-defined graphic VAL string Evaluates x (without its bounding quotes) as a numerical expression. Error C if x contains a syntax error, or gives a string value. Other errors possible, depending on the expression VAL$ string Evaluates x (without its bounding quotes) as a string expression. Error C if x contains a syntax error or gives a numeric value. Other errors possible, as for VAL - number Negation The following are binary operations: + Addition (on numbers), or concatenation (on strings) - Subtraction * Multiplication / Division ­ Raising to a power. Error B if the left operand is negative = Equals > Greater than } Both operands must be of the < Less than } same type. The result is a <= Less than or equal to } number 1, if the comparison >= Greater than or equal to } holds and 0 if it does not <> Not equal to } Functions and operations have the following priorities: Operation Priority Subscripting and slicing 12 All functions except NOT and unary minus 11 ­ 10 Unary minus (i.e. minus just used to negate something) 9 *, / 8 +, - (minus used to subtract one number from another) 6 =, >, <, <=, >=, <> 5 NOT 4 AND 3 OR 2 Statements In this list, a represents a single letter v represents a variable x, y, z represent numerical expressions m, n represent numerical expressions that are rounded to the nearest integer e represents an expression f represents a string valued expression s represents a sequence of statements separated by colons c represents a sequence of colour items, each terminated by commas, or semi-colons; a colour item has the form of a PAPER, INK, FLASH, BRIGHT, INVERSE or OVER statement. Note that arbitrary expressions are allowed everywhere (except for the line number at the beginning of a statement). All statements except INPUT, DEF and DATA can be used either as commands or in programs (although they can be more sensible in one than the other). A command or program line can have several statements, separated by colons (:). There is no restriction on whereabouts in a line any particular statement can occur - although see IF and REM. BEEP x, y Sounds a note through the loudspeaker for x seconds at a pitch y semitones above middle C (or below if y is negative). BORDER m Sets the colour of the border of the screen and also the paper colour for the lower part of the screen. Error K is m not in the range 0 to 7. BRIGHT Sets brightness of characters subsequently printed. n=0 for normal, 1 for bright. 8 for transparent. Error K if n not 0, 1 or 8 CAT Does not work without Microdrive, etc CIRCLE x, y, z Draws an arc of a circle, centre (x,y), radius z CLEAR Deletes all variables, freeing the space they occupied. Does RESTORE and CLS, resets the PLOT position to the bottom left-hand corner and clears the GO SUB stack CLEAR n Like CLEAR. but if possible changes the system variable RAMTOP to n and puts the new GO SUB stack there CLOSE # Does not work without Microdrive, etc CLS (Clear Screen). Clears the display file CONTINUE Continues the program, starting where it left off last time it stopped with report other than 0. If the report was 9 or L, then continues with the following statement (taking jumps into account); otherwise repeats the one where the error occurred. If the last report was in a command line then CONTINUE will attempt to continue the command line and will either go into a loop if the error was in 0:1, give report 0 if it was in 0: 2, or give error N if it was 0: 3 or greater. CONTINUE appears as CONT on the keyboard COPY Sends a copy of the top 22 lines of display to the printer, if attached; otherwise does nothing. Note that COPY can not be used to print the automatic listings that appear on the screen. Report D if BREAK pressed DATA a1 , a2 , a3 , ... Part of the DATA list. Must be in a program DEF FN a (a1 . . . ak )=e User-defined function definition; must be in a program. Each of a and a1 to ak is either a single letter or a single letter followed by '$' for string argument or result. Takes the form DEF FN a()=e if no arguments DELETE f Does not work without Microdrive, etc DIM a( n1 , . . . ,nk ) Deletes any array with the name a, and sets up an array a of numbers with k dimensions n1 , ... ,nk. Initialises all the values to 0 DIM a$( n1 , . . . ,nk ) Deletes any array or string with the name a$, and sets up an array of characters with k dimensions nl ,...,nk. Initialises all the values to " ". This can be considered as an array of strings of fixed length nk , with k-l dimensions nl,...,nk-l . Error 4 occurs if there is no room to fit the array in. An array is undefined until it is dimensioned in a DIM statement DRAW x,y DRAW x,y,0 DRAW x,y,z Draws a line from the current plot position moving x horizontally and y vertically relative to it while turning through an angle z. Error B if it runs off the screen ERASE Does not work without Microdrive, etc. FLASH Defines whether characters will be flashing or steady. n=0 for steady, n=l for flash, n=8 for no change. FOR a=x TO y FOR a=x TO y STEP 1 FOR a=x TO y STEP z Deletes any simple variable a and sets up a control variable with value x, limit y, step z, and looping address referring to the statement after the FOR statement. Checks if the initial value is greater (if step>=0) or less (if step<0) than the limit, and if so then skips to statement NEXT a, giving error 1 if there is none. See NEXT. Error 4 occurs if there is no room for the control variable. FORMAT f Does not work without the Microdrive, etc GOSUB n Pushes the line number of the GOSUB statement onto a stack; then as GO TO n. Error 4 can occur if there are not enough RETURNs GO TO n Jumps to line n (or, if there is none, the first line after that) IF x THEN s If x true (non-zero) then s is executed. Note that s comprises all the statements to the end of the line. The form 'IF x THEN line number' is not allowed. INK n Sets the ink (foreground) colour of characters subsequently printed. n is in the range 0 to 7 for a colour, n=8 for transparent or 9 for contrast. See The television screen - Appendix B. Error K if n not in the range 0 to 9. INPUT The ' . . . ' is a sequence of INPUT items, separated as in a PRINT statement by commas, semicolons or apostrophes. An INPUT item can be (i) Any PRINT item not beginning with a letter (ii) A variable name, or (iii) LINE, then a string type variable name. The PRINT items and separators in (i) are treated exactly as in PRINT, except that everything is printed in the lower part of the screen. For (ii) the computer stops and waits for input of an expression from the keyboard; the value of this is assigned to the variable. The input is echoed in the usual way and syntax errors give the flashing ?. For string type expressions, the input buffer is initialised to contain two string quotes (which can be erased if necessary). If the first character in the input is STOP, the program stops with error H. (iii) is like (ii) except that the input is treated as a string literal without quotes, and the STOP mechanism doesn't work; to stop it you must type ê instead. INVERSE n Controls inversion of characters subsequently printed. If n=0, characters are printed in normal video, as ink colour on paper colour. If n=1, characters are printed in inverse video, i.e. paper colour on ink colour. See The television screen - Appendix B. Error K if n is not 0 or 1 LET v=e Assigns the value of e to the variable v. LET cannot be omitted. A simple variable is undefined until it is assigned to in a LET, READ or INPUT statement. If v is a subscripted string variable, or a sliced string variable (substring), then the assignment is Procrustean (fixed length): the string value of e is either truncated or filled out with spaces on the right, to make it the same length as the variable v LIST LIST 0 LIST n Lists the program to the upper part of the screen, starting at the first line whose number is at least n, and makes n the current line LLIST LLIST 0 LLIST n Like LIST, but using the printer LOAD f Loads program and variables LOAD f DATA () Loads a numeric array LOAD f DATA $() Loads character array $ LOAD f CODE m,n Loads at most n bytes, starting at address m LOAD f CODE m Loads bytes starting at address m LOAD f CODE Loads bytes back to the address they were saved from. LOAD f SCREEN$ LOAD f CODE 16384,6912. Searches for file of the right sort on cassette tape and loads it, deleting previous versions in memory. See Chapter 20 LPRINT Like PRINT but using the printer MERGE f Like LOAD f. but does not delete old program lines and variables except to make way for new ones with the same line number or name. MOVE f1,f2 Does not work without the Microdrive, etc NEW Starts the BASIC system off anew, deleting program and variables, and using the memory up to and including the byte whose address is in the system variable RAMBOT and preserves the system variables UDG, P RAMT, RASP and PIP NEXT a (i) Finds the control variable (ii) Adds its step to its value (iii) If the step>=0 and the value>the limit; or if the step<0 and the value=1013, then it is printed using scientific notation. The mantissa part has up to eight digits (with no trailing zeros), and the decimal point (absent if only one digit) is after the first. The exponent part is E, followed by + or -, followed by one or two digits. Otherwise x is printed in ordinary decimal notation with up to eight significant digits, and no trailing zeros after the decimal point. A decimal point right at the beginning is always followed by a zero, so for instance .03 and 0.3 are printed as such. 0 is printed as a single digit 0. (iii) a string expression The tokens in the string are expanded, possibly with a space before or after. Control characters have their control effect. Unrecognized characters print as ?. (iv) AT m,n Outputs an AT control character followed by a byte for m (the line number) and a byte for n (the column number). (v) TAB n Outputs a TAB control character followed by two bytes for n (less significant byte first), the TAB stop. (vi) A colour item, which takes the form of a PAPER, INK, FLASH, BRIGHT, INVERSE or OVER statement RANDOMIZE RANDOMIZE 0 RANDOMIZE n Sets the system variable (called SEED) used to generate the next value of RND. If n<>0, SEED is given the value n; if n=0 then it is given the value of another system variable (called FRAMES) that counts the frames so far displayed on the television, and so should be fairly random. RANDOMIZE appears as RAND on the keyboard. Error B occurs if n is not in the range 0 to 65535 READ vl , v2 , . . . vk Assigns to the variables using successive expressions in the DATA list. Error C if an expression is the wrong type. Error E if there are variables left to be read when the DATA list is exhausted REM . . . No effect. ' . . . ' can be any sequence of characters except ENTER. This can include : , so no statements are possible after the REM statement on the same line RESTORE RESTORE 0 RESTORE n Restores the DATA pointer to the first DATA statement in a line with number at least n: the next READ statement will start reading there RETURN Takes a reference to a statement off the GO SUB stack, and jumps to the line after it. Error 7 occurs when there is no statement reference on the stack. There is some mistake in your program GO SUBs are not properly balanced by RETURNs RUN RUN 0 RUN n CLEAR, and then GO TO n SAVE f Saves the program and variables SAVE f LINE m Saves the program and variables so that if they are loaded there is an automatic jump to line m SAVE f DATA () Saves the numeric array SAVE f DATA $() Saves the character array $ SAVE f CODE m,n Saves n bytes starting at address m SAVE f SCREEN$ SAVE f CODE 16384,6912. Saves information on cassette, giving it the name f Error F if f is empty or has length eleven or more. See Chapter 20 STOP Stops the program with report 9. CONTINUE will resume with the following statement VERIFY The same as LOAD except that the data is not loaded into RAM, but compared against what is already there. Error R if one of the comparisons shows different bytes. APPENDIX D Example programs This appendix contains some example programs to demonstrate the abilities of the ZX Spectrum. The first of these programs requires a date to be input and gives the day of the week which corresponds to this date. 10 REM convert date to day 20 DIM d$(7,6): REM days of week 30 FOR n=1 TO 7: READ d$(n): NEXT n 40 DIM m(12): REM lengths of months 50 FOR n=1 TO 12: READ m(n): NEXT n 100 REM input date 110 INPUT "day?";day 120 INPUT "month?";month 130 INPUT "year (20th century only)?";year 140 IF year<1901 THEN PRINT "20th century starts at 1901": GO TO 100 150 IF year>2000 THEN PRINT "20th century ends at 2000": GO TO 100 160 IF month<1 THEN GO TO 210 170 IF month>12 THEN GO TO 210 180 IF year/4-INT(year/4)=0 THEN LET m(2)=29: REM leap year 190 IF day>m(month) THEN PRINT "This month has only "; m(month); " days.": GO TO 500 200 IF day>0 THEN GO TO 300 210 PRINT "Stuff and nonsense. Give me a real date." 220 GO TO 500 300 REM convert date to number of days since start of century 310 LET y=year-1901 320 LET b=365*y+INT (y/4): REM number of days to start of year 330 FOR n=1 TO month 1: REM add on previous months 340 LET b=b+m(n): NEXT n 350 LET b=b+day 400 REM convert to day of week 410 LET b=b-7*INT (b/7)+1 420 PRINT day;"/";month;"/";year 430 FOR n=6 TO 3 STEP -1: REM remove trailing spaces 440 IF d$(b,n) <> " " THEN GO TO 460 450 NEXT n 460 LET e$=d$(b, TO n) 470 PRINT" is a "; e$; "day" 500 LET m(2)=28: REM restore February 510 INPUT "again?", a$ 520 IF a$="n" THEN GO TO 540 530 IF a$ <> "N" THEN GO TO 100 1000 REM days of week 1010 DATA "Mon", "Tues", "Wednes" 1020 DATA "Thurs", "Fri", "Satur", "Sun" 1100 REM lengths of months 1110 DATA 31, 28, 31, 30, 31, 30 1120 DATA 31, 31, 30, 31, 30, 31 This program handles yards, feet and inches. 10 INPUT "yards?",yd,"feet?",ft, "inches?",in 40 GO SUB 2000: REM print the values 50 PRINT '" = "; 70 GO SUB 1000: REM the adjustment 80 GO SUB 2000: REM print the adjusted values 90 PRINT 100 GOTO 10 1000 REM subroutine to adjust yd, ft, in to the normal form for yards, feet and inches 1010 LET in=36*yd+12*ft+in: REM now everything is in inches 1030 LET s=SGN in: LET in=ABS in: REM we work with in positive, holding its sign in s 1060 LET ft=INT (in/12): LET in=(in-12*ft)*s: REM now in is ok 1080 LET yd=INT (ft/3)*s: LET ft=ft*s-3*yd: RETURN 2000 REM subroutine to print yd, ft and in 2010 PRINT yd;"yd";ft;"ft";in;"in";: RETURN Here is a program to throw coins for the I Ching. (Unfortunately it produces the patterns upside down. but you might not worry about this.) 5 RANDOMIZE 10 FOR m=1 TO 6: REM for 6 throws 20 LET c=0: REM initialize coin total to 0 30 FOR n=1 TO 3: REM for 3 coins 40 LET c=c+2+INT (2*RND) 50 NEXT n 60: PRINT " "; 70 FOR n=1 TO 2: REM 1st for the thrown hexagram, 2nd for the changes 80 PRINT "___"; 90 IF c=7 THEN PRINT " "; 100 IF c=8 THEN PRINT " "; 110 IF c=6 THEN PRINT "X";: LET c=7 120 IF c=9 THEN PRINT "0";: LET c=8 130 PRINT "___ "; 140 NEXT n 150 PRINT 160 INPUT a$ 170 NEXT m: NEW To use this, type it in and run it, and then press ENTER five times to get the two hexagrams. Look these up in a copy of the Chinese Book of Changes. The text will describe a situation and the courses of action appropriate to it, and you must ponder deeply to discover the parallels between that and your own life. Press ENTER a sixth time, and the program will erase itself this is to discourage you from using it frivolously. Many people find the texts are always more apt than they would expect on grounds of chance; this may or may not be the case with your ZX Spectrum In general, computers are pretty godless creatures. Here is a program to play 'Pangolins'. You think up an animal, and the computer tries to guess what it is, by asking you questions that can be answered 'yes' or 'no'. If it's never heard of your animal before, it asks you to give it some question that it can use next time to find out whether someone's given it your new animal. 5 REM Pangolins 10 LET nq=100: REM number of questions and animals 15 DIM q$(nq,50): DIM a(nq,2): DIM r$(1) 20 LET qf=8 30 FOR n=1 TO qf/2 1 40 READ q$(n): READ a(n,1): READ a(n,2) 50 NEXT n 60 FOR n=n TO qf-1 70 READ q$(n): NEXT n 100 REM start playing 110 PRINT "Think of an animal.","Press any key to continue." 120 PAUSE 0 130 LET c=1: REM start with 1st question 140 IF a(c,1)=0 THEN GO TO 300 150 LET p$=q$(c): GO SUB 910 160 PRINT "?": GO SUB 1000 170 LET in=1: IF r$="y" THEN GO TO 210 180 IF r$="Y" THEN GO TO 210 190 LET in=2: IF r$="n" THEN GO TO 210 200 IF r$<>"N" THEN GO TO 150 210 LET c=a(c,in): GO TO 140 300 REM animal 310 PRINT "Are you thinking of" 320 LET P$=q$(c): GO SUB 900: PRINT "?" 330 GO SUB 1000 340 IF r$="y" THEN GO TO 400 350 IF r$="Y" THEN GO TO 400 360 IF r$="n" THEN GO TO 500 370 IF r$="N" THEN GO TO 500 380 PRINT "Answer me properly when I'm","talking to you.": GO TO 300 400 REM guessed it 410 PRINT "I thought as much.": GO TO 800 500 REM new animal 510 IF qf>nq-1 THEN PRINT "I'm sure your animal is very", "interesting, but I don't have","room for it just now.": GO TO 800 520 LET q$(qf)=q$(c): REM move old animal 530 PRINT "What is it, then?": INPUT q$(qf+1) 540 PRINT "Tell me a question which dist ","inguishes between " 550 LET p$=q$(qf): GO SUB 900: PRINT " and" 560 LET p$=q$(qf+1): GO SUB 900: PRINT " " 570 INPUT s$: LET b=LEN s$ 580 IF s$(b)="?" THEN LET b=b-1 590 LET q$(c)=s$(TO b): REM insert question 600 PRINT "What is the answer for" 610 LET p$=q$(qf+1): GO SUB 900: PRINT "?" 620 GO SUB 1000 630 LET in=1: LET io=2: REM answers for new and old animals 640 IF r$="y" THEN GO T0 700 650 IF r$="Y" THEN GO TO 700 660 LET in=2: LET io=1 670 IF r$="n" THEN GO TO 700 680 IF r$="N" THEN GO TO 700 690 PRINT "That's no good. ": GO TO 600 700 REM update answers 710 LET a(c,in)=qf+1: LET a(c,io)=qf 720 LET qf=qf+2: REM next free animal space 730 PRINT "That fooled me." 800 REM again? 810 PRINT "Do you want another go?": GO SUB 1000 820 IF r$="y" THEN GO TO 100 830 IF r$="Y" THEN GO TO 100 840 STOP 900 REM print without trailing spaces 905 PRINT " "; 910 FOR n=50 TO 1 STEP -1 920 IF p$(n)<>" " THEN GO TO 940 930 NEXT n 940 PRINT p$(TO n);: RETURN 1000 REM get reply 1010 INPUT r$: IF r$="" THEN RETURN 1020 LET r$=r$(1): RETURN 2000 REM initial animals 2010 DATA "Does it live in the sea",4,2 2020 DATA "Is it scaly",3,5 2030 DATA "Does it eat ants",6,7 2040 DATA "a whale", "a blancmange", "a pangolin", "an ant" Here is a program to draw a Union Flag. 5 REM union flag 10 LET r=2: LET w=7: LET b=1 20 BORDER 0: PAPER b: INK w: CLS 30 REM black in bottom of screen 40 INVERSE 1 50 FOR n=40 TO 0 STEP -8 60.PLOT PAPER 0;7,n: DRAW PAPER 0;241,0 70 NEXT n: INVERSE 0 100 REM draw in white parts 105 REM St. George 110 FOR n=0 TO 7 120 PLOT 104+n,175: DRAW 0,-35 130 PLOT 151-n,175: DRAW 0,-35 140 PLOT 151-n,48: DRAW 0,35 150 PLOT 104+n,48: DRAW 0,35 160 NEXT n 200 FOR n=0 TO 11 210 PLOT 0,139-n: DRAW 111,0 220 PLOT 255,139-n: DRAW 111,0 230 PLOT 255,84+n: DRAW -111,0 240 PLOT 0,84+n: DRAW 111,0 250 NEXT n 300 REM St. Andrew 310 FOR n=0 TO 35 320 PLOT 1+2*n,175-n: DRAW 32,0 330 PLOT 224-2*n,175-n: DRAW 16,0 340 PLOT 254-2*n,48+n: DRAW-32,0 350 PLOT 17+2*n,48+n: DRAW 16,0 360 NEXT n 370 FOR n=0 TO 19 380 PLOT 185+2*n,140+n: DRAW 32,0 390 PLOT 200+2*n,83-n: DRAW 16,0 400 PLOT 39-2*n,83-n: DRAW 32,0 410 PLOT 54-2*n,140+n: DRAW -16,0 420 NEXT n 425 REM fill in extra bits 430 FOR n=0 TO 15 440 PLOT 255,160+n: DRAW 2*n-30,0 450 PLOT 0,63-n DRAW 31-2*n,0 460 NEXT n 470 FOR n=0 TO 7 480 PLOT 0,160+n: DRAW 14 2*n,0 485 PLOT 255,63-n: DRAW 2*n-15,0 490 NEXT n 500 REM red stripes 510 INVERSE 1 520 REM St George 530 FOR n=96 TO 120 STEP 8 540 PLOT PAPER r;7,n: DRAW PAPER r;241,0 550 NEXT n 560 FOR n=112 TO 136 STEP 8 570 PLOT PAPER r;n,168: DRAW PAPER r;0,-113 580 NEXT n 600 REM St Patrick 610 PLOT PAPER r;170,140: DRAW PAPER r;70,35 620 PLOT PAPER r;179,140: DRAW PAPER r;70,35 630 PLOT PAPER r;199,83: DRAW PAPER r;56,-28 640 PLOT PAPER r;184,83: DRAW PAPER r;70,-35 650 PLOT PAPER r;86,83: DRAW PAPER r;-70,-35 660 PLOT PAPER r;72,83: DRAW PAPER r;-70,-35 670 PLOT PAPER r;56,140: DRAW PAPER r;-56,28 680 PLOT PAPER r;71,140: DRAW PAPER r;-70,35 690 INVERSE 0: PAPER 0: INK 7 If you're not British, have a go at drawing your own flag. Tricolours are fairly easy, although some of the colours - for instance the orange in the Irish flag - might present difficulties. If you're an American, you might be able to fit the character * in. Here is a program to play hangman. One player enters a word, and the other guesses 5 REM hangman 10 REM set up screen 20 INK 0: PAPER 7: CLS 30 LET x=240: GO SUB 1000: REM draw man 40 PLOT 238,128: DRAW 4,0: REM mouth 100 REM set up word 110 INPUT w$: REM word to guess 120 LET b=LEN w$: LET v$=" " 130 FOR n=2 TO b: LET v$=v$+" " 140 NEXT n: REM v$=word guessed so far 150 LET c=0: LET d=0: REM guess & mistake counts 160 FOR n=0 TO b-1 170 PRINT AT 20,n;" "; 180 NEXT n: REM write 's instead of letters 200 INPUT "Guess a letter ";g$ 210 IF g$="" THEN GO TO 200 220 LET g$=g$(1): REM 1st letter only 230 PRINT AT 0,c;g$ 240 LET c=c+1: LET u$=v$ 250 FOR n=1 TO bL REM update guessed word 260 IF w$(n)=g$ THEN LET v$(n)=g$ 270 NEXT n 280 PRINT AT l9,0;v$ 290 IF v$=w$ THEN GO TO 500: REM word guessed 300 IF v$<>u$ THEN GO TO 200: REM guess was right 400 REM draw next part of gallows 410 IF d=8 THEN GO TO 600: REM hanged 420 LET d=d+1 430 READ x0,y0,x,y 440 PLOT x0,y0: DRAW x,y 450 GO TO 200 500 REM free man 510 OVER 1: REM rub out man 520 LET x=240: GO SUB 1000 530 PLOT 238,128: DRAW 4,0: REM mouth 540 OVER 0: REM redraw man 550 LET x=146: GO SUB 1000 560 PLOT 143,129: DRAW 6,0, PI/2: REM smile 570 GO TO 800 600 REM hang man 610 OVER 1: REM rub out floor 620 PLOT 255,65: DRAW -48,0 630 DRAW 0,-48: REM open trapdoor 640 PLOT 238,128: DRAW 4,0: REM rub out mouth 650 REM move limbs 655 REM arms 660 PLOT 255,117: DRAW -15,-15: DRAW -15,15 670 OVER 0 680 PLOT 236,81: DRAW 4,21: DRAW 4, 21 690 OVER 1: REM legs 700 PLOT 255,66: DRAW -15,15: DRAW -15,-15 710 OVER 0 720 PLOT 236,60: DRAW 4,21: DRAW 4,-21 730 PLOT 237,127: DRAW 6,0,-PI/2: REM frown 740 PRINT AT 19,0;w$ 800 INPUT "again? ";a$ 810 IF a$="" THEN GO TO 850 820 LET a$=a$(1) 830 IF a$="n" THEN STOP 840 IF a$(1)="N" THEN STOP 850 RESTORE: GO TO 5 1000 REM draw man at column x 1010 REM head 1020 CIRCLE x,132,8 1030 PLOT x+4,134: PLOT x-4,134: PLOT x,131 1040 REM body 1050 PLOT x,123: DRAW 0,-20 1055 PLOT x,101: DRAW 0,-19 1060 REM legs 1070 PLOT x-15,66: DRAW 15,15: DRAW 15,-15 1080 REM arms 1090 PLOT x-15,117: DRAW 15,-15: DRAW 15,15 1100 RETURN 2000 DATA 120,65,135,0,184,65,0,91 2010 DATA 168,65,16,16,184,81,16,-16 2020 DATA 184,156,68,0,184,140,16,16 2030 DATA 204,156,-20,-20,240,156,0,-16 APPENDIX E Binary and hexadecimal This appendix describes how computers count, using the binary system. Most European languages count using a more or less regular pattern of tens - in English, for example, although it starts off a bit erratically, it soon settles down into regular groups: twenty, twenty one, twenty two, . . . twenty nine thirty, thirty one, thirty two, . . . thirty nine forty, forty one, forty two, . . . forty nine and so on, and this is made even more systematic with the Arabic numerals that we use. However, the only reason for using ten is that we happen to have ten fingers and thumbs. Instead of using the decimal system, with ten as its base, computers use a form of binary called hexadecimal (or hex, for short), based on sixteen. As there are only ten digits available in our number system we need six extra digits to do the counting. So we use A, B, C, D, E and F. And what comes after F? Just as we, with ten fingers, write 10 for ten, so computers write 10 for sixteen. Their number system starts off: Hex English 0 nought 1 one 2 two : : : : 9 nine just as ours does, but then it carries on A ten B eleven C twelve D thirteen E fourteen F fifteen 10 sixteen 11 seventeen : : : : 19 twenty five 1A twenty six 1B twenty seven : : : : 1F thirty one 20 thirty two 21 thirty three : : : : 9E one hundred and fifty eight 9F one hundred and fifty nine A0 one hundred and sixty A1 one hundred and sixty one : : B4 one hundred and eighty : : FE two hundred and fifty four FF two hundred and fifty five 100 two hundred and fifty six If you are using hex notation and you want to make the fact quite plain, then write 'h' at the end of the number, and say 'hex'. For instance, for one hundred and fifty eight, write '9Eh' and say 'nine E hex'. You will be wondering what all this has to do with computers. In fact, computers behave as though they had only two digits, represented by a low voltage, or off (0), and a high voltage, or on (1). This is called the binary system, and the two binary digits are called bits: so a bit is either 0 or 1. In the various systems, counting starts off English Decimal Hexadecimal Binary nought 0 0 0 or 0000 one 1 1 1 or 0001 two 2 2 10 or 0010 three 3 3 11 or 0011 four 4 4 100 or 0100 five 5 5 101 or 0101 six 6 6 110 or 0110 seven 7 7 111 or 0111 eight 8 8 1000 nine 9 9 1001 ten 10 A 1010 eleven 11 B 1011 twelve 12 C 1100 thirteen 13 D 1101 fourteen 14 E 1110 fifteen 15 F 1111 sixteen 16 10 10000 The important point is that sixteen is equal to two raised to the fourth power, and this makes converting between hex and binary very easy. To convert hex to binary, change each hex digit into four bits, using the table above. To convert binary to hex, divide the binary number into groups of four bits, starting on the right, and then change each group into the corresponding hex digit. For this reason, although strictly speaking computers use a pure binary system, humans often write the numbers stored inside a computer using hex notation. The bits inside the computer are mostly grouped into sets of eight, or bytes. A single byte can represent any number from nought to two hundred and fifty five (11111111 binary or FF hex), or alternatively any character in the ZX Spectrum 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 or four hex digits, and represents a number from 0 to (in decimal) 2l6-l=65535. A byte is always eight bits, but words vary in length from computer to computer. The BIN notation in Chapter 14 provides a means of writing numbers in binary on the ZX Spectrum: 'BIN 0' represents nought, 'BIN 1' represents one, 'BIN 10' represents two, and so on. You can only use 0's and 1 's for this, so the number must be a non negative whole number; for instance you can't write 'BIN -11' for minus three - you must write '-BIN 11' instead. The number must also be no greater than decimal 65535 - i.e. it can't have more than sixteen bits. ATTR really was binary. If you convert the result from ATTR into binary, you can write it in eight bits. The first is 1 for flashing, 0 for steady. The second is 1 for bright, 0 for normal. The next three are the code for the paper colour, written in binary. The last three are the code for the ink colour, written in binary. The colour codes also use binary: each code written in binary can be written in three bits, the first for green, the second for red and the third for blue. Black has no light at all, so all the bits are 0 (off). Therefore the code for black is 000 in binary, or nought. The pure colours, green, red and blue have just one bit 1 (on) out of the three. Their codes are 100, 010 and 001 in binary, or four, two and one. The other colours are mixtures of these, so their codes in binary have two or more bits.