Sunteți pe pagina 1din 116

SINCLAIR ZX SPECTRUM

BASIC Programming
By Steven Vickers
Edited by Robin Bradbeer
Converted to electronic text by
Chris Owen
(Internet email: convg.unit.no)
_______________________________________________
Original edition published in 1982-83
by Sinclair Research Limited
This version published in 199
by !hris O"en on behal# o# $mstrad plc
!over illustration by
%ohn &arris o# 'oung $rtists
________________________________________________
Amstrad plc 1995 - all rights reserved
________________________________________________
This manual is freely distributable but may
not be distributed without this notice.
CONTENTS
Chapter 1: Introduction
$ guide to the () Spectrum *eyboard and a description o# the display+
Chapter 2: Basic programming concepts
,rograms- line numbers- editing programs using - and EDIT- RUN- LIST-
GO TO- ONTINUE- IN!UT- NE"- RE#- !RINT- STO! in IN!UT data-
$RE%&
Chapter 3: Decisions
I'- STO!, =, <, , <=, =, <
Chapter !: Looping
'OR- NE(T- TO- STE!+ .ntroducing 'OR)NE(T loops
Chapter ": Subroutines
GO SU$- RETURN
Chapter #: READ, DATA, RESTORE
Chapter $: Expressions
/athematical e0pressions using 1- -- 2- 3- scienti#ic notation and variable
names+
Chapter %: Strings
&andling strings and slicing+
Chapter &: Functions
4ser-de#inable #unctions and others readily available on the () Spectrum using
DE', LEN- STR*- +%L- SGN- %$S- INT- S,R- 'N
Chapter 1': Mathematical functions
.ncluding simple trigonometry5 |, !I- E(!- LN- SIN- OS- T%N- %SN- %S- %TN
Chapter 11: Random numbers
4sing R%NDO#I-E and RND(
Chapter 12: Arrays
Strings and numeric arrays - DI#(
Chapter 13: Conditions
Logical e0pressions5 %ND, OR- NOT(
Chapter 1!: The character set
$ loo* at the () character set including graphics and ho" to construct your o"n
graphic characters5 ODE- .R*- !O&E- !EE&- USR- $IN
Chapter 1": More about PRINT and INPUT
Some more complicated uses o# these commands using separators5 / 0 1 - T%$-
%T- LINE and LS+
Chapter 1#: Colours
IN&- !%!ER- 'L%S.- $RIG.T- IN+ERSE- O+ER- $ORDER
Chapter 1$: Graphics
!LOT- DR%"- IRLE- !OINT
Chapter 1%: Motion
$nimated graphics using !%USE- IN&E2* and !EE&(
Chapter 1&: BEEP
The sound capabilities o# the () Spectrum using $EE!(
Chapter 2': Tape Storage
&o" to store your programs on cassette tape5 S%+E- LO%D- +ERI'2- #ERGE
Chapter 21: The ZX Printer
LLIST- L!RINT- O!2
Chapter 22: Other equipment
!onnecting the () Spectrum to other machines and devices+
Chapter 23: IN and OUT
.nput3Output ports and their uses5 IN- OUT
Chapter 2!: The memory
$ loo* at the internal "or*ings o# the () Spectrum5 LE%R
Chapter 2": The system variables
Chapter 2#: Using machine code
.ntroducing USR "ith a numeric argument
))))))))))))))))))))))))))))
*++E,-I. *: The character set
*++E,-I. B: Reports
*++E,-I. C: A description of the ZX Spectrum for reference
*++E,-I. -: Example programs
*++E,-I. E: Binary and hexadecimal
CHAPTER 1
Introduction
/hether yo0 read the Introd0ctory boo1 2ir3t, or came 3trai4ht here, yo0 3ho0ld be a5are that command3 are obeyed
3trai4ht a5ay, and in3tr0ction3 be4in 5ith a line n0mber and are 3tored a5ay 2or later( 6o0 3ho0ld al3o be a5are o2
the command3: +7I,8, 9E8, and I,+:8 (5hich can be 03ed on all machine3 that 03e B*;IC), and B<7-E7,
+*+E7 and BEE+ (5hich are 03ed on the ;pectr0m)(

8hi3 B*;IC man0al 3tart3 by repeatin4 3ome thin43 4iven in the introd0ctory boo1let, b0t in m0ch more detail,
tellin4 yo0 exactly 5hat yo0 can and cannot do( 6o0 5ill al3o 2ind 3ome exerci3e3 at the end o2 each chapter( -on=t
i4nore the3e> many o2 them ill03trate point3 that are hinted at in the text( 9oo1 thro04h them, and do any that intere3t
yo0, or that 3eem to cover 4ro0nd that yo0 don=t 0nder3tand properly(

/hatever el3e yo0 do, 1eep 03in4 the comp0ter( I2 yo0 have the ?0e3tion @5hat doe3 it do i2 I tell it 30ch and 30chA@
then the an35er i3 ea3y: type it in and 3ee( /henever the man0al tell3 yo0 to type 3omethin4 in, al5ay3 a31 yo0r3el2,
@5hat co0ld I type in3teadA@, and try o0t yo0r replie3( 8he more o2 yo0r o5n pro4ram3 yo0 5rite, the better yo0 5ill
0nder3tand the comp0ter(

*t the end o2 thi3 pro4rammin4 man0al are 3ome appendice3( 8he3e incl0de 3ection3 on the 5ay the memory i3
or4ani3ed, ho5 the comp0ter manip0late3 n0mber3, and a 3erie3 o2 example pro4ram3 ill03tratin4 the po5er o2 the B.
;pectr0m(
The keyboard
B. ;pectr0m character3 compri3e not only the 3in4le symbols (letter3, di4it3, etc), b0t al3o the compo0nd to*ens
(1ey5ord3, 20nction name3, etc) and all the3e are entered 2rom the 1eyboard rather than bein4 3pelled o0t( 8o obtain
all the3e 20nction3 and command3, 3ome 1ey3 have 2ive or more
di3tinct meanin43, 4iven partly by 3hi2tin4 the 1ey3 (i(e( pre33in4 either the C*+; ;CID8 1ey or the ;6EB<9
;CID8 1ey at the 3ame time a3 the re?0ired one) and partly by havin4 the machine in di22erent mode3(
8he mode i3 indicated by the cursor, a 2la3hin4 letter that 3ho53 5here the next character 2rom the 1eyboard 5ill be
in3erted:
K (2or 1ey5ord3) mode a0tomatically replace3 L mode 5hen the machine i3 expectin4 a command or
pro4ram line (rather than I,+:8 data), and 2rom it3 po3ition on the line it 1no53 it 3ho0ld expect a line n0mber or a
1ey5ord( 8hi3 i3 at the be4innin4 o2 the line, or F03t a2ter 8CE,, or F03t a2ter: (except in a 3trin4)( I2 0n3hi2ted, the
next 1ey 5ill be interpreted a3 either a 1ey5ord (5ritten on the 1ey3), or a di4it(
L (2or letter3) mode normally occ0r3 at all other time3( I2 0n3hi2ted, the next 1ey 5ill be interpreted a3 the
main 3ymbol on that 1ey, in lo5er ca3e 2or letter3(
In both K and L mode3, ;6EB<9 ;CID8 and a 1ey 5ill be interpreted a3 the 30b3idiary red character on
the 1ey and C*+; ;CID8 5ith a di4it 1ey 5ill be interpreted a3 the control 20nction 5ritten in 5hite above the 1ey
C*+; ;CID8 5ith other 1ey3 doe3 not a22ect the 1ey5ord3 in K mode, and in L mode it convert3 lo5er ca3e to
capital3(
C (2or capital3) mode i3 a variant o2 L mode in 5hich all letter3 appear a3 capital3( C*+; 9<CG ca03e3 a
chan4e 2rom L mode to C mode or bac1 a4ain(
E (2or extended) mode i3 03ed 2or obtainin4 20rther character3, mo3tly to1en3( It occ0r3 a2ter both 3hi2t 1ey3
are pre33ed to4ether, and la3t3 2or one 1ey depre33ion only( In thi3 mode, a letter 4ive3 one character or to1en (3ho5n
in 4reen above it) i2 0n3hi2ted, and another (3ho5n in red belo5 it) i2 pre33ed 5ith either 3hi2t( * di4it 1ey 4ive3 a
to1en i2 pre33ed 5ith ;6EB<9 ;CID8> other5i3e it 4ive3 a colo0r control 3e?0ence(
G (2or 4raphic3) mode occ0r3 a2ter H7*+CIC; (C*+; ;CID8 and &) i3 pre33ed, and la3t3 0ntil it i3 pre33ed
a4ain or & i3 pre33ed on it3 o5n( * di4it 1ey 5ill 4ive a mo3aic 4raphic, ?0it H7*+CIC; or -E9E8E, and each o2
the letter 1ey3 apart 2rom I, /, ., 6 and B, 5ill 4ive a 03erJde2ined 4raphic(
I2 any 1ey i3 held do5n 2or more than abo0t 2 or 3 3econd3, it 5ill 3tart a0toJrepeatin4(
Geyboard inp0t appear3 in the bottom hal2 o2 the 3creen a3 it i3 typed, each character (3in4le 3ymbol or compo0nd
to1en) bein4 in3erted F03t be2ore the c0r3or( 8he c0r3or can be moved le2t 5ith C*+; ;CID8 and ", or ri4ht 5ith
C*+; ;CID8 and %( 8he character be2ore the c0r3or can be deleted 5ith -E9E8E (C*+; ;CID8 and &)( (,ote: the
5hole line can be deleted by typin4 E-I8 (C*+; ;CID8 and 1) 2ollo5ed by E,8E7()
/hen E,8E7 i3 pre33ed, the line i3 exec0ted, entered into the pro4ram, or 03ed a3 I,+:8 data a3 appropriate,
0nle33 it contain3 a 3yntax error( In thi3 ca3e a 2la3hin4 ? appear3 next to the error(
*3 pro4ram line3 are entered, a li3tin4 i3 di3played in the top hal2 o2 the 3creen( 8he la3t line entered i3 called the
c0rrent line and i3 indicated by the 3ymbol >> thi3 can be moved by 03in4 the 1ey3 (C*+; ;CID8 and #) and
(C*+; ;CID8 and $)( I2 E-I8 (C*+; ;CID8 and 1) i3 pre33ed, the c0rrent line i3 bro04ht do5n to the bottom part
o2 the 3creen and can be edited(
/hen a command i3 exec0ted or a pro4ram r0n, o0tp0t i3 di3played in the top hal2 o2 the 3creen and remain3 0ntil a
pro4ram line i3 entered, or E,8E7 i3 pre33ed 5ith an empty line, or or i3 pre33ed( In the bottom part appear3 a
report 4ivin4 a code (di4it or letter) re2erred to in $ppendi0 6. 8he report remain3 on the 3creen 0ntil a 1ey i3 pre33ed
(and indicate3 K mode)(
In certain circ0m3tance3, C*+; ;CID8 5ith the ;+*CE 1ey act3 a3 a B7E*G, 3toppin4 the comp0ter 5ith report -
or 9( 8hi3 i3 reco4ni3ed
(i) at the end o2 a 3tatement 5hile a pro4ram i3 r0nnin4, or
(ii) 5hile the comp0ter i3 03in4 the ca33ette recorder or printer(
The television screen
8hi3 ha3 2! line3, each 32 character3 lon4, and i3 divided into t5o part3( 8he top part i3 at mo3t 22 line3 and di3play3
either a li3tin4 or pro4ram o0tp0t( /hen printin4 in the top part ha3 reached the bottom, it all 3croll3 0p one line> i2
thi3 5o0ld involve lo3in4 a line that yo0 have not had a chance to 3ee yet, then the comp0ter 3top3 5ith the me33a4e
3crollA( +re33in4 the 1ey3 ,, ;+*CE or ;8<+ 5ill ma1e the pro4ram 3top 5ith report D BREAK - CONT repeats>
any other 1ey 5ill let the 3crollin4 contin0e( 8he bottom part i3 03ed 2or inp0ttin4 command3, pro4ram line3, and
I,+:8 data, and al3o 2or di3playin4 report3( 8he bottom part 3tart3 o22 a3 t5o line3 (the 0pper one blan1), b0t it
expand3 to accommodate 5hatever i3 typed in( /hen it reache3 the c0rrent print po3ition in the top hal2, 20rther
expan3ion3 5ill ma1e the top hal2 3croll 0p(
CHAPTER 2
6asic ,rogramming !oncepts
Summary
Programs
Line numbers
Editing programs using ,, and EDIT
RUN, LIST
GO TO, CONTINUE, INPUT, NEW, REM, PRINT
STOP in INPUT data
BREAK
8ype in the3e t5o line3 o2 a comp0ter pro4ram to print o0t the 30m o2 t5o n0mber3:
20 PRINT a
10 LET a=10
3o that the 3creen loo13 li1e thi3:
*3 yo0 already 1no5, beca03e the3e line3 be4an 5ith n0mber3, they 5ere not obeyed immediately b0t 3tored a5ay,
a3 pro4ram line3( 6o0 5ill al3o have noticed here that the line n0mber3 4overn the order o2 the line3 5ithin the
pro4ram: thi3 i3 mo3t important 5hen the pro4ram i3 r0n, b0t it i3 al3o re2lected in the order o2 the line3 in the li3tin4
that yo0 can 3ee on the 3creen no5(
;o 2ar yo0 have only entered one n0mber, 3o type
15 LET b=15
and in it 4oe3( It 5o0ld have been impo33ible to in3ert thi3 line bet5een the 2ir3t t5o i2 they had been n0mbered 1 and
2 in3tead o2 1' and 2' (line n0mber3 m03t be 5hole n0mber3 bet5een 1 and &&&&), 3o that i3 5hy, 5hen 2ir3t typin4
in a pro4ram, it i3 4ood practice to leave 4ap3 bet5een the line n0mber3(
,o5 yo0 need to chan4e line 2' to
20 PRINT a+b
6o0 co0ld type o0t the replacement in 20ll, b0t it i3 ea3ier to 03e the E-I8 2acility de3cribed in the introd0ctory
boo1let( 8he > by line 1" i3 called the pro4ram c0r3or, and the line it point3 to i3 the c0rrent line( 8hi3 i3 030ally the
la3t line that yo0 entered, b0t yo0 can 03e the or 1ey3 to move the pro4ram c0r3or do5n or 0p( (8ry it, leavin4
the pro4ram c0r3or event0ally at line 2'()
/hen yo0 pre33 the E-I8 1ey, then a copy o2 the c0rrent line 5ill be di3played at the bottom o2 the 3creen J in yo0r
ca3e, a copy o2 line 2'( Cold do5n the 1ey 0ntil the L c0r3or move3 to the end o2 the line, and then type
+b (5itho0t E,8E7)
8he line at the bottom 3ho0ld no5 read
20 PRINT a+b
8ype E,8E7 and it 5ill replace the old line 2', 3o that the 3creen loo13 li1e thi3:
70n thi3 pro4ram 03in4 7:, and E,8E7 and the 30m 5ill be di3played(
70n the pro4ram a4ain and then type
PRINT a, b
8he variable3 are 3till there, even tho04h the pro4ram ha3 2ini3hed(
8here i3 a 03e20l method 03in4 E-I8 to 4et rid o2 the bottom part o2 the 3creen( 8ype in a load o2 r0bbi3h (5itho0t
E,8E7) and then decide that yo0 don=t 5ant it a2ter all( <ne 5ay to delete it i3 to hold the -E9E8E 1ey do5n 0ntil
the line i3 4one> b0t another 5ay i3 a3 2ollo53( I2 yo0 pre33 E-I8, the r0bbi3h at the bottom o2 the 3creen 5ill be
replaced by a copy o2 the c0rrent line( I2 yo0 no5 pre33 E,8E7, the c0rrent line 5ill be p0t bac1 in the pro4ram
0naltered, leavin4 the bottom part o2 the 3creen clear(
I2 yo0 enter a line by mi3ta1e, 3ay
12 LET b=8
it 5ill 4o 0p into the pro4ram and yo0 5ill reali3e yo0r mi3ta1e( 8o delete thi3 0nnece33ary line, type
12 (5ith E,8E7 o2 co0r3e)
6o0 5ill notice 5ith 30rpri3e that the pro4ram c0r3or ha3 4one( 6o0 3ho0ld ima4ine it a3 bein4 hidden in bet5een
line3 1' and 1", 3o i2 yo0 pre33 it 5ill move 0p to line 1', 5hile i2 yo0 pre33 it 5ill move do5n to line 1"(
8ype
12 (and E,8E7)
*4ain, the pro4ram c0r3or 5ill be hidden bet5een line3 1' and 1"( ,o5 pre33 E-I8 and line 1" 5ill come do5n:
5hen the pro4ram c0r3or i3 hidden bet5een t5o line3, E-I8 brin43 do5n the next line 2ollo5in4 the ne5 line
n0mber( 8ype E,8E7 to clear the bottom part o2 the 3creen(
,o5 type
30 (and E,8E7)
8hi3 time, the pro4ram c0r3or i3 hidden a2ter the end o2 the pro4ram: and i2 yo0 pre33 E-I8, then line 2' 5ill be
bro04ht do5n(
9a3tly, type
LIST 15
6o0 5ill no5 3ee on the 3creen
15 LET b=15
20 PRINT a+b
9ine 1' ha3 vani3hed 2rom the 3creen( b0t it i3 3till in yo0r pro4ram J 5hich yo0 can prove by pre33in4 E,8E7( 8he
only e22ect3 o2 9I;8 1" are to prod0ce a li3tin4 that 3tart3 at line 1"( and to p0t the pro4ram c0r3or at line 1"( I2 yo0
have a very lon4 pro4ram, then 9I;8 5ill probably be a more 03e20l 5ay o2 movin4 the pro4ram c0r3or than and
(
8hi3 ill03trate3 another 03e o2 line n0mber3: they act a3 name3 2or the pro4ram line3 3o that yo0 can re2er to them,
rather li1e the 5ay in 5hich variable3 have name3(
9I;8 on it3 o5n ma1e3 the li3tin4 3tart at the be4innin4 o2 a pro4ram(
*nother command 3een in the introd0ctory boo1let i3:
NEW
8hi3 era3e3 any old pro4ram3 and variable3 in the comp0ter( ,o5 care20lly type in thi3 pro4ram, 5hich chan4e3
Dahrenheit temperat0re3 to Centi4rade(
10 REM temperature co!er"#o
20 PRINT $%e& '$, $%e& ($
30 PRINT
)0 INP*T $Eter %e& '$, '
50 PRINT ',+',32-.5/0
10 23 T3 )0
6o0 5ill need to type the 5ord3 in line 1'( *l3o, altho04h H< 8< ha3 a 3pace in it, it i3 really all one 1ey5ord (on
H)(
,o5 r0n it( 6o0 5ill 3ee the headin43 printed on the 3creen by line 2', b0t 5hat happened to line 1'A *pparently
the comp0ter ha3 completely i4nored it( /ell, it ha3( 7EE in line 1' 3tand3 2or remar1, or reminder, and i3 there
3olely to remind yo0 o2 5hat the pro4ram doe3( * 7EE command con3i3t3 o2 7EE 2ollo5ed by anythin4 yo0 li1e,
and the comp0ter 5ill i4nore it ri4ht 0p to the end o2 the line(
By no5, the comp0ter ha3 4ot to the I,+:8 command on line !' and i3 5aitin4 2or yo0 to type in a val0e 2or the
variable D J yo0 can tell thi3 beca03e 5here yo0 mi4ht have expected a K c0r3or there i3 in3tead an L c0r3or( Enter a
n0mber> remember E,8E7( ,o5 the comp0ter ha3 di3played the re30lt and i3 5aitin4 2or another n0mber( 8hi3 i3
beca03e o2 line #', H< 8< !', 5hich mean3 exactly 5hat it 3ay3( In3tead o2 r0nnin4 o0t o2 pro4ram and 3toppin4, the
comp0ter F0mp3 bac1 to line !' and 3tart3 a4ain( ;o, enter another temperat0re( *2ter a 2e5 more o2 the3e yo0 mi4ht
be 5onderin4 i2 the machine 5ill ever 4et bored 5ith thi3, it 5on=t( ,ext time it a313 2or another n0mber, type ;8<+(
8he comp0ter come3 bac1 5ith a report 4 ST3P # INP*T # 5#e )061, 5hich tell3 yo0 5hy it 3topped, and 5here
(in the 2ir3t command o2 line !')(
I2 yo0 5ant to contin0e the pro4ram type
(3NTIN*E
and the comp0ter 5ill a31 yo0 2or another n0mber(
/hen C<,8I,:E i3 03ed the comp0ter remember3 the line n0mber in the la3t report that it 3ent yo0, a3 lon4 a3 it
5a3 not 0 37, and F0mp3 bac1 to that line: in o0r ca3e, thi3 involve3 F0mpin4 to line !', the I,+:8 command(
7eplace line #' by H< 8< 31 J it 5ill ma1e no perceptible di22erence to the r0nnin4 o2 the pro4ram( I2 the line
n0mber in a H< 8< command re2er3 to a nonJexi3tent line, then the F0mp i3 to the next line a2ter the 4iven n0mber(
8he 3ame 4oe3 2or 7:,> in 2act 7:, on it3 o5n act0ally mean3 7:, '(
,o5 type in n0mber3 0ntil the 3creen 3tart3 4ettin4 20ll( /hen it i3 20ll, the comp0ter 5ill move the 5hole o2 the top
hal2 o2 the 3creen 0p one line to ma1e room, lo3in4 the headin4 o22 the top( 8hi3 i3 called 3crollin4(
/hen yo0 are tired o2 thi3, 3top the pro4ram 03in4 ;8<+ and 4et the li3tin4 by pre33in4 E,8E7(
9oo1 at the +7I,8 3tatement on line "'( 8he p0nct0ation in thi3 J the comma (,) i3 very important, and yo0 3ho0ld
remember that it 2ollo53 m0ch more de2inite r0le3 than the p0nct0ation in En4li3h(
Comma3 are 03ed to ma1e the printin4 3tart either at the le2t hand mar4in, or in the middle o2 the 3creen, dependin4
on 5hich come3 next( 8h03 in line "', the comma ca03e3 the centi4rade temperat0re to be printed in the middle o2 the
line( /ith a 3emicolon, on the other hand, the next n0mber or 3trin4 i3 printed immediately a2ter the precedin4 one(
6o0 can 3ee thi3 in line "', i2 the comma i3 replaced by a 3emicolon(
*nother p0nct0ation mar1 yo0 can 03e li1e thi3 in +7I,8 command3 i3 the apo3trophe (=)( 8hi3 ma1e3 5hatever i3
printed next appear at the be4innin4 o2 the next line on the 3creen b0t thi3 happen3 any5ay at the end o2 each +7I,8
command, 3o yo0 5ill not need the apo3trophe very m0ch( 8hi3 i3 5hy the +7I,8 command in line "' al5ay3 3tart3
it3 printin4 on a ne5 line, and it i3 al3o 5hy the +7I,8 command in line 3' prod0ce3 a blan1 line(
I2 yo0 5ant to inhibit thi3, 3o that a2ter one +7I,8 command the next one carrie3 on on the 3ame line, yo0 can p0t a
comma or 3emicolon at the end o2 the 2ir3t( 8o 3ee ho5 thi3 5or13, replace line "' in t0rn by each o2
50 PRINT ',
50 PRINT '8
and
50 PRINT '
and r0n each ver3ion J 2or 4ood mea30re yo0 co0ld al3o try
50 PRINT '9
8he one 5ith the comma 3pread3 everythin4 o0t in t5o col0mn3, that 5ith the 3emicolon cram3 everythin4 to4ether,
that 5itho0t either allo53 a line 2or each n0mber and 3o doe3 that 5ith the apo3trophe J the apo3trophe 4ive3 a ne5
line o2 it3 o5n, b0t inhibit3 the a0tomatic one(
7emember the di22erence bet5een comma3 and 3emicolon3 in +7I,8 command3> al3o, do not con203e them 5ith the
colon3 (:) that are 03ed to 3eparate command3 in a 3in4le line(
,o5 type in the3e extra line3:
100 REM t:#" po5#te pro&ram remember" ;our ame
110 INP*T <
120 PRINT $4e55o $8<8$=$
130 23 T3 110
8hi3 i3 a 3eparate pro4ram 2rom the la3t one, b0t yo0 can 1eep them both in the comp0ter at the 3ame time( 8o r0n
the ne5 one, type
R*N 100
Beca03e thi3 pro4ram inp0t3 a 3trin4 in3tead o2 a n0mber, it print3 o0t t5o 3trin4 ?0ote3 J thi3 i3 a reminder to yo0,
and it 030ally 3ave3 yo0 3ome typin4 a3 5ell( 8ry it once 5ith any alia3 yo0 care to ma1e 0p 2or yo0r3el2(
,ext time ro0nd, yo0 5ill 4et t5o 3trin4 ?0ote3 a4ain( b0t yo0 don=t have to 03e them i2 yo0 don=t 5ant to( 8ry thi3,
2or example( 70b them o0t (5ith and -E9E8E t5ice), and type
<
;ince there are no 3trin4 ?0ote3, the comp0ter 1no53 that it ha3 to do 3ome calc0lation: the calc0lation in thi3 ca3e i3
to 2ind the val0e o2 the 3trin4 variable called nK, 5hich i3 5hatever name yo0 happen to have typed in la3t time ro0nd(
<2 co0r3e, the I,+:8 3tatement act3 li1e 9E8 nK=nK, 3o the val0e o2 nK i3 0nchan4ed(
8he next time ro0nd, 2or compari3on, type
<
a4ain, thi3 time 5itho0t r0bbin4 o0t the 3trin4 ?0ote3( ,o5, F03t to con203e yo0, the variable nK ha3 the val0e @nK@(

I2 yo0 5ant to 03e ;8<+ 2or 3trin4 inp0t, yo0 m03t 2ir3t move the c0r3or bac1 to the be4innin4 o2 the line, 03in4 (
,o5 loo1 bac1 at that 7:, 1'' 5e had earlier on( 8hat F03t F0mp3 to line 1'', 3o co0ldn=t 5e have 3aid H< 8< 1''
in3teadA In thi3 ca3e, it 3o happen3 that the an35er i3 ye3> b0t there i3 a di22erence( 7:, 1'' 2ir3t o2 all clear3 all the
variable3 and the 3creen, and a2ter that 5or13 F03t li1e H< 8< 1''( H< 8< 1'' doe3n=t clear anythin4( 8here may
5ell be occa3ion3 5here yo0 5ant to r0n a pro4ram 5itho0t clearin4 any variable3> here H< 8< 5o0ld be nece33ary
and 7:, co0ld be di3a3tro03, 3o it i3 better not to 4et into the habit o2 a0tomatically typin4 7:, to r0n a pro4ram(
*nother di22erence i3 that yo0 can type 7:, 5itho0t a line n0mber, and it 3tart3 o22 at the 2ir3t line in the pro4ram(
H< 8< m03t al5ay3 have a line n0mber(
Both the3e pro4ram3 3topped beca03e yo0 typed ;8<+ in the inp0t line> 3ometime3 J by mi3ta1e J yo0 5rite a
pro4ram that yo0 can=t 3top and 5on=t 3top it3el2( 8ype
200 23 T3 200
R*N 200
8hi3 loo13 all 3et to 4o on 2or ever 0nle33 yo0 p0ll the pl04 o0t> b0t there i3 a le33 dra3tic remedy( +re33 C*+;
;CID8 5ith the ;+*CE 1ey, 5hich ha3 B7E*G 5ritten above it( 8he pro4ram 5ill 3top, 3ayin4 L >RE?7 #to
pro&ram(
*t the end o2 every 3tatement, the pro4ram loo13 to 3ee i2 the3e 1ey3 are pre33ed> and i2 they are, then it 3top3( 8he
B7E*G 1ey can al3o be 03ed 5hen yo0 are in the middle o2 03in4 the ca33ette recorder or the printer, or vario03
other bit3 o2 machinery that yo0 can attach to the comp0ter J F03t in ca3e the comp0ter i3 5aitin4 2or them to do
3omethin4 b0t they=re not doin4 it(
In the3e ca3e3 there i3 a di22erent report, @ >RE?7 , (3NT repeat"( C<,8I,:E in thi3 ca3e (and in 2act in mo3t
other ca3e3 too) repeat3 the 3tatement 5here the pro4ram 5a3 3topped> b0t a2ter the report L >RE?7 #to
pro&ram, C<,8I,:E carrie3 3trai4ht on 5ith the next 3tatement a2ter allo5in4 2or any F0mp3 to be made(
70n the name pro4ram a4ain and 5hen it a313 yo0 2or inp0t type
< (a2ter removin4 the ?0ote3)
nK i3 an 0nde2ined variable and yo0 4et an error report 26 Aar#ab5e ot Bou%(
I2 yo0 no5 type
LET <=$"omet:#& %eB##te$
(5hich ha3 it3 o5n report o2 0 37, 061) and
(3NTIN*E
yo0 5ill 2ind that yo0 can 03e nK a3 inp0t data 5itho0t any tro0ble(
In thi3 ca3e C<,8I,:E doe3 a F0mp to the I,+:8 command in line 11'( It di3re4ard3 the report 2rom the 9E8
3tatement beca03e that 3aid =<G=, and F0mp3 to the command re2erred to in the previo03 report, the 2ir3t command in
line 11'( 8hi3 i3 intended to be 03e20l( I2 a pro4ram 3top3 over 3ome error then yo0 can do all 3ort3 o2 thin43 to 2ix it,
and C<,8I,:E 5ill 3till 5or1 a2ter5ard3(
*3 5e 3aid be2ore, the report L >RE?7 #to pro&ram i3 3pecial, beca03e a2ter it C<,8I,:E doe3 not repeat the
command 5here the pro4ram 3topped(
8he a0tomatic li3tin43 (the one3 that are not the re30lt o2 a 9I;8 command b0t occ0r a2ter enterin4 a ne5 line) may
5ell have yo0 p0LLled( I2 yo0 type in a pro4ram 5ith "' line3, all 7EE 3tatement3,
1 REM
2 REM
3 REM
6 6
6 6
)0 REM
50 REM
then yo0 5ill be able to experiment(
8he 2ir3t thin4 to remember i3 that the c0rrent line (5ith >) 5ill al5ay3 appear on the 3creen, and 030ally near the
middle(
8ype
LIST (and E,8E7 o2 co0r3e)
and 5hen it a313 "cro55C (beca03e it ha3 2illed 0p the 3creen) pre33 n 2or =,o=( 8he comp0ter 5ill 4ive the report @
>RE?7 , (3NT repeat" a3 tho04h yo0 had typed B7E*G( 6o0 mi4ht at 3ome 3ta4e 2ind o0t 5hat happen3 i2 yo0
pre33 y in3tead o2 n> n, ;+*CE and ;8<+ co0nt a3 ,o, 5hile everythin4 el3e co0nt3 a3 6e3(
,o5 pre33 E,8E7 a4ain to 4et an a0tomatic li3tin4 and yo0 3ho0ld 3ee line3 1 to 22 on the 3creen( ,o5 type
23 REM
and yo0 4et line3 2 to 23 on the 3creen> type
28 REM
and yo0 4et line3 $ to 2%( (In both ca3e3, by typin4 a ne5 line, yo0 have moved the pro4ram c0r3or 3o that a ne5
li3tin4 ha3 been made()
Eaybe thi3 loo13 a little arbitrary to yo0( It i3 act0ally tryin4 to 4ive yo0 exactly 5hat yo0 5ant, altho04h, h0man3
bein4 0npredictable creat0re3, it doe3n=t al5ay3 40e33 ri4ht(
8he comp0ter 1eep3 a record not only o2 the c0rrent line, the one that ha3 to appear on the 3creen, b0t al3o the top
line on the 3creen( /hen it trie3 to ma1e a li3tin4, the 2ir3t thin4 it doe3 i3 compare the top line to the c0rrent line( I2
the top line come3 a2ter, then there i3 no point in 3tartin4 there, 3o it 03e3 the c0rrent line 2or a ne5 top line and ma1e3
it3 li3tin4( <ther5i3e, it3 method i3 to 3tart ma1in4 the li3tin4 2rom the top line, and carry on 0ntil it ha3 li3ted the
c0rrent line, 3crollin4 i2 nece33ary(
Co5ever, it 2ir3t doe3 a ro04h calc0lation to 3ee ho5 lon4 thi3 5o0ld ta1e, and i2 the an35er i3 m0ch too lon4, then
it move3 the top line do5n to be a lot clo3er to the c0rrent line( ,o5, havin4 5or1ed o0t it3 top line, it 3tart3 li3tin4
2rom there( I2, 5hen it reache3 the end o2 the pro4ram or the bottom o2 the 3creen, the c0rrent line ha3 been li3ted,
then it 3top3( <ther5i3e, it 3croll3 0ntil the c0rrent line i3 on the 3creen, and 2or each extra line that it li3t3 it move3 the
top line do5n one 3o that the top line dri2t3 into the nei4hbo0rhood o2 the c0rrent line(
Experiment 5ith movin4 the c0rrent line abo0t by typin4
line n0mber REM
9I;8 move3 the c0rrent line b0t not the top line, 3o 30b3e?0ent li3tin43 mi4ht be di22erent( Dor in3tance, type
LIST
to 4et the 9I;8 li3tin4 and then pre33 E,8E7 a4ain to ma1e line ' the top line( 6o0 3ho0ld have line3 1 to 22 on the
3creen( 8ype
LIST 22
5hich 4ive3 yo0 line3 22 to !3> 5hen yo0 pre33 E,8E7 a4ain, yo0 4et bac1 line3 1 to 22( 8hi3 tend3 to be more
03e20l 2or 3hort pro4ram3 than 2or lon4 one3(
:3in4 the pro4ram 20ll o2 7EE3 above, type
LIST
and then n 5hen it a313 yo0 "cro55C( ,o5 type
(3NTIN*E
C<,8I,:E i3 a bit ?0ir1y here, beca03e the bottom part o2 the 3creen 4oe3 blan1> b0t yo0 can re3tore normality
5ith B7E*G( 8he rea3on i3 that 9I;8 5a3 the 2ir3t command in the line, 3o C<,8I,:E repeat3 thi3 command(
:n2ort0nately, the 2ir3t command in the line i3 no5 C<,8I,:E it3el2 3o the comp0ter F03t 3it3 there doin4
C<,8I,:E over and over a4ain 0ntil yo0 3top it(
6o0 can vary thi3 by replacin4 9I;8 5ith
6 LIST
2or 5hich C<,8I,:E 4ive3 ' <G (beca03e C<,8I,:E F0mp3 to the 3econd command in the line, 5hich i3 ta1en to
be it3 end) or
66 LIST
2or 5hich C<,8I,:E 4ive3 N Statemet 5o"t (beca03e C<,8I,:E F0mp3 to the third command in the line,
5hich no lon4er exi3t3)(
6o0 have no5 3een the 3tatement3 +7I,8, 9E8, I,+:8, 7:,, 9I;8, H< 8<, C<,8I,:E, ,E/ and 7EE, and
they can all be 03ed either a3 direct command3 or in pro4ram line3 J thi3 i3 tr0e o2 almo3t all command3 in B.
;pectr0m B*;IC( 7:,, 9I;8, C<,8I,:E and ,E/ are not 030ally o2 m0ch 03e in a pro4ram, b0t they can be
03ed(
Exercises
1( +0t a 9I;8 3tatement in a pro4ram, 3o that 5hen yo0 r0n it, it li3t3 it3el2(
2( /rite a pro4ram to inp0t price3 and print o0t the tax d0e (at 1" per cent)( +0t in +7I,8 3tatement3 3o that the
comp0ter anno0nce3 5hat it i3 4oin4 to do, and a313 2or the inp0t price 5ith extrava4ant
politene33( Eodi2y the pro4ram 3o that yo0 can al3o inp0t the tax rate (to allo5 2or Lero ratin43 or 20t0re chan4e3)(
3( /rite a pro4ram to print a r0nnin4 total o2 n0mber3 yo0 inp0t( (;044e3tion: have t5o variable3 called total J 3et to
' to be4in 5ith J and item( Inp0t item, add it to total, print them both, and 4o ro0nd a4ain()
!( /hat 5o0ld C<,8I,:E and ,E/ do in a pro4ramA Can yo0 thin1 o2 any 03e3 at all 2or thi3A
CHAPTER 3
7ecisions
Summary
IF, STOP
, <, >, <, >, <>
*ll the pro4ram3 5e have 3een 3o 2ar have been pretty predictable J they 5ent 3trai4ht thro04h the in3tr0ction3, and
then 5ent bac1 to the be4innin4 a4ain( 8hi3 i3 not very 03e20l( In practice the comp0ter 5o0ld be expected to ma1e
deci3ion3 and act accordin4ly( 8he in3tr0ction 03ed ha3 the
2orm ( ( ( ID 3omethir,4 i3 tr0e, or not tr0e 8CE, do 3omethin4 el3e(
Dor example, 03e ,E/ to clear the previo03 pro4ram 2rom the comp0ter and type in and r0n thi3 pro4ram( (8hi3 i3
clearly meant 2or t5o people to playM)
10 REM 2ue"" t:e umber
20 INP*T a6 (LS
30 INP*T $2ue"" t:e umber$, b
)0 I' b=a T4EN PRINT $T:at #" correct$6 ST3P
50 I' bDa T4EN PRINT $T:at #" too "ma55, tr; a&a#$
10 I' b>a T4EN PRINT $T:at #" too b#&, tr; a&a#$
E0 23 T3 30
6o0 can 3ee that an ID 3tatement ta1e3 the 2orm
I' condition T4EN F F F
5here the =( ( (= 3tand3 2or a 3e?0ence o2 command3, 3eparated by colon3 in the 030al 5ay( 8he condition i3 3omethin4
that i3 4oin4 to be 5or1ed o0t a3 either tr0e or 2al3e: i2 it come3 o0t a3 tr0e then the 3tatement3 in the re3t o2 the line
a2ter 8CE, are exec0ted, b0t other5i3e they are 31ipped over, and the pro4ram exec0te3 the next in3tr0ction(
8he 3imple3t condition3 compare t5o n0mber3 or t5o 3trin43: they can te3t 5hether t5o n0mber3 are e?0al or
5hether one i3 bi44er than the other> and they can te3t 5hether t5o 3trin43 are e?0al, or (ro04hly) one come3 be2ore
the other in alphabetical order( 8hey 03e the relation3 =, <, , <=, =
and <(
= mean3 =e?0al3=( *ltho04h it i3 the 3ame 3ymbol a3 the = in a 9E8 command, it i3 03ed in ?0ite a di22erent 3en3e(

< (;6EB<9 ;CID8 5ith 7) mean3 =i3 le33 than= 3o that
1 < 2
J2 < J1
J 3 < 1
are all tr0e, b0t
1 < '
' < J2
are 2al3e(
9ine !' compare3 a and b( I2 they are e?0al then the pro4ram i3 halted by the ;8<+ command( 8he report at the
bottom o2 the 3creen 0 ST3P, "tatemet, 3063 3ho53 that the third 3tatement, or command, in line 3' ca03ed the
pro4ram to halt, i(e( ;8<+(
9ine "' determine3 5hether b i3 le33 than a, and line #' 5hether b i3 4reater than a( I2 one o2 the3e condition3 i3 tr0e
then the appropriate comment i3 printed, and the pro4ram 5or13 it3 5ay to line $' 5hich tell3the comp0ter to 4o bac1
to line 3' and 3tart all over a4ain(
8he C9;, clear 3creen, command in line 2' 5a3 to 3top the other per3on 3eein4 5hat yo0 p0t in(
;o (;6EB<9 ;CID8 5ith 8) mean3 =i3 4reater than=, and i3 F03t li1e < b0t the other 5ay ro0nd( 6o0 can
remember 5hich i3 5hich, beca03e the thin end point3 to the n0mber that i3 30ppo3ed to be 3maller(
<= (;6EB<9 ;CID8 5ith N J do not type it a3 < 2ollo5ed by =) mean3 =i3 le33 than or e?0al to=, 3o that it i3 li1e <
except that it i3 tr0e even i2 the t5o n0mber3 are e?0al: th03 2<=2 i3 tr0e, b0t 2<2 i3 2al3e(
= (;6EB<9 ;CID8 5ith E) mean3 =i3 4reater than or e?0al to= and i3 3imilarly li1e (
< (;6EB<9 ;CID8 5ith /) mean3 =i3 not e?0al to=, the oppo3ite in meanin4 to =(
Eathematician3 030ally 5rite <=, = and < a3 s, >and =( 8hey al3o 5rite thin43 li1e =2<3<!= to mean =2<3 and
3<!=, b0t thi3 i3 not po33ible in B*;IC(
,ote: in 3ome ver3ion3 o2 B*;IC J b0t not on the B. ;pectr0m J the ID 3tatement can have the 2orm
I' condition T4EN line n0mber
8hi3 mean3 the 3ame a3
I' condition T4EN 23 T3 line n0mber
Exercises
1( 8ry thi3 pro4ram:
10 PRINT $G$6 ST3P6 PRINT A
/hen yo0 r0n it, it 5ill di3play x and 3top 5ith report 0 ST3P "tatemet, 1062( ,o5 type
(3NTIN*E
6o0 mi4ht expect thi3 to F0mp bac1 to the ;8<+ command J C<,8I,:E 030ally repeat3 the 3tatement re2erred to in
the report( Co5ever, here thi3 5o0ld not be very 03e20l, beca03e the comp0ter 5o0ld F03t 3top a4ain 5itho0t
di3playin4 y( 8here2ore, thin43 are arran4ed 3o that a2ter report & C<,8I,:E F0mp3 to the command a2ter the ;8<+
command J 3o in o0r example, a2ter C<,8I,:E, the comp0ter print3 I and reache3 the end o2 the pro4ram(
CHAPTER 4
Looping
Summary
FOR, NEXT
TO, STEP
;0ppo3e yo0 5ant to inp0t 2ive n0mber3 and add them to4ether( <ne 5ay (don=t type thi3 in 0nle33 yo0 are 2eelin4
d0ti20l) i3 to 5rite
10 LET tota5=0
20 INP*T a
30 LET tota5=tota5+a
)0 INP*T a
50 LET tota5=tota5+a
10 INP*T a
E0 LET tota5=tota5+a
80 INP*T a
00 LET tota5=tota5+a
100 INP*T a
110 LET tota5=tota5+a
120 PRINT tota5
8hi3 method i3 not 4ood pro4rammin4 practice( It may be F03t abo0t controllable 2or 2ive n0mber3, b0t yo0 can
ima4ine ho5 tedio03 a pro4ram li1e thi3 to add ten n0mber3 5o0ld be, and to add a h0ndred 5o0ld be F03t
impo33ible(
E0ch better i3 to 3et 0p a variable to co0nt 0p to " and then 3top the pro4ram, li1e thi3 (5hich yo0 3ho0ld type in):
10 LET tota5=0
20 LET cout=1
30 INP*T a
)0 REM cout=umber oB t#me" t:at a :a" bee #put "o Bar
50 LET tota5=tota5+a
10 LET cout=cout+1
E0 I' coutD=5 T4EN 23 T3 30
80 PRINT tota5
,otice ho5 ea3y it 5o0ld be to chan4e line $' 3o that thi3 pro4ram add3 ten n0mber3, or even a h0ndred(
8hi3 3ort o2 co0ntin4 i3 3o 03e20l that there are t5o 3pecial command3 to ma1e it ea3ier: the D<7 command and the
,E.8 command( 8hey are al5ay3 03ed to4ether( :3in4 the3e, the pro4ram yo0 have F03t typed in doe3 exactly the
3ame a3
10 LET tota5=0
20 '3R c=1 T35
30 INP*T a
)0 REM c=umber oB t#me" t:at a :a" bee #put "o Bar
50 LET tota5=tota5+a
10 NEHT c
80 PRINT tota5
(8o 4et thi3 pro4ram 2rom the previo03 one, yo0 F03t have to edit line3 2', !', #', and $'( 8< i3 ;6EB<9 ;CID8
5ith D()
,ote that 5e have chan4ed co0nt to c( 8he co0ntin4 variable J or control variable o2 a D<7 J ,E.8 loop m03t have
a 3in4le letter 2or it3 name(
8he e22ect o2 thi3 pro4ram i3 that c r0n3 thro04h the val0e3 1 (the initial val0e), 2, 3, ! and " (the limit), and 2or each
one, line3 3', !' and "' are exec0ted( 8hen, 5hen c ha3 2ini3hed it3 2ive val0e3, line %' i3 exec0ted(
*n extra 30btlety to thi3 i3 that the control variable doe3 not have to 4o 0p by 1 each time: yo0 can chan4e thi3 1 to
anythin4 yo0 li1e by 03in4 a ;8E+ part in the D<7 command( 8he mo3t 4eneral 2orm 2or a D<7 command i3
'3R control variable = initial val0e T3 limit STEP 3tep
5here the control variable i3 a 3in4le letter, and the initial val0e, limit and 3tep are all thin43 that the comp0ter can
calc0late a3 n0mber3 J li1e the act0al n0mber3 them3elve3, or 30m3, or the name3 o2
n0meric variable3( ;o, i2 yo0 replace line 2' in the pro4ram by
20 '3R c=1 T3 5 STEP 3/2
then c 5ill r0n thro04h the val0e3 1, 2(" and !( ,otice that yo0 don=t have to re3trict yo0r3el2 to 5hole n0mber3, and
al3o that the control val0e doe3 not have to hit the limit exactly J it carrie3 on loopin4 a3
lon4 a3 it i3 le33 than or e?0al to the limit(
8ry thi3 pro4ram, to print o0t the n0mber3 2rom 1 to 1' in rever3e order(
10 '3R =10 T3 1 STEP ,1
20 PRINT
30 NEHT
/e 3aid be2ore that the pro4ram carrie3 on loopin4 a3 lon4 a3 the control variable i3 le33 than or e?0al to the limit( I2
yo0 5or1 o0t 5hat thi3 5o0ld mean in thi3 ca3e( yo0 5ill 3ee that it 4ive3 non3en3e( 8he normal r0le ha3 to be
modi2ied: 5hen the 3tep i3 ne4ative, the pro4ram carrie3 on loopin4 a3 lon4 a3 the control variable i3 4reater than or
e?0al to the limit(
6o0 m03t be care20l i2 yo0 are r0nnin4 t5o D<7 J ,E.8 loop3 to4ether, one in3ide the other( 8ry thi3 pro4ram,
5hich print3 o0t the n0mber3 2or a complete 3et o2 3ix 3pot dominoe3(
10 '3R m=0 T3 1 O
20 '3R =0 T3 m O O
30 PRINT m8$6$88$ $8 O nJloop O mJloop
)0 NEHT O O
50 PRINT O
10 NEHT m O
6o0 can 3ee that the nJloop i3 entirely in3ide the mJloop J they are properly ne3ted( /hat m03t be avoided i3 havin4
t5o D<7 J ,E.8 loop3 that overlap 5itho0t either bein4 entirely in3ide the other, li1e thi3:
5 REM t:#" pro&ram #" Iro&
10 '3R m=0 T3 1 O
20 '3R =0 T3 m O mJloop O
30 PRINT m8$6$88$ $8 O O
)0 NEHT m O O nJloop
50 PRINT O
10 NEHT O
85o D<7 J ,E.8 loop3 m03t either be one in3ide the other, or be completely 3eparate(
*nother thin4 to avoid i3 F0mpin4 into the middle o2 a D<7 J ,E.8 loop 2rom the o0t3ide( 8he control variable i3
only 3et 0p properly 5hen it3 D<7 3tatement i3 exec0ted, and i2 yo0 mi33 thi3 o0t the ,E.8 3tatement 5ill con203e
the comp0ter( 6o0 5ill probably 4et an error report 3ayin4 NEHT I#t:out '3R or !ar#ab5e ot Bou%(
8here i3 nothin4 5hatever to 3top yo0 03in4 D<7 and ,E.8 in a direct command( Dor example, try:
'3R m=0 T3 106 PRINT m6 NEHT m
6o0 can 3ometime3 03e thi3 a3 a (3ome5hat arti2icial) 5ay o2 4ettin4 ro0nd the re3triction that yo0 cannot H< 8<
any5here in3ide a command J beca03e a command ha3 no line n0rrber( Dor in3tance,
'3R m=0 T3 1 STEP 06 INP*T a6 PRINT a6 NEHT m
8he 3tep o2 Lero here ma1e3 the command repeat it3el2 2orever(
8hi3 3ort o2 thin4 i3 not really recommended, beca03e i2 an error crop3 0p then yo0 have lo3t the command and 5ill
have to type it in a4ain J and C<,8I,:E 5ill not 5or1(
Exercises
1( * control variable ha3 not F03t a name and a val0e, li1e an ordinary variable, b0t al3o a limit, a 3tep, and a
re2erence to the 3tatement a2ter the corre3pondin4 D<7 3tatement( +er30ade yo0r3el2 that 5hen the D<7 3tatement i3
exec0ted all thi3 in2ormation i3 available (03in4 the initial val0e a3 the 2ir3t val0e the variable ta1e3), and al3o that thi3
in2ormation i3 eno04h 2or the ,E.8 3tatement to 1no5 by ho5 m0ch to increa3e the val0e, 5hether to F0mp bac1,
and i2 3o 5here to F0mp bac1 to(
2( 70n the third pro4ram above and then type
PRINT c
/hy i3 the an35er #, and not "A
(*n35er: the ,E.8 command in line #' i3 exec0ted 2ive time3, and each time 1 i3 added to c( 8he la3t time, c
become3 #> and then the ,E.8 command decide3 not to loop bac1, b0t to carry on, c bein4 pa3t it3 limit()
/hat happen3 i2 yo0 p0t ;8E+ 2 in line 2'A
3( Chan4e the third pro4ram 3o that in3tead o2 a0tomatically addin4 2ive n0mber3, it a313 yo0 to inp0t ho5 many
n0mber3 yo0 5ant addin4( /hen yo0 r0n thi3 pro4ram, 5hat happen3 i2 yo0 inp0t ', meanin4 that yo0 5ant no
n0mber3 addin4A /hy mi4ht yo0 expect thi3 to ca03e problem3 2or the
comp0ter, even tho04h it i3 clear 5hat yo0 meanA (8he comp0ter ha3 to ma1e a 3earch 2or the
command ,E.8 c, 5hich i3 not 030ally nece33ary() In 2act thi3 ha3 all been ta1en care o2(
!( In line 1' o2 the 2o0rth pro4ram above, chan4e 1' to 1'' and r0n the pro4ram( It 5ill print the n0mber3 2rom 1''
to $& on the 3creen, and then 3ay "cro55C at the bottom( 8hi3 i3 to 4ive yo0 a chance to 3ee the n0mber3 that are abo0t
to be 3crolled o22 the top( I2 yo0 pre33 n, ;8<+ or the B7E*G 1ey, the pro4ram 5ill 3top 5ith the report @ >RE?7,
(3NT repeat"( I2 yo0 pre33 any other 1ey, then it 5ill print another 22 line3 and a31 yo0 a4ain(
"( -elete line 3' 2rom the 2o0rth pro4ram( /hen yo0 r0n the ne5 c0rtailed pro4ram, it 5ill print the 2ir3t n0mber and
3top 5ith the me33a4e 0 37( I2 yo0 type
NEHT
the pro4ram 5ill 4o once ro0nd the loop, printin4 o0t the next n0mber(
CHAPTER 5
Subroutines
Summary
GO SUB, RETURN
;ometime3 di22erent part3 o2 the pro4ram 5ill have rather 3imilar Fob3 to do, and yo0 5ill 2ind yo0r3el2 typin4 the
3ame line3 in t5o or more time3> ho5ever thi3 i3 not nece33ary( 6o0 can type the line3 in once, in a 2orm 1no5n a3 a
30bro0tine, and then 03e, or call, them any5here el3e in the pro4ram 5itho0t havin4 to type them in a4ain(
8o do thi3, yo0 03e the 3tatement3 H< ;:B (H< to ;:Bro0tine) and 7E8:7,( 8hi3 ta1e3 the 2orm
23 S*> n
5here n i3 the line n0mber o2 the 2ir3t line in the 30bro0tine( It i3 F03t li1e H< 8< n except that the comp0ter
remember3 5here the H< ;:B 3tatement 5a3 3o that it can come bac1 a4ain a2ter doin4 the 30bro0tine( It doe3 thi3
by p0ttin4 the line n0mber and the 3tatement n0mber 5ithin the line (to4ether the3e con3tit0te the ret0m addre33) on
top o2 a pile o2 them (the H< ;:B 3tac1)>
RET*RN
ta1e3 the top return address o22 the H< ;:B 3tac1, and 4oe3 to the 3tatement a2ter it(
*3 an example, let=3 loo1 at the n0mber 40e33in4 pro4ram a4ain( 7etype it a3 2ollo53:
10 REM $? rearra&e% &ue""#& &ame$
20 INP*T a6 (LS
30 INP*T $2ue"" t:e umber $,b
)0 I' a=b T4EN PRINT $(orrect$6 ST3P
50 I' aDb T4EN23 S*> 100
10 I' a>b T4EN 23 S*> 100
E0 23 T3 30
100 PRINT $Tr; a&a#$
110 RET*RN
8he H< 8< 3tatement in line $' i3 very important beca03e other5i3e the pro4ram 5ill r0n on into the 30bro0tine
and ca03e an error (E RET*RN I#t:out 23 S*>) 5hen the 7E8:7, 3tatement i3 reached(
Cere i3 another rather 3illy pro4ram ill03tratin4 the 03e o2 H< ;:B(
100 LET G=10
110 23S*> 500
120 PRINT "
130 LET G=G+)
1)0 23 S*> 500
150 PRINT "
110 LET G=G+2
1E0 23 S*> 500
180 PRINT "
100 ST3P
500 LET "=0
510 '3R ;=1 T3 G
520 LET "="+;
530 NEHT ;
5)0 RET*RN
/hen thi3 pro4ram i3 r0n, 3ee i2 yo0 can 5or1 o0t 5hat i3 happenin4( 8he 30bro0tine 3tart3 at line "''(
* 30bro0tine can happily call another, or even it3el2 (a 30bro0tine that call3 it3el2 i3 recursive), 3o don=t be a2raid o2
havin4 3everal layer3(
CHAPTER 6
R8$7- 7$T$- R8STOR8
Summary
READ, DATA, RESTORE
In 3ome previo03 pro4ram3 5e 3a5 that in2ormation, or data, can be entered directly into the comp0ter 03in4 the
I,+:8 3tatement( ;ometime3 thi3 can be very tedio03, e3pecially i2 a lot o2 the data i3 repeated every time the
pro4ram i3 r0n( 6o0 can 3ave a lot o2 time by 03in4 the 7E*-, -*8* and 7E;8<7E command3( Dor example:
10 RE?@ a,b,c
20 PRINT a,b,c
30 @?T? 10,20,30
)0 ST3P
* 7E*- 3tatement con3i3t3 o2 7E*- 2ollo5ed by a li3t o2 the name3 o2 variable3, 3eparated by comma3( It 5or13
rather li1e an I,+:8 3tatement, except that in3tead o2 4ettin4 yo0 to type in the val0e3 to 4ive to the variable3, the
comp0ter loo13 0p the val0e3 in the -*8* 3tatement(
Each -*8* 3tatement i3 a li3t o2 expre33ion3 J n0meric or 3trin4 expre33ion3 3eparated by comma3( 6o0 can p0t
them any5here yo0 li1e in a pro4ram, beca03e the comp0ter i4nore3 them except 5hen it i3 doin4 a 7E*-( 6o0 m03t
ima4ine the expre33ion3 2rom all the -*8* 3tatement3 in the pro4ram a3 bein4 p0t to4ether to 2orm one lon4 li3t o2
expre33ion3, the -*8* li3t( 8he 2ir3t time the comp0ter 4oe3 to 7E*- a val0e, it ta1e3 the 2ir3t expre33ion 2rom the
-*8* li3t> the next time, it ta1e3 the 3econd> and th03 a3 it meet3 30cce33ive 7E*- 3tatement3, it 5or13 it3 5ay
thro04h the -*8* li3t( (I2 it trie3 to 4o pa3t the end o2 the -*8* li3t, then it 4ive3 an error()
,ote that it=3 a 5a3te o2 time p0ttin4 -*8* 3tatement3 in a direct command, beca03e 7E*- 5ill not 2ind them(
-*8* 3tatement3 have to 4o in the pro4ram( 9et=3 3ee ho5 the3e 2it to4ether in the pro4ram yo0=ve F03t typed in( 9ine
1' tell3 the comp0ter to read three piece3 o2 data and 4ive them the variable3 a, b and c( 9ine 2' then 3ay3 +7I,8
the3e variable3( 8he -*8* 3tatement in line 3' 4ive3 the val0e3 o2 a, b and c( 9ine !' 3top3 the pro4ram( 8o 3ee the
order in 5hich thin43 5or1 chan4e line 2' to:
20 PRINT b,c,a
8he in2ormation in -*8* can be part o2 a D<7 ( ( ( ,E.8 loop( 8ype in
10 '3R =1 T3 1
20 RE?@ @
30 @?T? 2,),1,8,10,12
)0 PRINT @
50 NEHT
10 ST3P
/hen thi3 pro4ram i3 7:, yo0 can 3ee the 7E*- 3tatement movin4 thro04h the -*8* li3t( -*8* 3tatement3 can
al3o contain 3trin4 variable3( Dor example:
10 RE?@ %<
20 PRINT $T:e %ate #"$,%<
30 @?T? $Jue 1"t, 1082$
)0 ST3P
8hi3 i3 the 3imple 5ay o2 2etchin4 expre33ion3 2rom the -*8* li3t: 3tart at the be4innin4 and 5or1 thro04h 0ntil yo0
reach the end( Co5ever, yo0 can ma1e the comp0ter F0mp abo0t in the -*8* li3t, 03in4 the 7E;8<7E 3tatement(
8hi3 ha3 7E;8<7E, 2ollo5ed by a line n0mber, and ma1e3 30b3e?0ent 7E*- 3tatement3 3tart 4ettin4 their data 2rom
the 2ir3t -*8* 3tatement at or a2ter the 4iven line n0mber( (6o0 can mi33 o0t the line n0mber, in 5hich ca3e it i3 a3
tho04h yo0 had typed the line n0mber o2 the 2ir3t line in the pro4ram()
8ry thi3 pro4ram:
10 RE?@ a,b
20 PRINT a,b
30 REST3RE 10
)0 RE?@ G,;,K
50 PRINT G,;,K
10 @?T? 1,2,3
E0 ST3P
In thi3 pro4ram the data re?0ired by line 1' made a=1 and b=2( 8he 7E;8<7E 1' in3tr0ction re3et the variable3,
and allo5ed x, y and L to be 7E*- 3tartin4 2rom the 2ir3t n0mber in the -*8* 3tatement( 7er0n thi3 pro4ram
5itho0t line 3' and 3ee 5hat happen3(
CHAPTER 7
80pressions
Summary
Operations: +, -, `, /
Expressions, scientific notation, variable names
6o0 have already 3een 3ome o2 the 5ay3 in 5hich the B. ;pectr0m can calc0late 5ith n0mber3( It can per2orm the
2o0r arithmetic operation3 P, J, Q and R (remember that Q i3 03ed 2or m0ltiplication, and R i3 03ed 2or divi3ion), and it
can 2ind the val0e o2 a variable, 4iven it3 name(
8he example:
LET taG="um.15/100
4ive3 F03t a hint o2 the very important 2act that the3e calc0lation3 can be combined( ;0ch a combination( Ii1e
30mQ1"R1'', i3 called an expre33ion: 3o an expre33ion i3 F03t a 3hortJhand 5ay o2 tellin4 the comp0ter to do 3everal
calc0lation3, one a2ter the other( In o0r example, the expre33ion 30mQ1"R1'' mean3 =loo1 0p the val0e o2 the variable
called @30m@, m0ltiply it by 1", and divide by 1''=(
I2 yo0 haven=t yet done 3o, 5e recommend that yo0 loo1 thro04h the introd0ctory boo1let to 3ee ho5 the B.
;pectr0m handle3 n0mber3, and the order in 5hich it eval0ate3 mathematical expre33ion3(
8o recap:
E0ltiplication3 and divi3ion3 are done 2ir3t( 8hey have higher priority than addition and 30btraction( 7elative to
each other, m0ltiplication and divi3ion have the 3ame priority, 5hich mean3 that the m0ltiplication3 and divi3ion3 are
done in order 2rom le2t to ri4ht( /hen they are dealt 5ith, the addition3 and 30btraction3 come next J the3e a4ain have
the 3ame priority a3 each other, 3o 5e do them in order 2rom le2t to ri4ht(
*ltho04h all yo0 really need to 1no5 i3 5hether one operation ha3 a hi4her or lo5er priority than another, the
comp0ter doe3 thi3 by havin4 a n0mber bet5een 1 and 1# to repre3ent the priority o2 each operation:
Q and R have priority %, and P and J have priority #(
8hi3 order o2 calc0lation i3 ab3ol0tely ri4id, b0t yo0 can circ0mvent it by 03in4 brac1et3: anythin4 in brac1et3 i3
eval0ated 2ir3t and then treated a3 a 3in4le n0mber(
Expre33ion3 are 03e20l beca03e, 5henever the comp0ter i3 expectin4 a n0mber 2rom yo0, yo0 can 4ive it an
expre33ion in3tead and it 5ill 5or1 o0t the an35er( 8he exception3 to thi3 r0le are 3o 2e5 that they 5ill be 3tated
explicitly in every ca3e(
6o0 can add to4ether a3 many 3trin43 (or 3trin4 variable3) a3 yo0 li1e in a 3in4le expre33ion, and i2 yo0 5ant, yo0
can even 03e brac1et3(
/e really o04ht to tell yo0 5hat yo0 can and cannot 03e a3 the name3 o2 variable3( *3 5e have already 3aid, the
name o2 a 3trin4 variable ha3 to be a 3in4le letter 2ollo5ed by K> and the name o2 the control variable o2 a D<7J,E.8
loop m03t be a 3in4le letter> b0t the name3 o2 ordinary n0meric variable3 are m0ch 2reer( 8hey can 03e any letter3 or
di4it3 a3 lon4 a3 the 2ir3t one i3 a letter( 6o0 can p0t 3pace3 in a3 5ell to ma1e it ea3ier to read, b0t they 5on=t co0nt
a3 part o2 the name( *l3o, it doe3n=t ma1e any di22erence to the name 5hether yo0 type it in capital3 or lo5er ca3e
letter3(
Cere are 3ome example3 o2 the name3 o2 variable3 that are allo5ed:

x
t42
this name is so long that I shall never be able to type it out again
without making a mistake
now we are six Sthe3e la3t t5o name3 are con3idered the 3ame, and
nOWWeaReSiX re2er to the 3ame variableT
8he3e are not allo5ed to be the name3 o2 variable3:
2001 Sit be4in3 5ith a di4itT
3 bears Sbe4in3 5ith a di4itT
M`A`S`H SQ i3 not a letter or a di4itT
Fotherington-Thomas SJ i3 not a letter or a di4itT
,0merical expre33ion3 can be repre3ented by a n0mber and exponent: a4ain re2er to the introd0ctory boo1let( 8ry
the 2ollo5in4 to prove the point:
PRINT 2F3)e0
PRINT 2F3)e1
PRINT 2F3)e2
and 3o on 0p to
PRINT 2F3)e15
6o0 5ill 3ee that a2ter a 5hile the comp0ter al3o 3tart3 03in4 3cienti2ic notation( 8hi3 i3 beca03e no more than
2o0rteen character3 can be 03ed to 5rite a n0mber( ;imilarly, try
PRINT 2F3)e,1
PRINT 2F3)e,2
and 3o on(
+7I,8 4ive3 only ei4ht 3i4ni2icant di4it3 o2 a n0mber( 8ry
PRINT )20)01E205, )20)01E205,)20eE
8hi3 prove3 that the comp0ter can hold the di4it3 o2 !2&!&#$2&", even tho04h it i3 not prepared to di3play them all
at once(
8he B. ;pectr0m 03e3 2loatin4 point arithmetic, 5hich mean3 that it 1eep3 3eparate the di4it3 o2 a n0mber (it3
manti33a) and the po3ition o2 the point (the exponentt( 8hi3 i3 not al5ay3 exact, even 2or 5hole n0mber3( 8ype
PRINT 1e10+1,1e10,1e10,1e10+1
,0mber3 are held to abo0t nine and a hal2 di4it3 acc0racy, 3o 1 e1' i3 too bi4 to be held exactly ri4ht( 8he
inacc0racy (act0ally abo0t 2) i3 more than 1, 3o the n0mber3 1e1' and 1e1'P1 appear to the comp0ter to be e?0al(
Dor an even more pec0liar example, type
PRINT 5e0+1,5e0
Cere the inacc0racy in "e& i3 only abo0t 1, and the 1 to be added on in 2act 4et3 rounded up to 2( 8he n0mber3
"e&P1 and "e&P2 appear to the comp0ter to be e?0al(
8he lar4e3t inte4er (5hole n0mber) that can be held completely acc0rately i3 1 le33 than 32 2=3 m0ltiplied to4ether
(or !,2&!,&#$,2&")(
8he 3trin4 @@ 5ith no character3 at all i3 called the empty or n0ll 3trin4( 7emember that 3pace3 are 3i4ni2icant and an
empty 3trin4 i3 not the 3ame a3 one containin4 nothin4 b0t 3pace3(
8ry:
PRINT $4a!e ;ou B##":e% $'#e&a" WaLe$ ;etC$
/hen yo0 pre33 E,8E7, yo0 5ill 4et the 2la3hin4 ?0e3tion mar1 that 3ho53 there i3 a mi3ta1e 3ome5here in the
line( /hen the comp0ter 2ind3 the do0ble ?0ote3 at the be4innin4 o2 @Dinne4an3 /a1e@, it ima4ine3 that the3e mar1
the end o2 the 3trin4 @have yo0 2ini3hed@, and it then can=t 5or1 o0t 5hat =Dinne4an3 /a1e= mean3(
8here i3 a 3pecial device to 4et over thi3: 5henever yo0 5ant to 5rite a 3trin4 ?0ote 3ymbol in the middle o2 a 3trin4,
yo0 m03t 5rite it t5ice, li1e thi3:
PRINT $4a!e ;ou B##":e% $$'#e&a" WaLe$$ ;etC$
*3 yo0 can 3ee 2rom 5hat i3 printed on the 3creen, each do0ble ?0ote i3 only really there once> yo0 F03t have to type
it t5ice to 4et it reco4niLed(
CHAPTER 8
Strings
Summary
Slicing. using TO. Note that this notation is not standard BASIC.
Hiven a 3trin4, a 30b3trin4 o2 it con3i3t3 o2 3ome con3ec0tive character3 2rom it, ta1en in 3e?0ence( 8h03 @3trin4@ i3 a
30b3trin4 o2 @bi44er 3trin4@, b0t @b 3tin4@ and @bi4 re4@ are not(
8here i3 a notation called slicing 2or de3cribin4 30b3trin43, and thi3 can be applied to arbitrary 3trin4 expre33ion3(
8he 4eneral 2orm i3

string expression (start T3 finish)
3o that, 2or in3tance,
"abcdef"(2 TO 5)"bcde"
I2 yo0 omit the 3tart, then 1 i3 a330med> i2 yo0 omit the 2ini3h then the len4th o2 the 3trin4 i3 a330med( 8h03
"abcdef"( TO 5)"abcdef"(1 TO 5)"abcde"
"abcdef"(2 TO )"abcdef"(2 TO 6)"bcdef"
"abcdef"( TO )"abcdef"(1 TO 6)"abcdef"
(6o0 can al3o 5rite thi3 la3t one a3 @abcde2@(), 2or 5hat it=3 5orth()
* 3li4htly di22erent 2orm mi33e3 o0t the 8< and F03t ha3 one n0mber(
"abcdef"(3)"abcdef"(3 TO 3)"c"
*ltho04h normally both 3tart and 2ini3h m03t re2er to exi3tin4 part3 o2 the 3trin4, thi3 r0le i3 overridden by another
one: i2 the 3tart i3 more than the 2ini3h, then the re30lt i3 the empty 3trin4( ;o
"abcdef"(5 TO 7)
4ive3 error 3 30b3cript 5ron4 beca03e the 3trin4 only contain3 # character3 and $ i3 too many, b0t
"abcdef"(8 TO 7)""
and
"abcdef"(1 TO 0)""
8he 3tart and 2ini3h m03t not be ne4ative, or yo0 4et error > #te&er out oB ra&e( 8hi3 next pro4ram i3 a 3imple
one ill03tratin4 3ome o2 the3e r0le3(
10 LET a<=$abc%eB$
20 '3R =1 T3 1
30 PRINT a<+ T3 1-
)0 NEHT
50 ST3P
8ype ,E/ 5hen thi3 pro4ram ha3 been r0n and enter the next pro4ram:
10 LET a<=$?>LE W?S 1$
20 '3R =1 T3 10
30 PRINT a<+ T3 10-,a<++10,- T3 10-
)0 NEHT
50 ST3P
Dor 3trin4 variable3, 5e can not only extract 30b3trin43, b0t al3o a33i4n to them( Dor in3tance, type
LET a<=$59m t:e MH Spectrum$
and then
LET a<+5 T3 8-=$......$
and
PRINT a<
,otice ho5 3ince the 30b3trin4 aK(" 8< %) i3 only ! character3 lon4, only the 2ir3t 2o0r 3tar3 have been 03ed( 8hi3 i3
a characteri3tic o2 a33i4nin4 to 30b3trin43: the 30b3trin4 ha3 to be exactly the 3ame len4th a2ter5ard3 a3 it 5a3 be2ore(
8o ma1e 30re thi3 happen3, the 3trin4 that i3 bein4 a33i4ned to it i3 c0t o22 on the ri4ht i2 it i3 too lon4, or 2illed o0t
5ith 3pace3 i2 it i3 too 3hort J thi3 i3 called +rocr03tean a33i4nment a2ter the innJ1eeper +rocr03te3 5ho 03ed to ma1e
30re that hi3 40e3t3 2itted the bed by either 3tretchin4 them o0t on a rac1 or c0ttin4 their 2eet o22(
I2 yo0 no5 try
LET a<+-=$4e55o t:ere$
and
PRINT a<8$F$
yo0 5ill 3ee that the 3ame thin4 ha3 happened a4ain (thi3 time 5ith 3pace3 p0t in) beca03e aK() co0nt3 a3 a 30b3trin4(
LET a<=$4e55o t:ere$
5ill do it properly(
Complicated 3trin4 expre33ion3 5ill need brac1et3 ro0nd them be2ore they can be 3liced( Dor example,
$abc$+$%eB$+1 T3 2-=$abc%e$
N$abc$+$%eB$-+1 T3 2-=$ab$
Exercise
1( 8ry 5ritin4 a pro4ram to print o0t the day o2 the 5ee1 03in4 3trin4 3licin4( Cint: let the 3trin4 be
;0nEon80e3/ed8h0r3Dri;at(
CHAPTER 9
9unctions
Summary
DEF
LEN, STR$. VAL, SGN. ABS, INT, SQR
FN
Con3ider the 3a03a4e machine( 6o0 p0t a l0mp o2 meat in at one end, t0rn a handle, and o0t come3 a 3a03a4e at the
other end( * l0mp o2 por1 4ive3 a por1 3a03a4e, a l0mp o2 2i3h 4ive3 a 2i3h 3a03a4e, and a load o2 bee2 a bee2 3a03a4e(
D0nction3 are practically indi3tin40i3hable 2rom 3a03a4e machine3 b0t there i3 a di22erence: they 5or1 on n0mber3
and 3trin43 in3tead o2 meat( 6o0 30pply one val0e (called the argument), mince it 0p by doin4 3ome calc0lation3 on
it, and event0ally 4et another val0e, the result(
Meat in Sausage Machine Sausage out
Argument in Function Result out
-i22erent ar40ment3 4ive di22erent re30lt3, and i2 the ar40ment i3 completely inappropriate the 20nction 5ill 3top and
4ive an error report(
U03t a3 yo0 can have di22erent machine3 to ma1e di22erent prod0ct3 J one 2or 3a03a4e3( another 2or di3h cloth3, and a
third 2or 2i3hJ2in4er3 and 3o on, di22erent 20nction3 5ill do di22erent calc0lation3( Each 5ill have it3 o5n val0e to
di3tin40i3h it 2rom the other3(
6o0 03e a 20nction in expre33ion3 by typin4 it3 name 2ollo5ed by the ar40ment, and 5hen the expre33ion i3
eval0ated the re30lt o2 the 20nction 5ill be 5or1ed o0t(
*3 an example, there i3 a 20nction called 9E,, 5hich 5or13 o0t the len4th o2 a 3trin4( It3 ar40ment i3 the 3trin4
5ho3e len4th yo0 5ant to 2ind, and it3 re30lt i3 the len4th, 3o that i2 yo0 type
PRINT LEN $S#c5a#r$
the comp0ter 5ill 5rite the an35er %( the n0mber o2 letter3 in =;inclair=( (8o 4et 9E,( a3 5ith mo3t 20nction name3,
yo0 m03t 03e extended mode: pre33 C*+; ;CID8 and ;6EB<9 ;CID8 at the 3ame time to chan4e the c0r3or 2rom
L to E, and then pre33 the G 1ey()
I2 yo0 mix 20nction3 and operation3 in a 3in4le expre33ion, then the 20nction3 5ill be 5or1ed o0t be2ore the
operation3 *4ain, ho5ever, yo0 can circ0mvent thi3 r0le by 03in4 brac1et3( Dor in3tance, here are t5o expre33ion3
5hich di22er only in the brac1et3, and yet the calc0lation3 are per2ormed in an entirely di22erent order in each ca3e
(altho04h, a3 it happen3, the end re30lt3 are the 3ame)(
LEN $'re%$+ LEN $>5o&&"$ LEN +$'re%$+$>5o&&"$-
)+LEN $>5o&&"$ LEN +$'re%>5o&&"$-
)+1 LEN $'re%>5o&&"$
10 10
Cere are 3ome more 20nction3(
;87K convert3 n0mber3 into 3trin43: it3 ar40ment i3 a n0mber, and it3 re30lt i3 the 3trin4 that 5o0ld appear on the
3creen i2 the n0mber 5ere di3played by a +7I,8 3tatement( ,ote ho5 it3 name end3 in a K 3i4n to 3ho5 that it3 re30lt
i3 a 3trin4( Dor example, yo0 co0ld 3ay
LET a<=STR< 1e2
5hich 5o0ld have exactly the 3ame e22ect a3 typin4
LET a<=$100$
<r yo0 co0ld 3ay
PRINT LEN STR< 100,000
and 4et the an35er 3, beca03e ;87K 1''(''''=@1''@(
I*9 i3 li1e ;87K in rever3e: it convert3 3trin43 into n0mber3( Dor in3tance,
A?L $3F5$=3F5
In a 3en3e, I*9 i3 the rever3e o2 ;87K, beca03e i2 yo0 ta1e any n0mber, apply ;87K to it, and then apply I*9 to
it, yo0 4et bac1 to the n0mber yo0 2ir3t tho04ht o2(
Co5ever, i2 yo0 ta1e a 3trin4, apply I*9 to it, and then apply ;87K to it, yo0 do not al5ay3 4et bac1 to yo0r
ori4inal 3trin4(
I*9 i3 an extremely po5er20l 20nction, beca03e the 3trin4 5hich i3 it3 ar40ment i3 not re3tricted to loo1in4 li1e a
plain n0mber J it can be any n0meric expre33ion( 8h03, 2or in3tance,
A?L $2.3$=1
or even,
A?L +$2$+$3$- = 1
8here are t5o proce33e3 at 5or1 here( In the 2ir3t, the ar40ment o2 I*9 i3 eval0ated a3 a 3trin4: the 3trin4
expre33ion @2@P@Q3@ i3 eval0ated to 4ive the 3trin4 @2Q3@( 8hen, the 3trin4 ha3 it3 do0ble ?0ote3 3tripped o22, and
5hat i3 le2t i3 eval0ated a3 a n0mber: 3o 2Q3 i3 eval0ated to 4ive the n0mber #(
8hi3 can 4et pretty con203in4 i2 yo0 don=t 1eep yo0r 5it3 abo0t yo0> 2or in3tance,
PRINT A?L $A?L$$A?L$$$$2$$$$$$$
(7emember that in3ide a 3trin4 a 3trin4 ?0ote m03t be 5ritten t5ice( I2 yo0 4o do5n into 20rther depth3 o2 3trin43,
then yo0 2ind that 3trin4 ?0ote3 need to be ?0adr0pled or even oct0pled()
8here i3 another 20nction, rather 3imilar to I*9, altho04h probably le33 03e20l, called I*9K( It3 ar40ment i3 3till a
3trin4, b0t it3 re30lt i3 al3o a 3trin4( 8o 3ee ho5 thi3 5or13, recall ho5 I*9 4oe3 in t5o 3tep3: 2ir3t it3 ar40ment i3
eval0ated a3 a 3trin4, then the 3trin4 ?0ote3 3tripped o22 thi3, and 5hatever i3 le2t i3 eval0ated a3 a n0mber( /ith
I*9K, the 2ir3t 3tep i3 the 3ame, b0t a2ter the 3trin4 ?0ote3 have been 3tripped o22 in the 3econd 3tep, 5hatever i3 le2t
i3 eval0ated a3 another 3trin4( 8h03
A?L< $$$'ru#t puc:$$$ = $'ru#t puc:$
(,otice ho5 the 3trin4 ?0ote3 proli2erate a4ain() -o
LET a<=$00$
and print o0t all o2 the 2ollo5in4: I*9 aK, I*9 @aK@, I*9 @@@aK@@@, I*9K aK, I*9K @aK@ and I*9K @@@aK@@@(
;ome o2 the3e 5ill 5or1, and 3ome o2 them 5on=t> try to explain all the an35er3( (Geep a cool head()
;H, i3 the sign 20nction (3ometime3 called signum)( It i3 the 2ir3t 20nction yo0 have 3een that ha3 nothin4 to do 5ith
3trin43, beca03e both it3 ar40ment and it3 re30lt are n0mber3( 8he re30lt i3 P1 i2 the ar40ment
i3 po3itive, ' i2 the ar40ment i3 Lero, and J1 i2 the ar40ment i3 ne4ative(
*B; i3 another 20nction 5ho3e ar40ment and re30lt are both n0mber3( It convert3 the ar40ment into a po3itive
n0mber (5hich i3 the re30lt) by 2or4ettin4 the 3i4n, 3o that 2or in3tance
?>S ,3F2 = ?>S 3F2 = 3F2
I,8 3tand3 2or =inte4er part= J an integer i3 a 5hole n0mber, po33ibly ne4ative( 8hi3 20nction convert3 a 2ractional
n0mber into an inte4er by thro5in4 a5ay the 2ractional part, 3o that 2or in3tance,
INT 3F0=3
Be care20l 5hen yo0 are applyin4 it to ne4ative n0mber3, beca03e it al5ay3 ro0nd3 do5n: th03, 2or in3tance,
INT ,3F0=,)
;N7 calc0late3 the s:uare root o2 a n0mber J the re30lt that, 5hen m0ltiplied by it3el2, 4ive3 the ar40ment( Dor
in3tance,
SQR 4 2 beca03e 2Q2=!
SQR 0.25 0.5 beca03e '("Q'("='(2"
SQR 2 1.4142136 (approximately) beca03e 1(!1!213#Q1(!1!213#=2(''''''1
I2 yo0 m0ltiply any n0mber (even a ne4ative one) by it3el2, the an35er i3 al5ay3 po3itive( 8hi3 mean3 that ne4ative
n0mber3 do not have 3?0are root3, 3o i2 yo0 apply ;N7 to a ne4ative ar40ment yo0 4et an error report ? I!a5#%
?r&umet(
6o0 can al3o de2ine 20nction3 o2 yo0 o5n( +o33ible name3 2or the3e are D, 2ollo5ed by a letter (i2 the re30lt i3 a
n0mber) or D, 2ollo5ed by a letter 2ollo5ed by K (i2 the re30lt i3 a 3trin4)( 8he3e are m0ch 3tricter abo0t brac1et3: the
ar40ment m03t be enclo3ed in brac1et3(
6o0 de2ine a 20nction by p0ttin4 a -ED 3tatement 3ome5here in the pro4ram( Dor in3tance, here i3 the de2inition o2
a 20nction D, 3 5ho3e re30lt i3 the 3?0are o2 the ar40ment:
10 @E' 'N "+G-=G.G6 REM t:e "Ouare oB G
-ED i3 obtained in extended mode, 03in4 ;6EB<9 ;CID8 and 1( /hen yo0 type thi3, the comp0ter 5ill 4ive yo0
D, a0tomatically, beca03e in a -ED 3tatement the -ED i3 al5ay3 2ollo5ed immediately by D,( *2ter thi3, the 3
complete3 the name D, 3 o2 the 20nction(
8he x in brac1et3 i3 a name by 5hich yo0 5i3h to re2er to the ar40ment o2 the 20nction( 6o0 can 03e any 3in4le letter
yo0 li1e 2or thi3 (or, i2 the ar40ment i3 a 3trin4, a 3in4le letter 2ollo5ed by K)(
*2ter the = 3i4n come3 the act0al de2inition o2 the 20nction( 8hi3 can be any expre33ion, and it can al3o re2er to the
ar40ment 03in4 the name yo0=ve 4iven it (in thi3 ca3e, x) a3 tho04h it 5ere an ordinary variable(
/hen yo0 have entered thi3 line, yo0 can invo1e the 20nction F03t li1e one o2 the comp0ter=3 o5n 20nction3, by
typin4 it3 name, D, 3, 2ollo5ed by the ar40ment( 7emember that 5hen yo0 have de2ined a 20nction yo0r3el2, the
ar40ment m03t be enclo3ed in brac1et3( 8ry it o0t a 2e5 time3:
PRINT 'N "+2-
PRINT 'N "+3+)-
PRINT 1+1NT 'N " +LEN $c:#cLe$/2+3-
<nce yo0 have p0t the corre3pondin4 -ED 3tatement into the pro4ram, yo0 can 03e yo0r o5n 20nction3 in
expre33ion3 F03t a3 2reely a3 yo0 can 03e the comp0ter=3(
,ote: in 3ome dialect3 o2 B*;IC yo0 m03t even enclo3e the ar40ment o2 one o2 the comp0ter=3 20nction3 in brac1et3(
8hi3 i3 not the ca3e in B. ;pectr0m B*;IC(
I,8 al5ay3 ro0nd3 do5n( 8o ro0nd to the neare3t inte4er, add (" 2ir3t J yo0 co0ld 5rite yo0r o5n 20nction to do thi3(
20 @E' 'N r+G-=5NT +G+3F5-6 REM &#!e" G rou%e% to t:e eare"t #te&erF
6o0 5ill then 4et, 2or in3tance,
'N r+2F0- = 3 'N r+2F)- = 2
'N r+,2F0- = ,3 'N r+,2F)- = ,2
Compare the3e 5ith the an35er3 yo0 4et 5hen yo0 03e I,8 in3tead o2 D, r( 8ype in and r0n the 2ollo5in4:
10 LET G=06 LET ;=06 LET a=10
20 @E' 'N p+G,;-=a+G
30 @E' 'N O+-=a+G.;
)0 PRINT 'N p+2,3-,'N O+-
8here are a lot o2 30btle point3 in thi3 pro4ram(
Dir3t, a 20nction i3 not re3tricted to F03t one ar40ment: it can have more, or even none at all J b0t yo0 m03t 3till
al5ay3 1eep the brac1et3(
;econd, it doe3n=t matter 5hereabo0t3 in the pro4ram yo0 p0t the -ED 3tatement3( *2ter the comp0ter ha3 exec0ted
line 1', it 3imply 31ip3 over line3 2' and 3' to 4et to line !'( 8hey do, ho5ever, have to be 3ome5here in the
pro4ram( 8hey can=t be in a command(
8hird, x and y are both the name3 o2 variable3 in the pro4ram a3 a 5hole, and the name3 o2 ar40ment3 2or the
20nction D, p( D, p temporarily 2or4et3 abo0t the variable3 called x and y, b0t 3ince it ha3 no ar40ment called a, it
3till remember3 the variable a( 8h03 5hen D, p(2,3) i3 bein4 eval0ated, a ha3 the val0e 1' beca03e it i3 the variable, x
ha3 the val0e 2 beca03e it i3 the 2ir3t ar40ment, and y ha3 the val0e 3 beca03e it i3 the 3econd ar40ment( 8he re30lt i3
then, 1'P2Q3=1#( /hen D, ?() i3 bein? eval0ated, on the other hand( there are no ara0ment3( 3o a( x and v all 3till
re2er to the variable3 and have val0e3 1', ' and ' re3pectively( 8he an35er in thi3 ca3e i3 1'P'Q'=1'(
,o5 chan4e line 2' to
20 @E' 'N p+G,;-='N O+-
8hi3 time, D, p(2,3) 5ill have the val0e 1' beca03e D, ? 5ill 3till 4o bac1 to the variable3 x and y rather than 03in4
the ar40ment3 o2 D, p(
;ome B*;lC3 (not the B. ;pectr0m B*;IC) have 20nction3 called 9ED8K, 7IHC8K, EI-K and 89K(
9ED8K (aK,n) 4ive3 the 30b3trin4 o2 aK con3i3tin4 o2 the 2ir3t n character3(
7IHC8K (aK,n) 4ive3 the 30b3trin4 o2 aK con3i3tin4 o2 the character3 2rom nth on(
EI-K (aK, n1, n2) 4ive3 the 30b3trin4 o2 aK con3i3tin4 o2 n2 character3 3tartin4 at the n1th(
89K (aK) 4ive3 the 30b3trin4 o2 aK con3i3tin4 o2 all it3 character3 except the 2ir3t(
6o0 can 5rite 3ome 03erJde2ined 20nction3 to do the 3ame: e(4(
10 @E' 'N t<+a<-=a<+2 T3 -6 REM TL<
20 @E' 'N I<+a< -=a<+ T3 -6 REM LE'T<
Chec1 that the3e 5or1 5ith 3trin43 o2 len4th ' or 1(
,ote that o0r D, IK ha3 t5o ar40ment3, one a n0mber and the other a 3trin4( * 20nction can have 0p to 2# n0meric
ar40ment3 (5hy 2#A) and at the 3ame time 0p to 2# 3trin4 ar40ment3(
Exercise
:3e the 20nction D, 3(x)=xQx to te3t ;N7: yo0 3ho0ld 2ind that
'N "+SPR G-=G

i2 yo0 30b3tit0te any po3itive n0mber 2or x, and
SPR 'N "+G-=?>S G

5hether x i3 po3itive or ne4ative (/hy the *B;A)(
CHAPTER 10
/athematical #unctions
Summary
PI, EXP, LN, SIN, COS, TAN, ASN, ACS, ATN
8hi3 chapter deal3 5ith the mathematic3 that the B. ;pectr0m can handle( N0ite po33ibly yo0 5ill never have to 03e
any o2 thi3 at all, 3o i2 yo0 2ind it too heavy 4oin4, don=t be a2raid o2 31ippin4 it( It cover3 the operation | (rai3in4 to a
po5er), the 20nction3 E.+ and 9,, and the tri4onometrical 20nction3 ;I,, C<;, 8*, and their inver3e3 *;,, *C;,
and *8,(
| and EXP
6o0 can rai3e one n0mber to the po5er o2 another J that mean3 =m0ltiply the 2ir3t n0mber by it3el2 the 3econd
n0mber o2 time3=( 8hi3 i3 normally 3ho5n by 5ritin4 the 3econd n0mber F03t above and to the ri4ht o2 the 2ir3t
n0mber> b0t obvio03ly thi3 5o0ld be di22ic0lt on a comp0ter 3o 5e 03e the 3ymbol | in3tead( Dor example, the po5er3
o2 2 are
2|12
2|22`24 (2 3?0ared)
2|32`2`28 (2 c0bed)
2|42`2`2`216 (2 to the po5er 2o0r)
8h03 at it3 mo3t elementary level, =a|b= mean3 =a m0ltiplied by it3el2 b time3=, b0t obvio03ly thi3 only ma1e3 3en3e i2
b i3 a po3itive 5hole n0mber( 8o 2ind a de2inition that 5or13 2or other val0e3 o2 b, 5e con3ider the r0le
a|(b+c) a|b-a|c
(,otice that 5e 4ive | a hi4her priority than Q and R 3o that 5hen there are 3everal operation3 in one expre33ion, the
|3 are eval0ated be2ore the |3 and R3() 6o0 3ho0ld not need m0ch convincin4 that thi3 5or13 5hen b and c are both
po3itive 5hole n0mber3> b0t i2 5e decide that 5e 5ant it to 5or1 even 5hen they are not, then 5e 2ind o0r3elve3
compelled to accept that
a|0 1
a|(-b) 1/a|b
a|(1/b) = the bth root o2 a, 5hich i3 to 3ay, the n0mber that yo0 have to m0ltiply by it3el2 b time3 to 4et a
and
a|(b`c)(a|b)|c
I2 yo0 have never 3een any o2 thi3 be2ore then don=t try to remember it 3trai4ht a5ay> F03t remember that
a|t (-1)1/a
and
a|t (1/2)SQR a
and maybe 5hen yo0 are 2amiliar 5ith the3e the re3t 5ill be4in to ma1e 3en3e(
Experiment 5ith all thi3 by tryin4 thi3 pro4ram:

10 INP*T a,b,c
20 PRINT a|+b+c-,a|b.a|c
30 23 T3 1)
<2 co0r3e, i2 the r0le 5e 4ave earlier i3 tr0e, then each time ro0nd the t5o n0mber3 that the comp0ter print3 o0t 5ill
be e?0al( (,ote J beca03e o2 the 5ay the comp0ter 5or13 o0t t, the n0mber on the le2t J a in thi3 ca3e J m03t never be
ne4ative()
* rather typical example o2 5hat thi3 20nction can be 03ed 2or i3 that o2 compo0nd intere3t( ;0ppo3e yo0 1eep 3ome
o2 yo0r money in a b0ildin4 3ociety and they 4ive 1"V intere3t per year( 8hen a2ter one year yo0 5ill have not F03t
the 1''V that yo0 had any5ay, b0t al3o the 1"V intere3t that the b0ildin4 3ociety have 4iven yo0, ma1in4 alto4ether
11"V o2 5hat yo0 had ori4inally( 8o p0t it another 5ay, yo0 have m0ltiplied yo0r 30m o2 money by 1(1", and thi3 i3
tr0e ho5ever m0ch yo0 had there in the 2ir3t place( *2ter another year, the 3ame 5ill have happened a4ain, 3o that yo0
5ill then have 1(1"Q1(1"=1(1"12=1(322" time3 yo0r ori4inal 30m o2 money( In 4eneral, a2ter y year3, yo0 5ill have
1(1"|y time3 5hat yo0 3tarted o0t 5ith(
I2 yo0 try thi3 command
'3R ;=0 T3 1086 PRINT ;,10.1F15|Q6 NEHT ;
yo0 5ill 3ee that even 3tartin4 o22 2rom F03t W1', it all mo0nt3 0p ?0ite ?0ic1ly, and 5hat i3 more, it 4et3 2a3ter and
2a3ter a3 time 4oe3 on( (*ltho04h even 3o, yo0 mi4ht 3till 2ind that it doe3n=t 1eep 0p 5ith in2lation()
8hi3 3ort o2 behavio0r, 5here a2ter a 2ixed interval o2 time 3ome ?0antity m0ltiplie3 it3el2 by a 2ixed proportion, i3
called exponential 4ro5th, and it i3 calc0lated by rai3in4 a 2ixed n0mber to the po5er o2 the time(
;0ppo3e yo0 did thi3:
10 @E' 'N a+G-=a|G
Cere, a i3 more or le33 2ixed, by 9E8 3tatement3: it3 val0e 5ill corre3pond to the intere3t rate, 5hich chan4e3 only
every 3o o2ten(
8here i3 a certain val0e 2or a that ma1e3 the 20nction D, a loo1 e3pecially pretty to the trained eye o2 a
mathematician: and thi3 val0e i3 called e( 8he B. ;pectr0m ha3 a 20nction called E.+ de2ined by
EHP G=e|G
:n2ort0nately, e it3el2 i3 not an e3pecially pretty n0mber: it i3 an in2inite nonJrec0rrin4 decimal( 6o0 can 3ee it3 2ir3t
2e5 decimal place3 by doin4
PRINT EHP 1
beca03e E.+ 1 = e|1 = e( <2 co0r3e, thi3 i3 F03t an approximation( 6o0 can never 5rite do5n e exactly(
LN
8he inver3e o2 an exponential 20nction i3 a logarithmic 20nction: the lo4arithm (to ba3e a) o2 a n0mber 0 i3 the
po5er to 5hich yo0 have to rai3e a to 4et the n0mber 0, and it i3 5ritten lo4
a
x( 8h03 by de2inition a|lo4
a
x=x> and it i3
al3o tr0e that lo4 (a|x)=x(
6o0 may 5ell already 1no5 ho5 to 03e ba3e 1' lo4arithm3 2or doin4 m0ltiplication3> the3e are called common
lo4arithm3( 8he B. ;pectr0m ha3 a 20nction 9, 5hich calc0late3 lo4arithm3 to the ba3e e> the3e are called nat0ral
lo4arithm3( 8o calc0late lo4arithm3 to any other ba3e, yo0 m03t divide the nat0ral lo4arithm by the nat0ral lo4arithm
o2 the ba3e:
lo4
a
x = LN x/ LN a
PI
Hiven any circle, yo0 can 2ind it3 perimeter (the di3tance ro0nd it3 ed4e> o2ten called it3 circum#erence) by
m0ltiplyin4 it3 diameter (5idth) by a n0mber called t( (t i3 a Hree1 p, and it i3 03ed beca03e it 3tand3 2or perimeter(
It3 name i3 pi()
9i1e e, t i3 an in2inite nonJrec0rrin4 decimal> it 3tart3 o22 a3 3(1!1"&2#"3"%&((((8he 5ord +I on the ;pectr0m
(extended mode, then E) i3 ta1en a3 3tandin4 2or thi3 n0mber J try +7I,8 +I(
SIN, COS and TAN; ASN, ACS and ATN
8he trigonometrical 20nction3 mea30re 5hat happen3 5hen a point move3 ro0nd a circle( Cere i3 a circle o2 radi03 1
(1 5hatA It doe3n=t matter, a3 lon4 a3 5e 1eep to the 3ame 0nit all the 5ay thro04h( 8here i3 nothin4 to 3top yo0
inventin4 a ne5 0nit o2 yo0r o5n 2or every circle that yo0 happen to be intere3ted in) and a point movin4 ro0nd it(
8he point 3tarted at the 3 o=cloc1 po3ition, and then moved ro0nd in an antiJcloc15i3e direction(
/e have al3o dra5n in t5o line3 called a0es thro04h the centre o2 the circle( 8he one thro04h & o=cloc1 and 3
o=cloc1 i3 called the 0-a0is, and the one thro04h # o=cloc1 and 12 o=cloc1 i3 called the y-a0is(
8o 3peci2y 5here the point i3, yo0 3ay ho5 2ar it ha3 moved ro0nd the circle 2rom it3 3 o=cloc1 3tartin4 po3ition: let
03 call thi3 di3tance a( /e 1no5 that the circ0m2erence o2 the circle i3 2t (beca03e it3 radi03 i3 1 and it3 diameter i3
th03 2): 3o 5hen it ha3 moved a ?0arter o2 the 5ay ro0nd the circle, a=tR2> 5hen it ha3 moved hal25ay ro0nd, a=t>
and 5hen it ha3 moved the 5hole 5ay ro0nd, a=2t(
Hiven the c0rved di3tance ro0nd the ed4e, a, t5o other di3tance3 yo0 mi4ht li1e to 1no5 are ho5 2ar the point i3 to
the ri4ht o2 the yJaxi3, and ho5 2ar it i3 above the xJaxi3( 8he3e are called, re3pectively, the cosine and sine o2 a( 8he
20nction3 C<; and ;I, on the comp0ter 5ill calc0late the3e(
,ote that i2 the point 4oe3 to the le2t o2 the yJaxi3, then the co3ine become3 ne4ative> and i2 the point 4oe3 belo5 the
xJaxi3, the 3ine become3 ne4ative(
*nother property i3 that once a ha3 4ot 0p to 2t, the point i3 bac1 5here it 3tarted and the 3ine and co3ine 3tart
ta1in4 the 3ame val0e3 all over a4ain:
SIN +a+2.PI- = SIN a
(3S +a+2.PI- = (3S a
8he tangent o2 a i3 de2ined to be the 3ine divided by the co3ine> the corre3pondin4 20nction on the comp0ter i3
called 8*,(
;ometime3 5e need to 5or1 the3e 20nction3 o0t in rever3e, 2indin4 the val0e o2 a that ha3 4iven 3ine, co3ine or
tan4ent( 8he 20rction3 to do thi3 are called arcsine (*;, on the comp0ter), arccosine (*C;) and arctangent (*8,)(
In the dia4ram o2 the point movin4 ro0nd the circle, loo1 at the radi03 Foinin4 the centre to the point( 6o0 3ho0ld be
able to 3ee that the di3tance 5e have called a, the di3tance that the point ha3 moved ro0nd the ed4e o2 the circle, i3 a
5ay o2 mea30rin4 the an4le thro04h 5hich the radi03 ha3 moved a5ay 2rom the xJaxi3( /hen a=t/2, the an4le i3 &'
de4ree3> 5hen a=t the an4le i3 1%' de4ree3> and 3o ro0nd to 5hen a=2t, and the an4le i3 3#' de4ree3( 6o0 mi4ht F03t
a3 5ell 2or4et abo0t de4ree3, and mea30re the an4le in term3 o2 a alone: 5e 3ay then that 5e are mea30rin4 the an4le
in radians( 8h03 tR2 radian3=&' de4ree3 and 3o on(
6o0 m03t al5ay3 remember that on the B. ;pectr0m ;I,, C<; and 3o on 03e radian3 and not de4ree3( 8o convert
de4ree3 to radian3, divide by 1%' and m0ltiply by t> to convert bac1 2rom radian3 to de4ree3, yo0 divide by t and
m0ltiply by 1%'(
CHAPTER 11
Random numbers
Summary
RANDOMIZE
RND
8hi3 chapter deal3 5ith the 20nction 7,- and the 1ey5ord 7*,-<EIBE( 8hey are both 03ed in connection 5ith
random n0mber3, 3o yo0 m03t be care20l not to 4et them mixed 0p( 8hey are both on the 3ame 1ey (8)>
7*,-<EIBE ha3 had to be abbreviated to 7*,-(
In 3ome 5ay3 7,- i3 li1e a 20nction: it doe3 calc0lation3 and prod0ce3 a re30lt( It i3 0n030al in that it doe3 not need
an ar40ment(
Each time yo0 03e it, it3 re30lt i3 a ne5 random n0mber bet5een ' and 1( (;ometime3 it can ta1e the val0e ', b0t
never 1()
8ry
10 PRINT RN@
20 23 T3 10
to 3ee ho5 the an35er varie3( Can yo0 detect any pattern$ 6o0 3ho0ldn=t be able to> =random= mean3 that there i3 no
pattern(
*ct0ally, 7,- i3 not tr0ly random, beca03e it 2ollo53 a 2ixed 3e?0ence o2 #""3# n0mber3( Co5ever, the3e are 3o
thoro04hly F0mbled 0p that there are at lea3t no obvio03 pattern3 and 5e 3ay that 7,- i3 pseudo-random(
7,- 4ive3 a random n0mber bet5een ' and 1, b0t yo0 can ea3ily 4et random n0mber3 in other ran4e3( Dor in3tance,
"Q7,- i3 bet5een ' and ", and 1(3P'($Q7,- i3 bet5een 1(3 and 2( 8o 4et 5hole n0mber3, 03e I,8 (rememberin4
that I,8 al5ay3 ro0nd3 do5n) a3 in 1 PI,8 (7,-Q#), 5hich 5e 3hall 03e in a pro4ram to 3im0late dice( 7,-Q# i3 in
the ran4e ' to #, b0t 3ince it never act0ally reache3 #, I,8 (7,-Q#) i3 ',1,2,3,! or "(
Cere i3 the pro4ram:
10 REM %#ce t:roI#& pro&ram
20 (LS
30 '3R =1 T3 2
)0 PRINT 1+1NT +RN@.1-8$ $8
50 NEHT
10 INP*T a<6 23 T3 20
+re33 E,8E7 each time yo0 5ant to thro5 the dice(
8he 7*,-<EIBE 3tatement i3 03ed to ma1e 7,- 3tart o22 at a de2inite place in it3 3e?0ence o2 n0mber3, a3 yo0
can 3ee 5ith thi3 pro4ram:
10 R?N@3MIME 1
20 '3R =1 T3 56 PRINT RN@ ,6 NEHT
30 PRINT6 23 T3 10
*2ter each exec0tion o2 7*,-<EIBE 1, the 7,- 3e?0ence 3tart3 o22 a4ain 5ith '(''22$3""&#( 6o0 can 03e other
n0mber3 bet5een 1 and #""3" in the 7*,-<EIBE 3tatement to 3tart the 7,- 3e?0ence o22 at di22erent place3(
I2 yo0 had a pro4ram 5ith 7,- in it and it al3o had 3ome mi3ta1e3 that yo0 had not 2o0nd, then it 5o0ld help to 03e
7*,-<EIBE li1e thi3 3o that the pro4ram behaved the 3ame 5ay each time yo0 ran it(
7*,-<EIBE on it3 o5n (and 7*,-<EIBE ' ha3 the 3ame e22ect) i3 di22erent, beca03e it really doe3 randomiLe
7,- J yo0 can 3ee thi3 in the next pro4ram(
10 R?N@3MIME
20 PRINT RN@6 23 T3 10
8he 3e?0ence yo0 4et here i3 not very random, beca03e 7*,-<EIBE 03e3 the time 3ince the comp0ter 5a3
35itched on( ;ince thi3 ha3 4one 0p by the 3ame amo0nt each time 7*,-<EIBE i3 exec0ted, the next 7,- doe3
more or le33 the 3ame( 6o0 5o0ld 4et better randomne33 by replacin4 H< 8< 1' by H< 8< 2'(
,ote: Eo3t dialect3 o2 B*;IC 03e 7,- and 7*,-<EIBE to prod0ce random n0mber3, b0t not all 03e them in the
3ame 5ay(
Cere i3 a pro4ram to to33 coin3 and co0nt the n0mber3 o2 head3 and tail3(
10 LET :ea%"=06 LET ta#5"=0
20 LET co#=5NT +RN@.2-
30 I' co#=3 T4EN LET :ea%"=:ea%"+1
)0 I' co#=1 T4EN LET ta#5"=ta#5"+1
50 PRINT :ea%"8$,$8ta#5",
10 I' ta#5"D>1 T4EN PRINT :ea%"/ta#5"8
E0 PRINT6 23 T3 20
8he ratio o2 head3 to tail3 3ho0ld become approximately 1 i2 yo0 4o on lon4 eno04h, beca03e in the lon4 r0n yo0
expect approximately e?0al n0mber3 o2 head3 and tail3(
Exercises
1( 8e3t thi3 r0le:
;0ppo3e yo0 choo3e a n0mber bet5een 1 and %$2 and type
R?N@3MIME yo0r n0mber
8hen the next val0e o2 7,- 5ill be
(75`(your number+1) -1) /65536
2( (Dor mathematician3 only()
9et p be a (lar4e) prime, and let a be a primitive root mod0lo p(
8hen i2 b
i
i3 the re3id0e o2 a
i
mod0lo p (l s b
i
s pJl ), the 3e?0ence
b
i
J1
pJ1
i3 a cyclical 3e?0ence o2 pJ1 di3tinct n0mber3 in the ran4e ' to 1 (excl0din4 1)( By choo3in4 a 30itably, the3e can be
made to loo1 2airly random(
#""3$ i3 a Dermat prime, 2
l#
P1( Beca03e the m0ltiplicative 4ro0p o2 nonJLero re3id0e3 mod0lo #""3$ ha3 a po5er o2
2 a3 it3 order, a re3id0e i3 a primitive root i2 and only i2 it i3 not a ?0adratic re3id0e( :3e Ha033= la5 o2 ?0adratic
reciprocity to 3ho5 that $" i3 a primitive root mod0lo #""3$(
8he B. ;pectr0m 03e3 p=#""3$ and a=$", and 3tore3 3ome b
i
Jl in memory( 7,- entail3 replacin4 b
i
J1 in memory
by b
i11
J1, and yieldin4 the re30lt (b
i11
Jl) (pJl)( 7*,-<EIBE n (5ith 1sns#""3") ma1e3 b
i
e?0al to nP1(
7,- i3 approximately 0ni2ormly di3trib0ted over the ran4e ' to 1(
CHAPTER 12
$rrays
Summary
Arrays (the 5ay the B. ;pectr0m handle3 3trin4 array3 i3 3li4htly nonJ3tandard)(
DIM ( ( (
;0ppo3e yo0 have a li3t o2 n0mber3, 2or in3tance the mar13 o2 ten people in a cla33( 8o 3tore them in the comp0ter
yo0 co0ld 3et 0p a 3in4le variable 2or each per3on( b0t yo0 5o0ld 2ind them very a515ard( 6o0 mi4ht decide to call
the variable Blo443 1, Blo443 2, and 3o on 0p to Blo443 1', b0t the pro4ram to 3et 0p the3e ten n0mber3 5o0ld be
rather lon4 and borin4 to type in(
Co5 m0ch nicer it 5o0ld be i2 yo0 co0ld type thi3:
5 REM t:#" pro&ram I#55 ot IorL
10 '3R =1 T3 10
20 RE?@ >5o&&"
30 NEHT
)0 @?T? 10,2,5,10,11,3,11,1,0,1
/ell yo0 can=t(
Co5ever, there i3 a mechani3m by 5hich yo0 can apply thi3 idea, and it 03e3 arrays( *n array i3 a 3et o2 variable3,
it3 element3, all 5ith the 3ame name, and di3tin40i3hed only by a n0mber (the 30b3cript) 5ritten in brac1et3 a2ter the
name( In o0r example the name co0ld be b (li1e control variable3 o2 D<7 J ,E.8 loop3, the name o2 an array m03t
be a 3in4le letter), and the ten variable3 5o0ld then be b(l), b(2), and 3o on 0p to b(l')(
8he element3 o2 an array are called subscripted variable3, a3 oppo3ed to the 3imple variable3 that yo0 are already
2amiliar 5ith(
Be2ore yo0 can 03e an array, yo0 m03t re3erve 3ome 3pace 2or it in3ide the comp0ter, and yo0 do thi3 03in4 a -IE
(2or dimen3ion) 3tatement(
@IM b+10-
3et3 0p an array called b 5ith dimen3ion 1' (i(e( there are 1' 30b3cripted variable3 b(1),(((,b(1')) and initialiLe3 the 1'
val0e3 to '( It al3o delete3 any array called b that exi3ted previo03ly( (B0t not a 3imple variable( *n array and a
3imple n0merical variable 5ith the 3ame name can coexi3t, and there 3ho0ldn=t be any con203ion bet5een them
beca03e the array variable al5ay3 ha3 a 30b3cript)(
8he 30b3cript can be an arbitrary n0merical expre33ion, 3o no5 yo0 can 5rite
10 '3R =1 T3 10
20 RE?@ b+-
30 NEHT
)0 @?T? 10,2,5,10,11,3,11,1,0,1
6o0 can al3o 3et 0p array3 5ith more than one dimen3ion( In a t5o dimen3ional array yo0 need t5o n0mber3 to
3peci2y one o2 the element3 J rather li1e the line and col0mn n0mber3 to 3peci2y a character po3ition on the televi3ion
3creen J 3o it ha3 the 2orm o2 a table( *lternatively, i2 yo0 ima4ine the line and col0mn n0mber3 (t5o dimen3ion3) a3
re2errin4 to a printed pa4e, yo0 co0ld have an extra dimen3ion 2or the pa4e n0mber3( <2 co0r3e, 5e are tal1in4 abo0t
n0meric array3> 3o the element3 5o0ld not be printed character3 a3 in a boo1, b0t n0mber3( 8hin1 o2 the element3 o2 a
threeJdimen3ional array v a3 bein4 3peci2ied by v (pa4e n0mber, line n0mber, col0mn n0mber)(
Dor example, to 3et 0p a t5oJdimen3ional array c 5ith dimen3ion3 3 and #, yo0 03e a -IE 3tatement
@IM c+3,1-
8hi3 then 4ive3 yo0 3Q#=1% 30b3cripted variable3
c(1 ,1),c(1 ,2), . . .,c(1,6)
c(2, 1 ),c(2,2), . . .,c(2,6)
c(3, 1 ),c(3,2), . . .,c(3,6)
8he 3ame principle 5or13 2or any n0mber o2 dimen3ion3(
*ltho04h yo0 can have a n0mber and an array 5ith the 3ame name, yo0 cannot have t5o array3 5ith the 3ame name,
even i2 they have di22erent n0mber3 o2 dimen3ion3(
8here are al3o 3trin4 array3( 8he 3trin43 in an array di22er 2rom 3imple 3trin43 in that they are o2 2ixed len4th and
a33i4nment to them i3 al5ay3 +rocr03tean J chopped o22 or padded 5ith 3pace3( *nother 5ay o2 thin1in4 o2 them i3 a3
array3 (5ith one extra dimen3ion) o2 3in4le character3( 8he name o2 a 3trin4 array i3 a 3in4le letter 2ollo5ed by K, and
a 3trin4 array and a 3imple 3trin4 variable cannot have the 3ame name (0nli1e the ca3e 2or n0mber3)(
;0ppo3e then, that yo0 5ant an array aK o2 2ive 3trin43( 6o0 m03t decide ho5 lon4 the3e 3trin43 are to be J let 03
30ppo3e that 1' character3 each i3 lon4 eno04h( 6o0 then 3ay
@IM a<+5,10- (type thi3 in)
8hi3 3et3 0p a "Q1' array o2 character3, b0t yo0 can al3o thin1 o2 each ro5 a3 bein4 a 3trin4:
a$(1)a$(1,1) a$(1,2) . . . a$(1,10)
a$(2)a$(2,1) a$(2,2) . . . a$(2,10)
: : : :
a$(5)a$(5,1) a$(5,2) . . . a$(5,10)
I2 yo0 4ive the 3ame n0mber o2 30b3cript3 (t5o in thi3 ca3e) a3 there 5ere dimen3ion3 in the -IE 3tatement, then
yo0 4et a 3in4le character> b0t i2 yo0 mi33 the la3t one o0t, then yo0 4et a 2ixed len4th 3trin4( ;o, 2or in3tance, *K(2,$)
i3 the $th character in the 3trin4 *K(2)> 03in4 the 3licin4 notation, 5e co0ld al3o 5rite thi3 a3 *K(2)($)( ,o5 type
LET a<+2-=$123)51E800$
and
PRINT a<+2-,a<+2,E-
Dor the la3t 30b3cript (the one yo0 can mi33 o0t), yo0 can al3o have a 3licer, 3o that 2or in3tance
a<+2,) T3 8-=a<+2-+) T3 8-=$)51E8$
7emember:
In a 3trin4 array, all the 3trin43 have the 3ame, 2ixed len4th( 8he -IE 3tatement ha3 an extra n0mber (the la3t one) to
3peci2y thi3 len4th( /hen yo0 5rite do5n a 30b3cripted variable 2or a 3trin4 array, yo0 can p0t in an extra n0mber, or
a 3licer, to corre3pond 5ith the extra n0mber in the -IE
3tatement( 6o0 can have 3trin4 array3 5ith no dimen3ion3( 8ype
@IM a<+10-
and yo0 5ill 2ind that aK behave3 F03t li1e a 3trin4 variable, except that it al5ay3 ha3 len4th 1', and a33i4nment to it i3
al5ay3 procr03tean(
Exercises
1( :3e 7E*- and -*8* 3tatement3 to 3et 0p an array mK o2 t5elve 3trin43 in 5hich mK(n) i3 the name o2 the nth
month( (Cint: the -IE 3tatement 5ill be -IE mK(12,&)( 8e3t it by printin4 o0t all the m;(n) (03e a loop)(
8ype
PRINT $oI #" t:e mot: oB $8m<+5-8$#&$8$ I:e merr; 5a%" are p5a;#&$

/hat can yo0 do abo0t all tho3e 3pace3A
CHAPTER 13
!onditions
Summary
AND, OR
NOT
/e 3a5 in Chapter 3 ho5 an ID 3tatement ta1e3 the 2orm
I' condition T4EN ( ( (
8he condition3 there 5ere the relation3 (=, <, , <=, = and <), 5hich compare t5o n0mber3 or t5o 3trin43( 6o0
can al3o combine 3everal o2 the3e, 03in4 the lo4ical operation3, *,-, <7 and ,<8(
<ne relation *,- another relation i3 tr0e 5henever both relation3 are tr0e, 3o yo0 co0ld have a line li1e:
I' a<=$;e"$ ?N@ G>0 T4EN PRINT G
in 5hich x only 4et3 printed i2 aK===ye3@ and x'( 8he B*;IC here i3 3o clo3e to En4li3h that it hardly 3eem3 5orth
3pellin4 o0t the:detail3( *3 in En4li3h, yo0 can Foin lot3 o2 relation3 to4ether 5ith *,-, and then the 5hole lot i3 tr0e
i2 all the individ0al relation3 are(
<ne relation <7 another i3 tr0e 5henever at lea3t one o2 the t5o relation3 i3 tr0e( (7emember that it i3 3till tr0e i2
both the relation3 are tr0e> thi3 i3 3omethin4 that En4li3h doe3n=t al5ay3 imply()
8he ,<8 relation3hip t0rn3 thin43 0p3ide do5n( 8he ,<8 relation i3 tr0e 5henever the relation i3 2al3e, and 2al3e
5henever it i3 tr0eM
9o4ical expre33ion3 can be made 5ith relation3 and *,-, <7 and ,<8, F03t a3 n0merical expre33ion3 can be made
5ith n0mber3 and P, J and 3o on> yo0 can even p0t them in brac1et3 i2 nece33ary( 8hey have prioritie3 in the 3ame
5ay a3 the 030al operation3 P, J, Q, R and |, do: <7 ha3 the lo5e3t priority, then *,-, then ,<8, then the relation3,
and the 030al operation3(
,<8 i3 really a 20nction, 5ith an ar40ment and a re30lt, b0t it3 priority i3 m0ch lo5er than that o2 other 20nction3(
8here2ore it3 ar40ment doe3 not need brac1et3 0nle33 it contain3 *,- or <7 (or both)( ,<8 a=b mean3 the 3ame a3
,<8 (a=b) (and the 3ame a3 a<b, o2 co0r3e)(
< i3 the ne4ation o2 = in the 3en3e that it i3 tr0e i2, and only i2, = i3 2al3e( In other 5ord3,
a<>b i3 the 3ame a3 NOT ab
and al3o
NOT a<>b i3 the 3ame a3 ab
+er30ade yo0r3el2 that = and <= are the ne4ation3 o2 < and re3pectively: th03 yo0 can al5ay3 4et rid o2 ,<8
2rom in 2ront o2 a relation by chan4in4 the relation(
*l3o,
NOT (a 2ir3t lo4ical expre33ion AND a 3econd)
i3 the 3ame a3
NOT (the 2ir3t) OR NOT (the 3econd)
and
NOT (a 2ir3t lo4ical expre33ion OR a 3econd)
i3 the 3ame a3
NOT (the 2ir3t) AND NOT (the 3econd)(
:3in4 thi3 yo0 can 5or1 ,<83 thro04h brac1et3 0ntil event0ally they are all applied to relation3, and then yo0 can
4et rid o2 them( 9o4ically 3pea1in4, ,<8 i3 0nnece33ary, altho04h yo0 mi4ht 3till 2ind that 03in4 it
ma1e3 a pro4ram clearer(
8he 2ollo5in4 3ection i3 ?0ite complicated, and can be 31ipped by the 2aintheartedM
8ry

PRINT 1=2,1 D >2
5hich yo0 mi4ht expect to 4ive a 3yntax error( In 2act, a3 2ar a3 the
comp0ter i3 concerned, there i3 no 30ch thin4 a3 a lo4ical val0e: in3tead
it 03e3 ordinary n0mber3, 30bFect to a 2e5 r0le3(
(i) =, <, , <=, = and < all 4ive n0meric re30lt3: 1 2or tr0e, and ' 2or 2al3e( 8h03 the +7I,8 command above
printed ' 2or =1=2=, 5hich i3 2al3e, and 1 2or =1<2=, 5hich i3 tr0e(
(ii) In
I' condition T4EN ( ( (

the condition can be act0ally any n0meric expre33ion( I2 it3 val0e i3 ', then it co0nt3 a3 2al3e, and any other val0e
lincl0din4 the val0e o2 1 that a tr0e relation 4ive3) co0nt3 a3 tr0e( 8h03 the ID 3tatement mean3 exactly the 3ame a3
I' condition D>0 T4EN ( ( (
(iii) *,-, <7 and ,<8 are al3o n0mberJval0ed operation3(
0 *,- y ha3 the val0e X0 i2 y i3 tr0e (nonJLero)
X' (2al3e), i2 y i3 2al3e (Lero)
0 <7 y ha3 the val0e X1 (tr0e), i2 y i3 tr0e (non Lero)
X0, i2 y i3 2al3e (Lero)
,<8 0 ha3 the val0e X' (2al3e), i2 0 i3 tr0e (nonJLero)
X1 (tr0e), i2 0 i3 2al3e (Lero)
(,otice that =tr0e= mean3 =nonJLero= 5hen 5e=re chec1in4 a 4iven val0e, b0t it mean3 =1= 5hen 5e=re prod0cin4 a ne5
one()
7ead thro04h the chapter a4ain in the li4ht o2 thi3 revelation, ma1in4 30re that it all 5or13(
In the expre33ion3 x *,- y, x <7 y and ,<8 x, x and y 5ill 030ally ta1e the val0e3 ' and 1 2or 2al3e and tr0e(
/or1 o0t the ten di22erent combination3 (2o0r 2or *,-, 2o0r 2or <7 and t5o 2or ,<8) and chec1 that
they do 5hat the chapter lead3 yo0 to expect them to do(
8ry thi3 pro4ram:
10 INP*T a
20 INP*T b
30 PRINT +a ?N@ a>=b-++b ?N@ aDb-
)0 23 T3 10
Each time it print3 the lar4er o2 the t5o n0mber3 a and b( Convince yo0r3el2 that yo0 can thin1 o2

x AND y
a3 meanin4
x i2 y (el3e the re30lt i3 ')
and o2
x OR y
a3 meanin4
x 0nle33 y (in 5hich ca3e the re30lt i3 1)
*n expre33ion 03in4 *,- or <7 li1e thi3 i3 called a conditional expre33ion( *n example 03in4 <7 co0ld be
LET tota5 pr#ce=pr#ce 5e"" taG.+1F15 3R !<=$Kero rate%$-
,otice ho5 *,- tend3 to 4o 5ith addition (beca03e it3 de2a0lt val0e i3 '), and <7 tend3 to 4o 5ith m0ltiplication
(beca03e it3 de2a0lt val0e i3 1)(
6o0 can al3o ma1e 3trin4 val0ed conditional expre33ion3, b0t only 03in4 *,-(
x$ AND y ha3 the val0e XxK i2 y i3 nonJLero
X@@ i2 y i3 Lero
3o it mean3 xK i2 y (el3e the empty 3trin4)(
8ry thi3 pro4ram, 5hich inp0t3 t5o 3trin43 and p0t3 them in alphabetical order(
10 INP*T $t;pe # tIo "tr#&"$9a<,b<
20 I' a<>b< T4EN LET c<=a<6 LET a<=b<6 LET b<=c<
30 PRINT a<8$ $8+$D$ ?N@ a<Db<-++$=$ ?N@ a<=b<-8$ $8b<
)0 23 T3 10
Exercise
1( B*;IC can 3ometime3 5or1 alon4 di22erent line3 2rom En4li3h( Con3ider, 2or in3tance, the En4li3h cla03e =I2 a
doe3n=t e?0al b or c=( Co5 5o0ld yo0 5rite thi3 in B*;ICA 8he an35er i3 not
IF A<>B OR C
nor i3 it
IF A<>B OR A<>C
CHAPTER 14
The !haracter Set
Summary
CODE, CHR$
POKE, PEEK
USR
BIN
8he letter3, di4it3, p0nct0ation mar13 and 3o on that can appear in 3trin43 are called character3, and they ma1e 0p the
alphabet, or character set, that the B. ;pectr0m 03e3( Eo3t o2 the3e character3 are 3in4le symbols, b0t there are 3ome
more, called to*ens, that repre3ent 5hole 5ord3, 30ch a3 +7I,8, ;8<+, < and 3o on(
8here are 2"# character3, and each one ha3 a code bet5een ' and 2""( 8here i3 a complete li3t o2 them in *ppendix
*( 8o convert bet5een code3 and character3, there are t5o 20nction3, C<-E and CC7K(
C<-E i3 applied to a 3trin4, and 4ive3 the code o2 the 2ir3t character in the 3trin4 (or ' i2 the 3trin4 i3 empty)(
CC7K i3 applied to a n0mber, and 4ive3 the 3in4le character 3trin4 5ho3ecode i3 that n0mber(
8hi3 pro4ram print3 o0t the entire character 3et:
10 '3R a=32 T3 2556 PRINT (4RS a86 NEHT a
*t the top yo0 can 3ee a 3pace, 1" 3ymbol3 and p0nct0ation mar13, the ten di4it3, 3even more 3ymbol3, the capital
letter3, 3ix more 3ymbol3, the lo5er ca3e letter3 and 2ive more 3ymbol3( 8he3e are all (except W and Y) ta1en 2rom a
5idelyJ03ed 3et o2 character3 1no5n a3 *;CII (3tandin4 2or *merican ;tandard Code3 2or In2ormation Interchan4e)>
*;CII al3o a33i4n3 n0meric code3 to the3e character3, and the3e are the code3 that the B. ;pectr0m 03e3(
8he re3t o2 the character3 are not part o2 *;CII, and are pec0liar to the B. ;pectr0m( Dir3t amon43t them are a
3pace and 1" pattern3 o2 blac1 and 5hite blob3( 8he3e are called the graphics 3ymbol3 and can be 03ed 2or dra5in4
pict0re3( 6o0 can enter the3e 2rom the 1eyboard, 03in4 5hat i3 called 4raphic3 mode( I2 yo0 pre33 H7*+CIC; (C*+;
;CID8 5ith &) then the c0r3or 5ill chan4e to G( ,o5 the 1ey3 2or the di4it3 1 to % 5ill 4ive the 4raphic3 3ymbol3: on
their o5n they 4ive the 3ymbol3 dra5n on the 1ey3> and 5ith either 3hi2t pre33ed they 4ive the 3ame 3ymbol b0t
inverted, i(e( blac1 become3 5hite, and vice ver3a(
7e4ardle33 o2 3hi2t3, di4it & ta1e3 yo0 bac1 to normal (L) mode and di4it ' i3 -E9E8E(
Cere are the 3ixteen 4raphic3 3ymbol3:
*2ter the 4raphic3 3ymbol3, yo0 5ill 3ee 5hat appear3 to be another copy o2 the alphabet 2rom * to :( 8he3e are
character3 that yo0 can rede2ine yo0r3el2( altho04h 5hen the machine i3 2ir3t 35itched on they are 3et a3 letter3 J they
are called user-de#ined 4raphic3( 6o0 can type the3e in 2rom the 1eyboard by 4oin4 into 4raphic3 mode, and then
03in4 the letter3 1ey3 2rom * to :(
8o de2ine a ne5 character 2or yo0r3el2, 2ollo5 thi3 recipe J it de2ine3 a character to 3ho5 t(
(i) /or1 o0t 5hat the character loo13 li1e( Each character ha3 an %x% 3?0are o2 dot3, each o2 5hich can 3ho5 either
the paper colo0r or the in1 colo0r (3ee the introd0ctory boo1let)( 6o0=d dra5 a dia4ram 3omethin4 li1e thi3, 5ith
blac1 3?0are3 2or the in1 colo0r:
/e=ve le2t a 1 3?0are mar4in ro0nd the ed4e beca03e the other letter3 all have one (except 2or lo5er ca3e letter3 5ith
tail3, 5here the tail 4oe3 ri4ht do5n to the bottom)(
(ii) /or1 o0t 5hich 03erJde2ined 4raphic i3 to 3ho5 nJ let=3 3ay the one corre3pondin4 to +, 3o that i2 yo0 pre33 + in
4raphic3 mode yo0 4et t(
(iii) ;tore the ne5 pattern( Each 03erJde2ined 4raphic ha3 it3 pattern 3tored a3 ei4ht n0mber3, one 2or each ro5( 6o0
can 5rite each o2 the3e n0mber3 a3 BI, 2ollo5ed by ei4ht '=3 or 1 =3 J ' 2or paper, 1 2or in1 J 3o that the ei4ht n0mber3
2or o0r t character are
>IN 00000000
>IN 00000000
>IN 00000010
>IN 00111100
>IN 01010100
>IN 00010100
>IN 00010100
>IN 00000000
(I2 yo0 1no5 abo0t binary n0mber3, then it 3ho0ld help yo0 to 1no5 that BI, i3 03ed to 5rite a n0mber in binary
in3tead o2 the 030al decimal()
8he3e ei4ht n0mber3 are 3tored in memory, in ei4ht place3, each o2 5hich ha3 an addre33( 8he addre33 o2 the 2ir3t
byte, or 4ro0p o2 ei4ht di4it3, i3 :;7 @+@ (+ beca03e that i3 5hat 5e cho3e in (ii)), that o2 the 3econd i3 :;7 @+@P1,
and 3o on 0p to the ei4hth, 5hich ha3 addre33 :;7 @+@P$(
:;7 here i3 a 20nction to convert a 3trin4 ar40ment into the addre33 o2 the 2ir3t byte in memory 2or the
corre3pondin4 03erJde2ined 4raphic( 8he 3trin4 ar40ment m03t be a 3in4le character 5hich can be either the 03erJ
de2ined 4raphic it3el2 or the corre3pondin4 letter (in 0pper or lo5er ca3e)( 8here i3 another 03e 2or :;7, 5hen it3
ar40ment i3 a n0mber, 5hich 5ill be dealt 5ith(
Even i2 yo0 don=t 0nder3tand thi3, the 2ollo5in4 pro4ram 5ill do it 2or yo0:
10 '3R =0 T3 E
20 INP*T roI6 P37E *SR $P$+,roI
30 NEHT
It 5ill 3top 2or I,+:8 data ei4ht time3 to ailo5 yo0 to type in the ei4ht BI, n0mber3 above J type them in the ri4ht
order, 3tartin4 5ith the top ro5(
8he +<GE 3tatement 3tore3 a n0mber directly in memory location, bypa33in4 the mechani3m3 normally 03ed by the
B*;IC( 8he oppo3ite o2 +<GE i3 +EEG, and thi3 allo53 03 to loo1 at the content3 o2 a memory location altho04h it
doe3 not act0ally alter the content3 o2 that location( 8hey 5ill be dealt 5ith properly in Chapter 2!(
*2ter the 03erJde2ined 4raphic3 come the to1en3(
6o0 5ill have noticed that 5e have not printed o0t the 2ir3t 32 character3, 5ith code3 ' to 31( 8he3e are control
character3( 8hey don=t prod0ce anythin4 printable, b0t have 3ome le33 tan4ible e22ect on the televi3ion, or they are
03ed 2or controllin4 3omethin4 other than the televi3ion, and the televi3ion print3 A to 3ho5 that it doe3n=t 0nder3tand
them( 8hey are de3cribed more 20lly in *ppendix *(
8hree that the televi3ion 03e3 are tho3e 5ith code3 #, % and 13> on the 5hole, CC7K % i3 the only one yo0 are li1ely
to 2ind 03e20l(
CC7K # print3 3pace3 in exactly the 3ame 5ay a3 a cornma doe3 in a +7I,8 3tatement 2or in3tance
PRINT 18 (4R< 182
doe3 the 3ame a3
PRINT 1,2
<bvio03ly thi3 i3 not a very clear 5ay o2 03in4 it( * more 30btle 5ay i3 to 3ay
LET a<=$1$+(4R< 1+$2$
PRINT a<
CC7K % i3 =bac13pace=: it move3 the print po3ition bac1 one place J try
PRINT $123)$8 (4R< 88$5$
5hich print3 0p 123"
CC7K 13 i3 =ne5line=: it move3 the print po3ition on to the be4innin4 o2 the next line(
8he televi3ion al3o 03e3 tho3e 5ith code3 1# to 23> the3e are explained in Chapter3 1" and 1#( *ll the control
character3 are li3ted in *ppendix *(
:3in4 the code3 2or the character3 5e can extend the concept o2 =alphabetical orderin4= to cover 3trin43 containin4
any character3, not F03t letter3( I2 in3tead o2 thin1in4 in term3 o2 the 030al alphabet o2 2# letter3 5e 03e the extended
alphabet o2 2"# character3, in the 3ame order a3 their code3, then the principle i3 exactly the 3ame( Dor in3tance, the3e
3trin43 are in their B. ;pectr0m alphabetical order( (,otice the rather odd 2eat0re that lo5er ca3e letter3 come a2ter
all the capital3: 3o @a@ come3 a2ter @B@> al3o, 3pace3 matter()
CHR$ 3+"ZOOLOGICAL GARDENS"
CHR$ 8+"AARDVARK HUNTING"
"AAAARGH!"
"(Parenthetical remark)"
"100"
"129.95 inc. VAT"
"AASVOGEL"
"Aardvark"
"PRINT"
"Zoo"
"interpolationl"
"aardvark"
"aasvogel"
"zoo"
"zoology
Cere i3 the r0le 2or 2indin4 o0t 5hich order t5o 3trin43 come in( Dir3t, compare the 2ir3t character3( I2 they are
di22erent, then one o2 them ha3 it3 code le33 than the other, and the 3trin4 it came 2rom i3 the earlier (le33er) o2 the t5o
3trin43( I2 they are the 3ame, then 4o on to compare the next character3( I2 in thi3 proce33 one o2 the 3trin43 r0n3 o0t
be2ore the other, then that 3trin4 i3 the earlier, other5i3e they m03t be e?0al(
8he relation3 =, <, , <=, = and < are 03ed 2or 3trin43 a3 5ell a3 2or n0mber3: < mean3 =come3 be2ore= and
mean3 =come3 a2ter=, 3o that
"AA man"<"AARDVARK"
"AARDVARK">"AA man"

are both tr0e(
<= and = 5or1 the 3ame 5ay a3 they do 2or n0mber3, 3o that
"The same string"<"The same string"
i3 tr0e, b0t
"The same string"<"The same string"
i3 2al3e(
Experiment on all thi3 03in4 the pro4ram here, 5hich inp0t3 t5o 3trin43 and p0t3 them in order(
10 INP*T $T;pe # tIo "tr#&"6$, a<, b<
20 I' a<>b< T4EN LET c<=a<6 LET a<=b<6 LET b<=c<
30 PRINT a<8$ $8
)0 I' a<Db< T4EN PRINT $D$86 23 T3 10
50 PRINT $=$
10 PRINT $ $8b<
E0 23 T3 10
,ote ho5 5e have to introd0ce cK in line 2' 5hen 5e 35ap over aK and bK,
LET a<=b<6 LET b<=a<
5o0ld not have the de3ired e22ect(
8hi3 pro4ram 3et3 0p 03erJde2ined 4raphic3 to 3ho5 che33 piece3:
P 2or pa5n
R 2or roo1
N 2or 1ni4ht
B 2or bi3hop
K 2or 1in4
Q 2or ?0een
Che33 piece3:
5 LET b=>IN 011111006 LET c=>IN 001110006 LET %=>IN 00010000
10 '3R =1 T3 16 RE?@ p<6 REM 1 p#ece"
20 '3R B=0 T3 E6 REM rea% p#ece #to 8 b;te"
30 RE?@ a6 P37E *SR p<+B,a
)0 NEHT B
50 NEHT
100 REM b#":op
110 @?T? $b$,0,%, >IN 00101000,>IN 01000100
120 @?T? >IN 01101100,c,b,0
130 REM L#&
1)0 @?T? $L$,0,%,c,%
150 @?T? c, >IN 01000100,c,0
110 REM rooL
1E0 @?T? $r$,0, >IN 01010100,b,c
180 @?T? c,b,b,0
100 REM Ouee
200 @?T? $O$,0, >IN 01010100, >IN 00101000,%
210 @?T? >IN 01101100,b,b,0
220 REM paI
230 @?T? $p$,0,0,%,c
2)0 @?T? c,%,b,0
250 REM L#&:t
210 @?T? $$,0,%,c, >IN 01111000
2E0 @?T? >IN 00011000,c,b,0
,ote that ' can be 03ed in3tead o2 BI, ''''''''(
/hen yo0 have r0n the pro4ram, loo1 at the piece3 by 4oin4 into 4raphic3 mode(
Exercises
1( Ima4ine the 3pace 2or one 3ymbol divided 0p into 2o0r ?0arter3 li1e a Battenb0r4 ca1e( 8hen i2 each ?0arter can be
either blac1 or 5hite, there are 2x2x2x2=1# po33ibilitie3( Dind them all in the character 3et(
2( 70n thi3 pro4ram:
10 INP*T a
20 PRINT (4R< a8
30 23 T3 10
I2 yo0 experiment 5ith it, yo0=ll 2ind that CC7K a i3 ro0nded to the neare3t 5hole n0mber> and i2 a i3 not in the ran4e
' to 2"" then the pro4ram 3top3 5ith error report > #te&er out oB ra&e(
3( /hich o2 the3e t5o i3 the le33erA
"EVIL"
"evil"
!( /or1 o0t ho5 to modi2y the pro4ram to 3et 0p 03erJde2ined 4raphic3 3o that it 03e3 7E*- and -*8* 3tatement3
in3tead o2 the I,+:8 3tatement(
CHAPTER 15
/ore about ,R.;T and .;,4T
Summary

CLS
PRINT items: nothing at all
Expressions (numeric or string type): TAB numeric expression, AT numeric expression, numeric
expression
PRINT separators: ,; '
INPUT items: variables (numeric or string type)
LINE string variable
Any PRINT item not beginning with a letter. (Tokens are not considered as beginning with a letter.)
Scrolling.
SCREEN$
6o0 have already 3een +7I,8 03ed ?0ite a lot, 3o yo0 5ill have a ro04h idea o2 ho5 it i3 03ed( Expre33ion3 5ho3e
val0e3 are printed are called +7I,8 items, and they are 3eparated by comma3 or 3emicolon3, 5hich are called +7I,8
separators( * +7I,8 item can al3o be nothin4 at all, 5hich i3 a 5ay o2 explainin4 5hat happen3 5hen yo0 03e t5o
comma3 in a ro5(
8here are t5o more 1ind3 o2 +7I,8 item3, 5hich are 03ed to tell the comp0ter not 5hat, b0t 5here to print( Dor
example +7I,8 *8 11,1#>@Q@ print3 a 3tar in the middle o2 the 3creen(
AT line, column
move3 the +7I,8 po3ition (the place 5here the next item i3 to be printed) to the line and col0mn 3peci2ied( 9ine3 are
n0mbered 2rom ' (at the top) to 21, and col0mn3 2rom ' (on the le2t) to 31(
;C7EE,K i3 the rever3e 20nction to +7I,8 *8, and 5ill tell yo0 (5ithin limit3) 5hat character i3 at a partic0lar
po3ition on the 3creen( It 03e3 line and col0mn n0mber3 in the 3ame 5ay a3 +7I,8 *8, b0t enclo3ed in brac1et3: 2or
in3tance
PRINT S(REEN< +11,11-
5ill retrieve the 3tar yo0 printed in the para4raph above(
Character3 ta1en 2rom to1en3 print normally, a3 3in4le character3, and 3pace3 ret0rn a3 3pace3( 9ine3 dra5n by
+9<8, -7*/ or CI7C9E, 03erJde2ined character3 and 4raphic3 character3 ret0rn a3 a n0ll (empty) 3trin4, ho5ever(
8he 3ame applie3 i2 <IE7 ha3 been 03ed to create a compo3ite character(
TAB column
print3 eno04h 3pace3 to move the +7I,8 po3ition to the col0mn 3peci2ied( It 3tay3 on the 3ame line( or, i2 thi3 5o0ld
involve bac13pacin4, move3 on to the next one( ,ote that the comp0ter red0ce3 the col0mn n0mber =mod0lo 32= (it
divide3 by 32 and ta1e3 the remainder)> 3o 8*B 33 mean3 the 3ame a3 8*B 1(
*3 an example,
PRINT T?> 30818T?> 128$(otet"$8 ?T 3,18$(4?PTER$8T?> 2)8$pa&e$
i3 ho5 yo0 mi4ht print o0t the headin4 o2 a content3 pa4e on pa4e 1 o2 a boo1(

8ry r0nnin4 thi3:
10 '3R =8 T3 23
20 PRINT T?> 8.88
30 NEHT
8hi3 3ho53 5hat i3 meant by the 8*B n0mber3 bein4 red0ced mod0lo 32(
Dor a more ele4ant example, chan4e the % in line 2' to a #(
;ome 3mall point3:
(i) 8he3e ne5 item3 are be3t terminated 5ith 3emicolon3, a3 5e have done above( 6o0 can 03e comma3 (or nothin4, at
the end o2 the 3tatement), b0t thi3 mean3 that a2ter havin4 care20lly 3et 0p the +7I,8 po3ition yo0 immediately move
it on a4ain not 030ally terribly 03e20l(
(ii) 6o0 cannot print on the bottom t5o line3 (22 and 23) on the 3creen beca03e they are re3erved 2or command3,
I,+:8 data, report3 and 3o on( 7e2erence3 to =the bottom line= 030ally mean line 21(
(iii) 6o0 can 03e *8 to p0t the +7I,8 po3ition even 5here there i3 already 3omethin4 printed> the old 3t022 5ill be
obliterated 5hen yo0 print more(
*nother 3tatement connected 5ith +7I,8 i3 C9;( 8hi3 clear3 the 5hole 3creen, 3omethin4 that i3 al3o done by
C9E*7 and 7:,(
/hen the printin4 reache3 the bottom o2 the 3creen, it 3tart3 to 3croll 0p5ard3 rather li1e a type5riter( 6o0 can 3ee
thi3 i2 yo0 do
(LS6 '3R =1 T3 226 PRINT 6 NEHT
and then do
PRINT 00
a 2e5 time3(
I2 the comp0ter i3 printin4 o0t ream3 and ream3 o2 3t022, then it ta1e3 4reat care to ma1e 30re that nothin4 i3 3crolled
o22 the top o2 the 3creen 0ntil yo0 have had a chance to loo1 at it properly( 6o0 can 3ee thi3 happenin4 i2 yo0 type
(LS6 '3R =1 T3 1006 PRINT 6 NEHT
/hen it ha3 printed a 3creen 20ll, it 5ill 3top, 5ritin4 "cro55C at the bottom o2 the 3creen( 6o0 can no5 in3pect the
2ir3t 22 n0mber3 at yo0r lei30re( /hen yo0 have 2ini3hed 5ith them, pre33 y (2or =ye3=) and the comp0ter 5ill 4ive yo0
another 3creen 20ll o2 n0mber3( *ct0ally, any 1ey 5ill ma1e the comp0ter carry on except n (2or =no=), ;8<+
(;6EB<9 ;CID8 and a), or ;+*CE (the B7E*G 1ey)( 8he3e 5ill ma1e the comp0ter 3top r0nnin4 the pro4ram
5ith a report @ >RE?7 , (3NT repeat"(
8he I,+:8 3tatement can do m0ch more than 5e have told yo0 3o 2ar( 6o0 have already 3een I,+:8 3tatement3
li1e
INP*T $4oI o5% are ;ouC$, a&e
in 5hich the comp0ter print3 the caption Co5 old are yo0A at the bottom o2 the 3creen, and then yo0 have to type in
yo0r a4e(
In 2act, an I,+:8 3tatement i3 made 0p o2 item3 and 3eparator3 in exactly the 3ame 5ay a3 a +7I,8 3tatement i3, 3o
Co5 old are yo0A and a4e are both I,+:8 item3( I,+:8 item3 are 4enerally the 3ame a3 +7I,8 item3, b0t there are
3ome very important di22erence3(
Dir3t, an obvio03 extra I,+:8 item i3 the variable 5ho3e val0e yo0 are to type in a4e in o0r example above( 8he
r0le i3 that i2 an I,+:8 item be4in3 5ith a letter, it m03t be a variable 5ho3e val0e i3 to be inp0t(
;econd, thi3 5o0ld 3eem to mean that yo0 can=t print o0t the val0e3 o2 variable3 a3 part o2 a caption> ho5ever, yo0
can 4et ro0nd thi3 by p0ttin4 brac1et3 ro0nd the variable( *ny expre33ion that 3tart3 5ith a letter m03t be enclo3ed in
brac1et3 i2 it i3 to be printed a3 part o2 a caption(
*ny 1ind o2 +7I,8 item that i3 not a22ected by the3e r0le3 i3 al3o an I,+:8 item( Cere i3 an example to ill03trate
5hat=3 4oin4 on:
LET m; a&e = INT +RN@ . 100-6 INP*T +$I am $8m; a&e8 $F $-8$4oI o5% are ;ouC$, ;our a&e
my age i3 contained in brac1et3, 3o it3 val0e 4et3 printed o0t( your age i3 not contained in brac1et3, 3o yo0 have to
type it3 val0e in(
Everythin4 that an I,+:8 3tatement 5rite3 4oe3 to the bottom part o2 the 3creen, 5hich act3 3ome5hat
independently o2 the top hal2( In partic0lar, it3 line3 are n0mbered relative to the top line o2 the bottom hal2, even i2
thi3 ha3 moved 0p the act0al televi3ion 3creen (5hich it doe3 i2 yo0 type lot3 and lot3 o2 I,+:8 data)(
8o 3ee ho5 *8 5or13 in I,+:8 3tatement3, try r0nnin4 thi3:
10 INP*T $T:#" #" 5#e 1F$,a<8 ?T 0,08$T:#" #" 5#e 0F$,a<8 ?T 2,08
$T:#" #" 5#e 2F$,a<8 ?T 1,08 $T:#" #" "t#55 5#e 1F$,a<
(F03t pre33 E,8E7 each time it 3top3() /hen 8hi3 i3 line 2( i3 printed, the lo5er part o2 the 3creen move3 0p to ma1e
room 2or it> b0t the n0mberin4 move3 0p a3 5ell, 3o that the line3 o2 text 1eep their 3ame n0mber3(
,o5 try thi3:
10 '3R =0 T3 106 PRINT ?T ,0886 NEHT
20 INP*T ?T 0,08a<8 ?T 1,08a<8 ?T 2,08a<8 ?T 3,08a<8 ?T ),08a<8 ?T 5,08a<8
*3 the lo5er part o2 the 3creen 4oe3 0p and 0p, the 0pper part i3 0ndi3t0rbed 0ntil the lo5er part threaten3 to 5rite
on the 3ame line a3 the +7I,8 po3ition( 8hen the 0pper part 3tart3 3crollin4 0p to avoid thi3(
*nother re2inement to the I,+:8 3tatement that 5e haven=t 3een yet i3 called 9I,E inp0t and i3 a di22erent 5ay o2
inp0ttin4 3trin4 variable3( I2 yo0 5rite 9I,E be2ore the name o2 a 3trin4 variable to be inp0t, a3 in
INP*T LINE a<
then the comp0ter 5ill not 4ive yo0 the 3trin4 ?0ote3 that it normally doe3 2or a 3trin4 variable, altho04h it 5ill
pretend to it3el2 that they are there( ;o i2 yo0 type in
cat
a3 the I,+:8 data, aK 5ill be 4iven the val0e cat( Beca03e the 3trin4 ?0ote3 do not appear on the 3trin4, yo0 cannot
delete them and type in a di22erent 3ort o2 3trin4 expre33ion 2or the I,+:8 data( 7emember that yo0 cannot 03e 9I,E
2or n0meric variable3(
8he control character3 CC7K B and CC7K 23 have e22ect3 rather li1e *8 and 8*B( 8hey are rather odd a3 control
character3, beca03e 5henever one i3 3ent to the televi3ion to be printed, it m03t be 2ollo5ed by t5o more character3
that do not have their 030al e22ect: they are treated a3 n0mber3 (their code3) to 3peci2y the line and col0mn (2or *8) or
the tab po3ition (2or 8*B)( 6o0 5ill almo3t al5ay3 2ind it ea3ier to 03e *8 and 8*B in the 030al 5ay rather than the
control character3, b0t they mi4ht be 03e20l in 3ome circ0m3tance3( 8he *8 control character i3 CC7K 22( 8he 2ir3t
character a2ter it 3peci2ie3 the line n0mber and the 3econd the col0mn n0mber, 3o that
PRINT (4R< 22+(4R< 1 +(4R< c8
ha3 exactly the 3ame e22ect a3
PRINT ?T 1,c8
8hi3 i3 3o even i2 CC7K 1 or CC7K c 5o0ld normally have a di22erent meanin4 (2or in3tance i2 c=13)> the CC7K 22
be2ore them override3 that(
8he 8*B control character i3 CC7K 23 and the t5o character3 a2ter it are 03ed to 4ive a n0mber bet5een ' and
#""3" 3peci2yin4 the n0mber yo0 5o0ld have in a 8*B item:
PRINT (4RS 23+(4RS a+(4RS b8
ha3 the 3ame e22ect a3
PRINT T?> a+251.b8
6o0 can 03e +<GE to 3top the comp0ter a31in4 yo0 "cro55C by doin4
P37E 23102,255
every 3o o2ten( *2ter thi3 it 5ill 3croll 0p 2"" time3 be2ore 3toppin4 5ith "cro55C( *3 an example, try
10 '3R =0 T3 10000
20 PRINT 6 P37E 23102,255
30 NEHT
and 5atch everythin4 5hiLL o22 the 3creenM
Exercises
1( 8ry thi3 pro4ram on 3ome children, to te3t their m0ltiplication table3(
10 LET m<=$ $
20 LET a=INT +RN@.12-+16 LET b=5NT +RN@.12-+1
30 INP*T +m<- 9 9 $I:at #" $8+a- $8+b-8$C$8c
100 I' c=a,b T4EN LET m<=$R#&:tF$6 23 T3 20
110 LET m<=99Wro&F Tr; a&a#F$6 23 T3 30
I2 they are perceptive, they mi4ht mana4e to 5or1 o0t that they do not have to do the calc0lation them3elve3( Dor
in3tance, i2 the comp0ter a313 them to type the an35er to 2Q3, all they have to type in i3 2Q3(
<ne 5ay o2 4ettin4 ro0nd thi3 i3 to ma1e them inp0t 3trin43 in3tead o2 n0mber3( 7eplace c in line 3' by cK( and in
line 1'' by I*9 cK, and in3ert a line
)0 I' c<D> STR< A?L c< T4EN LET m<=$T;pe #t proper5;, a" a umber,$6
23 T3 30
8hat 5ill 2ool them( *2ter a 2e5 more day3, ho5ever, one o2 them may di3cover that they can 4et ro0nd thi3 by
r0bbin4 o0t the 3trin4 ?0ote3 and typin4 in ;87K (2Q3)( 8o 3top 0p thi3 loophole, yo0 can replace cK in line 3' by
9I,E cK(
CHAPTER 16
!olours
Summary
INK, PAPER, FLASH, BRIGHT, INVERSE, OVER
BORDER
70n thi3 pro4ram:
10 '3R m=) T3 16 >RI24T m
20 '3R =1 T3 1)
30 '3R c=) T3 E
)0 P?PER c6 PRINT $ $86 REM ) co5oure% "pace"
50 NEHT c6 NEHT 6 NEHT m
10 '3R m=0 T3 16 >RI24T m6 P?PER E
E0 '3R c=0 T3 3
80 IN7 c6 PRINT c8$ $8
00 NEHT c6 P?PER 0
100 '3R c=) T0 E
110 IN7 c6 PRINT c8$ $8
120 NEHT c6 NEHT m
130 P?PER E6 IN7 06 >RI24T 0
8hi3 3ho53 the ei4ht colo0r3 (incl0din4 5hite and blac1) and the t5o level3 o2 bri4htne33 that the B. ;pectr0m can
prod0ce on a colo0r televi3ion( (I2 yo0r televi3ion i3 blac1 and 5hite, then yo0 5ill F03t 3ee vario03 3hade3 o2 4rey()
Cere i3 a li3t o2 them 2or re2erence> they are al3o 5ritten over the appropriate n0mber 1ey3(
' J blac1
1 J bl0e
2 J red
3 J p0rple, or ma4enta
! J 4reen
" J pale bl0e, technically called cyan
# J yello5
$ J 5hite
<n a blac1 and 5hite televi3ion, the3e n0mber3 are in order o2 bri4htne33(
8o 03e the3e colo0r3 properly, yo0 need to 0nder3tand a bit abo0t ho5 the pict0re i3 arran4ed(
8he pict0re i3 divided 0p into $#% (2! line3 o2 32) po3ition3 5here character3 can be printed, and each character i3
printed a3 an %x% 3?0are o2 dot3 li1e that belo5 2or a( 8hi3 3ho0ld remind yo0 o2 the 03erJde2ined 4raphic3 in Chapter
1!, 5here 5e had '3 2or the 5hite dot3 and 13 2or the blac1 dot3(
8he character po3ition al3o ha3 a33ociated 5ith it t5o colo0r3: the in1, or 2ore4ro0nd colo0r, 5hich i3 the colo0r 2or
the blac1 dot3 in o0r 3?0are, and the paper, or bac14ro0nd colo0r, 5hich i3 03ed 2or the 5hite dot3( 8o 3tart o22 5ith,
every po3ition ha3 blac1 in1 and 5hite paper 3o 5ritin4 appear3 a3 blac1 on 5hite(
8he character po3ition al3o ha3 a bri4htne33 (normal or extra bri4ht) and 3omethin4 to 3ay 5hether it 2la3he3 or not J
2la3hin4 i3 done by 35appin4 the in1 and paper colo0r3( 8hi3 can all be coded into n0mber3, 3o a character po3ition
then ha3
(i) an %x% 3?0are o2 '3 and 1 3 to de2ine the 3hape o2 the character, 5ith ' 2or paper and 1 2or in1,
(ii) in1 and paper colo0r3, each coded into a n0mber bet5een ' and $,
(iii) a bri4htne33 J ' 2or normal, 1 2or extra bri4ht and
(iv) a 2la3h n0mber J ' 2or 3teady, 1 2or 2la3hin4(
,ote that 3ince the in1 and paper colo0r3 cover a 5hole character po3ition, yo0 cannot po33ibly have more than t5o
colo0r3 in a 4iven bloc1 o2 #! dot3( 8he 3ame 4oe3 2or the bri4htne33 and 2la3h n0mber: they re2er to the 5hole
character po3ition, not individ0al dot3( 8he colo0r3, bri4htne33 and 2la3h n0mber at a 4iven po3ition are called
attrib0te3(
/hen yo0 print 3omethin4 on the 3creen, yo0 chan4e the dot pattern at that po3ition> it i3 le33 obvio03, b0t 3till tr0e,
that yo0 al3o chan4e the attrib0te3 at that po3ition( 8o 3tart o22 5ith yo0 do not notice thi3 beca03e everythin4 i3
printed 5ith blac1 in1 on 5hite paper (and normal bri4htne33 and no 2la3hin4), b0t yo0 can vary thi3 5ith the I,G,
+*+E7, B7IHC8 and D9*;C 3tatement3( 8ry
P?PER 5
and then print a 2e5 thin43: they 5ill all appear on cyan paper, beca03e a3 they are printed the paper colo0r3 at the
po3ition3 they occ0py are 3et to cyan (5hich ha3 code ")(
8he other3 5or1 the 3ame 5ay, 3o a2ter
PAPER n0mber bet5een ' and $
INK n0mber bet5een ' and $
BRIGHT ' or 1 O 8hin1 o2 ' a3 o22
or O and 1 a3 on
FLASH ' or 1 O
any printin4 5ill 3et the corre3pondin4 attrib0te at all the character po3ition3 it 03e3( 8ry 3ome o2 the3e o0t( 6o0
3ho0ld no5 be able to 3ee ho5 the pro4ram at the be4innin4 5or1ed (remember that a 3pace i3 a character that ha3
I,G and +*+E7 the 3ame colo0r)(
8here are 3ome more n0mber3 yo0 can 03e in the3e 3tatement3 that have le33 direct e22ect3(
% can be 03ed in all 2o0r 3tatement3, and mean3 =tran3parent= in the 3en3e that the old attrib0te 3ho53 thro04h(
;0ppo3e, 2or in3tance, that yo0 do
P?PER 8
,o character po3ition 5ill ever have it3 paper colo0r 3et to % beca03e there i3 no 30ch colo0r> 5hat happen3 i3 that
5hen a po3ition i3 printed on, it3 paper colo0r i3 le2t the 3ame a3 it 5a3 be2ore( I,G %, B7IHC8 % and D9*;C %
5or1 the 3ame 5ay 2or the other attrib0te3(
& can be 03ed only 5ith +*+E7 and I,G, and mean3 =contra3t=( 8he colo0r (in1 or paper) that yo0 03e it 5ith i3
made to contra3t 5ith the other by bein4 made 5hite i2 the other i3 a dar1 colo0r (blac1, bl0e, red or ma4enta), and
blac1 i2 the other i3 a li4ht colo0r (4reen, cyan, yello5 or 5hite)(
8ry thi3 by doin4
IN7 06 '3R c=0 T3 E6 P?PER c6 PRINT c6 NEHT c
* more impre33ive di3play o2 it3 po5er i3 to r0n the pro4ram at the be4innin4 to ma1e colo0red 3tripe3, and then
doin4
IN7 06 P?PER 86 PRINT ?T 0,886 '3R =5 T3 10806 PRINT 86 NEXT n
8he in1 colo0r here i3 al5ay3 made to contra3t 5ith the old paper colo0r at each po3ition(
Colo0r televi3ion relie3 on the rather c0rio03 2act that the h0man eye can only really 3ee three colo0r3 J the primary
colo0r3, bl0e, red and 4reen( 8he other colo0r3 are mixt0re3 o2 the3e( Dor in3tance, ma4enta i3 made by mixin4 bl0e
5ith red J 5hich i3 5hy it3 code, 3, i3 the 30m o2 the code3 2or bl0e and red(
8o 3ee ho5 all ei4ht colo0r3 2it to4ether, ima4ine three rectan40lar 3potli4ht3, colo0red bl0e, red and 4reen, 3hinin4
at not ?0ite the 3ame place on a piece o2 5hite paper in the dar1( /here they overlap yo0 5ill 3ee mixt0re3 o2
colo0r3, a3 3ho5n by thi3 pro4ram (note that in1 3pace3 are obtained by 03in4 either ;CID8 5ith % 5hen in H mode):
10 >3R@ER 06 P?PER 06 IN7 E6 (LS
20 '3R a=1 T3 1
30 PRINT T?> 18 IN7 18 $$6 REM 18 #L "Ouare"
)0 NEHT a
50 LET %ata5#e=20
10 23 S*> 1000
E0 LET %ata5#e=210
80 23 S*> 1000
00 ST3P
200 @?T? 2,3,E,5,)
210 @?T? 2,2,1,),)
1000 '3R a=1 T3 1
1010 REST3RE %ata5#e
1020 '3R b=1 T3 5
1030 RE?@ c6 PRINT IN7 c8$$86 REM 1 #L "Ouare"
10)0 NEHT b6 PRINT6 NEHT a
1050 RET*RN
8here i3 a 20nction called *887 that 2ind3 o0t 5hat the attrib0te3 are at a 4iven po3ition on the 3creen( It i3 a 2airly
complicated 20nction, 3o it ha3 been rele4ated to the end o2 thi3 chapter(
8here are t5o more 3tatement3, I,IE7;E and <IE7, 5hich control not the attrib0te3, b0t the dot pattern that i3
printed on the 3creen( 8hey 03e the n0mber3 ' 2or o22 and 1 2or on in the 3ame 5ay a3 D9*;C and B7IHC8 do, b0t
tho3e are the only po33ibilitie3( I2 yo0 do I,IE7;E 1, then the dot pattern3 printed 5ill be the inver3e o2 their 030al
2orm: paper dot3 5ill be replaced by in1 dot3 and vice ver3a( 8h03 a 5o0ld be printed a3
I2 (a3 at 35itchJon) 5e have blac1 in1 and 5hite paper, then thi3 a 5ill appear a3 5hite on blac1 J b0t 5e 3till have
blac1 in1 and 5hite paper at that character po3ition( It i3 the dot3 that have chan4ed(
8he 3tatement
3AER 1
3et3 into action a partic0lar 3ort o2 overprintin4( ,ormally 5hen 3omethin4 i3 5ritten into a character po3ition it
completely obliterate3 5hat 5a3 there be2ore> b0t no5 the ne5 character 5ill 3imply be added in on top o2 the old one
(b0t 3ee Exerci3e 1)( 8hi3 can be partic0larly 03e20l 2or 5ritin4 compo3ite character3, li1e letter3 5ith accent3 on
them, a3 in thi3 pro4ram to print o0t Herman letter3 J an =o= 5ith an 0mla0t above it( (-o ,E/ 2ir3t()
10 3AER 1
20 '3R =1 T3 32
30 PRINT $o$8 (4RS 88$$$$8
)0 NEHT
(notice the control character CC7; % 5hich bac13 0p one 3pace()
8here i3 another 5ay o2 03in4 I,G, +*+E7 and 3o on 5hich yo0 5ill probably 2ind more 03e20l than havin4 them a3
3tatement3( 6o0 can p0t them a3 item3 in a +7I,8 3tatement (2ollo5ed by >), and they then do exactly the 3ame a3
they 5o0ld have done i2 they had been 03ed a3 3tatement3 on their o5n, except that their e22ect i3 only temporary: it
la3t3 a3 2ar a3 the end o2 the +7I,8 3tatement that contain3 them( 8h03 i2 yo0 type
PRINT P?PER 18$G$86 PRINT $;$
then only the x 5ill be on yello5(
I,G and the re3t 5hen 03ed a3 3tatement3 do not a22ect the colo0r3 o2 the lo5er part o2 the 3creen, 5here command3
and I,+:8 data are typed in( 8he lo5er part o2 the 3creen 03e3 the colo0r o2 the border 2or it3 paper colo0r and code
& 2or contra3t 2or it3 I,G colo0r, ha3 2la3hin4 o22, and everythin4 at normal bri4htne33( 6o0 can chan4e the border
colo0r to any o2 the ei4ht normal colo0r3 (not % or &) 03in4 3tatement
>3R@ER colo0r
/hen yo0 type in I,+:8 data, it 2ollo53 thi3 r0le o2 03in4 contra3tin4 in1 on border colo0red paper> b0t yo0 can
chan4e the colo0r o2 the caption3 5ritten by the comp0ter by 03in4 I,G and +*+E7 (and 3o on) item3 in the I,+:8
3tatement, F03t a3 yo0 5o0ld in a +7I,8 3tatement( 8heir e22ect la3t3 either to the end o2 the 3tatement, or 0ntil 3ome
I,+:8 data i3 typed in, 5hichever come3 2ir3t( 8ry
INP*T 'L?S4 18 IN7 18$W:at #" ;our umberC$8
8here i3 one more 5ay o2 chan4in4 the colo0r3 by 03in4 control character3 J rather li1e the control character3 2or *8
and 8*B in Chapter 1"(
CHR$ 16 corre3pond3 to I,G
CHR$ 17 corre3pond3 to +*+E7
CHR$ 18 corre3pond3 to D9*;C
CHR$ 19 corre3pond3 to B7IHC8
CHR$ 20 corre3pond3 to I,IE7;E
CHR$ 21 corre3pond3 to <IE7
8he3e are each 2ollo5ed by one character that 3ho53 a colo0r by it3 code: 3o (2or in3tance)
PRINT (4R< 11+(4R< 08 F F F
ha3 the 3ame e22ect a3
PRINT IN7 08 F F F
<n the 5hole, yo0 5o0ld not bother to 03e the3e control character3 beca03e yo0 mi4ht F03t a3 5ell 03e the colo0r
item3( Co5ever, one very 03e20l thin4 yo0 can do 5ith them i3 p0t them in pro4ram3: thi3 re30lt3 in di22erent part3
bein4 li3ted in di22erent colo0r3, to 3et them apart 2rom each other or even F03t to loo1 pretty( 6o0 m03t p0t them in
a2ter the line n0mber, or they 5ill F03t 4et lo3t(
8o 4et the3e into the pro4ram, yo0 have to enter them 2rom the 1eyboard, mo3tly 03in4 extended mode 5ith the
di4it3(
8he di4it3 ' to $ 3et the corre3pondin4 colo0rJ in1 i2 C*+; ;CID8 i3 al3o pre33ed, paper i2 it i3 not( Eore preci3ely,
i2 yo0 are in E mode and yo0 pre33 a di4it (let 03 3ay # 2or yello5> at any rate it ha3 to be bet5een ' and $ J not % or
&) then t5o character3 are in3erted: 2ir3t CC7; 1$ 2or +*+E7, and CC7K # meanin4 =3et it to yello5=( I2 yo0 had been
pre33in4 C*+; ;CID8 5hen yo0 pre33ed the di4it, yo0 5o0ld have 4ot CC7; 1# meanin4 =3et in1 colo0r= in3tead o2
CC7; 1$(
;ince the3e are t5o character3 yo0 can 4et 3ome odd e22ect3 5hen yo0 r0b them o0t J yo0 m03t pre33 -E9E8E
t5ice, and a2ter the 2ir3t time yo0 5ill o2ten 4et a ?0e3tion mar1 or even odder thin43 appearin4( -o not 5orry> F03t
pre33 -E9E8E a4ain(
and can al3o behave 3tran4ely 5hile the c0r3or i3 movin4 pa3t the control character3(
;till in extended mode,
% 4ive3 CC7K 1& and CC7K ' 2or normal bri4htne33
& 4ive3 CC7K 1& and CC7K 1 2or extra bri4htne33
C*+; ;CID8 5ith % 4ive3 CC7K 1% and CC7K ' 2or no 2la3hin4
C*+; ;CID8 5ith & 4ive3 CC7K 1% and CC7K 1 2or 2la3hin4
8here are a co0ple more in ordinary (9) mode:
C*+; ;CID8 5ith 3 4ive3 CC7; 2' and CC7K ' 2or normal character3
C*+; ;CID8 5ith ! 4ive3 CC7K 2' and CC7; 1 2or inver3e character3
8o 30mmariLe( here i3 a complete de3cription o2 the top ro5 on the 1eyboard:
8he *887 20nction ha3 the 2orm
?TTR (line col0mn)
It3 t5o ar40ment3 are the line and col0mn n0mber3 that yo0 5o0ld 03e in an *8 item, and it3 re30lt i3 a n0mber that
3ho53 the colo0r3 and 3o on at the corre3pondin4 character po3ition on the televi3ion 3creen 6o0 can 03e thi3 a3
2reely in expre33ion3 a3 yo0 can any other 20nction(
8he n0mber that i3 the re30lt i3 the 30m o2 2o0r other n0mber3 a3 2ollo53:
12% I2 the character po3ition i3 2la3hin4, ' i2 it i3 3teady
#! i2 the character po3ition i3 bri4ht, ' i2 it i3 normal
%Q the code 2or the paper colo0r
the code 2or the in1 colo0r
Dor in3tance, i2 the character po3ition i3 2la3hin4 and normal 5ith yello5 paper and bl0e in1 then the 2o0r n0mber3
that 5e have to add to4ether are 12%, ', %Q#=!% and 1, ma1in4 1$$ alto4ether( 8e3t thi3 5ith
PRINT ?T 0,08 'L?S4 18 P?PER 18 IN7 18$ $8 ?TTR +0,0
Exercises
1( 8ry
PRINT $>$8 (4R< 88 3AER 18$/$8
/here the R ha3 c0t thro04h the B, it ha3 le2t a 5hite dot 8hi3 i3 the 5ay overprintin4 5or13 on the B. ;pectr0m:
t5o paper3 or t5o in13 4ive a paper, one o2 each 4ive3 an in1( 8hi3 ha3 the intere3tin4 property that i2 yo0 overprint
5ith the 3ame thin4 t5ice yo0 4et bac1 5hat yo0 3tarted o22 5ith( I2 yo0 no5 type
PRINT (4R< 88 3AER 18$r9
5hy do vo0 recover an 0nblemi3hed BA
P?PER 06 IN7 0
J i3n=t it F03t a3 5ell that the3e don=t a22ect the lo5er part o2 the 3creenA ,o5 type
>3R@ER 0
and 3ee ho5 5ell the comp0ter loo13 a2ter yo0M
3( 70n thi3 pro4ram:
10 P37E 2252E+RN@.E0), RN@.12E
20 23 T3 10
,ever mind ho5 thi3 5or13> it i3 chan4in4 the colo0r3 o2 3?0are3 on the televi3ion 3creen and the 7,-3 3ho0ld
en30re that thi3 happen3 randomly( 8he dia4onal 3tripe3 that yo0 event0ally 3ee are a mani2e3tation o2 the hidden
pattern in 7,- J the pattern that ma1e3 it p3e0dorandom in3tead o2 tr0ly random(
!( 8ype or 9<*- in the che33 piece character3 in Chapter 1!, and then type in thi3 pro4ram 5hich dra53 a dia4ram
o2 a che33 po3ition 03in4 them(
5 REM %raI b5aL boar%
10 LET bb=16 LET bI=26 REM re% a% b5ue Bor boar%
15 P?PER bI6 IN7 bb6 (LS
20 PL3T E0,1286 REM bor%er
30 @R?W 15,06 @R?W 0,,15
)0 @R?W ,15,06 @R?W 0,15
50 P?PER bb
10 REM boar%
E0 '3R =0 T3 36 '3R m=0 T3 3
80 PRINT ?T 1+2., 11+2.m8$ $
00 PRINT ?T E+2., 10+2.m8$ $
100 NEHT m6 NEHT
110 P?PER 8
120 LET pI=16 LET pb=56 REM co5our" oB I:#te a% b5acL p#ece"
200 @IM b< +8,8-6 REM po"#t#o" oB p#ece"
205 REM "et up ##t#a5 po"#t#o"
210 LET b<+1-=$rbOLbr$
220 LET bS+2-=$pppppppp$
230 LET b<+E-= PPPPPPPP
2)0 LET b<+8-=$RN>P7>NR$
300 REM %#"p5a; boar%
310 '3R =1 T3 86 '3R m=1 T3 8
320 LET bc=(3@E b<+,m-6 IN7 pI
325 I' bc=(3@E$ $ T4EN 23 T3 3506 REM "pace
330 I' bc>(3@E $M$ T4EN IN7 pb6 LET bc=bc,326 REM 5oIer ca"e Bor b5acL
3)0 LET bc=bc+E06 REM co!ert to &rap:#c"
350 PRINT ?T 5+, 0+m8 (4R< bc
310 NEHT m6 NEHT
)00 P?PER E6 IN7 0
CHAPTER 17
<raphics
Summary
PLOT, DRAW, CIRCLE
POINT
pixels
In thi3 chapter 5e 3hall 3ee ho5 to dra5 pict0re3 on the B. ;pectr0m( 8he part o2 the 3creen yo0 can 03e ha3 22
line3 and 32 col0mn3, ma1in4 22Q32=$'! character po3ition3( *3 yo0 may remember 2rom Chapter 1#, each o2 the3e
character po3ition3 i3 made o2 an % by % 3?0are o2 dot3, and the3e are called pixel3 (pict0re element3)(
* pixel i3 3peci2ied by t5o n0mber3, it3 coordinate3( 8he 2ir3t, it3 0 coordinate, 3ay3 ho5 2ar it i3 acro33 2rom the
extreme le2tJhand col0mn( (7emember, 0 i3 a cro33), the 3econd, it3 y coordinate, 3ay3 ho5 2ar it i3 0p 2rom the
bottom (5i3e 0p)( 8he3e coordinate3 are 030ally 5ritten a3 a pair in brac1et3, 3o (','), (2"",'), (',1$") and (2"",1$")
are the bottom le2tJ, bottom ri4htJ, top le2tJ and top ri4htJcorner3(
8he 3tatement
PL3T 0 coordinate, y coordinate
in13 in the pixel 5ith the3e coordinate3, 3o thi3 mea3le3 pro4ram
10 PL3T INT +RN@.251-, INT+RN@.1E1-6 INP*T a<6 23 T3 10
plot3 a random point each time yo0 pre33 E,8E7(
Cere i3 a rather more intere3tin4 pro4ram( It plot3 a 4raph o2 the 20nction ;I, (a 3ine 5ave) 2or val0e3 bet5een '
and 2t(
10 '3R =0 T3 255
20 PL3T ,88+80.S5N +/128.PI-
30 NEHT
8hi3 next pro4ram plot3 a 4raph o2 ;N7 (part o2 a parabola) bet5een ' and !:
10 '3R =0 T3 255
20 PL3T ,80.SPR +/1)-
30 NEHT
,otice that pixel coordinate3 are rather di22erent 2rom the line and col0mn in an *8 item( 6o0 may 2ind the dia4ram
in Chapter 1" 03e20l 5hen 5or1in4 o0t pixel coordinate3 and line and col0mn n0mber3(
8o help yo0 5ith yo0r pict0re3, the comp0ter 5ill dra5 3trai4ht line3, circle3 and part3 o2 circle3 2or yo0, 03in4 the
-7*/ and CI7C9E 3tatement3(
8he 3tatement -7*/ to dra5 a 3trai4ht line ta1e3 the 2orm
@R?W 0,y
8he 3tartin4 place o2 the line i3 the pixel 5here the la3t +9<8, -7*/ or CI7C9E 3tatement le2t o22 (thi3 i3 called
the +9<8 po3ition> 7:,, C9E*7, C9; and ,E/ re3et it to the bottom le2t hand corner, at (',')), and the 2ini3hin4
place i3 0 pixel3 to the ri4ht o2 that and y pixel3 0p( 8he -7*/ 3tatement on it3 o5n determine3 the len4th and
direction o2 the line, b0t not it3 3tartin4 point(
Experiment 5ith a 2e5 +9<8 and -7*/ command3, 2or in3tance
PL3T 0,1006 @R?W 80,,35
PL3T 00,1506 @R?W 80,,35
,otice that the n0mber3 in a -7*/ 3tatement can be ne4ative, altho04h tho3e in a +9<8 3tatement can=t(
6o0 can al3o plot and dra5 in colo0r, altho04h yo0 have to bear in mind that colo0r3 al5ay3 cover the 5hole o2 a
character po3ition and cannot be 3peci2ied 2or individ0al pixel3( /hen a pixel i3 plotted, it i3 3et to 3ho5 the 20ll in1
colo0r, and the 5hole o2 the character po3ition containin4 it i3 4iven the c0rrent in1 colo0r( 8hi3 pro4ram
demon3trate3 thi3:
10 >3R@ER 06 P?PER 06 IN7 E6 (LS6 REM b5acL oue "cree
20 LET G1=06 LET ;1=06 REM "tart oB 5#e
30 LET c=16 REM Bor #L co5our, "tart#& b5ue
)0 LET G2=5NT +RN@.251-6 LET ;2=5NT +RN@.1E1-6
REM ra%om B##": oB 5#e
50 @R?W IN7 c8G2,G1,;2,;1
10 LET G1=G26 LET ;1=;26 REM eGt 5#e "tart" I:ere 5a"t oe B##":e%
E0 LET c=c+16 I' c=8 T4EN LET c=16 REM eI co5our
80 23 T3 )0
8he line3 3eem to 4et broader a3 the pro4ram 4oe3 on, and thi3 i3 beca03e a line chan4e3 the colo0r3 o2 all the in1ed
in pixel3 o2 all the character po3ition3 that it pa33e3 thro04h( ,ote that yo0 can embed +*+E7, I,G, D9*;C,
B7IHC8, I,IE7;E and <IE7 item3 in a +9<8 or -7*/ 3tatement F03t a3 yo0 co0ld 5ith +7I,8 and I,+:8(
8hey 4o bet5een the 1ey 5ord and the coordinate3, and are terminated by either 3emicolon3 or comma3(
*n extra 2rill 5ith -7*/ i3 that yo0 can 03e it to dra5 part3 o2 circle3 in3tead o2 3trai4ht line3, by 03in4 an extra
n0mber to 3peci2y an an4le to be t0rned thro04h: the 2orm i3
@R?W x,y,a
0 and y are 03ed to 3peci2y the 2ini3hin4 point o2 the line F03t a3 be2ore and a i3 the n0mber o2 radian3 that it m03t t0rn
thro04h a3 it 4oe3 J i2 a i3 a po3itive it t0rn3 to the le2t, 5hile i2 a i3 a ne4ative it t0rn3 to the ri4ht( *nother 5ay o2
3eein4 a i3 a3 3ho5in4 the 2raction o2 a complete circle that 5ill be dra5n: a complete circle i3 2n radian3, 3o i2 a= t it
5ill dra5 a 3emicircle, i2 a=' "Qt a ?0arter o2 a circle, and 3o on(
Dor in3tance 30ppo3e a=t( 8hen 5hatever val0e3 0 and y ta1e, a 3emicircle 5ill be dra5n( 70n
10 PL3T 100,1006 @R?W 50,50, P5
5hich 5ill dra5 thi3:
2ini3h at (1"',1"')
3tart at (1'',1'')
8he dra5in4 3tart3 o22 in a 3o0thJea3terly direction, b0t by the time it 3top3 it i3 4oin4 northJ5e3t: in bet5een it ha3
t0rned ro0nd thro04h 1%' de4ree3, or t radian3 (the val0e o2 a)(
70n the pro4ram 3everal time3, 5ith +l replaced by vario03 other expre33ion3 e(4( J+I, +IR2, 3Q+IR2, +IR!, 1,'(
8he la3t 3tatement in thi3 chapter i3 the CI7C9E 3tatement, 5hich dra53 an entire circle( 6o0 3peci2y the
coordinate3 o2 the centre and the radi03 o2 the circle 03in4
(IR(LE x coordinate, y coordinate, radi03
U03t a3 5ith +9<8 and -7*/, yo0 can p0t the vario03 3ort3 o2 colo0r item3 in at the be4innin4 o2 a CI7C9E
3tatement(
8he +<I,8 20nction tell3 yo0 5hether a pixel i3 in1 or paper colo0r( It ha3 t5o ar40ment3, the coordinate3 o2 the
pixel (and they m03t be enclo3ed in brac1et3)> and it3 re30lt i3 ' i2 the pixel i3 paper colo0r, 1 i2 it i3 in1 colo0r( 8ry
(LS6 PRINT P3INT +0,0-6 PL3T 0,06 PRINT P3INT +0,0-
8ype
P?PER E6 IN7 0
and let 03 inve3ti4ate ho5 I,IE7;E and <IE7 5or1 in3ide a +9<8 3tatement( 8he3e t5o a22ect F03t the relevant
pixel, and not the re3t o2 the character po3ition3( 8hey are normally o22 (') in a +9<8 3tatement, 3o yo0 only need to
mention them to t0rn them on (1)(
Cere i3 a li3t o2 the po33ibilitie3 2or re2erence:
PLOT> J thi3 i3 the 030al 2orm( It plot3 an in1 dot, i(e( 3et3 the pixel to 3ho5 the in1 colo0r(
PLOT INVERSE 1> J thi3 plot3 a dot o2 in1 eradicator, i(e( it 3et3 the pixel to 3ho5 the paper colo0r(
PLOT OVER 1> J thi3 chan4e3 the pixel over 2rom 5hatever it 5a3 be2ore: 3o i2 it 5a3 in1 colo0r it become3 paper
colo0r, and vice ver3a(
PLOT INVERSE 1; OVER 1> J thi3 leave3 the pixel exactly a3 it 5a3 be2ore> b0t note that it al3o chan4e3 the +9<8
po3ition, 3o yo0 mi4ht 03e it 3imply to do that(
*3 another example o2 03in4 the <IE7 3tatement 2ill the 3creen 0p 5ith 5ritin4 03in4 blac1 on 5hite, and then type
PL3T 0,06 @R?W 3AER 18255,1E5
8hi3 5ill dra5 a 2airly decent line, even tho04h it ha3 4ap3 in it 5herever it hit3 3ome 5ritin4( ,o5 do exactly the
3ame command a4ain( 8he line 5ill vani3h 5itho0t leavin4 any trace3 5hat3oever( 8hi3 i3 the 4reat advanta4e o2
<IE7 1( I2 yo0 had dra5n the line 03in4
PL3T 0,06 @R?W 255,1E5
and era3ed it 03in4
PL3T 0,06 @R?W INAERSE 18255,1E5
then yo0 5o0ld al3o have era3ed 3ome o2 the 5ritin4(
,o5 try
PL3T 0,06 @R?W 3AER 18250,1E5
and try to 0ndra5 it by
@R?W 3AER 18,250,,1E5
8hi3 doe3n=t ?0ite 5or1, beca03e the pixel3 the line 03e3 on the 5ay bac1 are not ?0ite the 3ame a3 the one3 that it
03ed on the 5ay do5n( 6o0 m03t 0ndra5 a line in exactly the 3ame direction a3 yo0 dre5 it(
<ne 5ay to 4et 0n030al colo0r3 i3 to 3pec1le t5o normal one3 to4ether in a 3in4le 3?0are, 03in4 a 03erJde2ined
4raphic( 70n thi3 pro4ram:
1000 '3R =0 T3 1 STEP 2
1010 P37E *SR $a$+, >IN 010101016 P37E *SR $a$++1, >IN 10101010
1020 NEHT
5hich 4ive3 the 03erJde2ined 4raphic corre3pondin4 to a che33board pattern( I2 yo0 print thi3 character (4raphic3
mode, then a) in red in1 on yello5 paper, yo0 5ill 2ind it 4ive3 a rea3onably acceptable oran4e(
Exercises
1( +lay abo0t 5ith +*+E7, I,G, D9*;C and B7IHC8 item3 in a +9<8 3tatement( 8he3e are the part3 that a22ect the
5hole o2 the character po3ition containin4 the pixel( ,ormally it i3 a3 tho04h the +9<8 3tatement had 3tarted o22
PL3T P?PER 88 'L?S4 88 >RI24T 88 ( ( (
and only the in1 colo0r o2 a character po3ition i3 altered 5hen 3omethin4 i3 plotted there, b0t yo0 can chan4e thi3 i2
yo0 5ant(
Be e3pecially care20l 5hen 03in4 colo0r3 5ith I,IE7;E 1( beca03e thi3 3et3 the pixel to 3ho5 the paper colo0r, b0t
chan4e3 the in1 colo0r and thi3 mi4ht not be 5hat yo0 expect(
2( 8ry to dra5 circle3 03in4 ;I, and C<; (i2 yo0 have read Chapter 1', try to 5or1 o0t ho5)( 70n thi3:
10 '3R =0 T3 2.PI STEP P5 /180
20 PL3T 100+80.(3S ,8E+80.S5N
30 NEHT
)0 (IR(LE 150,8E,80
6o0 can 3ee that the CI7C9E 3tatement i3 m0ch ?0ic1er, even i2 le33 acc0rate(
3( 8ry
(IR(LE 100,8E,806 @R?W 50,50
6o0 can 3ee 2rom thi3 that the CI7C9E 3tatement leave3 the +9<8 po3ition at a rather indeterminate place J it i3
al5ay3 3ome5here abo0t hal2 5ay 0p the ri4ht hand 3ide o2 the circle( 6o0 5ill 030ally need to 2ollo5 the CI7C9E
3tatement 5ith a +9<8 3tatement be2ore yo0 do any more dra5in4(
!( Cere i3 a pro4ram to dra5 the 4raph o2 almo3t any 20nction( It 2ir3t a313 yo0 2or a n0mber n> it 5ill plot the val0e3
2rom -n to Pn( It then a313 yo0 2or the 20nction it3el2, inp0t a3 a 3trin4( 8he 3trin4 3ho0ld be an expre33ion 03in4 0 a3
the ar40ment o2 the 20nction(
10 PL3T 0,8E6 @R?W 255,0
20 PL3T 12E,06 @R?W 0,1E5
30 INP*T ", e<
35 LET t=0
)0 '3R B=0 T3 255
50 LET G=+B,128-."/1286 LET ;=A?L e<
10 I' ?>S ;>8E T4EN LET t=06 23 T3 100
E0 I' N3T t T4EN PL3T B,;+886 LET t=16 23 T3 100
80 @R?W 1,;,o5% ;
100 LET o5% ;=INT +;+F5-
110 NEHT B
70n it and, a3 an example, type in 1' 2or the n0mber n and 1'Q8*, 0 2or the 20nction( It 5ill plot a 4raph o2 tan 0
a3 0 ran4e3 2rom J1' to P1'(
CHAPTER 18
/otion
Summary
PAUSE, INKEYS, PEEK
N0ite o2ten yo0 5ill 5ant to ma1e the pro4ram ta1e a 3peci2ied len4th o2 time, and 2or thi3 yo0 5ill 2ind the +*:;E
3tatement 03e20l(
P?*SE n
3top3 comp0tin4 and di3play3 the pict0re 2or n 2rame3 o2 the televi3ion (at "' 2rame3 per 3econd in E0rope or #' in
*merica)( n can be 0p to #""3", 5hich 4ive3 yo0 F03t 0nder 22 min0te3> i2 n=' then it mean3 =+*:;E 2or ever=(
* pa03e can al5ay3 be c0t 3hort by pre33in4 a 1ey (note that a C*+; ;ClD8ed 3pace 5ill ca03e a brea1 a3 5ell)(
6o0 have to pre33 the 1ey do5n a2ter the pa03e ha3 3tarted(
8hi3 pro4ram 5or13 the 3econd hand o2 a cloc1
10 REM '#r"t Ie %raI t:e c5ocL Bace
20 '3R =1 T3 12
30 PRINT ?T 10,10.(3S +/1.PI-,11+10.SIN +/1.PI-8
)0 NEHT
50 REM NoI Ie "tart t:e c5ocL
10 '3R t=0 T3 2000006 REM t #" t:e t#me # "eco%"
E0 LET a=t/30.PI6 REM a #" t:e a&5e oB t:e "eco% :a% # ra%#a"
80 LET "G=80.SIN a6 LET ";=80.(3S a
200 PL3T 128,886 @R?W 3AER 18"G,";6 REM %raI "eco% :a%
210 P?*SE )2
220 PL3T 128,886 @R?W 3AER 18"G,";6 REM era"e "eco% :a%
)00 NEHT t
8hi3 cloc1 5ill r0n do5n a2ter abo0t ""(" ho0r3 beca03e o2 line #', b0t yo0 can ea3ily ma1e it r0n lon4er( ,ote ho5
the timin4 i3 controlled by line 21'( 6o0 mi4ht expect +*:;E "' to ma1e it tic1 one a 3econd, b0t the comp0tin4
ta1e3 a bit o2 time a3 5ell and ha3 to be allo5ed 2or( 8hi3 i3 be3t done by trial and error, timin4 the comp0ter cloc1
a4ain3t a real one, and adF03tin4 line 21' 0ntil they a4ree( (6o0 can=t do thi3 very acc0rately> an adF03tment o2 one
2rame in one 3econd i3 2V or hal2 an ho0r in a day()
8here i3 a m0ch more acc0rate 5ay o2 mea30rin4 time( 8hi3 03e3 the content3 o2 certain memory location3( 8he data
3tored i3 retrieved by 03in4 +EEG( Chapter 2" explain3 5hat 5e=re loo1in4 at in detail( 8he expre33ion 03ed i3
+15531.PEE7 231E)+251.PEE7 231E3+PEE7 231E2-/50
8hi3 4ive3 the n0mber o2 3econd3 3ince the comp0ter 5a3 t0rned on (0p to abo0t 3 day3 and 21 ho0r3, 5hen it 4oe3
bac1 to ')(
Cere i3 a revi3ed cloc1 pro4ram to ma1e 03e o2 thi3:
10 REM '#r"t Ie %raI t:e c5ocL Bace
20 '3R =1 T3 12
30 PRINT ?T 10,10.(3S +/1.PI-,11+10.SIN +/1.PI-8
)0 NEHT
50 @E' 'N t+-=5NT +15531.PEE7 231E)+251.PEE7 231E3+ PEE7 231E2-/50-6
REM umber oB "eco%" "#ce "tart
100 REM NoI Ie "tart t:e c5ocL
110 LET t1='N t+-
120 LET a=t1/30.PI6 REM a #" t:e a&5e oB t:e "eco% :a% # ra%#a"
130 LET "G=E2.SIN a6 LET ";=E2.(3S a
1)0 PL3T 131,016 @R?W 3AER 18"G,";6 REM %raI :a%
200 LET t='N t+-
210 I' tD=t1 T4EN 23 T3 2006 REM Ia#t ut#5 t#me Bor eGt :a%
220 PL3T 131,016 @R?W 3AER 18"G,";6 REM rub out o5% :a%
230 LET t1=t6 23 T3 120
8he internal cloc1 that thi3 methcd 03e3 3ho0ld be acc0rate to abo0t ('1V a3 lon4 a3 the comp0ter i3 F03t r0nnin4 it3
pro4ram, or 1' 3econd3 per day> b0t it 3top3 temporarily 5henever yo0 do BEE+, or a ca33ette tape operation, or 03e
the printer or any o2 the other extra piece3 o2 e?0ipment yo0 can 03e 5ith the comp0ter( *ll the3e 5ill ma1e it lo3e
time(
8he n0mber3 +EEG 23#$!, +EEG 23#$3 and +EEG 23#$2 are held in3ide the comp0ter and 03ed 2or co0ntin4 in
"'th3 o2 a 3econd Each i3 bet5een ' and 2"", and they 4rad0ally increa3e thro04h all the n0mber3 2rom ' to 2"">
a2ter 2"" they drop 3trai4ht bac1 to '(
8he one that increa3e3 mo3t o2ten i3 +EEG 23#$2( Every 1R"' 3econd it increa3e3 by 1( /hen it i3 at 2"", the next
increa3e ta1e3 it to ', and at the 3ame time it n0d4e3 +EEG 23#$3 by 0p to 1( /hen (every 2"#R"' 3econd3) +EEG
23#$3 i3 n0d4ed 2rom 2"" to ', it in t0rn n0d4e3 +EEG 23#$! 0p by 1( 8hi3 3ho0ld be eno04h to explain 5hy the
expre33ion above 5or13(
,o5, con3ider care20lly: 30ppo3e o0r three n0mber3 are ' (2or +EEG 23#$!), 2"" (2or +EEG 23#$3) and 2"" (2or
+EEG 23#$2)( 8hi3 mean3 that it i3 abo0t 21 min0te3 a2ter 35itchJon J o0r expre33ion o04ht to yield
+15531.0+251.255+255-/50= 1 31 0FE
B0t there i3 a hidden dan4er( 8he next time there i3 a 1R"' 3econd co0nt, the three n0mber3 5ill chan4e to 1, ' and '(
Every 3o o2ten, thi3 5ill happen 5hen yo0 are hal2 5ay thro04h eval0atin4 the expre33ion: the comp0ter 5o0ld
eval0ate +EEG 23#$! a3 ', b0t then chan4e the other t5o to ' be2ore it can pee1 them( 8he an35er 5o0ld then be
+15531.0+251.0+0-550=0
5hich i3 hopele33ly 5ron4(
* 3imple r0le to avoid thi3 problem i3 eval0ate the expre33ion t5ice in 30cce33ion and ta1e the lar4er an35er( I2 yo0
loo1 care20lly at the pro4ram above yo0 can 3ee that it doe3 thi3 implicitly(
Cere i3 a tric1 to apply the r0le( -e2ine 20nction3
10 @E' 'N m+G,;-=+G+;+?>S +G,;--/26 REM t:e 5ar&er oB G a% ;
20 @E' 'N u+-=+15531.PEE7 231E)+251.PEE7 231E3+PEE7 231E2-/506
REM t#me, ma; be Iro&
30 @E' 'N t+-='N m+'N u+-, 'N u+--6 REM t#me, r#&:t
6o0 can chan4e the three co0nter n0mber3 3o that they 4ive the real time in3tead o2 the time 3ince the comp0ter 5a3
35itched on( Dor in3tance, to 3et the time at 1'(''am, yo0 5or1 o0t that thi3 i3 1'Q#'Q#'Q"'=1%''''' 2i2tieth3 o2 a
3econd, and that
1800000=15531.2E+251.1 10+1)
8o 3et the three n0mber3 to 2$, 11& and #!, yo0 do
P37E 231E),2E6 P37E 231E3,1106 P37E 231E2,1)
In co0ntrie3 5ith main3 2re?0encie3 o2 #' CertL the3e pro4ram3 m03t replace ="'= by =#'= 5here appropriate(
8he 20nction I,GE6K (5hich ha3 no ar40ment) read3 the 1eyboard( I2 yo0 are pre33in4 exactly one 1ey (or a ;CID8
1ey and F03t one other 1ey) then the re30lt i3 the character that that 1ey 4ive3 in 9 mode> other5i3e the re30lt i3 the
empty 3trin4(
8ry thi3 pro4ram, 5hich 5or13 li1e a type5riter(
10 I' IN7EQ< D>$$ T4EN 23 T3 10
20 I' IN7EQ< =$$ T4EN 23 T3 20
30 PRINT IN7EQS8
)0 23 T3 10
Cere line 1' 5ait3 2or yo0 to li2t yo0r 2in4er o22 the 1eyboard and line 2' 5ait3 2or yo0 to pre33 a ne5 1ey(
7emember that 0nli1e I,+:8, I,GE6K doe3n=t 5ait 2or yo0( ;o yo0 don=t type E,8E7, b0t on the other hand i2
yo0 don=t type anythin4 at all then yo0=ve mi33ed yo0r chance(
Exercises
1( /hat happen3 i2 yo0 mi33 o0t line 1' in the type5riter pro4ramA
2( *nother 5ay o2 03in4 I,GE6; i3 in conF0nction 5ith +*:;E, a3 in thi3 alternative type5riter pro4ram(
10 P?*SE 0
20 PRINT IN7EQ<8
30 23 T3 10
8o ma1e thi3 5or1, 5hy i3 it e33ential that a pa03e 3ho0ld not 2ini3h i2 it 2ind3 yo0 already pre33in4 a 1ey 5hen it
3tart3A
3( *dapt the 3econd hand pro4ram 3o that it al3o 3ho53 min0te and ho0r hand3, dra5in4 them every min0te( I2 yo0=re
2eelin4 ambitio03, arran4e 3o that every ?0arter o2 an ho0r it p0t3 on 3ome 1ind o2 3ho5 J yo0 co0ld prod0ce the Bi4
Ben chime3 5ith BEE+( (;ee next chapter()
!( (Dor 3adi3t3() 8ry thi3:
10 I' IN7EQ< =$$ T4EN 23 T3 10
20 PRINT ?T 11,1)8$0*(4=$
30 I' IN7EQS D>$$ T4EN 23 T3 30
)0 PRINT ?T 11,1)8$ $
50 23 T3 10
CHAPTER 19
688,
Summary
BEEP
I2 yo0 haven=t already di3covered that the B. ;pectr0m ha3 a lo0d3pea1er b0ilt into it, read the Introd0ctory boo1let
be2ore carryin4 on(
8he lo0d3pea1er i3 3o0nded by 03in4 the BEE+ 3tatement,
>EEP duration, pitch
5here, a3 030al, =duration= and =pitch= repre3ent any n0merical expre33ion3( 8he d0ration i3 4iven in 3econd3, and the
pitch i3 4iven in 3emitone3 above middle C 03in4 ne4ative n0mber3 2or note3 belo5 middle C(
Cere i3 a dia4ram to 3ho5 the pitch val0e3 o2 all the note3 in one octave on the piano:
8o 4et hi4her or lo5er note3, yo0 have to add or 30btract 12 2or each octave that yo0 4o 0p or do5n(
I2 yo0 have a piano in 2ront o2 yo0 5hen yo0 are pro4rammin4 a t0ne, thi3 dia4ram 5ill probably be all that yo0
need to 5or1 o0t the pitch val0e3( I2, ho5ever, yo0 are tran3cribin4 3trai4ht 2rom 3ome 5ritten m03ic, then 5e 3044e3t
that yo0 dra5 a dia4ram o2 the 3tave 5ith the pitch val0e 5ritten a4ain3t each line and 3pace, ta1in4 the 1ey into
acco0nt(
Dor example, type:
10 PRINT $'rere 2u"ta!$
20 >EEP 1,06 >EEP 1,26 >EEP F5,36 >EEPF5,26 >EEP 1,0
30 >EEP 1,06 >EEP 1,26 >EEP F5,36 >EEPF5,26 >EEP 1,0
)0 >EEP 1,36 >EEP 1,56 >EEP 2,E
50 >EEP 1,36 >EEP 1,56 >EEP 2,E
10 >EEP FE5,E6 >EEP F25,86 >EEP F5,E6 >EEP F5,56>EEP F5,36
>EEPF5,26 >EEP 1,0
E0 >EEP FE5,E6 >EEP F25,86 >EEP F5,E6 >EEP F5,56 >EEP F5,36 >EEP F5,26
>EEP 1,0
80 >EEP 1,06 >EEP 1,,56 >EEP 2,0
00 >EEP 5,06 >EEP 1,,56 >EEP 2,0
/hen yo0 r0n thi3, yo0 3ho0ld 4et the 20neral march 2rom Eahler=3 2ir3t 3ymphony, the bit 5here the 4oblin3 b0ry
the :; Cavalry man(
;0ppo3e 2or example that yo0r t0ne i3 5ritten in the 1ey o2 C minor, li1e the Eahler above( 8he be4innin4 loo13
li1e thi3:
and yo0 can 5rite in the pitch val0e3 o2 the note3 li1e thi3:
/e have p0t in t5o led4er line3, F03t 2or 4ood mea30re( ,ote ho5 the E 2lat in the 1ey 3i4nat0re a22ect3 not only the
E in the top 3pace, 2lattenin4 it 2rom 1# to 1", b0t al3o the E on the bottom line, 2lattenin4 it 2rom ! to 3( It 3ho0ld
no5 be ?0ite ea3y to 2ind the pitch val0e o2 any note on the 3tave(
I2 yo0 5ant to chan4e the 1ey o2 the piece, the be3t thin4 i3 to 3et 0p a variable 1ey and in3ert 1eyP be2ore each pitch
val0e: th03 the 3econd line become3
20 >EEP 1,Le;+06 >EEP 1,Le;+26 >EEP ",Le;+36 >EEPF5,Le;+26
>EEP 1,Le;+0
Be2ore yo0 r0n a pro4ram yo0 m03t 4ive 1ey the appropriate val0e J ' 2or C minor, 2 2or - minor, 12 2or C minor an
octave 0p, and 3o on( 6o0 can 4et the comp0ter in t0ne 5ith another in3tr0ment by adF03tin4 1ey, 03in4 2ractional
val0e3(
6o0 al3o have to 5or1 o0t the d0ration3 o2 all the note3( ;ince thi3 i3 a 2airly 3lo5 piece, 5e have allo5ed one
3econd 2or a crotchet and ba3ed the re3t on that, hal2 a 3econd 2or a ?0aver and 3o on(
Eore 2lexible i3 to 3et 0p a variable crotchet to 3tore the len4th o2 a crotchet and 3peci2y the d0ration3 in term3 o2
thi3( 8hen line 2' 5o0ld become
20 >EEP crotc:et,Le;+06 >EEP crotc:et,Le;+26 >EEP crotc:et/2,Le;+36
>EEP crotchet/2,key+2: BEEP crotchet,key+0
(6o0 5ill probably 5ant to 4ive crotchet and *ey 3horter name3()
By 4ivin4 crotchet appropriate val0e3, yo0 can ea3ily vary the 3peed o2 the piece(
7emember that beca03e there i3 only one lo0d3pea1er in the comp0ter yo0 can only play one note at a time, 3o yo0
are re3tricted to 0nharmoniLed t0ne3( I2 yo0 5ant any more yo0 m03t 3in4 it yo0r3el2(
8ry pro4rammin4 t0ne3 in 2or yo0r3el2 J 3tart o22 5ith 2airly 3imple one3 li1e =8hree Blind Eice=( I2 yo0 have neither
piano nor 5ritten m03ic, 4et hold o2 a very 3imple in3tr0ment li1e a tin 5hi3tle or a recorder, and 5or1 the t0ne3 o0t
on that( 6o0 co0ld ma1e a chart 3ho5in4 the pitch val0e 2or each note that yo0 can play on thi3 in3tr0ment(
8ype:
'3R =0 T3 10006 >EEP F5,6 NEHT
8hi3 5ill play note3 a3 hi4h a3 it can, and then 3top 5ith error report B inte4er o0t o2 ran4e( 6o0 can print o0t n to
2ind o0t ho5 hi4h it did act0ally 4et(
8ry the 3ame thin4, b0t 4oin4 do5n into the lo5 note3( 8he very lo5e3t note3 5ill F03t 3o0nd li1e clic13> in 2act the
hi4her note3 are al3o made o2 clic13 in the 3ame 5ay, b0t 2a3ter, 3o that the ear cannot di3tin40i3h them(
<nly the middle ran4e o2 note3 are really any 4ood 2or m03ic> the lo5 note3 3o0nd too m0ch li1e clic13, and the hi4h
note3 are thin and tend to 5arble a bit(
8ype in thi3 pro4ram line:
10 >EEP F5,06 >EEP F5,26 >EEP F5,)6 >EEP F5,56 >EEP F5,E6 >EEP F5,06
>EEP F5,116 >EEP F5,126 ST3P
8hi3 play3 the 3cale o2 C maFor, 5hich 03e3 all the 5hite note3 on the piano 2rom middle C to the next C 0p( 8he
5ay thi3 3cale i3 t0ned i3 exactly the 3ame a3 on a piano, and i3 called evenJtempered t0nin4 beca03e the pitch interval
o2 a 3emitone i3 the 3ame all the 5ay 0p the 3cale( * violini3t, ho5ever, 5o0ld play the 3cale very 3li4htly di22erently,
adF03tin4 all the note3 to ma1e them 3o0nd more plea3in4 to the ear( Ce can do thi3 F03t by movin4 hi3 2in4er3 very
3li4htly 0p or do5n the 3trin4 in a 5ay that a piani3t can=t(
8he nat0ral 3cale, 5hich i3 5hat the violini3t play3, come3 o0t li1e thi3:
20 >EEP F5,06 >EEP F5,2F0306 >EEP F5,3F816 >EEP F5,)F086 >EEP F5,EF026
>EEP F5,8F8)6 >EEP F5,10F886 >EEP F5,126 ST3P
6o0 may or may not be able to detect any di22erence bet5een the3e t5o> 3ome people can( 8he 2ir3t noticeable
di22erence i3 that the third note i3 3li4htly 2latter in the nat0rally tempered 3cale(l2 yo0 are a real per2ectioni3t, yo0
mi4ht li1e to pro4ram yo0r t0ne3 to 03e thi3 nat0ral 3cale in3tead o2 the evenJtempered one( 8he di3advanta4e i3 that
altho04h it 5or13 per2ectly in the 1ey o2 C, in other 1ey3 it 5or13 le33 5ell J they all have their o5n nat0ral 3cale3 J
and in 3ome 1ey3 it 5or13 very badly indeed( 8he evenJtempered 3cale i3 only 3li4htly o22, and 5or13 e?0ally 5ell in
all 1ey3(
8hi3 i3 le33 o2 a problem on the comp0ter, o2 co0r3e, beca03e yo0 can 03e the tric1 o2 addin4 on a variable 1ey(
;ome m03ic J notably Indian m03ic J 03e3 interval3 o2 pitch 3maller than a 3emitone( 6o0 can pro4ram the3e into the
BEE+ 3tatement 5itho0t any tro0ble> 2or in3tance the ?0artertone above middle C ha3 a pitch val0e o2 ("(
6o0 can ma1e the 1eyboard beep in3tead o2 clic1in4 by
P37E 23100,255
8he 3econd n0mber in thi3 determine3 the len4th o2 the beep (try vario03 val0e3 bet5een ' and 2"")( /hen it i3 ',
the beep i3 3o 3hort that it 3o0nd3 li1e a 3o2t clic1(
I2 yo0 are intere3ted in doin4 more 5ith 3o0nd 2rom the ;pectr0m, li1e hearin4 the 3o0nd that BEE+ ma1e3 on
3omethin4 other the internal 3pea1er, yo0 5ill 2ind that the 3i4nal i3 pre3ent on both the =EIC= and the =E*7= 3oc1et3(
It 5ill be at a hi4her level on the =E*7= 3oc1et, b0t other5i3e they are the 3ame( 6o0 may 03e thi3 to connect an
earphone or a pair o2 headphone3 to yo0r ;pectr0m( 8hi3 5ill not c0t o0t the internal lo0d3pea1er( I2 yo0 are really
1een to ma1e a lot o2 noi3e yo0 co0ld connect it 0p to an ampli2ier J the =EIC= 3oc1et 5ill probably 4ive abo0t the
ri4ht level J or yo0 co0ld record the 3o0nd onto tape and 4et the ;pectr0m to play alon4 5ith it3el2 (
6o0 5ill not dama4e the ;pectr0m even i2 yo0 3hortJcirc0it the =EIC= or =E*7= 3oc1et3, 3o experiment to 2ind 5hich
4ive3 the be3t o0tp0t 2or 5hat yo0 5ant to do(
Exercise
1( 7e5rite the Eahler pro4ram 3o that it 03e3 D<7 loop3 to repeat the bar3(
2( +ro4ram the comp0ter 3o that it play3 not only the 20neral march, b0t al3o the re3t o2 Eahler=3 2ir3t 3ymphony(
CHAPTER 21
The () printer
Summary
LPRINT, LLIST, COPY
,ote: ,one o2 the3e 3tatement3 i3 3tandard B*;IC, altho04h 9+7I,8 i3 03ed by 3ome other comp0ter3(
I2 yo0 have a B. printer, yo0 5ill have 3ome operatin4 in3tr0ction3 5ith it( 8hi3 chapter cover3 the B*;IC
3tatement3 needed to ma1e it 5or1(
8he 2ir3t t5o, 9+7I,8 and 99I;8, are F03t li1e +7I,8 and 9I;8, except that they 03e the printer in3tead o2 the
televi3ion( (8he 9 i3 an hi3torical accident( /hen B*;IC 5a3 invented it 030ally 03ed an electric type5riter in3tead
o2 a televi3ion, 3o +7I,8 really did mean print( I2 yo0 5anted ma33e3 o2 o0tp0t yo0 5o0ld 03e a very 2a3t line printer
attached to the comp0ter, and an 9+7I,8 3tatement meanin4 =9ine printer +7I,8=()
8ry thi3 pro4ram 2or example(
10 LPRINT $T:#" pro&ram$F9
20 LLIST
30 LPRINT 9$pr#t" out t:e c:aracter "etF$9
)0 '3R =32 T3 255
50 LPRINT (4R< 8
10 NEHT
8he third 3tatement, C<+6, print3 o0t a copy o2 the televi3ion 3creen( Dor in3tance, type 9I;8 to 4et a li3tin4 on the
3creen o2 the pro4ram above, and type
(3PQ
,ote that C<+6 doe3n=t 5or1 5ith one o2 the li3tin43 that the comp0ter p0t3 0p a0tomatically, beca03e that i3
cleared 5henever a command i3 obeyed( 6o0 m03t either 03e 9I;8 2ir3t, or 03e 99I;8 and 2or4et abo0t C<+6(
6o0 can al5ay3 3top the printer 5hen it i3 r0nnin4 by pre33in4 the B7E*G 1ey (C*+; ;CID8 and ;+*CE)(
I2 yo0 exec0te the3e 3tatement3 5itho0t the printer attached, it 3ho0ld lo3e all the o0tp0t and carry on 5ith the next
3tatement(
8ry thi3:
10 '3R =31 T3 3 STEP,1
20 PRINT ?T 31,,8 (4RS +(3@E $3$+-8
30 NEHT
6o0 5ill 3ee a pattern o2 character3 5or1in4 do5n dia4onally 2rom the top ri4htJhand corner 0ntil it reache3 the
bottom o2 the 3creen, 5hen the pro4ram a313 i2 yo0 5ant to 3croll(
,o5 chan4e *8 31Jn,n in line 2' to 8*B n( 8he pro4ram 5ill have exactly the 3ame e22ect a3 be2ore(
,o5 chan4e +7I,8 in line 2' to 9+7I,8( 8hi3 time there 5ill be no 3crollA, 5hich 3ho0ld not occ0r 5ith the
printer, and the pattern 5ill carry 3trai4ht on 5ith the letter3 D to <(
,o5 chan4e 8*B n to *8 31Jn,n 3till 03in4 9+7I,8( 8hi3 time yo0 5ill 4et F03t a 3in4le line o2 3ymbol3( 8he
rea3on 2or the di22erence i3 that the o0tp0t 2rom +7I,8 i3 not printed 3trai4ht a5ay, b0t arran4e3 in a b022er 3tore a
pict0re one line lon4 o2 5hat the comp0ter 5ill 3end to the printer 5hen it 4et3 ro0nd to it( 8he printin4 ta1e3 place
(i) 5hen the b022er i3 20ll,
(ii) a2ter an 9+7I,8 3tatement that doe3 not end in a comma or 3emicolon,
(iii) 5hen a comma, apo3trophe or 8*B item re?0ire3 a ne5 line, or
(iv) at the end o2 a pro4ram, i2 there i3 anythin4 le2t 0nprinted(
(iii) explain3 5hy o0r pro4ram 5ith 8*B 5or13 the 5ay it doe3( *3 2or *8, the line n0mber i3 i4nored and the
9+7I,8 po3ition (li1e the +7I,8 po3ition, b0t 2or the printer in3tead o2 the televi3ion) i3 chan4ed to the col0mn
n0mber( *n *8 item can never ca03e a line to be 3ent to the printer(
Exercise
1 ( Ea1e a printed 4raph o2 ;I, by r0nnin4 the pro4ram in Chapter 1$ and then 03in4 C<+6(
CHAPTER 22
Other 8:uipment
8here i3 other e?0ipment that yo0 5ill be able to attach to the ;pectr0m(
8he ZX Microdrive i3 a hi4h 3peed ma33 3tora4e device, and i3 m0ch more 2lexible in the 5ay it can be 03ed than a
ca33ette recorder( It 5ill operate not only 5ith ;*IE, IE7ID6, 9<*- and EE7HE, b0t al3o 5ith +7I,8, 9I;8,
I,+:8 and I,GE6;(
8he network i3 03ed 2or connectin4 3everal ;pectr0m3 3o that they can tal1 to each other J one o2 the 03e3 o2 thi3 i3
that yo0 then need only one Eicrodrive to 3erve 3everal comp0ter3(
8he RS232 interface i3 a 3tandard connection that allo53 yo0 to lin1 a ;pectr0m 5ith 1eyboard3, printer3,
comp0ter3 and vario03 other machine3 even i2 they 5ere not de3i4ned 3peci2ically 2or the ;pectr0m(
8he3e 5ill 03e 3ome extra 1ey5ord3 that are on the 1eyboard, b0t cannot be 03ed 5itho0t the extra attachment3: they
are <+E,Z, C9<;EZ, E<IE, E7*;E, C*8 and D<7E*8(

CHAPTER 23
.; and O4T
Summary
OUT
IN
8he proce33or can read 2rom and (at lea3t 5ith 7*E) 5rite to memory by 03in4 +EEG and +<GE( 8he proce33or
it3el2 doe3 not really care 5hether memory i3 7<E, 7*E or even nothin4 at all> it F03t 1no53 that there are #""3#
memory addre33e3, and it can read a byte 2rom each one (even i2 it=3 non3en3e), and 5rite a byte to each one (even i2 it
4et3 lo3t)( In a completely analo4o03 5ay there are #""3# o2 5hat are called IR< port3 (3tandin4 2or Inp0tR<0tp0t
port3)( 8he3e are 03ed by the proce33or 2or comm0nicatin4 5ith thin43 li1e the 1eyboard or the printer, and they can
be controlled 2rom the B*;IC by 03in4 the I, 20nction and the <:8 3tatement(
I, i3 a 20nction li1e +EEG(
IN address
It ha3 one ar40ment, the port addre33, and it3 re30lt i3 a byte read 2rom that port( <:8 i3 a 3tatement li1e +<GE(
3*T address- value
5rite3 the 4iven val0e to the port 5ith the 4iven addre33( Co5 the addre33 i3 interpreted depend3 very m0ch on the
re3t o2 the comp0ter> ?0ite o2ten, many di22erent addre33e3 5ill mean the 3ame( <n the ;pectr0m it i3 mo3t 3en3ible to
ima4ine the addre33 bein4 5ritten in binary, beca03e the individ0al bit3 tend to 5or1 independently( 8here are 1# bit3,
5hich 5e 3hall call (03in4 * 2or addre33)
*1", *1!, *13, *12, ( ( ( ( ( ( , *2, *l, *'
Cere *' i3 the 1 3 bit, *l the 23 bit, *2 the !3 bit and 3o on( Bit3 *', *l, *2, *3 and *! are the important one3(
8hey are normally 1, b0t i2 any one o2 them i3 ' thi3 tell3 the comp0ter to do 3omethin4 3peci2ic( 8he comp0ter
cannot cope 5ith more than one thin4 at a time, 3o no more than one o2 the3e 2ive bit3 3ho0ld be '( Bit3 *# and *$
are i4nored, 3o i2 yo0 are a 5iLard 5ith electronic3 yo0 can 03e them yo0r3el2( 8he be3t addre33e3 to 03e are tho3e that
are 1 le33 than a m0ltiple o2 32, 3o that *',(((*! are all 1( Bit3 *%, *& and 3o on are 3ometime3 03ed to 4ive extra
in2ormation(
8he byte read or 5ritten ha3 % bit3, and the3e are o2ten re2erred to (03in4 - 2or data) a3 -$, -#,((((, -l, -'( Cere i3 a
li3t o2 the port addre33e3 03ed(
8here i3 a 3et o2 inp0t addre33e3 that read the 1eyboard and al3o the E*7 3oc1et(
8he 1eyboard i3 dividecl 0p into % hal2 ro53 o2 " 1ey3 each(
IN 65278 read3 the hal2 ro5 C*+; ;CID8 to I
IN 65022 read3 the hal2 ro5 * to H
IN 64510 read3 the hal2 ro5 N to 8
IN 63486 read3 the hal2 ro5 1 to "
IN 61438 read3 the hal2 ro5 < to #
IN 57342 read3 the hal2 ro5 + to $
IN 49150 read3 the hal2 ro5 E,8E7 to C
IN 32766 read3 the hal2 ro5 ;+*CE to B
(8he3e addre33e3 are 2"!P2"#Q(2""J21 n) a3 n 4oe3 2rom ' to $()
In the byte read in, bit3 -' to -! 3tand 2or the 2ive 1ey3 in the 4iven hal2 ro5 J -' 2or the o0t3ide 1ey, -! 2or the
one neare3t the middle( 8he bit i3 ' i2 the 1ey i3 pre33ed, 1 i2 it i3 not( -# i3 the val0e at the E*7 3oc1et(
+ort addre33 2"! in o0tp0t drive3 the lo0d3pea1er (C!) and the EIC 3oc1et (-3), and al3o 3et3 the border colo0r
(-2, -1 and -')(
+ort addre33 2"1 r0n3 the printer, both in readin4 and 5ritin4: readin4 2ind3 o0t 5hether the printer i3 ready 2or
more, and 5ritin4 3end3 o0t dot3 to be printed(
+ort addre33e3 2"!, 2!$ and 23& are 03ed 2or the extra device3 mentioned in Chapter 22(
70n thi3 pro4ram
10 '3R =0 T3 E6 REM :a5B,roI umber
20 LET a=25)+251.+255,2 -
30 PRINT ?T 0,08 IN a6 23 T3 30
and play aro0nd by pre33in4 1ey3( /hen yo0 4et bored 5ith each hal2Jro5, pre33 B7E*G and then type
NEHT
8he control, data and addre33 b033e3 are all expo3ed at the bac1 o2 the ;pectr0m, 3o yo0 can do almo3t anythin4
5ith a ;pectr0m that yo0 can 5ith a B%'( ;ometime3, tho04h, the ;pectr0m hard5are mi4ht 4et in the 5ay( Cere i3 a
dia4ram o2 the expo3ed connection3 at the bac1:
CHAPTER 24
The memory
Summary
CLEAR
-eep in3ide the comp0ter, everythin4 i3 3tored a3 byte3, i(e( n0mber3 bet5een ' and 2""( 6o0 may thin1 yo0 have
3tored a5ay the price o2 5ool or the addre33 o2 yo0r 2ertiliLer 30pplier3, b0t it ha3 all been converted into collection3
o2 byte3 and byte3 are 5hat the comp0ter 3ee3(
Each place 5here a byte can be 3tored ha3 an addre33, 5hich i3 a n0mber bet5een ' and DDDDh (3o an addre33 can
be 3tored a3 t5o byte3), 3o yo0 mi4ht thin1 o2 the memory a3 a lon4 ro5 o2 n0mbered boxe3, each o2 5hich can
contain a byte( ,ot all the boxe3 are the 3ame, ho5ever( In tile 3tandard 1#G 7*E machine, the boxe3 2rom %'''h to
DDDDh are 3imply mi33in4 alto4ether( 8he boxe3 2rom !'''h to $DDDh are 7*E boxe3, 5hich mean3 yo0 can open
the lid and alter the content3, and tho3e 2rom ' to 3DDDh are 7<E boxe3, 5hich have 4la33 top3 b0t cannot be
opened( 6o0 F03t have to read 5hatever 5a3 p0t in them 5hen the comp0ter 5a3 made(
8o in3pect the content3 o2 a box, 5e 03e the +EEG 20nction: it3 ar40ment i3 the addre33 o2 the box, and it3 re30lt i3
the content3( Dor in3tance, thi3 pro4ram print3 o0t the 2ir3t 21 byte3 in 7<E (and their addre33e3):
10 PRINT $?%%re""$8 T?> 88 $>;te$
20 '3R a =0 T3 20
30 PRINT a8 T?> 88 PEE7 a
)0 NEHT a
*ll the3e byte3 5ill probably be ?0ite meanin4le33 to yo0, b0t the proce33or chip 0nder3tand3 them to be in3t0ction3
tellin4 it 5hat to do(
8o chan4e the content3 o2 a box (i2 it i3 7*E), 5e 03e the +<GE 3tatement( It ha3 the 2orm
P37E address- ne" contents
5here =addre33= and =ne5 content3= 3tand 2or n0meric expre33ion3( Dor in3tance, i2 yo0 3ay
P37E 31000,5E
the byte at addre33 31''' i3 4iven the ne5 val0e "$ J type
PRINT PEE7 31000
to prove thi3( (8ry po1in4 in other val0e3, to 3ho5 that there i3 no cheatin4() 8he ne5 val0e m03t be bet5een J2""
and P2"", and i2 it i3 ne4ative then 2"# i3 added to it
8he ability to po1e 4ive3 yo0 immen3e po5er over the comp0ter i2 yo0 1no5 ho5 to 5ield it> and immen3e
de3tr0ctive po33ibilitie3 i2 yo0 don=t( It i3 very ea3y, by po1in4 the 5ron4 val0e in the 5ron4 addre33, to lo3e va3t
pro4ram3 that too1 yo0 ho0r3 to type in( Dort0nately, yo0 5on=t do the comp0ter any permanent dama4e(
/e 3hall no5 ta1e a more detailed loo1 at ho5 the 7*E i3 03ed b0t don=t bother to read thi3 0nle33 yo0=re
intere3ted(
8he memory i3 divided into di22erent area3 (3ho5n on the bi4 dia4ram) 2or 3torin4 di22erent 1ind3 o2 in2ormation(
8he area3 are only lar4e eno04h 2or the in2ormation that they act0ally contain, and i2 yo0 in3ert 3ome more at a 4iven
point (2or in3tance by addin4 a pro4ram line or variable) 3pace i3 made by 3hi2tin4 0p everythin4 above that point(
Conver3ely, i2 yo0 delete in2ormation then everythin4 i3 3hi2ted do5n(
8he di3play 2ile 3tore3 the televi3ion pict0re( It i3 rather c0rio03ly laid o0t, 3o yo0 probably 5on=t 5ant to +EEG or
+<GE in it( Each character po3ition on the 3creen ha3 an %x% 3?0are o2 dot3, and each dot can be either ' (paper) or 1
(in1) and by 03in4 binary notation 5e can 3tore the pattern a3 % byte3, one 2or each ro5( Co5ever, the3e % byte3 are
not 3tored to4ether( 8he corre3pondin4 ro53 in the 32 character3 o2 a 3in4le line are 3tored to4ether a3 a 3can o2 32
byte3, beca03e thi3 i3 5hat the electron beam in the televi3ion need3 a3 it 3can3 2rom the le2t hand 3ide o2 the 3creen to
the other( ;ince the complete pict0re ha3 2! line3 o2 % 3can3 each, yo0 mi4ht expect the total o2 1$2 3can3 to be 3tored
in order, one a2ter the other> yo0=d be 5ron4( Dir3t come the top 3can3 o2 line3 ' to $, then the next 3can3 o2 line3 ' to
$, and 3o on to the bottom 3can3 o2 line3 ' to $> then the 3ame 2or line3 % to 1"> and then the 3ame 2or line3 1# to 23(
8he 0p3hot o2 all thi3 i3 that i2 yo0=re 03ed to a comp0ter that 03e3 +EEG and +<GE on the 3creen, yo0=ll have to 3tart
03in4 ;C7EE,K and +7I,8 *8 in3tead, or +9<8 and +<I,8(
8he attrib0te3 are the colo0r3 and 3o on 2or each character po3ition, 03in4 the 2ormat o2 *887( 8he3e are 3tored line
by line in the order yo0=d expect(
8he printer b022er 3tore3 the character3 de3tined 2or the printer(
8he 3y3tem variable3 contain vario03 piece3 o2 in2ormation that tell the comp0ter 5hat 3ort o2 3tate the comp0ter i3
in( 8hey are li3ted 20lly in the next chapter, b0t 2or the moment note that there are 3ome (called CC*,;, +7<H,
I*7;, E 9I,E and 3o on) that contain the addre33e3 o2 the bo0ndarie3 bet5een the vario03 area3 in memory( 8he3e
are not B*;IC variable3, and their name3 5ill not be reco4niLed by the comp0ter(
8he Eicrodrive map3 are only 03ed 5ith the Eicrodrive( ,ormally there i3 nothin4 there(
8he channel in2ormation contain3 in2ormation abo0t the inp0t and o0tp0t device3, namely the 1eyboard (5ith the
lo5er hal2 o2 the 3creen), the 0pper hal2 o2 the 3creen, and the printer(
Each line o2 B*;IC pro4ram ha3 the 2orm:
,ote that, in contra3t 5ith all other ca3e3 o2 t5o byte n0mber3 in the B%', the line n0mber here i3 3tored 5ith it3
more 3i4ni2icant byte 2ir3t: that i3 to 3ay, in the order that yo0 5rite them do5n in(
* n0merical con3tant in the pro4ram i3 2ollo5ed by it3 binary 2orm, 03in4 the character CC7K 1! 2ollo5ed by 2ive
byte3 2or the n0mber it3el2(
8he variable3 have di22erent 2ormat3 accordin4 to their di22erent 2eat0re3( 8he letter3 in the name3 3ho0ld be
ima4ined a3 3tartin4 o22 in lo5er ca3e(
8he order o2 the element3 i3:
2ir3t, the element3 2or 5hich the 2ir3t 30b3cript i3 1>
next, the element3 2or 5hich the 2ir3t 30b3cript i3 2>
next, the element3 2or 5hich the 2ir3t 30b3cript i3 3>
and 3o on 2or all po33ible val0e3 o2 the 2ir3t 30b3cript(

8he element3 5ith a 4iven 2ir3t 30b3cript are ordered in the 3ame 5ay 03in4 the 3econd 30b3cript, and 3o on do5n to
the la3t(
*3 an example, the element3 o2 the 3Q# array b in Chapter 12 are 3tored in the order b(1,1) b(1,2) b(1,3) b(1,!) b
(1,") b(1,#) b(2,1) b(2,2) (((( b(2,#) b(3,1) b(3,2) ((( b(3,#)(

8he calc0lator i3 the part o2 the B*;IC 3y3tem that deal3 5ith arithmetic, and the n0mber3 on 5hich it i3 operatin4
are held mo3tly in the calc0lator 3tac1(
8he 3pare part contain3 the 3pace 3o 2ar 0n03ed(
8he machine 3tac1 i3 the 3tac1 03ed by the B%' proce33or to hold ret0rn addre33e3 and 3o on(

8he H<;:B 3tac1 5a3 mentioned in Chapter "(
8he byte pointed to by 7*E8<+ ha3 the hi4he3t addre33 03ed by the B*;IC 3y3tem( Even ,E/, 5hich clear3 the
7*E o0t, only doe3 3o a3 2ar a3 thi3 3o it doe3n=t chan4e the 03er de2ined 4raphic3( 6o0 can chan4e the addre33
7*E8<+ by p0ttin4 a n0mber in a clear 3tatement:
(LE?R ne5 7*E8<+
8hi3
(i) clear3 o0t all the variable3
(ii) clear3 the di3play 2ile (li1e C9;)
(iii) re3et3 the +9<8 po3ition to the bottom le2t hand corner
(iv) doe3 7E;8<7E (v) clear3 the H<;:B 3tac1 and p0t3 it at the ne5 7*E8<+ a330min4 that thi3 lie3 bet5een
the calc0lator 3tac1 and the phy3ical end o2 7*E> other5i3e it leave3 7*E8<+ a3 it 5a3(
7:, al3o doe3 C9E*7, altho04h it never chan4e3 7*E8<+(
:3in4 C9E*7 in thi3 5ay, yo0 can either move 7*E8<+ 0p to ma1e more room 2or the B*;IC by over5ritin4 the
03er de2ined 4raphic3, or yo0 can move it do5n to ma1e more 7*E that i3 pre3erved 2rom ,E/(
8ype ,E/, then C9E*7 23%'' to 4et 3ome idea o2 5hat happen3 to the machine 5hen it 2ill3 0p(
<ne o2 the 2ir3t thin43 yo0 5ill notice i2 yo0 3tart typin4 in a pro4ram i3 that a2ter a 5hile the comp0ter 3top3
acceptin4 any more and b0LLe3 at yo0( It mean3 the comp0ter i3 choc1 a bloc1 and yo0 5ill have to empty it 3li4htly(
8here are al3o t5o error me33a4e3 5ith ro04hly the 3ame meanin4, ! Eemory 20ll and H ,o room 2or line(
8he b0LL al3o occ0r3 5hen yo0 type in a line lon4er than 23 line3 then yo0r typin4 i3 not bein4 i4nored, tho04h
yo0 cannot 3ee it> b0t the b0LL 3o0nd3 to di3co0ra4e yo0 2rom doin4 any more(
6o0 can adF03t the len4th o2 the b0LL by po1in4 a n0mber into addre33 23#'%( 8he 030al len4th ha3 n0mber #!(
*ny n0mber (except ') can be 5ritten 0ni?0ely a3 P m x 2e 5here
P i3 the 3i4n,
m i3 the manti33a, and lie3 bet5een [ and 1 (it cannot be 1),
and e i3 the exponent, a 5hole n0mber (po33ibly ne4ative)(
;0ppo3e yo0 5rite m in the binary 3cale( Beca03e it i3 a 2raction, it 5ill have a binary point (li1e the decimal point in
the 3cale o2 ten) and then a binary 2raction (li1e a decimal 2raction)> 3o in binary:
a hal2 i3 5ritten (1
a ?0arter i3 5ritten ('1
three ?0arter3 i3 5ritten (11
a tenth i3 5ritten ('''11''11''11''11''11 ((( and 3o on(
/ith o0r n0mber m, beca03e it i3 le33 than 1, there are no bit3 be2ore the binary point, and beca03e it i3 at lea3t [,
the bit immediately a2ter the binary point i3 a 1(
8o 3tore the n0mber in the comp0ter, 5e 03e 2ive byte3, a3 2ollo53:
(i) 5rite the 2ir3t ei4ht bit3 o2 the manti33a in the 3econd byte (5e 1no5 that the 2ir3t bit i3 1), the 3econd ei4ht bit3 in
the third byte, the third ei4ht bit3 in the 2o0rth byte and the 2o0rth ei4ht bit3 in the 2i2th byte>
(ii) replace the 2ir3t bit in the 3econd byte 5hich 5e 1no5 i3 1 by the 3i4n: ' 2or pl03, 1 2or min03>
(iii) 5rite the exponent P12% in the 2ir3t byte( Dor in3tance, 30ppo3e o0r n0mber i3
1
R
1'

1
R
1'
=!R"x2
J3
8h03 the manti33a m i3 (11''11''11''11''11''11''11''11'' in binary (3ince the 33rd bit i3 1, 5e 3hall ro0nd the
32nd 0p 2rom ' to 1), and the exponent e i3 3(
*pplyin4 o0r three r0le3 4ive3 the 2ive byte3
8here i3 an alternative 5ay o2 3torin4 5hole n0mber3 bet5een #""3" and P#""3":
(i) the 2ir3t byte i3 '(
(ii) the 3econd byte i3 ' 2or a po3itive n0mber, DDh 2or a ne4ative one,
(iii) the third and 2o0rth byte3 are the le33 and more 3i4ni2icant byte3 o2 the n0mber (or the n0mber P131'$2 i2 it i3
ne4ative),
(iv) the 2i2th byte i3 '(
CHAPTER 25
The system variables
8he byte3 in memory 2rom 23""2 to 23$33 are 3et a3ide 2or 3peci2ic 03e3 by the 3y3tem( 6o0 can pee1 them to 2ind
o0t vario03 thin43 abo0t the 3y3tem, and 3ome o2 them can be 03e20lly po1ed( 8hey are li3ted here 5ith their 03e3(
8he3e are called 3y3tem variable3, and have name3, b0t do not con203e them 5ith the variable3 03ed by the B*;IC(
8he comp0ter 5ill not reco4niLe the name3 a3 re2errin4 to 3y3tem variable3, and they are 4iven 3olely a3 mnemonic3
2or 5e h0man3 ( 8he abbreviation3 in col0mn 1 have the 2ollo5in4 meanin43:
X 8he variable3 3ho0ld not be po1ed beca03e the 3y3tem mi4ht cra3h(
N +o1in4 the variable 5ill have no la3tin4 e22ect

8he n0mber in col0mn 1 i3 the n0mber o2 byte3 in the variable( Dor t5o byte3, the 2ir3t one i3 the le33 3i4ni2icant
byte the rever3e o2 5hat yo0 mi4ht expect( ;o to po1e a val0e v to a t5o byte variable at addre33 n, 03e
P37E ,!,251.1NT +!/251-
P37E +1,5NT +!/251-
and to pee1 it3 val0e, 03e the expre33ion
PEE7 +251.PEE7 ++1-
Notes Address Name Contents
,% 23""2 G;8*8E :3ed in readin4 the 1eyboard(
,l 23"#' 9*;8 G ;tore3 ne5ly pre33ed 1ey(
1 23"#1 7E+-E9 8ime (in "'th3 o2 a 3econd in #'th3 o2 a 3econd
in ,( *merica) that a 1ey m03t be held do5n
be2ore it repeat3( 8hi3 3tart3 o22 at 3", b0t yo0
can +<GE in other val0e3(
1 23"#2 7E++E7 -elay (in "'th3 o2 a 3econd in #'th3 o2 a
3econd in ,( *merica) bet5een 30cce33ive
repeat3 o2 a 1ey held do5n: initially "(
,2 23"#3 -ED*-- *ddre33 o2 ar40ment3 o2 03er de2ined 20nction i2
one i3 bein4 eval0ated> other5i3e '(
,l 23"#" G -*8* ;tore3 2nd byte o2 colo0r control3 entered 2rom
1eyboard (
,2 23"## 8I-*8* ;tore3 byte3 o2 coio0r, *8 and 8*B control3
4oin4 to televi3ion(
.3% 23"#% ;87E; *ddre33e3 o2 channel3 attached to 3tream3(
2 23#'# CC*7; 2"# le33 than addre33 o2 character 3et (5hich
3tart3 5ith 3pace and carrie3 on to the copyri4ht
3ymbol)( ,ormally in 7<E, b0t yo0 can 3et 0p
yo0r o5n in 7*E and ma1e CC*7; point to it(
1 23#'% 7*;+ 9en4th o2 5arnin4 b0LL(
1 23#'& +I+ 9en4th o2 1eyboard clic1(
1 23#1' E77 ,7 1 le33 than the report code( ;tart3 o22 at 2"" (2or 1)
3o +EEG 23#1' 4ive3 2""(
.1 23#11 D9*H; Iario03 2la43 to control the B*;IC 3y3tem(
.1 23#12 8I D9*H Dla43 a33ociated 5ith the televi3ion(
.2 23#13 E77 ;+ *ddre33 o2 item on machine 3tac1 to be 03ed a3
error ret0rn(
,2 23#1" 9I;8 ;+ *ddre33 o2 ret0rn addre33 2rom a0tomatic li3tin4(
,1 23#1$ E<-E ;peci2ie3 G, 9, C( E or H c0r3or(
2 23#1% ,E/++C 9ine to be F0mped to(
23#2' ,;++C ;tatement n0mber in line to be F0mped to(
+o1in4 2ir3t ,E/++C and then ,;++C 2orce3 a
F0mp to a 3peci2ied 3tatement in a line(
2 23#21 ++C 9ine n0mber o2 3tatement c0rrently bein4
exec0ted(
1 23#23 ;:B++C ,0mber 5ithin line o2 3tatement bein4
exec0ted(
1 23#2! B<7-C7 Border colo0r Q %> al3o contain3 the attrib0te3
normally 03ed 2or the lo5er hal2 o2 the 3creen(
2 23#2" E ++C ,0mber o2 c0rrent line (5ith pro4ram c0r3or)(
.2 23#2$ I*7; *ddre33 o2 variable3(
,2 23#2& -E;8 *ddre33 o2 variable in a33i4nment(
.2 23#31 CC*,; *ddre33 o2 channel data(
.2 23#33 C:7CC9 *ddre33 o2 in2ormation c0rrently bein4 03ed 2or
inp0t and o0tp0t(
.2 23#3" +7<H *ddre33 o2 B*;IC pro4ram(
.2 23#3$ ,.89I, *ddre33 o2 next line in pro4ram(
.2 23#3& -*8*-- *ddre33 o2 terminator o2 la3t -*8* item(
.2 23#!1 E 9I,E *ddre33 o2 command bein4 typed in(
2 23#!3 G C:7 *ddre33 o2 c0r3or(
.2 23#!" CC *-- *ddre33 o2 the next character to be interpreted:
the character a2ter the ar40ment o2 +EEG, or the
,E/9I,E at the end o2 a +<GE 3tatement(
2 23#!$ . +87 *ddre33 o2 the character a2ter the A mar1er(
.2 23#!& /<7G;+ *ddre33 o2 temporary 5or1 3pace(
.2 23#"1 ;8GB<8 *ddre33 o2 bottom o2 calc0lator 3tac1(
.2 23#"3 ;8GE,- *ddre33 o2 3tart o2 3pare 3pace(
,1 23#"" B7EH Calc0lator=3 b re4i3ter(
,2 23#"# EEE *ddre33 o2 area 03ed 2or calc0lator=3 memory(
(:30ally EEEB<8, b0t not al5ay3()
23#"% D9*H;2 Eore 2la43(
.1 23#"& -D ;B 8he n0mber o2 line3 (incl0din4 one blan1 line) in
the lo5er part o2 the 3creen(
2 23##' ; 8<+ 8he n0mber o2 the top pro4ram line in a0tomatic
li3tin43(
2 23##2 <9-++C 9ine n0mber to 5hich C<,8I,:E F0mp3(
23##! <;+CC ,0mber 5ithin line o2 3tatement to 5hich
C<,8I,:E F0mp3(
,1 23##" D9*H. Iario03 2la43(
,2 23### ;879E, 9en4th o2 3trin4 type de3tination in a33i4nment(
,2 23##% 8 *--7 *ddre33 o2 next item in 3yntax table (very
0nli1ely to be 03e20l)(
2 23#$' ;EE- 8he 3eed 2or 7,-( 8hi3 i3 the variable that i3 3et
by 7*,-<EIBE(
3 23#$2 D7*EE; 3 byte (lea3t 3i4ni2icant 2ir3t), 2rame co0nter(
Incremented every 2'm3( ;ee Chapter 1%(
2 23#$" :-H *ddre33 o2 13t 03er de2ined 4raphic 6o0 can
chan4e thi3 2or in3tance to 3ave 3pace by havin4
2e5er 03er de2ined 4raphic3(
1 23#$$ C<<7-; xJcoordinate o2 la3t point plotted(
1 23#$% yJcoordinate o2 la3t point plotted(
1 23#$& + +<;, 33 col0mn n0mber o2 printer po3ition
1 23#%' +7 CC 9e33 3i4ni2icant byte o2 addre33 o2 next po3ition
2or 9+7I,8 to print at (in printer b022er)(
1 23#%1 ,ot 03ed(
2 23#%2 ECC< E 33 col0mn n0mber and 2! line n0mber (in lo5er
hal2) o2 end o2 inp0t b022er(
2 23#%! -D CC *ddre33 in di3play 2ile o2 +7I,8 po3ition(
2 23#%# -DCC9 9i1e -D CC 2or lo5er part o2 3creen(
.1 23#%% ; +<;, 33 col0mn n0mber 2or +7I,8 po3ition
.1 23#%& 2! line n0mber 2or +7I,8 po3ition(
.2 23#&' ;+<;,9 9i1e ; +<;, 2or lo5er part
23#&2 ;C7 C8 Co0nt3 3croll3: it i3 al5ay3 1 more than the
n0mber o2 3croll3 that 5ill be done be2ore
3toppin4 5ith "cro55C I2 yo0 1eep po1in4 thi3
5ith a n0mber bi44er than 1 (3ay 2""), the
3creen 5ill 3croll on and on 5itho0t a31in4 yo0(
1 23#&3 *887 + +ermanent c0rrent colo0r3, etc (a3 3et 0p by
colo0r 3tatement3)(
1 23#&! E*;G + :3ed 2or tran3parent colo0r3, etc( *ny bit that i3 1
3ho53 that the corre3pondin4 attrib0te bit i3
ta1en not 2rom *887 +, b0t 2rom 5hat i3 already
on the 3creen(
,1 23#&" *887 8 8emporary c0rrent colo0r3, etc (a3 3et 0p by
colo0r item3)(
,1 23#&# E*;G 8 9i1e E*;G +, b0t temporary(
23#&$ + D9*H Eore 2la43(
,3' 23#&% EEEB<8 Calc0lator=3 memory area> 03ed to 3tore n0mber3
that cannot conveniently be p0t on the calc0lator
3tac1(
2 23$2% ,ot 03ed(
2 23$3' 7*E8<+ *ddre33 o2 la3t byte o2 B*;IC 3y3tem area(
2 23$32 + 7*E8 *ddre33 o2 la3t byte o2 phy3ical 7*E(
8hi3 pro4ram tell3 yo0 the 2ir3t 22 byte3 o2 the variable3 area:
10 '3R =0 T3 21
20 PRINT PEE7 +PEE7 2312E+251.PEE7 23128+-
30 NEHT
8ry to match 0p the control variable n 5ith the de3cription3 above( ,o5 chan4e line 2' to
20 PRINT PEE7 +23E55+-
8hi3 tell3 yo0 the 2ir3t 22 byte3 o2 the pro4ram area( Eatch the3e 0p 5ith the pro4ram it3el2(
CHAPTER 26
4sing machine code
Summary
USR with numeric argument
8hi3 chapter i3 5ritten 2or tho3e 5ho 0nder3tand B%' machine code, the 3et o2 in3tr0ction3 that the B%' proce33or
chip 03e3( I2 yo0 do not, b0t 5o0ld li1e to, there are plenty o2 boo13 abo0t it( 6o0 5ant to 4et one called 3omethin4
alon4 the line3 o2 @(8= /achine code Sor a33embly lan40a4eT #or the absolute beginner@, and i2 it mention3 the
;pectr0m, 3o m0ch the better(
8he3e pro4ram3 are normally 5ritten in a33embly lan40a4e, 5hich, altho04h cryptic, are not too di22ic0lt to
0nder3tand 5ith practice( (6o0 can 3ee the a33embly lan40a4e in3tr0ction3 in *ppendix *() Co5ever, to r0n them on
the comp0ter yo0 need to code the pro4ram into a 3e?0ence o2 byte3 in thi3 2orm it i3 called machine code( 8hi3
tran3lation i3 030ally done by the comp0ter it3el2, 03in4 a pro4ram called an a33embler( 8here i3 no a33embler b0ilt in
to the ;pectr0m, b0t yo0 may 5ell be able to b0y one on ca33ette( Dailin4 that, yo0 5ill have to do the tran3lation
yo0r3el2, provided that the pro4ram i3 not too lon4(
9et=3 ta1e a3 an example the pro4ram
ld bc, &&
ret
5hich load3 the bc re4i3ter pair 5ith &&( 8hi3 tran3late3 into the 2o0r machine code byte3 1, &&, ' (2or ld bc, &&) and
2'1 (2or ret)( (I2 yo0 loo1 0p 1 and 2'1 in *ppendix *, yo0 5ill 2ind ld bc, ,, J 5here ,, 3tand3 2or any t5o byte
n0mber J and ret()
/hen yo0 have 4ot yo0r machine code pro4ram, the next 3tep i3 to 4et it into the comp0ter( (*n a33embler 5o0ld
probably do thi3 a0tomatically() 6o0 need to decide 5hereabo0t3 in memory to p0t it, and the be3t thin4 i3 to ma1e
extra 3pace 2or it bet5een the B*;IC area and the 03er de2ined 4raphic3(
;0ppo3e, 2or in3tance, that yo0 have a 1#G ;pectr0m( 8o 3tart o22 5ith, the top end o2 7*E ha3
I2 yo0 type
(LE?R 32)00
thi3 5ill 4ive yo0 a 3pace o2 1'' (2or 4ood mea30re) byte3 3tartin4 at addre33 32"''(
8o p0t in the machine
code pro4ram, yo0 5o0ld r0n a B*;IC pro4ram 3omethin4 li1e
10 LET a=32500
20 RE?@ 6 P37E a,
30 LETa=a+1623T320
)0 @?T? 1,00,0,201
(8hi3 5ill 3top 5ith report E 3ut oB @?T? 5hen it ha3 2illed in the 2o0r byte3 yo0 3peci2ied()
8o r0n the machine code, yo0 03e the 20nction :;7 b0t thi3 time 5ith a n0meric ar40ment, the 3tartin4 addre33( It3
re30lt i3 the val0e o2 the bc re4i3ter on ret0rn 2rom the machine code pro4ram, 3o i2 yo0 do
PRINT *SR 32500
yo0 4et the an35er &&(
8he ret0rn addre33 to the B*;IC i3 3tac1ed in the 030al 5ay, 3o ret0rn i3 by a B%' ret in3tr0ction( 6o0 3ho0ld not
03e the iy and i re4i3ter3 in a machine code ro0tine(
6o0 can 3ave yo0r machine code pro4ram ea3ily eno04h 5ith
S?AE $3ome name$ (3@E 32500,)
<n the 2ace o2 it, there i3 no 5ay o2 3avin4 it 3o that 5hen loaded it a0tomatically r0n3 it3el2, b0t yo0 can 4et ro0nd
thi3 by 03in4 a B*;IC pro4ram(
10 L3?@ $$ (3@E 32500,)
20 PRINT *SR 32500
-o 2ir3t
S?AE $3ome name$ LINE
and then
S?AE $GGGG$ (3@E 32500,)
L3?@ $3ome name$
5ill then load and a0tomatically r0n the B*;IC pro4ram, and the B*;IC pro4ram 5ill load and r0n the machine
code(
APPENDIX A
The character set
8hi3 i3 the complete ;pectr0m character 3et, 5ith code3 in decimal and hex( I2 one ima4ine3 the code3 a3 bein4 B%'
machine code in3tr0ction3, then the ri4ht hand col0mn3 4ive the corre3pondin4 a33embly lan40a4e mnemonic3( *3
yo0 are probably a5are i2 yo0 0nder3tand the3e thin43, certain B%' in3tr0ction3 are compo0nd3 3tartin4 5ith CBh or
E-h> the t5o ri4ht hand col0mn3 4ive the3e(
ode haracter .e3 -45 %ssembler after $ 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 PRIN co!!a 06 ld b,N rlc (hl)
" #$I 0" rlca rlc a
% cursor le&t 0% e' a&,a&( rrc b
) cursor ri*ht 0) add hl,bc rrc c
10 cursor do+n 0, ld a,(bc) rrc d
11 cursor up 0% dec bc rrc e
12 $#-## 0. incc rrch
13 #N#R 0$ dec c rrc
14 nu!ber 0# ld c,N rrc (hl)
15 not used 0/ rrca rrc a
16 IN0 control 10 d1n2 $I3 rl b
1" P,P#R control 11 ld de,NN rlc
1% /-,34 control 12 ld (de),a rl d
1) 5RI64 control 13 inc de rl e
20 IN7#R3# control 14 inc d rl h
21 87#R control 15 dec d rl l
22 , control 16 ld d,N rl (hl)
23 ,5 control 1" rla rl a
24 } 1% 1r $I3 rr b
25 } 1) add hl,de rr c
26 } 1, ld a,(de) rr d
2" } 15 dec de rr e
2% } 1. inc e rr h
2) } 1$ dec e rr l
30 } 1# ld e,N rr (hl)
31 } 1/ rra rr a
32 space 20 1r n2, $l3 sla b
33 9 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
3" = 25 dec h sla i
3% > 26 ld h,N sla (hl)
3) , 2" daa sla a
40 ( 2% 1r 2,$l3 sra b
41 ) 2) add hl,hl sra c
42 ? 2, ld hl,(NN) sra d
43 @ 25 dec hl sra e
44 , 2. inc ll sra h
45 A 2$ dec l sra l
46 B 2/ ld l,N sra (hl)
4" C 2/ cpl sra a
4% 0 30 1r nc,$l3
4) 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 " 3" sc&
56 % 3% lr c,$l3 srl b
5" ) 3) add hl,sp srl c
5% D 3, ld a,(NN) srl d
5) E 35 dec sp srl e
60 F 3. inc a srl h
61 G 3$ dec a srl l
62 H 3# ld a,N srl (hl)
63 I 3/ cc& srl a
64 J 40 ld b,b bit 0,b in b,(c)
65 , 41 ld b,c bit 0,c out (c),b
66 5 42 ld b,d bit 0,d sbc hl,bc
6" . 43 ld b,e bit 0,e ld (NN),bc
6% $ 44 ld b,h bit 0,h ne*
6) # 45 ld b,l bit 0,1 retn
"0 / 46 ld b,(hl) bit 0,(hl) i! 0
"1 6 4" ld b,a bit 0,a ld i,a
"2 4 4% ld c,b bit 1,b in c,(c)
"3 I 4) ld cc bit 1,c out (c),c
"4 K 4, ld c,d bit i,d adc hl,bc
"5 0 45 ld c,e bit 1,e ld bc,(NN)
"6 - 4. ld c,h bit 1,h
"" L 4$ ld c,l bit 1,l reti
"% N 4# ld c,(hl) bit 1,(hl)
") 8 4/ ld c,a bit 1,a ld r,a
%0 P 50 ld d,b bit 2,b in d,(c)
%1 M 51 ld d,c bit 2,c out (c),d
%2 R 52 ld d,d bit 2,d sbc hl,de
%3 3 53 ld d,e bit 2,e ld (NN),de
%4 54 ld d,h bit 2,h
%5 N 55 ld d,l bit 2,l
%6 7 56 ld d,(hl) bit 2,(hl) i! 1
%" O 5" ld d,a bit 2,a ld a,i
%% P 5% ld e,b bit 3,b in e,(c)
%) Q 5) ld e,c bit 3,c out (c),e
)0 R 5, ld e,d bit 3,d adc hl,de
)1 S 55 ld e,e bit 3,e ld de,(NN)
)2 C 5. ld e,h bit 3,h
)3 T 5$ ld e,l bit 3,l
)4 U 5# ld e,(hl) bit 3,(hl) i! 2
)5 V 5/ ld e,a bit 3,a ld a,r
)6 W 60 ld h,b bit 4,b in h,(c)
)" a 61 ld h,c bit 4,c out (c),h
)% b 62 ld h,d bit 4,d sbc hl,hl
)) 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 & 66 ld h,(hl) bit 4,(hl)
103 * 6" ld h,a bit 4,a rrd
104 h 6% ld l,b bit 5,b in l,(c)
105 i 6) ld l,c bit 5,c out (c),l
106 1 6, ld l,d bit 5,d adc hl,hl
10" X 65 ld l,e bit 5,e ld hl,(NN),sp
10% l 6. ld l,h bit 5,h
10) ! 6$ ld l,l bit 5,l
110 n 6# ld l,(hl) bit 5,(hl)
111 o 6/ ld l,a bit 5,a rld
112 p "0 ld (hl),b bit 6,b in &,(c)
113 Y "1 ld (hl),c bit 6,c
114 r "2 ld (hl),d bit 6,d sbc hl,sp
115 s "3 ld (hl),e bit 6,e ld (NN),sp
116 t "4 ld (hl),h bit 6,h
11" u "5 ld (hl),l bit 6,l
11% Z "6 halt bit 6,(hl)
11) + "" ld (hl),a bit 6,a
120 ' "% ld a,b bit ",b in a,(c)
121 [ ") ld a,c bit ",c out (c),a
122 2 ", ld a,d bit ",d adc hl,sp
123 \ "5 ld a,e bit ",e ld sp,(NN)
124 ] ". lda,h bit ",h
125 } "$ ld al bit ",l
126 A "# ld a,(hl) bit ",(hl)
12" (.) "/ ld a,a bit ",a
12% %0 add a,b res 0,b
12) %1 add a,c res 0,c
130 %2 add a,d res 0,d
131 %3 add a,e res 0,e
132 %4 add a,h res 0,h
133 %5 add a,l res 0,l
134 %6 add a,(hl) res 0,(hl)
135 %" add a,a res 0,a
136 %% adc a,b res 1,b
13" %) adc a,c res 1,c
13% %, adc a,d res 1,d
13) %5 adc a,e res 1,e
140 %. adc a,h res 1,h
141 %$ adc a,l res 1,i
142 %# adc a,(hl) res 1,(hl)
143 %/ adc a,a res 1,a
144 (a) )0 sub b res 2,b
145 (b) )1 sub c res 2,c
146 (c) )2 sub d res 2,d
14" (d) )3 sub e res 2,e
14% (e) )4 sub h res 2,h
14) (&) )5 sub l res 2,l
150 (*) )6 sub (hl) res 2,(hl)
151 (h) )" sub a res 2,a
152 (i) )% sbc a,b res 3,b
153 (1) )) sbc a,c res 3,c
154 (X) user ), sbc a,d res 3,d
155 (l) *raphics )5 sbc a,e res 3,e
156 (!) ). sbc a,h res 3,h
15" (n) )$ sbc a,l res 3,l
15% (o) )# sbc a,(hl) res 3,(hl)
15) (p) )/ sbc a,a res 3,a
160 (Y) ,0 and b res 4,b ld
161 (r) ,1 and c res 4,c cpi
162 (s) ,2 and d res 4,d ini
163 (t) ,3 and e res 4,e outi
164 (u) ,4 and h res 4,h
165 RN$ ,5 and l res 4,l
166 IN0#Q< ,6 and (hl) res 4,(hl)
16" PI ," and a res 4,a
16% /N ,% 'or b res 5,b ldd
16) P8IN ,) 'or c res 5,c cpd
1"0 3.R##N< ,, 'or d res 5,d ind
1"1 ,R ,5 'or e res 5,e outd
1"2 , ,. 'or h res 5,h
1"3 ,5 ,$ 'or l res 5,i
1"4 7,-< ,# 'or (hl) res 5,(hl)
1"5 .8$# ,/ 'or a res 5,a
1"6 7,- 50 or b res 6,b ldir
1"" -#N 51 or c res 6,c cpir
1"% 3IN 52 or d res 6,d inir
1") .83 53 or e res 6,e otir
1%0 ,N 54 or h res 6,h
1%1 ,3N 55 or l res 6,l
1%2 ,.3 56 or (hl) res 6,(hl)
1%3 ,N 5" or a res 6,a
1%4 -N 5% cp b res ",b lddr
1%5 #PP 5) cp c res ",c cpdr
1%6 IN 5, cp d res ",d indr
1%" 38R 55 cp e res ",e otdr
1%% 36N 5. cp h res ",h
1%) ,53 5$ cp l res ",l
1)0 P##0 5# cp (hl) res ",(hl)
1)1 IN 5/ cp a res ",a
1)2 N3R .0 ret n2 set 0,b
1)3 3R< .1 pop bc set 0,c
1)4 .4R< .2 1p n2,NN set 0,d
1)5 N8 .3 1p NN set 0,e
1)6 5IN .4 call n2,NN set 0,h
1)" 8R .5 push bc set 0,l
1)% ,N$ .6 add a,N set 0,(hl)
1)) FG ." rst 0 set 0,a
200 HG .% ret 2 set 1,b
201 FH .) ret set l,c
202 -IN# ., 1p 2,NN set l,d
203 4#N .5 set l,e
204 8 .. call 2,NN set l,h
205 3#P .$ call NN set 1,l
206 $#/ /N .# adc a,N set 1,(hl)
20" ., ./ rst 5 set 1,a
20% /8RL, $0 ret nc set 2,b
20) L87# $1 pop de set 2,c
210 #R,3# $2 1pnc,NN set 2,d
211 8P#N ; $3 out (N),a set 2,e
212 .-83# ; $4 call nc,NN set 2,h
213 L#R6# $5 push de set 2,l
214 7#RI/Q $6 sub N set 2,(hl)
215 5##P $" rst 16 set 2,a
216 .IR.-# $% ret c set 3,b
21" IN0 $) e'' set 3,c
21% P,P#R $, 1pc,NN set 3,d
21) /-,34 $5 in a,(N) set 3,e
220 5RI64 $. call c,NN set 3,h
221 IN7#R3# $$ Spre&i'es set 3,l
instructions
usin* i'T
222 87#R $# sbc a,N set 3,(hl)
223 8N $/ rst 24 set 3,a
224 -PRIN #0 ret po set 4,b
225 --I3 #1 pop hl set 4,c
226 38P #2 1p po,NN set 4,d
22" R#,$ #3 e' (sp),hl set 4,e
22% $,, #4 call po,NN set 4,h
22) R#38R# #5 push hl set 4,l
230 N#O #6 and N set 4,(hl)
231 58R$#R #" rst 32 set 4,a
232 .8NINN# #% ret pe set 5,b
233 $IL #) 1p (hl) set 5,c
234 R#L #, 1p pe,NN set 5,d
235 /8R #5 e' de,hl set 5,e
236 68 8 #. call pe,NN set 5,h
23" 68 3N5 #$ set 5,l
23% INPN ## 'or N set 5,(hl)
23) -8,$ #/ rst 40 set 5,a
240 -I3 /0 ret p set 6,b
241 -# /l pop a& set 6,c
242 P,N3# /2 1p p,NN set 6,d
243 N#P /3 di set 6,e
244 P80# /4 call p,NN set 6,h
245 PRIN /5 push a& set 6,l
246 P-8 /6 or N set 6,(hl)
24" RNN /" rst 4% set 6,a
24% 3,7# /% ret ! set ",b
24) R,N$8LIR# /) ld sp,hl set ",c
250 I/ /, 1p !,NN set ",d
251 .-3 /5 ei set ",e
252 $R,O /. call !,NN set ",h
253 .-#,R /$ Spre&i'es set ",l
instructions
usin* i[T
254 R#NRN /# cp N set ",(hl)
255 .8PQ // rst 56 set ",a
APPENDIX B
Reports
8he3e appear at the bottom o2 the 3creen 5henever the comp0ter 3top3 exec0tin4 3ome B*;IC, and explain 5hy it
3topped, 5hether 2or a nat0ral rea3on, or beca03e an error occ0rred(
8he report ha3 a code n0mber or letter 3o that yo0 can re2er to the table here, a brie2 me33a4e explainin4 5hat
happened and the line n0mber and 3tatement n0mber 5ithin that line 5here it 3topped( (* command i3 3ho5n a3 line
'( /ithin a line, 3tatement 1 i3 at the be4innin4, 3tatement 2 come3 a2ter the 2ir3t colon or 8CE,, and 3o on()
8he behavio0r o2 C<,8I,:E depend3 very m0ch on the report3( ,ormally, C<,8I,:E 4oe3 to the line and
3tatement 3peci2ied in the la3t report, b0t there are exception3 5ith report3 ', & and - (al3o 3ee *ppendix C)(
Cere i3 a table 3ho5in4 all the report3( It al3o tell3 yo0 in 5hat circ0m3tance3 the report can occ0r, and thi3 re2er3
yo0 to *ppendix C( Dor in3tance error * Invalid ar40ment can occ0r 5ith ;N7, I,, *C; and *;, and the entrie3 2or
the3e in *ppendix C tell yo0 exactly 5hat ar40ment3 are invalid(
Code Meaning Situations
0 07 *ny
Success#ul completion- or >ump to a line number bigger than any e0isting+ This report does
not change the line and statement >umped to by !O;T.;48+
1 NEHT I#t:out '3R ,E.8
The control variable does not e0ist ?it has not been set up by a 9OR statement@- but there is
an ordinary variable "ith the same name+
2 Aar#ab5e ot Bou% *ny
9or a simple variable this "ill happen i# the variable is used be#ore it has been assigned to
in a L8T- R8$7 or .;,4T statement or loaded #rom tape or set up in a 9OR statement+ 9or
a subscripted variable it "ill happen i# the variable is used be#ore it has been dimensioned in
a 7./ statement or loaded #rom tape+
3 Sub"cr#pt Iro& ;0b3cripted variable3, ;0b3trin43
$ subscript is beyond the dimension o# the array- or there are the "rong number o#
subscripts+ .# the subscript is negative or bigger than A3- then error 6 "ill result+
) 3ut oB memor; 9E8, I,+:8, D<7, -IE, H< ;:B, 9<*-, EE7HE(
;ometime3 d0rin4 expre33ion eval0ation(
There is not enough room in the computer #or "hat you are trying to do+ .# the computer
really seems to be stuc* in this state- you may have to clear out the command line using
78L8T8 and then delete a program line or t"o ?"ith the intention o# putting them bac*
a#ter"ards@ to give yoursel# room to manoeuvre "ith - say - !L8$R+
5 3ut oB "cree I,+:8, +7I,8 *8
$n .;,4T statement has tried to generate more than 23 lines in the lo"er hal# o# the
screen+ $lso occurs "ith ,R.;T $T 22- + + +
1 Number too b#& *ny arithmetic
!alculations have led to a number greater than about 1=
38
+
E RET*RN I#t:out 23 S*> 7E8:7,
There has been one more R8T4R; than there "ere <O S46s+
8 E% oB B#5e Eicrodrive, etc, operation3
BundocumentedC
0 ST3P "tatemet ;8<+
$#ter this- !O;T.;48 "ill not repeat the STO,- but carries on "ith the statement a#ter+
? I!a5#% ar&umet ;N7, 9,, *;,, *C;, :;7 (5ith 3trin4 ar40ment)
The argument #or a #unction is no good #or some reason+
> Ite&er out oB ra&e 7:,, 7*,-<EIBE, +<GE, -IE, H< 8<, H< ;:B, 9I;8,
99I;8, +*:;E, +9<8, CC7K, +EEG, :;7 (5ith n0meric
ar40ment), *rray acce33
Dhen an integer is re:uired- the #loating point argument is rounded to the nearest integer+
.# this is outside a suitable range then error 6 results+ 9or array access- see also 8rror 3+
( No"e"e # >?SI( I*9, I*9K
The te0t o# the ?string@ argument does not #orm a valid e0pression+

@ >RE?7 , (3NT repeat" 9<*-, ;*IE, IE7ID6, EE7HE, 9+7I,8, 99I;8, C<+6(
*l3o 5hen the comp0ter a313 3crollA and yo0 type
,, ;+*CE or ;8<+(
6R8$E "as pressed during some peripheral operation+ The behaviour o# !O;T.;48
a#ter this report is normal in that it repeats the statement+ !ompare "ith report L+
E 3ut oB @?T? 7E*-
'ou have tried to R8$7 past the end o# the 7$T$ list+
' I!a5#% B#5e ame ;*IE
S$F8 "ith name empty or longer than 1= characters +
2 No room Bor 5#e Enterin4 a line into the pro4ram
There is not enough room le#t in memory to accommodate the ne" program line+
4 ST3P # INP*T I,+:8
Some .;,4T data started "ith STO,- or - #or .;,4T L.;8 - "as pressed+ 4nli*e the case
"ith report 9- a#ter report & !O;T.;48 "ill behave normally- by repeating the .;,4T
statement+
I '3R I#t:out NEHT D<7
There "as a 9OR loop to be e0ecuted no times ?e+g+ 9OR nG 1 TO =@ and the
corresponding ;8)T statement could not be #ound+
J I!a5#% I/3 %e!#ce Eicrodrive, etc, operation3
BundocumentedC
7 I!a5#% co5our I,G, +*+E7, B<7-E7, D9*;C, B7IHC8, I,IE7;E, <IE7>
al3o a2ter one o2 the corre3pondin4 control character3(
The number speci#ied is not an appropriate value+
L >RE?7 #to pro&ram *ny
6R8$E pressed- this is detected bet"een t"o statements+ The line and statement number in
the report re#er to the statement be#ore 6R8$E "as pressed- but !O;T.;48 goes to the
statement a#ter ?allo"ing #or any >umps to be done@- so it does not repeat any statements+
M R?MT3P o &oo% C9E*7> po33ibly in 7:,
The number speci#ied #or R$/TO, is either too big or too small+
N Statemet 5o"t 7E8:7,, ,E.8, C<,8I,:E
%ump to a statement that no longer e0ists+
3 I!a5#% "tream Eicrodrive,etc, operation3
BundocumentedC
P 'N I#t:out @E' D,
4ser-de#ined #unction
P Parameter error D,
Drong number o# arguments- or one o# them is the "rong type ?string instead o# number or
vice versa@+
R Tape 5oa%#& error IE7ID6 9<*- or EE7HE
$ #ile on tape "as #ound but #or some reason could not be read in- or "ould not veri#y+
APPENDIX C (Part 1)
$ description o# the () Spectrum #or re#erence
8he 2ir3t 3ection o2 thi3 appendix i3 a repeat o2 that part o2 the Introd0ction concernin4 the 1eyboard and 3creen(
The keyboard
B. ;pectr0m character3 compri3e not only the 3in4le 3ymbol3 (letter3, di4it3, etc), b0t al3o the compo0nd to1en3
(1ey5ord3, 20nction name3, etc) and all the3e are entered trom the 1eyboard rather than bein4 3pelled o0t( 8o obtain
all the3e 20nction3 and command3 3ome 1ey3 have 2ive or more di3tinct meanin43, 4iven partly by 3hi2tin4 the 1ey3
(i(e( pre33in4 either the C*+; ;CID8 1ey or the ;6EB<9 ;CID8 1ey at the 3ame time a3 the re?0ired one) and
partly by havin4 the machine in di22erent mode3(
8he mode i3 indicated by the c0r3or, a 2la3hin4 letter that 3ho53 5here the next character 2rom the 1eyboard 5ill be
in3erted(
K (2or 1ey5ord3) mode a0tomatically replace3 9 mode 5hen the machine i3 expectin4 a command or pro4ram line
(rather than I,+:8 data), and 2rom it3 po3ition on the line it 1no53 it 3ho0ld expect a line n0mber or a 1ey5ord( 8hi3
i3 at the be4innin4 o2 the line, or F03t a2ter 8CE,, or F03t a2ter: (except in a 3trin4)( I2 0n3hi2ted, the next 1ey 5ill be
interpreted a3 either a 1ey5ord (5ritten on the 1ey3), or a di4it(
L (2or letter3) mode normally occ0r3 at all other time3( I2 0n3hi2ted, the next 1ey 5ill be interpreted a3 the main
3ymbol on that 1ey, in lo5er ca3e 2or letter3(
In both K and L mode3, ;6EB<9 ;CID8 and a 1ey 5ill be interpreted a3 the 30b3idiary red character on the 1ey
and C*+; ;CID8 5ith a di4it 1ey 5ill be interpreted a3 the control 20nction 5ritten in 5hite above the 1ey( C*+;
;CID8 5ith other 1ey3 doe3 not a22ect the 1ey5ord3 in K mode, and in L mode it convert3 lo5er ca3e to capital3(
C (2or capital3) mode i3 a variant o2 L mode in 5hich all letter3 appear a3 capital3( C*+; 9<CG ca03e3 a chan4e
2rom L mode to C mode or bac1 a4ain(
E (2or extended) mode i3 03ed 2or obtainin4 20rther character3, mo3tly to1en3( It occ0r3 a2ter both 3hi2t 1ey3 are
pre33ed to4ether, and la3t3 2or one 1ey depre33ion only( In thi3 mode, a letter 4ive3 one character or to1en (3ho5n in
4reen above it) i2 0n3hi2ted, and another (3ho5n in red belo5 it) i2 pre33ed 5ith either 3hi2t( * di4it 1ey 4ive3 a to1en
i2 pre33ed 5ith ;6EB<9 ;CID8> other5i3e it 4ive3 a colo0r control 3e?0ence(
G (2or 4raphic3) mode occ0r3 a2ter H7*+CIC; (C*+; ;CID8 and &) i3 pre33ed, and la3t3 0ntil it i3 pre33ed a4ain(
* di4it 1ey 5ill 4ive a mo3aic 4raphic, ?0it H7*+CIC; or -E9E8E, and each o2 the letter 1ey3 apart 2rom I, /, .,
6 and B, 5ill 4ive a 03erJde2ined 4raphic(
I2 any 1ey i3 held do5n 2or more than abo0t 2 or 3 3econd3, it 5ill 3tart repeatin4(
Geyboard inp0t appear3 in the bottom hal2 o2 the 3creen a3 it i3 typed, each character (3in4le 3ymbol or compo0nd
to1en) bein4 in3erted F03t be2ore the c0r3or( 8he c0r3or can be moved le2t 5ith C*+; ;CID8 and ", or ri4ht 5ith
C*+; ;CID8 and %( 8he character be2ore the c0r3or can be deleted 5ith -E9E8E (C*+; ;CID8 and ')( (,ote: the
5hole line can be deleted by typin4 E-I8 (C*+; ;CID8 and 1) 2ollo5ed by E,8E7()
/hen E,8E7 i3 pre33ed, the line i3 exec0ted, entered into the pro4ram, or 03ed a3 I,+:8 data a3 appropriate,
0nle33 it contain3 a 3yntax error( In thi3 ca3e a 2la3hin4 C appear3 next to the error(
*3 pro4ram line3 are entered, a li3tin4 i3 di3played in the top hal2 o2 the 3creen( 8he manner in 5hich the li3tin4 i3
prod0ced i3 rather complicated, and explained more 20lly in Chapter 2( 8he la3t line entered i3 called the c0rrent line
and i3 indicated by the 3ymbol >, b0t thi3 can be chan4ed by 03in4 the 1ey3 (C*+; ;CID8 and #) and (C*+;
;CID8 and $)( I2 E-I8 (C*+; ;CID8 and 1) i3 pre33ed, the c0rrent line i3 bro04ht do5n to the bottom part o2 the
3creen and can be edited(
/hen a command i3 exec0ted or a pro4ram r0n, o0tp0t i3 di3played in the top hal2 o2 the 3creen and remain3 0ntil a
pro4ram line i3 entered, or E,8E7 i3 pre33ed 5ith an empty line, or or i3 pre33ed( In the bottom part appear3 a
report 4ivin4 a code (di4it or letter) re2errin4 yo0 to *ppendix B, a brie2 verbal 30mmary o2 5hat *ppendix B 3ay3,
the n0mber o2 the line containin4 the la3t 3tatement exec0ted (or ' 2or a command) and the po3ition o2 the 3tatement
5ithin the line( 8he report remain3 on the 3creen 0ntil a 1ey i3 pre33ed (and indicate3 K mode)(
In certain circ0m3tance3, C*+; ;CID8 5ith the ;+*CE 1ey act3 a3 a B7E*G, 3toppin4 the comp0ter 5ith report -
or 9( 8hi3 i3 reco4ni3ed
(i) at the end o2 a 3tatement 5hile a pro4ram i3 r0nnin4, or
(ii) 5hile the comp0ter i3 03in4 the ca33ette recorder or printer(
The television screen
8hi3 ha3 2! line3, each 32 character3 lon4, and i3 divided into t5o part3( 8he top part i3 at mo3t 22 line3 and di3play3
either a li3tin4 or pro4ram o0tp0t( /hen printin4 in the top part ha3 reached the bottom, it all 3croll3 0p one line> i2
thi3 5o0ld involve lo3in4 a line that yo0 have not had a chance to 3ee yet, then the comp0ter 3top3 5ith the me33a4e
3crollA( +re33in4 the 1ey3 ,, ;+*CE or ;8<+ 5ill ma1e the pro4ram 3top 5ith report - B7E*GJ C<,8 repeat3>
any other 1ey 5ill let the 3crollin4 contin0e( 8he bottom part i3 03ed 2or inp0ttin4 command3, pro4ram line3, and
I,+:8 data, and al3o 2or di3playin4 report3( 8he bottom part 3tart3 o2 a3 t5o line3 (the 0pper one blan1), b0t it
expand3 to accommodate 5hatever i3 typed in( /hen it reache3 the c0rrent print po3ition in the top hal2, 20rther
expan3ion3 5ill ma1e the top hal2 3croll 0p(
Each character po3ition ha3 attrib0te3 3peci2yin4 it3 paper (bac14ro0nd) and in1 (2ore4ro0nd) colo0r3, a t5oJlevel
bri4htne33, and 5hether it 2la3he3 or not( 8he available colo0r3 are blac1, bl0e, red, ma4enta, 4reen, yello5 and 5hite(
8he ed4e o2 the 3creen can be 3et to any o2 the colo0r3 03in4 the border 3tatement(
* character po3ition i3 divided into %x% pixel3 and hi4h re3ol0tion 4raphic3 are obtained by 3ettin4 the pixel3
individ0ally to 3ho5 either the in1 or paper colo0r 2or that character po3ition(
8he attrib0te3 at a character po3ition are adF03ted 5henever a character i3 5ritten there or a pixel i3 plotted 8he
exact manner o2 the adF03tment i3 determined by the pnntin4 parameter3( o2 5hich there are t5o 3et3 (called
permanent and temporary) o2 3ix: the +*+E7, I,G, D9*;C, B7IHC8, I,IE7;E and <IE7 parameter3( +ermanent
parameter3 2or the top part are 3et 0p by +*+E7, I,G, etc, 3tatement3, and la3t 0ntil 20rther notice( (Initially they are
blac1 in1 on 5hite paper( /ith normal bri4htne33, no 2la3hin4, normal video and no overprintin4)( +ermanent
parameter3 2or the bottom part 03e the border colo0r a3 the paper colo0r, 5ith a blac1 or 5hite contra3tin4 in1 colo0r,
normal bri4htne33, no 2la3hin4, normal video and no overprintin4(
8emporary parameter3 are 3et 0p by +*+E7, I,G, etc, item3, 5hich are embedded in +7I,8, 9+7I,8, I,+:8,
+9<8, -7*/ and CI7C9E 3tatement3, and al3o by +*+E7, I,G, etc control character3 5hen they are printed to
the televi3ion J they are 2ollo5ed by a 20rther byte to 3peci2y the parameter val0e( 8emporary parameter3 la3t only to
the end o2 the +7I,8 (or 5hatever) 3tatement, or, in I,+:8 3tatement3, 0ntil 3ome I,+:8 data i3 needed 2rom the
1eyboard, 5hen they are replaced by the permanent parameter3(
+*+E7 and I,G parameter3 are in the ran4e ' to &( +arameter3 ' to $ are the colo0r3 03ed 5hen a character i3
printed:
' blac1
1 bl0e
2 red
3 ma4enta
! 4reen
" cyan
# yello5
$ 5hite
+arameter % (=tran3parent=) 3peci2ie3 that the colo0r on the 3creen i3 to be le2t 0nchan4ed 5hen a character i3 printed(

+arameter & (=contra3t=) 3peci2ie3 that the colo0r in ?0e3tion (paper or in1) i3 to be made either 5hite or blac1 to
3ho5 0p a4ain3t the other colo0r(
D9*;C and B7IHC8 parameter3 are ',1 or %: 1 mean3 that 2la3hin4 or bri4htne33 i3 t0rned on, ' that it i3 t0rned
o22, and % (=tran3parent=) that it i3 le2t 0nchan4ed at any character po3ition(
<IE7 and I,IE7;E parameter3 are ' or 1:
<IE7 ' ne5 character3 obliterate old one3
<IE7 1 the bit pattern3 o2 the old and ne5 character3 are combined 03in4 an =excl03ive or= operation (overprintin4)
I,IE7;E ' ne5 character3 are printed a3 in1 colo0r on paper colo0r (normal video)
I,IE7;E 1 ne5 character3 are printed a3 paper colo0r on in1 colo0r (inver3e video)
/hen a 8*B control character i3 received by the televi3ion, t5o more byte3 are expected to 3peci2y a tab 3top n
(le33 3i4ni2icant byte 2ir3t)( 8hi3 i3 red0ced mod0lo 32 to n
=
(3ay), and then 3022icient 3pace3 are printed to move the
printin4 po3ition into col0mn n
=
( /hen a comma control character i3 received, then 3022icient 3pace3 (at lea3t one) are
printed to move the printin4 po3ition into col0mn ' or col0mn 1#( /hen an E,8E7 control character i3 received, the
printin4 po3ition i3 moved on to the next line(
The printer
<0tp0t to the B. printer i3 via a b022er one line (32 character3) lon4, and a line i3 3ent to the printer
(i) 5hen printin4 3pill3 over 2rom one line to the next,
(ii) 5hen an E,8E7 character i3 received,
(iii) at the end o2 the pro4ram, i2 there i3 anythin4 le2t 0nprinted,
(iv) 5hen a 8*B control or comma control move3 the printin4 po3ition on to a ne5 line(
8*B control3 and comma control3 o0tp0t 3pace3 in the 3ame 5ay a3 on the televi3ion(
8he *8 control chan4e3 the printin4 po3ition 03in4 the col0mn n0mber, and i4nore3 the line n0mber(
8he printer i3 a22ected by I,IE7;E and <IE7 control3 (and al3o 3tatement3) in the 3ame 5ay a3 the 3creen i3, b0t
not by +*+E7, I,G, D9*;C or B7IHC8(
8he printer 5ill 3top 5ith error B i2 B7E*G i3 pre33ed( I2 the printer i3 ab3ent the o0tp0t 5ill 3imply be lo3t(
APPENDIX C (Part 2)
The 6$S.!
,0mber3 are 3tored to an acc0racy o2 & or 1' di4it3( 8he lar4e3t n0mber yo0 can 4et i3 abo0t 1'3%, and the 3malle3t
(po3itive) n0mber i3 abo0t !Q1'J3&(
* n0mber i3 3tored in the B. ;pectr0m in 2loatin4 point binary 5ith one exponent byte e (l<=e<=2""), and 2o0r
manti33a byte3 m ([<=m<l)( 8hi3 repre3ent3 the n0mber mQ2
eJ12%

;ince [<=m<l, the mo3t 3i4ni2icant bit o2 the manti33a m i3 al5ay3 1( 8here2ore in act0al 2act 5e can replace it 5ith
a bit to 3ho5 the 3i4n J ' 2or po3itive n0mber3, 1 2or ne4ative(
;mall inte4er3 have a 3pecial repre3entation in 5hich the 2ir3t byte i3 ', the 3econd i3 a 3i4n byte (' or DDh) and the
third and 2o0rth are the inte4er in t5o3 complement 2orm, the le33 3i4ni2icant byte 2ir3t(
,0meric variable3 have name3 o2 arbitrary len4th, 3tartin4 5ith a letter and contin0in4 5ith letter3 and di4it3( ;pace3
and colo0r control3 are i4nored and all letter3 are converted to lo5erJca3e letter3(
Control variable3 o2 D<7J,E.8 loop3 have name3 a 3in4le letter lon4(
,0meric array3 have name3 a 3in4le letter lon4, 5hich may be the 3ame a3 the name o2 a 3imple variable( 8hey may
have arbitrarily many dimen3ion3 o2 arbitrary 3iLe( ;0b3cript3 3tart at 1(
;trin43 are completely 2lexible in len4th( 8he name o2 a 3trin4 con3i3t3 o2 a 3in4le letter 2ollo5ed by K(
;trin4 array3 can have arbitrarily many dimen3ion3 o2 arbitrary 3iLe( 8he name i3 a 3in4le letter 2ollo5ed by K and
may not be the 3ame a3 the name o2 a 3trin4( *ll the 3trin43 in a 4iven array have the 3ame 2ixed len4th, 5hich i3
3peci2ied a3 an extra, 2inal dimen3ion in the -IE 3tatement( ;0b3cript3 3tart at 1(
;licin4: ;0b3trin43 o2 3trin43 may be 3peci2ied 03in4 3licer3( * 3licer can be
(i) empty or
(ii) n0merical expre33ion or
(iii) optional n0merical expre33ion 8< optional n0merical expre33ion
and i3 03ed in expre33in4 a 30b3trin4 either by
(a) 3trin4 expre33ion (3licer)
(b) 3trin4 array variable (30b3cript,(((, 30b3cript, 3licer)
5hich mean3 the 3ame a3
string array variable (subscript . . . , subscript) (slicer)
In (a), 30ppo3e the 3trin4 expre33ion ha3 the val0e 3K(
I2 the 3licer i3 empty, the re30lt i3 3K con3idered a3 a 30b3trin4 o2 it3el2(
I2 the 3licer i3 a n0merical expre33ion 5ith val0e m, then the re30lt i3 the mth character o2 3K (a 30b3trin4 o2 len4th
1)(
I2 the 3licer ha3 the 2orm (iii), then 30ppo3e the 2ir3t n0merical expre33ion ha3 the val0e m (the de2a0lt val0e i3 1),
and the 3econd, n (the de2a0lt val0e i3 the len4th o2 3K)(
I2 1<=m<=n<=the len4th o2 3K then the re30lt i3 the 30b3trin4 o2 3K 3tartin4 5ith the mth character and endin4 5ith
the nth( I2 '<=n<m then the re30lt i3 the empty 3trin4( <ther5i3e, error 3 re30lt3(
;licin4 i3 per2ormed be2ore 20nction3 or operation3 are eval0ated, 0nle33 brac1et3 dictate other5i3e(

;0b3trin43 can be a33i4ned to (3ee 9E8)(
I2 a 3trin4 ?0ote i3 to be 5ritten in a 3trin4 literal, then it m03t be do0bled(
Functions
8he ar40ment o2 a 20nction doe3 not need brac1et3 i2 it i3 a con3tant or a (po33ibly 30b3cripted or 3liced) variable(
'unction Ty6e of ar7ument Result
839
?>S number *b3ol0te ma4nit0de
?(S number *rcco3ine in radian3(
Error * i2 x not in the ran4e J1 to P1
?N@ binary operation-
right operand al"ays
a number+ X * i2 B<'
;umeric le#t operand5 * *,- B = X ' i2 B='
;umeric right operand5 *K *,- B = X *K i2 B<'
X *K i2 B='
?SN number *rc3ine in radian3(
Error * i2 x not in the ran4e J1 to P1
?TN number *rctan4ent in radian3
?TTR t"o arguments- 0 and * n0mber 5ho3e binary 2orm code3 the
y- both numbersH attrib0te3 o2 line x, col0mn y on the televi3ion(
enclosed in brac*ets Bit $ (mo3t 3i4ni2icant) i3 1 2or 2la3hin4, ' 2or not
2la3hin4( Bit # i3 1 2or bri4ht, ' 2or normal( Bit3 "
to 3 are the paper colo0r( Bit3 2 to ' are the in1
colo0r(
Error B 0nle33 '<=x<=23 and '<=y<=31
>IN 8hi3 i3 not really a 20nction, b0t an alternative
notation 2or n0mber3: BI, 2ollo5ed by a
3e?0ence o2 '3 and 13 i3 the n0mber 5ith 30ch a
repre3entation in binary(
(4R< number 8he character 5ho3e code i3 x, ro0nded to the
neare3t inte4er
(3@E string 8he code o2 the 2ir3t character in x (or ' i2 x i3 the
empty 3trin4)
(3S number ?in radians@ Co3ine x
EHP number e
x
'N D, 2ollo5ed by a letter call3 0p a 03erJde2ined
20nction (3ee -ED)( 8he ar40ment3 m03t be
enclo3ed in brac1et3> even i2 there are no
ar40ment3 the brac1et3 m03t 3till be pre3ent(
IN number 8he re30lt o2 inp0ttin4 at proce33or level 2rom
port x ('<=x<=DDDDh) (load3 the bc re4i3ter
pair 5ith x and doe3 the a33embly lan40a4e
in3tr0ction in a(c))
IN7EQ< none 7ead3 the 1eyboard( 8he re30lt i3 the character
repre3entin4 (in L or C mode) the 1ey pre33ed
i2 there i3 exactly one, el3e the empty 3trin4(
INT number Inte4er part (al5ay3 ro0nd3 do5n)
LEN string 9en4th
LN number ,at0ral lo4arithm (to ba3e e)(
Error * i2 x<='
N3T number ' i2 x<', 1 i2 x='( ,<8 ha3 priority !
3R binary operation- a <7 b= X 1 i2 b<'
both operands X a i2 b='
numbers <7 ha3 priority 2
PEE7 number 8he val0e o2 the byte in memory 5ho3e addre33
i3 x (ro0nded to the neare3t inte4er)(
Error B i2 x i3 not in the ran4e ' to #""3"
PI none t (3(1!1"&2#" ( ( ( )
P3INT T"o arguments- 0 and 1 i2 the pixel at (x,y) i3 in1 colo0r( ' i2 it i3 paper
y- both numbersH colo0r(
enclosed in brac*ets Error B 0nle33 '<=x<=2"" and '<=y<= 1$"
RN@ none 8he next p3e0dorandom n0mber in a 3e?0ence
4enerated by ta1in4 the po5er3 o2 $" mod0lo
#""3$, 30btractin4 1 and dividin4 by #""3#(
'sy< 1
S(REEN< T"o arguments- 0 and 8he character that appear3, either normally or
y- both numbersH inverted, on the televi3ion at line x, col0mn y(
enclosed in brac*ets Hive3 the empty 3trin4, i2 the character i3 not
reco4ni3ed (
Error B 0nle33 '<=x<=23 and '<=y<=31
S2N number ;i4n0m: the 3i4n (J1 2or ne4ative, ' 2or Lero or
P1 2or po3itive) o2 0
SIN number ?in radians@ ;ine 0
SPR number ;?0are root(
Error * i2 x<'
STR< number 8he 3trin4 o2 character3 that 5o0ld be di3played
i2 0 5ere printed
T?N number ?in radians@ 8an4ent
*SR number Call3 the machine code 30bro0tine 5ho3e
3tartin4 addre33 i3 0( <n ret0rn, the re30lt i3 the
content3 o2 the bc re4i3ter pair
*SR string 8he addre33 o2 the bit pattern 2or the 03erJ
de2ined 4raphic corre3pondin4 to 0(
Error * i2 0 i3 not a 3in4le letter bet5een a and
0, or a 03erJde2ined 4raphic
A?L string Eval0ate3 0 (5itho0t it3 bo0ndin4 ?0ote3) a3 a
n0merical expre33ion(
Error C i2 0 contain3 a 3yntax error, or 4ive3 a
3trin4 val0e( <ther error3 po33ible, dependin4 on
the expre33ion
A?L< string Eval0ate3 0 (5itho0t it3 bo0ndin4 ?0ote3) a3 a
3trin4 expre33ion(
Error C i2 x contain3 a 3yntax error or 4ive3 a
n0meric val0e( <ther error3 po33ible, a3 2or I*9
J number ,e4ation
8he 2ollo5in4 are binary operation3:
P *ddition (on n0mber3), or concatenation (on 3trin43)
J ;0btraction
Q E0ltiplication
R -ivi3ion
| 7ai3in4 to a po5er( Error B i2 the le2t operand i3 ne4ative
= E?0al3
Hreater than O Both operand3 m03t be o2 the
< 9e33 than O 3ame type( 8he re30lt i3 a
<= 9e33 than or e?0al to O n0mber 1, i2 the compari3on
= Hreater than or e?0al to O hold3 and ' i2 it doe3 not
< ,ot e?0al to O

D0nction3 and operation3 have the 2ollo5in4 prioritie3:
Operation ,riority
;0b3criptin4 and 3licin4 12
*ll 20nction3 except ,<8 and 0nary min03 11
| 1'
:nary min03 (i(e( min03 F03t 03ed to ne4ate 3omethin4) &
Q, R %
P, J (min03 03ed to 30btract one n0mber 2rom another) #
=, , <, <=, =, < "
,<8 !
*,- 3
<7 2
Statements
In thi3 li3t,
o repre3ent3 a 3in4le letter
v repre3ent3 a variable
x, y, L repre3ent n0merical expre33ion3
m, n repre3ent n0merical expre33ion3 that are ro0nded to the neare3t inte4er
e repre3ent3 an expre33ion
2 repre3ent3 a 3trin4 val0ed expre33ion
3 repre3ent3 a 3e?0ence o2 3tatement3 3eparated by colon3
c repre3ent3 a 3e?0ence o2 colo0r item3, each terminated by comma3, or 3emiJcolon3>
a colo0r item ha3 the 2orm o2 a +*+E7, I,G, D9*;C, B7IHC8, I,IE7;E or <IE7
3tatement(
,ote that arbitrary expre33ion3 are allo5ed every5here (except 2or the line n0mber at the be4innin4 o2 a 3tatement)(
*ll 3tatement3 except I,+:8, -ED and -*8* can be 03ed either a3 command3 or in pro4ram3 (altho04h they can
be more 3en3ible in one than the other)( * command or pro4ram line can have 3everal 3tatement3, 3eparated by colon3
(:)( 8here i3 no re3triction on 5hereabo0t3 in a line any partic0lar 3tatement can occ0r J altho04h 3ee ID and 7EE(
>EEP x, y ;o0nd3 a note thro04h the lo0d3pea1er 2or x 3econd3 at
a pitch y 3emitone3 above middle C (or belo5 i2 y i3
ne4ative)(
>3R@ER m ;et3 the colo0r o2 the border o2 the 3creen and al3o the
paper colo0r 2or the lo5er part o2 the 3creen(
Error G i3 m not in the ran4e ' to $(
>RI24T ;et3 bri4htne33 o2 character3 30b3e?0ently printed(
n=' 2or normal, 1 2or bri4ht( % 2or tran3parent(
Error G i2 n not ', 1 or %
(?T -oe3 not 5or1 5itho0t Eicrodrive, etc
(IR(LE x, y, L -ra53 an arc o2 a circle, centre (x,y), radi03 L
(LE?R -elete3 all variable3, 2reein4 the 3pace they occ0pied(
-oe3 7E;8<7E and C9;, re3et3 the +9<8 po3ition
to the bottom le2tJhand corner and clear3 the H< ;:B
3tac1
(LE?R n 9i1e C9E*7( b0t i2 po33ible chan4e3 the 3y3tem
variable 7*E8<+ to n and p0t3 the ne5 H< ;:B
3tac1 there
(L3SE R -oe3 not 5or1 5itho0t Eicrodrive, etc
(LS (Clear ;creen)( Clear3 the di3play 2ile
(3NTIN*E Contin0e3 the pro4ram, 3tartin4 5here it le2t o22 la3t
time it 3topped 5ith report other than '( I2 the report
5a3 & or 9, then contin0e3 5ith the 2ollo5in4
3tatement (ta1in4 F0mp3 into acco0nt)> other5i3e
repeat3 the one 5here the error occ0rred(
I2 the la3t report 5a3 in a command line then
C<,8I,:E 5ill attempt to contin0e the command line
and 5ill either 4o into a loop i2 the error 5a3 in ':1,
4ive report ' i2 it 5a3 in ': 2, or 4ive error , i2 it 5a3 ': 3
or 4reater(
C<,8I,:E appear3 a3 C<,8 on the 1eyboard
(3PQ ;end3 a copy o2 the top 22 line3 o2 di3play to the
printer, i2 attached> other5i3e doe3 nothin4( ,ote that
C<+6 can not be 03ed to print the a0tomatic li3tin43
that appear on the 3creen(
7eport - i2 B7E*G pre33ed
@?T? o
1
, o
2
, o
3
, ((( +art o2 the -*8* li3t( E03t be in a pro4ram
@E' 'N o (o
1

( ( ( o
1
)=e :3erJde2ined 20nction de2inition> m03t be in a
pro4ram( Each o2 o and o
1
to o
1
i3 either a 3in4le letter
or a 3in4le letter 2ollo5ed by =K= 2or 3trin4 ar40ment or
re30lt(
8a1e3 the 2orm -ED D, o()=e i2 no ar40ment3
@ELETE 2 -oe3 not 5or1 5itho0t Eicrodrive, etc
@IM o( n
1
, ( ( ( ,n
1

) -elete3 any array 5ith the name o, and 3et3 0p an
array o o2 n0mber3 5ith 1 dimen3ion3 n
1
, ((( ,n
1
(
Initiali3e3 all the val0e3 to '
@IM oK( n
1
, ( ( ( ,n
1
) -elete3 any array or 3trin4 5ith the name oK, and 3et3
0p an array o2 character3 5ith 1 dimen3ion3 n
l
,(((,n
1
(
Initiali3e3 all the val0e3 to @ @( 8hi3 can be con3idered
a3 an array o2 3trin43 o2 2ixed len4th n
1

, 5ith 1Jl
dimen3ion3 n
l
,(((,n
1
J
l

(
Error ! occ0r3 i2 there i3 no room to 2it the array in(
*n array i3 0nde2ined 0ntil it i3 dimen3ioned in a -IE
3tatement
@R?W x,y -7*/ x,y,'
@R?W x,y,L -ra53 a line 2rom the c0rrent plot po3ition movin4 x
horiLontally and y vertically relative to it 5hile t0rnin4
thro04h an an4le L(
Error B i2 it r0n3 o22 the 3creen
ER?SE -oe3 not 5or1 5itho0t Eicrodrive, etc(
'L?S4 -e2ine3 5hether character3 5ill be 2la3hin4 or 3teady(
n=' 2or 3teady, n=l 2or 2la3h, n=% 2or no chan4e(
'3R o=x T3 y D<7 o=x 8< y ;8E+ 1
'3R o=x T3 y STEP L -elete3 any 3imple variable o and 3et3 0p a control
variable 5ith val0e x, limit y, 3tep L, and loopin4
addre33 re2errin4 to the 3tatement a2ter the D<7
3tatement( Chec13 i2 the initial val0e i3 4reater (i2
3tep=') or le33 (i2 3tep<') than the limit, and i2 3o
then 31ip3 to 3tatement ,E.8 o, 4ivin4 error 1 i2 there
i3 none( ;ee ,E.8(
Error ! occ0r3 i2 there i3 no room 2or the control variable(
'3RM?T 2 -oe3 not 5or1 5itho0t the Eicrodrive, etc
23S*> n +03he3 the line n0mber o2 the H<;:B 3tatement
onto a 3tac1> then a3 H< 8< n(
Error ! can occ0r i2 there are not eno04h 7E8:7,3
23 T3 n U0mp3 to line n (or, i2 there i3 none, the 2ir3t line a2ter
that)
I' x T4EN 3 I2 x tr0e (nonJLero) then 3 i3 exec0ted( ,ote that 3
compri3e3 all the 3tatement3 to the end o2 the line(
8he 2orm =ID x 8CE, line n0mber= i3 not allo5ed(
IN7 n ;et3 the in1 (2ore4ro0nd) colo0r o2 character3
30b3e?0ently printed( n i3 in the ran4e ' to $ 2or a
colo0r, n=% 2or tran3parent or & 2or contra3t( ;ee The
television screen J *ppendix B(
Error G i2 n not in the ran4e ' to &(
INP*T 8he = ( ( ( = i3 a 3e?0ence o2 I,+:8 item3, 3eparated a3 in
a +7I,8 3tatement by comma3, 3emicolon3 or
apo3trophe3( *n I,+:8 item can be
(i) *ny +7I,8 item not be4innin4 5ith a letter
(ii) * variable name, or
(iii) 9I,E, then a 3trin4 type variable name(
8he +7I,8 item3 and 3eparator3 in (i) are treated
exactly a3 in +7I,8, except that everythin4 i3 printed
in the lo5er part o2 the 3creen(
Dor (ii) the comp0ter 3top3 and 5ait3 2or inp0t o2 an
expre33ion 2rom the 1eyboard> the val0e o2 thi3 i3
a33i4ned to the variable( 8he inp0t i3 echoed in the
030al 5ay and 3yntax error3 4ive the 2la3hin4 C( Dor
3trin4 type expre33ion3, the inp0t b022er i3 initiali3ed to
contain t5o 3trin4 ?0ote3 (5hich can be era3ed i2
nece33ary)( I2 the 2ir3t character in the inp0t i3 ;8<+,
the pro4ram 3top3 5ith error C( (iii) i3 li1e (ii) except
that the inp0t i3 treated a3 a 3trin4 literal 5itho0t
?0ote3, and the ;8<+ mechani3m doe3n=t 5or1> to
3top it yo0 m03t type in3tead(
INAERSE n Control3 inver3ion o2 character3 30b3e?0ently printed(
I2 n=', character3 are printed in normal video, a3 in1
colo0r on paper colo0r(
I2 n=1, character3 are printed in inver3e video, i(e(
paper colo0r on in1 colo0r( ;ee The television screen J
*ppendix B(
Error G i2 n i3 not ' or 1
LET v=e *33i4n3 the val0e o2 e to the variable v( 9E8 cannot be
omitted( * 3imple variable i3 0nde2ined 0ntil it i3
a33i4ned to in a 9E8, 7E*- or I,+:8 3tatement( I2 v i3
a 30b3cripted 3trin4 variable, or a 3liced 3trin4 variable
(30b3trin4), then the a33i4nment i3 +rocr03tean (2ixed
len4th): the 3trin4 val0e o2 e i3 either tr0ncated or 2illed
o0t 5ith 3pace3 on the ri4ht, to ma1e it the 3ame
len4th a3 the variable v
LIST 9I;8 '
LIST n 9i3t3 the pro4ram to the 0pper part o2 the 3creen,
3tartin4 at the 2ir3t line 5ho3e n0mber i3 at lea3t n, and
ma1e3 n the c0rrent line
LLIST 99I;8 '
LLIST n 9i1e 9I;8, b0t 03in4 the printer
L3?@ 2 9oad3 pro4ram and variable3
L3?@ 2 @?T? () 9oad3 a n0meric array
L3?@ 2 @?T? K() 9oad3 character array K
L3?@ 2 (3@E m,n 9oad3 at mo3t n byte3, 3tartin4 at addre33 m
L3?@ 2 (3@E m 9oad3 byte3 3tartin4 at addre33 m
L3?@ 2 (3@E 9oad3 byte3 bac1 to the addre33 they 5ere 3aved 2rom(
L3?@ 2 S(REEN< 9<*- 2 C<-E 1#3%!,#&12(
;earche3 2or 2ile o2 the ri4ht 3ort on ca33ette tape
and load3 it, deletin4 previo03 ver3ion3 in memory(
;ee Chapter 2'
LPRINT 9i1e +7I,8 b0t 03in4 the printer
MER2E 2 9i1e 9<*- 2( b0t doe3 not delete old pro4ram line3 and
variable3 except to ma1e 5ay 2or ne5 one3 5ith the
3ame line n0mber or name(
M3AE 2
1
,2
2
-oe3 not 5or1 5itho0t the Eicrodrive, etc
NEW ;tart3 the B*;IC 3y3tem o22 ane5, deletin4 pro4ram
and variable3, and 03in4 the memory 0p to and
incl0din4 the byte 5ho3e addre33 i3 in the 3y3tem
variable 7*EB<8 and pre3erve3 the 3y3tem variable3
:-H, + 7*E8, 7*;+ and +I+
NEHT o (i) Dind3 the control variable
(ii) *dd3 it3 3tep to it3 val0e
(iii) I2 the 3tep=' and the val0ethe limit> or i2 the
3tep<' and the val0e<the limit, then F0mp3 to the
loopin4 3tatement(
Error 2 i2 there i3 no variable o
Error 1 i2 there i3 one, b0t it=3 not o control variable
3PEN R -oe3 not 5or1 5itho0t the Eicrodrive, etc
3*T m,n <0tp0t3 byte n at port m at the proce33or level( (9oad3
the bc re4i3ter pair 5ith m, the a re4i3ter 5ith n, and
doe3 the a33embly lan40a4e in3tr0ction: o0t (c),a()
'<=m<=#""3", J2""<=n<=2"", el3e error B
3AER n Control3 overprintin4 2or character3 30b3e?0ently
printed (
I2 n=', character3 obliterate previo03 character3 at
that po3ition(
I2 n=l, then ne5 character3 are mixed in 5ith old
character3 to 4ive in1 colo0r 5herever either (b0t not
both) had in1 colo0r, and paper colo0r i2 they 5ere both
paper or both in1 colo0r( ;ee The television screen J
*ppendix B(
Error G i2 n not ' or 1
P?PER n 9i1e I,G, b0t controllin4 the paper (bac14ro0nd) colo0r
P?*SE n ;top3 comp0tin4 and di3play3 the di3play 2ile 2or n
2rame3 (at "' 2rame3 per 3econd or #' 2rame3 per
3econd in ,orth *merica) or 0ntil a 1ey i3 pre33ed(
'<=nc=#""3", el3e error B( I2 n=' then the pa03e i3
not timed, b0t la3t3 0ntil a 1ey i3 pre33ed(
PL3T c>m,n +rint3 an in1 3pot (30bFect to <IE7 and I,IE7;E) at
the pixel (\m\, \n\)> move3 the +9<8 po3ition(
:nle33 the colo0r item3 c 3peci2y other5i3e, the in1
colo0r at the character po3ition containin4 the pixel i3
chan4ed to the c0rrent permanent in1 colo0r, and the
other (paper colo0r, 2la3hin4 and bri4htne33) are le2t
0nchan4ed(
'<=\m\<=2"", '<=\n\<=1$", el3e error B
P37E m,n /rite3 the val0e n to the byte in 3tore 5ith addre33 m(
'<=m<=#""3", J2""<=n<=2"", el3e error B
PRINT 8he = ( ( ( = i3 a 3e?0ence o2 +7I,8 item3, 3eparated by
comma3 , , 3emicolon3 ; or apo3trophe3 ' and they are
5ritten to the di3play 2ile 2or o0tp0t to the televi3ion
* 3emicolon ; bet5een t5o item3 ha3 no e22ect: it i3
03ed p0rely to 3eparate the item3( * comma , o0tp0t3
the comma control character, and an apo3trophe ' o0tp0t3
the E,8E7 character(
*t the end o2 the +7I,8 3tatement, i2 it doe3 not end in a
3emicolon, or comma, or apo3trophe, an E,8E7 character
i3 o0tp0t(

* +7I,8 item can be
(i) empty, i(e( nothin4(
(ii) a n0merical expre33ion
Dir3t a min03 3i4n i3 printed i2 the val0e i3 ne4ative(
,o5 let x be the mod0l03 o2 val0e(
I2 x<=1'
J"
or x=1'
13
, then it i3 printed 03in4 3cienti2ic
notation( 8he manti33a part ha3 0p to ei4ht di4it3 (5ith
no trailin4 Lero3), and the decimal point (ab3ent i2 only
one di4it) i3 a2ter the 2ir3t( 8he exponent part i3 E,
2ollo5ed by P or J, 2ollo5ed by one or t5o di4it3(
<ther5i3e x i3 printed in ordinary decimal notation 5ith
0p to ei4ht 3i4ni2icant di4it3, and no trailin4 Lero3 a2ter
the decimal point( * decimal point ri4ht at the be4innin4
i3 al5ay3 2ollo5ed by a Lero, 3o 2or in3tance ('3 and '(3 are
printed a3 30ch(
' i3 printed a3 a 3in4le di4it '(
(iii) a 3trin4 expre33ion
8he to1en3 in the 3trin4 are expanded, po33ibly 5ith a 3pace
be2ore or a2ter(
Control character3 have their control e22ect(
:nreco4niLed character3 print a3 A(
(iv) *8 m,n
<0tp0t3 an *8 control character 2ollo5ed by a byte 2or m (the
line n0mber) and a byte 2or n (the col0mn n0mber)(
(v) 8*B n
<0tp0t3 a 8*B control character 2ollo5ed by t5o byte3 2or n
(le33 3i4ni2icant byte 2ir3t), the 8*B 3top(
(vi) * colo0r item, 5hich ta1e3 the 2orm o2 a +*+E7, I,G, D9*;C,
B7IHC8, I,IE7;E or <IE7 3tatement
R?N@3MIME 7*,-<EIBE '
R?N@3MIME n ;et3 the 3y3tem variable (called ;EE-) 03ed to
4enerate the next val0e o2 7,-( I2 n<', ;EE- i3
4iven the val0e n> i2 n=' then it i3 4iven the val0e o2
another 3y3tem variable (called D7*EE;) that co0nt3
the 2rame3 3o 2ar di3played on the televi3ion, and 3o
3ho0ld be 2airly random(
7*,-<EIBE appear3 a3 7*,- on the 1eyboard(
Error B occ0r3 i2 n i3 not in the ran4e ' to #""3"
RE?@ v
l
, v
2
, ( ( ( v
1
*33i4n3 to the variable3 03in4 30cce33ive expre33ion3
in the -*8* li3t(
Error C i2 an expre33ion i3 the 5ron4 type(
Error E i2 there are variable3 le2t to be read 5hen the
-*8* li3t i3 exha03ted
REM ( ( ( ,o e22ect( = ( ( ( = can be any 3e?0ence o2 character3
except E,8E7( 8hi3 can incl0de : , 3o no 3tatement3
are po33ible a2ter the 7EE 3tatement on the 3ame line
REST3RE 7E;8<7E '
REST3RE n 7e3tore3 the -*8* pointer to the 2ir3t -*8*
3tatement in a line 5ith n0mber at lea3t n: the next
7E*- 3tatement 5ill 3tart readin4 there
RET*RN 8a1e3 a re2erence to a 3tatement o22 the H< ;:B
3tac1, and F0mp3 to the line a2ter it(
Error $ occ0r3 5hen there i3 no 3tatement re2erence
on the 3tac1( 8here i3 3ome mi3ta1e in yo0r pro4ram
H< ;:B3 are not properly balanced by 7E8:7,3
R*N 7:, '
R*N n C9E*7, and then H< 8< n
S?AE 2 ;ave3 the pro4ram and variable3
S?AE 2 LINE m ;ave3 the pro4ram and variable3 3o that i2 they are
loaded there i3 an a0tomatic F0mp to line m
S?AE 2 @?T? () ;ave3 the n0meric array
S?AE 2 @?T? K() ;ave3 the character array K
S?AE 2 (3@E m,n ;ave3 n byte3 3tartin4 at addre33 m
S?AE 2 S(REEN< ;*IE 2 C<-E 1#3%!,#&12(
;ave3 in2ormation on ca33ette, 4ivin4 it the name 2
Error D i2 2 i3 empty or ha3 len4th eleven or more(
;ee Chapter 2'
ST3P ;top3 the pro4ram 5ith report &( C<,8I,:E 5ill
re30me 5ith the 2ollo5in4 3tatement
AERI'Q 8he 3ame a3 9<*- except that the data i3 not loaded
into 7*E, b0t compared a4ain3t 5hat i3 already there(
Error 7 i2 one o2 the compari3on3 3ho53 di22erent byte3(
APPENDIX D
80ample programs
8hi3 appendix contain3 3ome example pro4ram3 to demon3trate the abilitie3 o2 the B. ;pectr0m(
8he 2ir3t o2 the3e pro4ram3 re?0ire3 a date to be inp0t and 4ive3 the day o2 the 5ee1 5hich corre3pond3 to thi3 date(
10 REM convert date to day
20 DIM d!",#$: REM day% o& 'eek
(0 )*R n+1 T* ": RE,D d!n$: NEXT n
-0 DIM .!12$: REM /en0th% o& .onth%
10 )*R n+1 T* 12: RE,D .!n$: NEXT n
100 REM 2n34t date
110 INP5T 6day768day
120 INP5T 6.onth768.onth
1(0 INP5T 6year !20th cent4ry on/y$768year
1-0 I) year91:01 T;EN PRINT 620th cent4ry %tart% at 1:016: <* T* 100
110 I) year=2000 T;EN PRINT 620th cent4ry end% at 20006: <* T* 100
1#0 I) .onth91 T;EN <* T* 210
1"0 I) .onth=12 T;EN <* T* 210
1>0 I) year/-?INT!year/-$+0 T;EN @ET .!2$+2:: REM /ea3 year
1:0 I) day=.!.onth$ T;EN PRINT 6Th2% .onth ha% on/y 68 .!.onth$8
6 day%A6: <* T* 100
200 I) day=0 T;EN <* T* (00
210 PRINT 6Bt4&& and non%en%eA <2ve .e a rea/ dateA6
220 <* T* 100
(00 REM convert date to n4.Cer o& day% %2nce %tart o& cent4ry
(10 @ET y+year?1:01
(20 @ET C+(#1Dy+INT !y/-$: REM n4.Cer o& day% to %tart o& year
((0 )*R n+1 T* .onth 1: REM add on 3rev2o4% .onth%
(-0 @ET C+C+.!n$: NEXT n
(10 @ET C+C+day
-00 REM convert to day o& 'eek
-10 @ET C+C?"DINT !C/"$+1
-20 PRINT day86/68.onth86/68year
-(0 )*R n+# T* ( BTEP ?1: REM re.ove tra2/2n0 %3ace%
--0 I) d!C,n$ 9= 6 6 T;EN <* T* -#0
-10 NEXT n
-#0 @ET e+d!C, T* n$
-"0 PRINT6 2% a 68 e8 6day6
100 @ET .!2$+2>: REM re%tore )eCr4ary
110 INP5T 6a0a2n76, a
120 I) a+6n6 T;EN <* T* 1-0
1(0 I) a 9= 6N6 T;EN <* T* 100
1000 REM day% o& 'eek
1010 D,T, 6Mon6, 6T4e%6, 6Eedne%6
1020 D,T, 6Th4r%6, 6)r26, 6Bat4r6, 6B4n6
1100 REM /en0th% o& .onth%
1110 D,T, (1, 2>, (1, (0, (1, (0
1120 D,T, (1, (1, (0, (1, (0, (1
8hi3 pro4ram handle3 yard3, 2eet and inche3(
10 INP*T $;ar%"C$,;%,$BeetC$,Bt, $#c:e"C$,#
)0 23 S*> 20006 REM pr#t t:e !a5ue"
50 PRINT 9$ = $8
E0 23 S*> 10006 REM t:e a%Su"tmet
80 23 S*> 20006 REM pr#t t:e a%Su"te% !a5ue"
00 PRINT
100 23T3 10
1000 REM "ubrout#e to a%Su"t ;%, Bt, # to t:e orma5 Borm Bor ;ar%",
Beet a% #c:e"
1010 LET #=31.;%+12.Bt+#6 REM oI e!er;t:#& #" # #c:e"
1030 LET "=S2N #6 LET #=?>S #6 REM Ie IorL I#t: # po"#t#!e,
:o5%#& #t" "#& # "
1010 LET Bt=INT +#/12-6 LET #=+#,12.Bt-."6 REM oI # #" oL
1080 LET ;%=INT +Bt/3-."6 LET Bt=Bt.",3.;%6 RET*RN
2000 REM "ubrout#e to pr#t ;%, Bt a% #
2010 PRINT ;%8$;%$8Bt8$Bt$8#8$#$86 RET*RN
Cere i3 a pro4ram to thro5 coin3 2or the I Chin4( (:n2ort0nately it prod0ce3 the pattern3 0p3ide do5n( b0t yo0
mi4ht not 5orry abo0t thi3()
5 R?N@3MIME
10 '3R m=1 T3 16 REM Bor 1 t:roI"
20 LET c=06 REM ##t#a5#Ke co# tota5 to 0
30 '3R =1 T3 36 REM Bor 3 co#"
)0 LET c=c+2+INT +2.RN@-
50 NEHT 106 PRINT $ $8
E0 '3R =1 T3 26 REM 1"t Bor t:e t:roI :eGa&ram, 2% Bor t:e c:a&e"
80 PRINT $TTT$8
00 I' c=E T4EN PRINT $ $8
100 I' c=8 T4EN PRINT $ $8
110 I' c=1 T4EN PRINT $H$86 LET c=E
120 I' c=0 T4EN PRINT $0$86 LET c=8
130 PRINT $TTT $8
1)0 NEHT
150 PRINT
110 INP*T a<
1E0 NEHT m6 NEW
8o 03e thi3, type it in and r0n it, and then pre33 E,8E7 2ive time3 to 4et the t5o hexa4ram3( 9oo1 the3e 0p in a
copy o2 the Chine3e Boo1 o2 Chan4e3( 8he text 5ill de3cribe a 3it0ation and the co0r3e3 o2 action appropriate to it,
and yo0 m03t ponder deeply to di3cover the parallel3 bet5een that and yo0r o5n li2e( +re33 E,8E7 a 3ixth time, and
the pro4ram 5ill era3e it3el2 thi3 i3 to di3co0ra4e yo0 2rom 03in4 it 2rivolo03ly(
Eany people 2ind the text3 are al5ay3 more apt than they 5o0ld expect on 4ro0nd3 o2 chance> thi3 may or may not
be the ca3e 5ith yo0r B. ;pectr0m In 4eneral, comp0ter3 are pretty 4odle33 creat0re3(
Cere i3 a pro4ram to play =+an4olin3=( 6o0 thin1 0p an animal, and the comp0ter trie3 to 40e33 5hat it i3, by a31in4
yo0 ?0e3tion3 that can be an35ered =ye3= or =no=( I2 it=3 never heard o2 yo0r animal be2ore, it a313 yo0 to 4ive it 3ome
?0e3tion that it can 03e next time to 2ind o0t 5hether 3omeone=3 4iven it yo0r ne5 animal(
1 REM Pan0o/2n%
10 @ET nF+100: REM n4.Cer o& F4e%t2on% and an2.a/%
11 DIM F!nF,10$: DIM a!nF,2$: DIM r!1$
20 @ET F&+>
(0 )*R n+1 T* F&/2 1
-0 RE,D F!n$: RE,D a!n,1$: RE,D a!n,2$
10 NEXT n
#0 )*R n+n T* F&?1
"0 RE,D F!n$: NEXT n
100 REM %tart 3/ay2n0
110 PRINT 6Th2nk o& an an2.a/A6,6Pre%% any key to cont2n4eA6
120 P,5BE 0
1(0 @ET c+1: REM %tart '2th 1%t F4e%t2on
1-0 I) a!c,1$+0 T;EN <* T* (00
110 @ET 3+F!c$: <* B5B :10
1#0 PRINT 676: <* B5B 1000
1"0 @ET 2n+1: I) r+6y6 T;EN <* T* 210
1>0 I) r+6G6 T;EN <* T* 210
1:0 @ET 2n+2: I) r+6n6 T;EN <* T* 210
200 I) r9=6N6 T;EN <* T* 110
210 @ET c+a!c,2n$: <* T* 1-0
(00 REM an2.a/
(10 PRINT 6,re yo4 th2nk2n0 o&6
(20 @ET P+F!c$: <* B5B :00: PRINT 676
((0 <* B5B 1000
(-0 I) r+6y6 T;EN <* T* -00
(10 I) r+6G6 T;EN <* T* -00
(#0 I) r+6n6 T;EN <* T* 100
("0 I) r+6N6 T;EN <* T* 100
(>0 PRINT 6,n%'er .e 3ro3er/y 'hen IH.6,6ta/k2n0 to yo4A6: <* T* (00
-00 REM 04e%%ed 2t
-10 PRINT 6I tho40ht a% .4chA6: <* T* >00
100 REM ne' an2.a/
110 I) F&=nF?1 T;EN PRINT 6IH. %4re yo4r an2.a/ 2% very6, 62ntere%t2n0,
C4t I donHt have6,6roo. &or 2t I4%t no'A6: <* T* >00
120 @ET F!F&$+F!c$: REM .ove o/d an2.a/
1(0 PRINT 6Ehat 2% 2t, then76: INP5T F!F&+1$
1-0 PRINT 6Te// .e a F4e%t2on 'h2ch d2%t 6,62n042%he% Cet'een 6
110 @ET 3+F!F&$: <* B5B :00: PRINT 6 and6
1#0 @ET 3+F!F&+1$: <* B5B :00: PRINT 6 6
1"0 INP5T %: @ET C+@EN %
1>0 I) %!C$+676 T;EN @ET C+C?1
1:0 @ET F!c$+%!T* C$: REM 2n%ert F4e%t2on
#00 PRINT 6Ehat 2% the an%'er &or6
#10 @ET 3+F!F&+1$: <* B5B :00: PRINT 676
#20 <* B5B 1000
#(0 @ET 2n+1: @ET 2o+2: REM an%'er% &or ne' and o/d an2.a/%
#-0 I) r+6y6 T;EN <* T0 "00
#10 I) r+6G6 T;EN <* T* "00
##0 @ET 2n+2: @ET 2o+1
#"0 I) r+6n6 T;EN <* T* "00
#>0 I) r+6N6 T;EN <* T* "00
#:0 PRINT 6ThatH% no 0oodA 6: <* T* #00
"00 REM 43date an%'er%
"10 @ET a!c,2n$+F&+1: @ET a!c,2o$+F&
"20 @ET F&+F&+2: REM neJt &ree an2.a/ %3ace
"(0 PRINT 6That &oo/ed .eA6
>00 REM a0a2n7
>10 PRINT 6Do yo4 'ant another 0o76: <* B5B 1000
>20 I) r+6y6 T;EN <* T* 100
>(0 I) r+6G6 T;EN <* T* 100
>-0 BT*P
:00 REM 3r2nt '2tho4t tra2/2n0 %3ace%
:01 PRINT 6 68
:10 )*R n+10 T* 1 BTEP ?1
:20 I) 3!n$9=6 6 T;EN <* T* :-0
:(0 NEXT n
:-0 PRINT 3!T* n$8: RET5RN
1000 REM 0et re3/y
1010 INP5T r: I) r+66 T;EN RET5RN
1020 @ET r+r!1$: RET5RN
2000 REM 2n2t2a/ an2.a/%
2010 D,T, 6Doe% 2t /2ve 2n the %ea6,-,2
2020 D,T, 6I% 2t %ca/y6,(,1
20(0 D,T, 6Doe% 2t eat ant%6,#,"
20-0 D,T, 6a 'ha/e6, 6a C/anc.an0e6, 6a 3an0o/2n6, 6an ant6
Cere i3 a pro4ram to dra5 a :nion Dla4(
1 REM 4n2on &/a0
10 @ET r+2: @ET '+": @ET C+1
20 B*RDER 0: P,PER C: INK ': L@B
(0 REM C/ack 2n Cotto. o& %creen
-0 INMERBE 1
10 )*R n+-0 T* 0 BTEP ?>
#0AP@*T P,PER 08",n: DR,E P,PER 082-1,0
"0 NEXT n: INMERBE 0
100 REM dra' 2n 'h2te 3art%
101 REM BtA <eor0e
110 )*R n+0 T* "
120 P@*T 10-+n,1"1: DR,E 0,?(1
1(0 P@*T 111?n,1"1: DR,E 0,?(1
1-0 P@*T 111?n,->: DR,E 0,(1
110 P@*T 10-+n,->: DR,E 0,(1
1#0 NEXT n
200 )*R n+0 T* 11
210 P@*T 0,1(:?n: DR,E 111,0
220 P@*T 211,1(:?n: DR,E 111,0
2(0 P@*T 211,>-+n: DR,E ?111,0
2-0 P@*T 0,>-+n: DR,E 111,0
210 NEXT n
(00 REM BtA ,ndre'
(10 )*R n+0 T* (1
(20 P@*T 1+2Dn,1"1?n: DR,E (2,0
((0 P@*T 22-?2Dn,1"1?n: DR,E 1#,0
(-0 P@*T 21-?2Dn,->+n: DR,E?(2,0
(10 P@*T 1"+2Dn,->+n: DR,E 1#,0
(#0 NEXT n
("0 )*R n+0 T* 1:
(>0 P@*T 1>1+2Dn,1-0+n: DR,E (2,0
(:0 P@*T 200+2Dn,>(?n: DR,E 1#,0
-00 P@*T (:?2Dn,>(?n: DR,E (2,0
-10 P@*T 1-?2Dn,1-0+n: DR,E ?1#,0
-20 NEXT n
-21 REM &2// 2n eJtra C2t%
-(0 )*R n+0 T* 11
--0 P@*T 211,1#0+n: DR,E 2Dn?(0,0
-10 P@*T 0,#(?n DR,E (1?2Dn,0
-#0 NEXT n
-"0 )*R n+0 T* "
->0 P@*T 0,1#0+n: DR,E 1- 2Dn,0
->1 P@*T 211,#(?n: DR,E 2Dn?11,0
-:0 NEXT n
100 REM red %tr23e%
110 INMERBE 1
120 REM Bt <eor0e
1(0 )*R n+:# T* 120 BTEP >
1-0 P@*T P,PER r8",n: DR,E P,PER r82-1,0
110 NEXT n
1#0 )*R n+112 T* 1(# BTEP >
1"0 P@*T P,PER r8n,1#>: DR,E P,PER r80,?11(
1>0 NEXT n
#00 REM Bt Patr2ck
#10 P@*T P,PER r81"0,1-0: DR,E P,PER r8"0,(1
#20 P@*T P,PER r81":,1-0: DR,E P,PER r8"0,(1
#(0 P@*T P,PER r81::,>(: DR,E P,PER r81#,?2>
#-0 P@*T P,PER r81>-,>(: DR,E P,PER r8"0,?(1
#10 P@*T P,PER r8>#,>(: DR,E P,PER r8?"0,?(1
##0 P@*T P,PER r8"2,>(: DR,E P,PER r8?"0,?(1
#"0 P@*T P,PER r81#,1-0: DR,E P,PER r8?1#,2>
#>0 P@*T P,PER r8"1,1-0: DR,E P,PER r8?"0,(1
#:0 INMERBE 0: P,PER 0: INK "
I2 yo0=re not Briti3h, have a 4o at dra5in4 yo0r o5n 2la4( 8ricolo0r3 are 2airly ea3y, altho04h 3ome o2 the colo0r3 J
2or in3tance the oran4e in the Iri3h 2la4 J mi4ht pre3ent di22ic0ltie3( I2 yo0=re an *merican, yo0 mi4ht be able to 2it the
character Q in(

Cere i3 a pro4ram to play han4man( <ne player enter3 a 5ord, and the other 40e33e3
5 REM :a&ma
10 REM "et up "cree
20 IN7 06 P?PER E6 (LS
30 LET G=2)06 23 S*> 10006 REM %raI ma
)0 PL3T 238,1286 @R?W ),06 REM mout:
100 REM "et up Ior%
110 INP*T I<6 REM Ior% to &ue""
120 LET b=LEN I<6 LET !<=$ $
130 '3R =2 T3 b6 LET !<=!<+$ $
1)0 NEHT 6 REM !<=Ior% &ue""e% "o Bar
150 LET c=06 LET %=06 REM &ue"" U m#"taLe cout"
110 '3R =0 T3 b,1
1E0 PRINT ?T 20,8$ $8
180 NEHT 6 REM Ir#te 9" #"tea% oB 5etter"
200 INP*T $2ue"" a 5etter $8&<
210 I' &<=$$ T4EN 23 T3 200
220 LET &<=&<+1-6 REM 1"t 5etter o5;
230 PRINT ?T 0,c8&<
2)0 LET c=c+16 LET u<=!<
250 '3R =1 T3 bL REM up%ate &ue""e% Ior%
210 I' I<+-=&< T4EN LET !<+-=&<
2E0 NEHT
280 PRINT ?T 50,08!<
200 I' !<=I< T4EN 23 T3 5006 REM Ior% &ue""e%
300 I' !<D>u< T4EN 23 T3 2006 REM &ue"" Ia" r#&:t
)00 REM %raI eGt part oB &a55oI"
)10 I' %=8 T4EN 23 T3 1006 REM :a&e%
)20 LET %=%+1
)30 RE?@ G0,;0,G,;
))0 PL3T G0,;06 @R?W G,;
)50 23 T3 200
500 REM Bree ma
510 3AER 16 REM rub out ma
520 LET G=2)06 23 S*> 1000
530 PL3T 238,1286 @R?W ),06 REM mout:
5)0 3AER 06 REM re%raI ma
550 LET G=1)16 23 S*> 1000
510 PL3T 1)3,1206 @R?W 1,0, PI/26 REM "m#5e
5E0 23 T3 800
100 REM :a& ma
110 3AER 16 REM rub out B5oor
120 PL3T 255,156 @R?W ,)8,0
130 @R?W 0,,)86 REM ope trap%oor
1)0 PL3T 238,1286 @R?W ),06 REM rub out mout:
150 REM mo!e 5#mb"
155 REM arm"
110 PL3T 255,11E6 @R?W ,15,,156 @R?W ,15,15
1E0 3AER 0
180 PL3T 231,816 @R?W ),216 @R?W ), 21
100 3AER 16 REM 5e&"
E00 PL3T 255,116 @R?W ,15,156 @R?W ,15,,15
E10 3AER 0
E20 PL3T 231,106 @R?W ),216 @R?W ),,21
E30 PL3T 23E,12E6 @R?W 1,0,,PI/26 REM BroI
E)0 PRINT ?T 10,08I<
800 INP*T $a&a#C $8a<
810 I' a<=$$ T4EN 23 T3 850
820 LET a<=a<+1-
830 I' a<=$$ T4EN ST3P
8)0 I' a<+1-=$N$ T4EN ST3P
850 REST3RE6 23 T3 5
1000 REM %raI ma at co5um G
1010 REM :ea%
1020 (IR(LE G,132,8
1030 PL3T G+),13)6 PL3T G,),13)6 PL3T G,131
10)0 REM bo%;
1050 PL3T G,1236 @R?W 0,,20
1055 PL3T G,1016 @R?W 0,,10
1010 REM 5e&"
10E0 PL3T G,15,116 @R?W 15,156 @R?W 15,,15
1080 REM arm"
1000 PL3T G,15,11E6 @R?W 15,,156 @R?W 15,15
1100 RET*RN
2000 @?T? 120,15,135,0,18),15,0,01
2010 @?T? 118,15,11,11,18),81,11,,11
2020 @?T? 18),151,18,0,18),1)0,11,11
2030 @?T? 20),151,,20,,20,2)0,151,0,,11


APPENDIX E
6inary and he0adecimal
8hi3 appendix de3cribe3 ho5 comp0ter3 co0nt, 03in4 the binary 3y3tem(
Eo3t E0ropean lan40a4e3 co0nt 03in4 a more or le33 re40lar pattern o2 ten3 J in En4li3h, 2or example, altho04h it
3tart3 o22 a bit erratically, it 3oon 3ettle3 do5n into re40lar 4ro0p3:
twenty, twenty one, twenty two, . . . twenty nine
thirty, thirty one, thirty two, . . . thirty nine
forty, forty one, forty two, . . . forty nine
and 3o on, and thi3 i3 made even more 3y3tematic 5ith the *rabic n0meral3 that 5e 03e( Co5ever, the only rea3on 2or
03in4 ten i3 that 5e happen to have ten 2in4er3 and th0mb3(
In3tead o2 03in4 the decimal 3y3tem, 5ith ten a3 it3 ba3e, comp0ter3 03e a 2orm o2 binary called hexadecimal (or hex,
2or 3hort), ba3ed on 3ixteen( *3 there are only ten di4it3 available in o0r n0mber 3y3tem 5e need 3ix extra di4it3 to do
the co0ntin4( ;o 5e 03e *, B, C, -, E and D( *nd 5hat come3 a2ter DA U03t a3 5e, 5ith ten 2in4er3, 5rite 1' 2or ten,
3o comp0ter3 5rite 1' 2or 3ixteen( 8heir n0mber 3y3tem 3tart3 o22:
Hex English
' no04ht
1 one
2 t5o
: :
: :
& nine
F03t a3 o0r3 doe3, b0t then it carrie3 on
* ten
B eleven
C t5elve
- thirteen
E 2o0rteen
D 2i2teen
1' 3ixteen
11 3eventeen
: :
: :
1& t5enty 2ive
1* t5enty 3ix
1B t5enty 3even
: :
: :
1D thirty one
2' thirty t5o
21 thirty three
: :
: :
&E one h0ndred and 2i2ty ei4ht
&D one h0ndred and 2i2ty nine
*' one h0ndred and 3ixty
*1 one h0ndred and 3ixty one
: :
B! one h0ndred and ei4hty
: :
DE t5o h0ndred and 2i2ty 2o0r
DD t5o h0ndred and 2i2ty 2ive
1'' t5o h0ndred and 2i2ty 3ix
I2 yo0 are 03in4 hex notation and yo0 5ant to ma1e the 2act ?0ite plain, then 5rite =h= at the end o2 the n0mber, and
3ay =hex=( Dor in3tance, 2or one h0ndred and 2i2ty ei4ht, 5rite =&Eh= and 3ay =nine E hex=(
6o0 5ill be 5onderin4 5hat all thi3 ha3 to do 5ith comp0ter3( In 2act, comp0ter3 behave a3 tho04h they had only
t5o di4it3, repre3ented by a lo5 volta4e, or o22 ('), and a hi4h volta4e, or on (1)( 8hi3 i3 called the binary 3y3tem, and
the t5o binary di4it3 are called bit3: 3o a bit i3 either ' or 1(
In the vario03 3y3tem3, co0ntin4 3tart3 o22
English Decimal Hexadecimal Binary
no04ht ' ' ' or ''''
one 1 1 1 or '''1
t5o 2 2 1' or ''1'
three 3 3 11 or ''11
2o0r ! ! 1'' or '1''
2ive " " 1'1 or '1'1
3ix # # 11' or '11'
3even $ $ 111 or '111
ei4ht % % 1'''
nine & & 1''1
ten 1' * 1'1'
eleven 11 B 1'11
t5elve 12 C 11''
thirteen 13 - 11'1
2o0rteen 1! E 111'
2i2teen 1" D 1111
3ixteen 1# 1' 1''''
8he important point i3 that 3ixteen i3 e?0al to t5o rai3ed to the 2o0rth po5er, and thi3 ma1e3 convertin4 bet5een hex
and binary very ea3y(
8o convert hex to binary, chan4e each hex di4it into 2o0r bit3, 03in4 the table above(
8o convert binary to hex, divide the binary n0mber into 4ro0p3 o2 2o0r bit3, 3tartin4 on the ri4ht, and then chan4e
each 4ro0p into the corre3pondin4 hex di4it(
Dor thi3 rea3on, altho04h 3trictly 3pea1in4 comp0ter3 03e a p0re binary 3y3tem, h0man3 o2ten 5rite the n0mber3
3tored in3ide a comp0ter 03in4 hex notation(
8he bit3 in3ide the comp0ter are mo3tly 4ro0ped into 3et3 o2 ei4ht, or byte3( * 3in4le byte can repre3ent any n0mber
2rom no04ht to t5o h0ndred and 2i2ty 2ive (11111111 binary or DD hex), or alternatively any character in the B.
;pectr0m character 3et( It3 val0e can be 5ritten 5ith t5o hex di4it3(
85o byte3 can be 4ro0ped to4ether to ma1e 5hat i3 technically called a 5ord( * 5ord can be 5ritten 03in4 3ixteen
bit3 or 2o0r hex di4it3, and repre3ent3 a n0mber 2rom ' to (in decimal) 2
l#
Jl=#""3"(
* byte i3 al5ay3 ei4ht bit3, b0t 5ord3 vary in len4th 2rom comp0ter to comp0ter(
8he BI, notation in Chapter 1! provide3 a mean3 o2 5ritin4 n0mber3 in binary on the B. ;pectr0m: =BI, '=
repre3ent3 no04ht, =BI, 1= repre3ent3 one, =BI, 1'= repre3ent3 t5o, and 3o on(
6o0 can only 03e '=3 and 1 =3 2or thi3, 3o the n0mber m03t be a non ne4ative 5hole n0mber> 2or in3tance yo0 can=t
5rite =BI, J11= 2or min03 three J yo0 m03t 5rite =JBI, 11= in3tead( 8he n0mber m03t al3o be no 4reater than decimal
#""3" J i(e( it can=t have more than 3ixteen bit3(
*887 really 5a3 binary( I2 yo0 convert the re30lt 2rom *887 into binary, yo0 can 5rite it in ei4ht bit3(
8he 2ir3t i3 1 2or 2la3hin4, ' 2or 3teady(
8he 3econd i3 1 2or bri4ht, ' 2or normal(
8he next three are the code 2or the paper colo0r, 5ritten in binary(
8he la3t three are the code 2or the in1 colo0r, 5ritten in binary(
8he colo0r code3 al3o 03e binary: each code 5ritten in binary can be 5ritten in three bit3, the 2ir3t 2or 4reen, the
3econd 2or red and the third 2or bl0e(
Blac1 ha3 no li4ht at all, 3o all the bit3 are ' (o22)( 8here2ore the code 2or blac1 i3 ''' in binary, or no04ht(
8he p0re colo0r3, 4reen, red and bl0e have F03t one bit 1 (on) o0t o2 the three( 8heir code3 are 1'', '1' and ''1 in
binary, or 2o0r, t5o and one(
8he other colo0r3 are mixt0re3 o2 the3e, 3o their code3 in binary have t5o or more bit3 1(
;I,C9*I7 B. ;+EC87:E
====================
B*;IC +ro4rammin4
JJJJJJJJJJJJJJJJJJJJJJJJJ
By ;teven Iic1er3
Edited by 7obin Bradbeer
Converted to *;CII text by
Chri3 <5en
(Internet email: co]nv4(0nit(no)
JJJJJJJJJJJJJJJJJJJJJJJJJJJJJ
<ri4inal edition p0bli3hed in 1&%2
by ;inclair 7e3earch 9imited
8hi3 ver3ion p0bli3hed in 1&&"
by Chri3 <5en on behal2 o2 *m3trad plc
JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ
Y *m3trad plc 1&&" J all ri4ht3 re3erved
JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ
8hi3 man0al i3 2reely di3trib0table b0t m03t
not be di3trib0ted 5itho0t thi3 notice(
JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ

S-ar putea să vă placă și