1 Start Here
If you bought the ATOM ready built, together with a power supply and a cable to connect it to a TV set, then carry on reading. Otherwise you should refer to the Technical Manual for details of how to assemble an ATOM kit, and for details of the required accessories. The ATOM connects to the aerial socket of an ordinary black-and-white or colour TV set. The ATOM will not affect the normal operation of the TV in any way. Connect the UHF output from the ATOM to the aerial socket of the TV set; see Fig. 1. Connect the ATOM's power supply to a mains socket, and plug the power connector into the back of the ATOM; again, see Fig. 1. Press the key marked BREAK on the top right of the ATOM's keyboard. Switch on the TV set, and turn the set's volume control down. The ATOM makes use af a TV channel that is not occupied by any TV stations, and it is necessary to tune to this channel in order to get the correct display from the ATOM. If the TV set you are usinq has push buttons to select stations, choose an unused button and tune the TV by rotating the button. If the TV has a single numbered tuning dial, turn the dial to somewhere near channel 36. Tune in the TV set until the screen is black, with the following display in the top left-hand corner of the screen: ACORN ATOM <_ Adjust the contrast and brightness controls so that the letters are clearly legible, and tune the TV set carefully until the letters are sharp and clear. The '<' sign is called the ATOM's 'prompt'. It indicates that the computer is waiting for something to be typed in; a command, perhaps, or a program. The white rectangle, '_', is called the 'cursor'; it indicates where on the screen the next character you type in will appear.l.l What the ATOM Can Do The ATOM understands the following special words and symbols: Commands LIST, LOAD, NEW. Functions ABS, BGET, CH, COUNT, EXT, FIN, FOUT, GET, LEN, PTR, RND, TOP. Connectives AND, OR, STEP, THEN, TO. _3_
Statements BPUT, CLEAR, DIM, DO, DRAW, END, FOR, GOSUB, GOTO, IF, INPUT, LET, LINK, MOVE, NEXT, OLD, PLOT, PRINT, PUT, REM, RETURN, RUN, SAVE, SGET, SHUT, SPUT, UNTIL, WAIT. Operators !, #, $, &, *, +, -, /, :, < =, >, ?, \, <>, < >=. These words and symbols will be explained over the course of the next 12 chapters; for the moment just observe that many of these words have an obvious meaning; for example, try typing: PRINT "HELLO" after the '>' prompt sign. Note that the quotation marks are obtained by holding down the SHIFT key and typing the '2' key. Now type RETURN to indicate that the line is finished, and the ATOM will do just that: HELLO>_ To perform calculations you just need to type PRINT followed by the expression you want to evaluate. For example, try: PRINT 7+6*2 When you type RETURN the answer will be printed out. You can try typing anything you like, but any words not on the above lists will probably cause an error. For example, try typinq: HELLO after the ATOM's ','’ prompt. The ATOM will reply with a 'bleep' and will print: ERROR 94 which means that HELLO is not one of the statements or commands that the ATOM understands.1.2 A Demonstration Now that you are in control of your ATOM you may like a quick demonstration of some more complicated things that it can do. No attempt is made here to explain how these examples work; for that you will have to read the rest of the first section of this manual. You can make ATOM do a lot of typing with very little effort; try entering: DO PRINT "ATOM-"; UNTIL 0 Note the difference between the '0' of DO, which is the letter '0', and the '0' at the end of the statement, which is the digit '0' on the top row of the keyboard. You will have to type the ESC (escape) key, which is at the top left of the keyboard, to stop this program. Now try typing in the following line: DO PRINT $RND&3+8,$8,$128; UNTIL 0 You will need to use the SHIFT key to get some of the special symbols. This program is longer than one line of the screen, but just keep typing and it will appear on a second line. Then press RETURN to run the program. Again, you will have to type ESC to stop this program. To demonstrate the graphics commands type: CLEARO; MOVE 10,0; DRAW 60,50 and the ATOM will draw a line on the screen. If you feel like trying a more complicated graphics program, type in the following: _4_
CLEAR0;MOVE32,24;Y=l;DOPLOT1,0,Y;PLOT1,Y,O;Y=-Y-2*Y/A.Y;U.O Press ESC to get back the ATOM's prompt. To demonstrate the ATOM's assembler enter the following line after the prompt: P=320;[INX; LDA 0,X; STA 4B002; JMP 320;] An assembler listing will be printed out, and the machine code will be put into memory at 320. To execute the program, type: LINK 320 and the ATOM will make a buzzing noise. It is playing the random contents of its memory through its internal loudspeaker. To stop the program you will have to type BREAK, because it is a machine-code program. You may question the usefulness of these examples, but they do illustrate the wide range of different tasks the ATOM is capable of. These 'programs' all fitted onto two lines of the display; to see what you will be able to do with a longer program take a look at the many examples later on in this manual.1.3 The Keyboard The ATOM keyboard is designed to the standard layout generally accepted in the computer industry; see Fig. 2. In most respects it is just like the keyboard of an ordinary typewriter, but there are some important differences. For a start there are several keys not found on typewriters, such as DELETE, REPT, CTRL, and BREAK. The purpose of each of these keys will be explained in the following sections. Another difference is that the letters A-Z will appear in capitals, rather than lower-case, when they are typed by themselves. Try typing in the letters 'ABC' and observe that they appear, as you type them, on the screen: >ABC_ From now on, in the examples, the cursor will not be drawn in for simplicity.
1.3.1 SHIFT Some keys carry two legends. For example, each digit key (except 0) also has a special symbol or punctuation mark above it. The lower symbol on each of these keys is obtained by simply typing that key; the upper symbol is obtained by holding one of the SHIFT keys down, and typing that key. This aspect of the keyboard is just like a typewriter. If the SHIFT key is held down in conjunction with one of the keys bearing a single legend, such as A-Z and 0, [, etc, then the character will appear inverted; i.e. as a black character on a white square. Inverted A-Z correspond to lower case letters, and will be represented by lower case letters a-z in this manual. Inverted 0, [, \ etc. will be represented by 0, g, g etc.
1.3.2 LOCK The LOCK key, when pressed on its own, changes the way the SHIFT key operates with the letters A-Z. Initially the keyboard will give inverted A-Z in conjunction with the SHIFT key, and plain A-Z otherwise. If the LOCK key is now pressed once the keyboard will normally give inverted A-Z, and will give plain A-Z only when the SHIFT key is held down. Pressing LOCK again will revert to the previous state. _5_
1.3.3 DELETE The advantage of a TV screen over a piece of paper is that mistakes can be corrected without trace of the error. The DELETE key will erase the last character on the line, and the cursor will back up one space. Mistakes can thus be deleted and retyped with little effort.
1.3.4 RETURN The RETURN key is a signal to the computer that you have finished typing in a line of characters. The cursor will move to the start of the next line, and the computer may respond to what you have typed by typing out a reply.
1.3.5 Repeat - REPT If the 'repeat' key, marked REPT, is held down with another key, that key is typed repeatedly. REPT is useful in conjunction with DELETE to erase several characters very rapidly. Note that pressing REPT on its own will have no effect.
1.3.6 Control - CTRL There are several special functions available from the keyboard which are obtained by typing certain keys with the 'control' key - marked CTRL - held down. Only the following two control functions will be mentioned here: CTRL-G gives a bleep in the ATOM's loudspeaker. CTRL-L clears the screen.
1.3.7 BREAK The BREAK key will reset the computer, and return it to the state it was in just after switching on. It should not normally be necessary to type BREAK, but some assembler programs can cause loops which cannot be stopped in any other way. Note that the contents of memory are preserved when BREAK is typed, and any stored program can be recovered.
1.4 Scrolling When the cursor reaches the bottom of the screen further lines typed in will cause the screen to 'scroll'; every line is shifted up so that you always see the last 16 lines of what has been typed, and the top line of text on the screen will be lost.
1.5 Storing Text Any line typed after the ATOM's '>' prompt which starts with a number is not executed, but stored as text in the ATOM's memory. Any type of input can be stored in this way; it could be the text of a document, a program in BASIC, an assembler program, or data for a program. This section shows how to enter a piece of text, which can then be stored on cassette, edited, or output to a printer. The same method would be used for entering a program. The line must start with a line number, which can be any number within the range 1 to 32767, and there is no need to use consecutive line numbers for consecutive lines; indeed, it is wise to choose line numbers spaced by about 10 as you will soon realise. After the line number you should type the line of text. For example, enter the following: _6_
10 IN XANADU DID KUBLA KHAN 20 A STATELY PLEASURE-DOME DECREE: 30 WHERE ALPH, THE SACRED RIVER, RAN 40 DOWN TO A SUNLESS SEA. Remember to type RETURN at the end of each line. Each line number can be followed by up to 64 characters; if you try to type more than 64 characters the ATOM will refuse to proceed until you have deleted some characters. The reason for spacing line numbers somewhat apart is that it is then a simple matter to insert new lines between existing lines. For example, to insert a line before line 40, type: 36 THROUGH CAVERNS MEASURELESS TO MAN The computer will sort the lines into the right order, according to their line numbers, irrespective of the order in which you entered them.1.6 Commands Commands typed in after the '>' prompt, without a preceding line number, and followed by RETURN, are executed immediately by ATOM rather than being stored in its memory. For example, now type the command: LIST This will cause the stored text to be typed out: 10 IN XANADU DID KUBLA KHAN 20 A STATELY PLEASURE-DOME DECREE: 30 WHERE ALPH, THE SACRED RIVER, RAN 36 THROUGH CAVERNS MEASURELESS TO MAN 40 DOWN TO A SUNLESS SEA. There are several options with the LIST command. For example: LIST 10 will list line 10 only. LIST 20,40 will list lines 20 to 40 inclusive. LIST 20, will list line 20 onwards. LIST ,30 will list up to line 30. A listing can be stopped by typing ESC (escape).
1.7 Editing One powerful feature of the ATOM's text and program storage is that stored lines can be modified very simply by typing the same line number followed by the new version. For example, to change line 20 in the text just type: 20 NEW LINE TWO and try listing the program again to see the effect. To delete a line simply type the line number followed by RETURN.
1.8 Other Commands Some other useful commands are described here: NEW will clear the stored text so that a new piece of text can be typed in. It should always be typed before entering a new piece of text. OLD can be typed after typing BREAK to retrieve the text previously in _7_
memory. Note that you should only type OLD if there is already text in memory.1.9 Errors By now you the ATOM will probably have made a 'bleep' followed by the message: ERROR X where X is the error code number. There are two possible reasons for errors: 1. You typed something, probably a command, that the ATOM was not expecting or could not interpret. 2. The ATOM was commanded to do something that it could not do. For example, typing 'ABC' followed by a RETURN will give the error message: ERROR 94 which is probably the most common error; it means that 'ABC' was not a legal command. Remember that it is impossible to cause physical damage to the ATOM, whatever you type at the keyboard. The worst you can do is to lose the stored text, and even that is extremely unlikely. Most errors are really warnings, and a complete explanation of all the error codes is given in Chapter 27.
1.10 Saving Text or Programs on Tape Having entered some stored text into the ATOM's memory, this section will show how to save this text, and load it back at a later time. Text and programs can be saved on standard cassette (or reel-to-reel) tapes using the ATOM's cassette interface. Connect the cassette output from the ATOM to the input of a cassette recorder, and the output from tne recorder to the input of the ATOM. The tape load routine uses software averaging techniques to minimise the likelihood of errors on loading, and no trouble should be experienced in transferring tapes from one machine to another.
1.10.1 Setting Up Before loading and saving files using the cassette interface it is worth entering the following simple routines to check that the cassette system is working correctly, and to find out the best setting of the recorder's volume control. Enter the following line after the ATON's prompt: DO BPUT A,88; WAIT; WAIT; WAIT; WAIT; UNTIL 0 Type RETURN and record on the recorder for a few minutes. To stop the program type ESC (escape). This program has recorded a sequence of Xs, in coded form, on the tape. If you play it back it should sound like a series of short buzzes. Now enter the following line, which is a program to read characters from the tape and print them on the screen: DO PRINT Sbget A; UNTIL 0 The dollar symbol is obtained by holding the SHIFT key down and typing '4'. Press RETURN, rewind the tape, and play back the 'X's that you recorded. If all is well a stream of 'X's should be printed out, and adjust the volume setting on the recorder so that no other characters appear, indicating errors. When you are satisfied that all is well, _8_
proceed to the next section.1.10.2 Text Files The information is stored as a stream of audible tones on tape; each section of information is referred to as a 'file'. Several different files can be saved on one tape, and they are identified by having unique 'filenames'. Filenames can be anything containing up to 16 letters, digits, or spaces: suitable names are "DATA FILE", "22/4/80", etc.
1.10.3 SAVE First check that the stored text is still there by typing LIST. To save the stored text to tape, type: SAVE "EXAMPLE" where "EXAMPLE" is the filename chosen for illustration. Type RETURN, and the message: RECORD TAPE will be printed on the screen. Put the tape recorder to record, and allow the tape to run well past the leader. Now type RETURN (or any other key) and the cursor will move to the start of the next line, indicating that the text is being recorded. After a short delay the '>' prompt will reappear, and you can turn the tape-recorder off.
1.10.4 *CAT The *CAT command will give a complete catalogue of all the files on a cassette. The '*' asterisk is used to distinguish the cassette operating-system commands from the BASIC commands. Rewind the tape and type: *CAT The ATOM will reply with: PLAY TAPE and you should then play the tape, and press any key to start the catalogue. As a file is encountered on the tape the filename will be printed out, together with additional information about the file: EXAMPLE XXXX XXXX XXXX XX where the 'X's represent four numbers which you can ignore for the moment (see Section 19.3 for details). When you have finished you can get back to the '>' prompt by typing CTRL (control).
1.10.5 LOAD Switch off the ATOM, in order to cause the saved text to be lost, and then switch on again and type: LOAD "EXAMPLE" The ATOM will reply with: PLAY TAPE and the tape should be rewound and played, and RETURN pressed. The computer will search through the tape for a file of the specified filename, EXAMPLE in this case, and then load it into its memory. If all is well the prompt should reappear, and then typing: LIST _9_
will give a listing of the text that was previously saved.1.10.6 File Blocks If you save a long file on tape, and play it back, you will discover that it is broken up into a number of short blocks, with gaps in between, and that when the file is catalogued its name appears several times, once for each block. This is done for greater reliability, and if the tape is damaged in the middle of one block it will still be possible to load back the other blocks of the file. One further message that may be given when loading tapes is: REWIND TAPE This implies that you have started playing the tape in the middle of the file you wanted to load. Rewind the tape, press RETURN, and the message: PLAY TAPE will be given again.
1.10.7 Errors when Using Tape If an error is found when loading back a tape file, the message: SUM ERROR 6 is given. This might be caused by bad adjustment of the tape-recorder playback volume, a damaged or dirty tape, or recording a file over part of a previous file. If you choose an invalid name for a file, the message: NAME ERROR 118 will be given. _10_
2 Calculating in BASIC
The ATOM computer understands a language called BASIC which, because of the ease of writing programs in it, has become the most popular language for use on small computer systems. BASIC was invented in 1964 at Dartmouth College, New Hampshire, and it stands for Beginner's All-purpose Symbolic Instruction Code. This chapter introduces some of the facilities available in the BASIC language. The BASIC language consists of 'statements', 'operators', and 'functions'. The 'statements' are words like PRINT and INPUT which tell the computer what you want to do; they are followed by the things you want the computer to operate on. The 'operators’ are special symbols such as the mathematical signs '+’ and '-' meaning 'add' and ’subtract'. The 'functions' are words like the statements, but they have a numerical value; for example, RND is a function which has a random value. 2.1 PRINT This is by far the most useful BASIC statement; it enables programs to print out the results of their calculations. Try typing: PRINT 7+3 The ATOM will print: 10> The '>' prompt reappears immediately after the answer, 10, is printed out. This is the best way to use BASIC as a simple calculator; type PRINT followed by the expression you want to evaluate. Try the effect of the following: PRINT 7-3 PRINT 7*3 PRINT 7/3 You will discover that '*’ means multiply; it is the standard multiply symbol on all computers. Also '/’ means divide, but you may be surprised that the answer to 7/3 is given as 2, not 2 and 1/2. ATOM BASIC only deals in whole numbers, or integers, so the remainder after the division is lost. The remainder can be obtained by typing: PRINT 7%3 The '%' operator means ’give remainder of division'. More complex expressions are evaluated according to the standard rules of mathematics, so the expression: PRINT 2+3*4-5 has the result 9. Multiplications and divisions are performed first, followed by additions and subtractions. Round brackets can be used to I make sure that operations are performed in the correct order; anything enclosed in brackets is evaluated first. Thus the above expression could also be written: _11_
PRINT (2+(3*4))-5 There is no limit to the complexity of expressions that ATOM BASIC can evaluate, provided they will fit on two lines of the VDU. You will notice that ATOM BASIC calculates extremely rapidly. Try typing: PRINT 9*9*9*9*9*9*9*9*9 ATOM BASIC can calculate with numbers between about 2000 million and -2000 million, which gives an accuracy of between nine and ten digits. Furthermore, because whole numbers are used, all calculations in this range are exact.2.1.1 Printing Several Things You can print the results of several calculations in one PRINT statement by separating them with commas: PRINT 7, 7*7, 7*7*7, 7*7*7*7 which will print out: 7 49 343 2401 Note that each number is printed out on the right-hand side of a column eight characters wide. This ensures that when large numbers of results are printed out they will be in neat columns on the screen. 2.1.2 Printing Strings PRINT can also be used to print out words, or indeed, any required group of characters. Arbitrary groups of characters are referred to simply as 'strings', and to identify the characters as a string they are enclosed in double quotes. For example: PRINT "THE RESULT" will cause: THE RESULT> to be printed out. The characters in quotes are copied faithfully, exactly as they appear in the PRINT statement. Thus you could type: PRINT "55*66=", 55*66 where the expression inside quotes is a string just like any other. This would print out: 55*66= 3630> 2.2 Variables - A to Z You will probably be familiar with the use of letters, such as X and N, to denote unknown quantities. E.g.: "the nth. degree", "X marks the spot", etc. In ATOM BASIC any letter of the alphabet, A to Z, may be used to denote an unknown quantity, and these are called 'variables'. The equals sign '=' is used to assign a particular value to a variable. For example, typing: X=6 will assign the value 6 to X. Now try: PRINT X and, as expected, the value of X will be printed out. Note the difference between this and: PRINT "X" _12_
The assignment statement 'X=6' should be read as 'X becomes 6' because it denotes an operation which changes the value of X, rather than a statement of fact about X. The following statement: X=X+1 is perfectly reasonable, and adds 1 to the previous value of X. In words, the new value of X is to become the old value of X plus one. Now that we can use variables to stand for numbers, they can also be used in expressions. For example, to print the first four powers of 12 we can type: T=12 PRINT T, T*T, T*T*T, T*T*T*T2.3 Getting the Right Answer Suppose you wanted to calculate half of 777. You might type: PRINT 777/2 and you would get the answer 388. Then, to get the remainder, you would type: PRINT 777%2 and the answer will be l. So the exact answer is 388 and one half. Suppose, however, you decided to try: PRINT 1/2*777 thinking it would give 'a half times 777', you would be surprised to get the answer 0. The reason lies in tie fact that the calculation is worked out from left to right in several stages, and at every stage only the whole-number part of the result is kept. First 1/2 is calculated, and the result is 0 because the remainder is not saved. Then this is multiplied by 777 to give 0! Fortunately there is a simple rule to avoid problems like this: Do Divisions Last! The division operation is the only one that can cause a loss of accuracy; all the other operations are exact. By doing divisions last the loss of accuracy is minimised. Applying this rule to the previous example, the division by two should be done last: PRINT (1*777)/2 which is obviously the same as what was written earlier.
2.3.1 Fixed-Point Calculations An alternative way to find half of 777 is to imagine the decimal point moved one place to the right, and write: PRINT 7770/2 The result will then be 3885, or, with the decimal point moved back to the correct place, 388.5. For example, in an accounting program you would use numbers to represent pence, rather than pounds. You could then work with sums of up to 20 million pounds. Results could be printed out as follows: PRINT R/100, "POllNDS", R%100, "PENCE"
2.4 Print Field Size - '@' Numbers are normally printed out right-justified in a field of 8 character spaces. If the number needs more than 8 spaces the field _13_
size will be exceeded, and the number will be printed in full without any extra spaces. Note that the minus sign is included in the field size for negative numbers. It is sometimes convenient to alter the size of the print field. The variable '@' determines this size, and can be altered for other field widths. For example: @=32 will print one number per line, because there are 32 character positions on each line. The value of '0’ can be zero, in which case no extra spaces will be inserted before the numbers.2.5 Printing a New Line A single quote in a PRINT statement will cause a return to the start of the next line. Thus: PRINT "A" ' "T" ' "0" ' "M" will print out: A T 0 M > This is an improvement over most other versions of BASIC, which would require four separate PRINT statements for this example.
2.6 Multiple-Statement Lines - ';' ATOM BASIC allows any number of statemerits to be strung together on each line provided they are separated by semicolons. For example the following line: A=1;B=2;C=3;PRINT A,B,C' will print: 1 2 3
2.7 Hexadecimal Numbers Numbers can also be represented in a notation called 'hexadecimal' which is especially useful for representing addresses in the computer. Hexadecimal notation is explained in section 13.1.1; all that needs to be mentioned here is that hexadecimal notation is just an alternative way of writing numbers which makes use of the digits 0 to 9 and the letters A to F. The '0' symbol, called 'hash', is used to introduce a hexadecimal number. Thus Oe9 is a perfectly good hexadecimal number (nothing to do with the variable E). PRINT #8000 will print: 32768> The PRINT statement prints the number out in decimal. 08000 is the address of the display area, and is a more convenient way of specifying this address than its decimal equivalent. A number can be printed in hexadecimal by prefixing it with an ’&' ampersand in the PRINT statement. Thus: PRINT &32768 _14_
will print: 8000>2.8 Logical Operations In addition to the arithmetic operations already described, ATOM BASIC provides three operations called 'logical operations': '&' (AND), '\' (OR), and ':' (Exclusive-OR). These are all operations between two numbers, so there is no danger of confusing this use of '&' with its use to specify printing in hex as covered in the previous section. These are especially useful when controlling external devices from a BASIC program. Note that the '\' symbol is obtained on the keyboard by typing ’shift \', and it will appear on the display as an inverted '\' · The following table gives the results of these three operations for the numbers 0 and 1:
Try typing the following: PRINT 0 & 1 PRINT 1 \ 1 PRINT 1 : 1 and verify that the results agree with the table.
Operands
A BA & B A \ B A:B 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0 2.9 Peeking and Poking Many BASICs have PEEK and POKE functions which do the following: PEEK looks at the contents of a place in memory, or memory location POKE changes the contents of a memory location. The '?' operator, called 'query', is used for poking and peeking in ATOM BASIC and it provides a more elegant mechanism than the two functions provided in other BASICs. The contents of some memory location whose address is A is given by typing: PRINT ?A For example, to look at the contents of location OCOOO type: PRINT ?#COOO and the result will be 60 (this is the first location in the ATOM ROM). To change the contents of a location whose address is A to 13 just type: ?A=13 For example, to change the contents of the memory location corresponding to the top left-hand corner of the screen type: _15_
?#8000=127 and a white block will appear in the top left of the screen (see section 18.5 for an explanation). As another useful example try: ?#EI=0 which will turn the cursor off. To turn the cursor back on again type: ?#E1=#80 _16_
3 Planning a Program
The first step in writing a program, whether it will eventually be programmed in BASIC or Assembler, is to express your problem in terms of simple steps that the computer can understand. The Atom could be put to an immense number of different uses; anything from solving mathematical problems, controlling external equipment, playing games, accounting and book-keeping, waveform processing, document preparation...etc. The list is endless. Obviously all these applications cannot be included in a computer's repertoire of operations. Instead what is provided is a versatile set of more fundamental operations and functions which, in combination, can be used to solve such problems. It is therefore,up to you to become familiar with the fundamental operations that are provided, and learn how to solve problems by combining these operations into programs. Programming is rather like trying to explain to a novice cook, who understands little more than the meanings of the operations 'stir', 'boil’, etc, how to bake a cake. The recipe corresponds to the program; it consists of a list of simple operations 'stir', 'bake', with certain objects such as 'flour', 'eggs': Recipe 1. Sponge Cake 1. Mix together 4 oz. sugar and 4 oz. butter. 2. Stir in 2 eggs. 3. Stir in 4 oz. flour. 4. Put into tins. 5. Bake for 20 mins. at Mark 4. 6. Remove from oven and eat. 7. END The recipe is written so that, provided all the ingredients are already to hand, the cook can follow each command in turn without having to look ahead and worry about what is to come. Similarly, a computer only executes one operation at a time, and cannot look ahead at what is to come.3.1 Flowcharts Before writing a program in BASIC or Assembler it is a good idea to draw a 'flowchart' indicating the operations required, and the order in which they should be performed. The generally accepted standard is for operations to be drawn inside rectangular boxes, with lines linking these boxes to show the flow of control. A simple flowchart for the program to bake a cake might be drawn as follows: _17_
3.2 Decisions Many recipes do not just contain a sequence of steps to be performed, but contain conditions under which several different courses of action should be taken. For example, for a perfect cake line 5 would be better written: 5. Bake until golden brown. It would then be necessary to open the oven door every five minutes and make a decision about the colour of the cake. Decisions are represented in flowcharts by diamond-shaped boxes, with multiple exits labelled with the possible outcomes of the decision. The new flowchart would then be: _18_
The action 'bake for 5 mins.' is repeatedly performed until the test 'is it brown?' gives the answer 'yes'. Of course the program would go dramatically wrong if the oven were not switched on; the program would remain trapped in a loop. With these two simple concepts, the action and the decision, almost anything can be flowcharted. Part of the trick in flowcharting programs is to decide how much detail to put into the flowchart. For example, in the cake program it would be possible to add the test 'is butter and sugar mixed?' and if not, loop back to the operation 'mix butter and sugar'. Usually flowcharts should be kept as short as possible so that the logic of the program is not obscured by a lot of unnecessary fine detail.3.3 Counting Recipes sometimes require a particular series of operations to be performed a fixed number of times. The following recipe for puff pastry illustrates this: Recipe 2. Puff Pastry 1. Mix 6 oz. flour with 2 oz. butter. 2. Roll out pastry. 3. Spread with 2 oz. butter. _19_
4. Fold in half. 5. Repeat steps 2 to 4 a further 3 times. 6. END In this recipe the cook has to perform operations a total of 4 times. A cook would probably keep a mental note of how many times he has performed these operations; for the sake of the flowchart it is convenient to give the number of operations a label, such as T. The flowchart of the puff pastry recipe would then be:
The loop consisting of statements 2 to 4 is performed 4 times; the test at the end gives the answer 'no' for T=l, 2, and 3, and the answer 'yes' for T=4. To perform an operation several times in a BASIC or Assembler program an identical method can be used; a counter, such as T, is used to count the number of operations and the counter is tested each time to determine whether enough operations have been completed.3.4 Subroutines A recipe may include a reference to another recipe. For example, a typical recipe for apple tart might be as follows: _20_
Recipe 3. Apple Tart 1. Peel and core 6 cooking apples. 2. Make pastry as in recipe 2. 3. Line tart tin with pastry. 4. Put in apple. 5. Bake for 40 mins. mark 4. 6. END To perform step 2 it is necessary to insert a marker in the book at the place of the original recipe, find the new recipe and follow it, and then return to the original recipe and carry on at the next statement. In computer programming a reference to a separate routine is termed a 'subroutine call'. The main recipe, for apple tart, is the main routine; one of its statements calls the recipe for puff pastry, the subroutine. Note that the subroutine could be referred to many times throughout the recipe book; in the recipe for steak and kidney pie, for example. One reason for giving it separately is to save space; otherwise it would have to be reproduced for every recipe that needed it. Note that, in order not to lose his place, the cook needed a marker to insert in the recipe book so that he should know where to return to at the end of the subroutine. In BASIC or assembler programs the computer keeps a record of where you were when you call a subroutine, and returns you there automatically at the end of the subroutine. In other respects, the process of executing a subroutine on a computer is just like this analogy.3.5 Planninq a Program Before writing a program in BASIC or Assembler it is a good idea to express the problem in one of the forms used in this chapter: l. As a list of numbered steps describing, in words, exactly what to do at each step. 2.As a flowchart using the following symbols: Having done this, the job of writing the program in BASIC or Assembler will be relatively easy. _21_
_22_
4 Writing a BASIC Program
Commands and statements typed after the ATOM's prompt are executed immediately, as we have seen in Chapter 3. However if you start the line with a number, the line is not executed but stored as text in the ATOM's memory.4.1 RUN First type 'NEW' to clear the text area. Then try typing in the following: 10 PRINT "A PROGRAM!" 20 END When these lines have been typed in you can list the text by typing LIST. Now type: RUN The stored text will be executed, one statement at a time, starting with the lowest-numbered statement, and the message 'A PROGRAM!' will be printed out. The text you entered formed a 'program', and the program was executed, statement by statement, when you typed RUN. The END statement is used to stop execution of the program; if it is ommitted an error message will be given.
4.2 INPUT Type NEW again, and then enter the following program: 10 INPUT N 20 N=N+1 30 PRINT N The INPUT statement enables you to supply numbers to a running program. When it is executed it will print a question mark and wait for a number to be typed in. The variable specified in the INPUT statement will then be set to the value typed in. To illustrate, type: RUN The program will add 1 to the number you type in; try running it again and try different numbers. The INPUT statement may contain more than one variable; a question mark will be printed for each one, and the values typed in will be assigned to the variables in turn. The INPUT statement may also contain strings; these will be printed out before each question mark. The following program illustrates this; it converts Farenheit to Celsius (Centigrade), giving the answer to the nearest degree: 10 INPUT "FARENHEIT" F 20 PRINT (10*F-315)/18 "CELSIUS" 30 END _23_
The value, in Farenheit, is stored in the variable F. The expression in the PRINT statement converts this to Celsius.4.3 Comments - REM The REM statement means 'remark’; everything on the line following the REM statement will be ignored when the program is being executed, so it can be used to insert comments into a program. For example: 5 REM PROGRAM FOR TEMPERATURE CONVERSION
4.4 Functions Functions are operations that return a value. Functions are like statements in that they have names, consisting of a sequence of letters, but unlike statements they return a value and so can appear within expressions.
4.4.1 RND The RND function returns a random number with a value anywhere between the most negative and most positive numbers that can be represented in BASIC. To obtain smaller random numbers the '%' remainder operator can be used; for example: PRINT RND%4 will print a number between -3 and +3.
4.4.2 TOP TOP returns the address of the first free memory location after the BASIC program. PRINT &TOP will print TOP in hexadecimal. This will be 48202 if you have not entered a program (or have just typed NEW) on the unexpanded ATOM, and #2902 on an expanded ATOM. PRINT TOP-#8200 is a useful way of finding out how many bytes are used up by a program; on an unexpanded ATOM there is a total of 512 bytes for programs.
4.4.3 ABS The ABS function can be used to give the absolute or positive value of a number; the number is written in brackets after the function name. For example: PRINT ABS(-57) will print 57. One use of ABS is in generating positive random numbers. For example: PRINT ABS(RND)%6 gives a random number between 0 and 5.
4.5 Escape - ESC It is possible to create programs which will never stop; see the following example in section 4.6. The escape key 'ESC' at the top left of the keyboard will stop any BASIC program and return control to the '>' prompt. _24_
4.6 GOTO In the above programs the statements were simply executed in ascending order of their line numbers. However it is sometimes necessary to transfer control forwards or backwards to somewhere other than the next numbered statement. The GOTO (go to) statement is used for this purpose; the GOTO statement specifies the statement to be executed next. For example, type: 1 REM Stars 10 PRINT "*" 20 GOTO 10 A flowchart for this program makes it clear that the program will never stop printing stars: To stop the program you will have to type ESC (escape). 4.6.1 Labels - a to z ATOM BASIC offers another option for the GOTO statement. Instead of giving the number of the statement to be executed next, a statement can be designated by a 'label', and the GOTO is followed by the required label. A label can be one of the lower-case letters a to z, which are obtained on the ATOM by typing the letter with the shift key held down. Labels appear on the VDU as upper-case inverted letters, so they are very easily identified in programs. For typographical convenience labels will be represented as lower-case letters in this manual. To illustrate the use of labels, rewrite the ’STARS' program as follows, using the label 's': 10s PRINT "*" 20 GOTO s Note that there must be no spaces between the line number and the label. There are two advantages to using labels, rather than line numbers, in GOTO statements. First, programs are clearer, and do not depend on how the program lines are numbered. Secondly, the GOTO statement is faster using a label than using a line number. To demonstrate this, enter the following program which generates a tone of 187 Hz in the loudspeaker: 10 P=#B002 20a ?P=?P:4; GOTO a This program works as follows: P is the location corresponding to the input/output port, and exclusive-ORing this location with 4 will change the output line connected to the loudspeaker. The frequency generated implies that the statements on line 20 are executed in about 2.5 milliseconds (twice per cycle). _25_
Try removing the label and rewrite the program as follows: 10 P=OB002 20 ?P=?P:4; GOTO 20 The GOTO statement is now slightly slower, and the tone generated will have the lower frequency of 144 Hz. The highest frequency that can be generated by a BASIC program is 322 Hz, as follows: 10 REM 322 Hz 20 P=#B002 30 FOR Z=O TO 10000000 STEP 4;?P=Z;N. To play tunes you will need to use an assembler program; see Section 15.4.4.6.2 Switches The GOTO statement may be followed by any expression which evaluates to a valid line number; for example: 10 REM Two-Way Switch 20 INPUT "TYPE 1 OR 2" L 30 GOTO (40*L) 40 PRINT "ONE" 50 END 80 PRINT "TWO" 90 END If L is 1 the expression (40*L) will be equal to 40, and the program will print 'ONE'. If L is 2 the expression will be equal to 80 and the program will print 'TWO’. The flowchart for this program is as follows:
4.6.3 Multi-Way Switches Finally here is an example of a multi-way switch using GOTO. The program calculates a random number between 0 and 5 and then goes to a _26_
line number between 30 and 35. Each of these lines consists of a PRINT statement which prints' the face of a dice. The single quote in the print statement gives a 'return' to the start of the next line. 10 REM Dice Tossing 20 GOTO (30+ABS(RND)%6) 30 PRINT'" *"''; END 31 PRINT" *"''"*"'; END 32 PRINT" *"'" *”’”*"'; END 33 PRINT"* *”''"* *"’; END 34 PRINT"* *"'" *"'”* *"'; END 35 PRINT"* *"'"* *"'"* *"'; END Description of Program: 20 Choose random number between 30 and 35 30-35 Print corresponding face of a dice Sample runs: >RUN * * * >RUN * * * * * >RUN * * * * * *4.7 Graphics The ATOM has no less than 9 different graphics modes available from BASIC. This section provides a brief introduction to graphics mode 0, the lowest resolution mode, which is available on the unexpanded ATOM. With more memory added to the ATOM the other graphics modes are available, and these are explained in Chapter 11. A special feature of mode 0 is that it is possible to mix graphics with any of the ATOM's characters. Graphics treats the screen as a piece of graph paper on which it is possible to draw lines and plot points. Points on the screen are called 'picture elements' or ’pixels' for short, because they are actually small squares. Each pixel on the screen is specified by its coordinates in the two directions, horizontal and vertical, and these coordinates will be referred to as X and Y respectively. The graphics screen is labelled as follows in mode 0:
0,47 | 63,47 | |
^ | Y | |
||
0,0 | -- X --> | 63,0 |
IF A=0 THEN PRINT "ZERO" will execute the PRINT statement, and print "ZERO", only if the condition A=0 is true; otherwise everything after THEN will be skipped and execution will continue with the next line.
4.8.2 THEN Statement The statement after THEN can be any statement, even an assignment statement as in: IF A=7 THEN A=6 Note that the meaning of each '=' sign is different. The first 'A=7' is a condition which can be either true or false; the second 'A=6' is an assignment statement which instructs the computer to set the variable A to the value 6. To make this distinction clear the above statement should be read as: 'If A is equal to 7 then A becomes 6'. 4.8.3 Conjunctions - AND and OR Conditions can be strung together using the conjunctions AND and OR, so, for example: 10 INPUT A,B 20 IF A=2 AND B=2 THEN PRINT "BOTHI 30 GOTO 10 _30_
will only print "BOTH" if both A and B are given the value 2. Alternatively: 10 INPUT A,B 20 IF A=2 OR B=2 THEN PRINT ”EITHER" 30 GOTO 40 will only print "EITHER" if at least one of A and B is equal to 2. 4.9 Logical Variables An alternative form for the condition in an IF...THEN statement is to specify a variable whose value denotes either 'true' or 'false'. The values 'true’ and 'false' are represented by 1 and 0 respectively, so: A=1; B=0 sets A to 'true' and B 'false'. Logical variables can be used in place of conditions in the IF statement; for example: IF A THEN PRINT "TRUE" will print "TRUE". A logical variable can also be set to the value of a condition: A=(L=100) This statement will set A to 'true' if L is 100, and to 'false’ otherwise. The condition must be placed in brackets as shown. 4.10 Iteration One way of printing the powers of 2 would be to write: 10 REM Powers of Two 20 P=1; T=2; @=0 30 PRINT "2 ", P, "= ", T ' 40 T=T*2; P=P+1 50 GOTO 30 which will print out: 2 ^ 1 = 2 2 ^ 2 = 4 2 ^ 3 = 8 2 ^ 4 = 16 2 ^ 5 = 32 2 ^ 6 = 64 and so on without stopping. This is a bit inelegant; suppose we wished to print out just the first 12 powers of 2. It is simply a matter of detecting when the 12th. power has just been printed out, and stopping then. This can be done with the IF statement as follows: 10 REM First Twelve Powers of Two 20 P=l; T=2; @=0 30 PRINT "2" ^, P, " = ", T ' 40 T=T*2; P=P+1 50 IF PC=12 GOTO 30 60 END The IF statement is followed by a GOTO statement; if P is less than 12 the condition will be true, and the program will go back to line 30. _31_
After the twelth power of 2 has been printed out P will have the value 13, which is not less than or equal to 12, and so the program will stop. With the IF statement we have the ability to make the computer do vast amounts of work as a result of very little effort on our part. For example we can print,256 exclamation marks simply by running the following program: 10 I=0 20 PRINT"!"; I=I+1 30 IF I<256 goto 20 40 end
5 Loops
The previous section showed how the IF statement could be used to cause the same statements to be executed several times. Recall the program: 10 I=0 20 PRINT"!"; I=I+1 30 IF I<256 goto 20 40 end which prints out 256 exclamation marks (half a screen full). this iterative loop is such a frequently-used operation in basic that all basics provide a special pair of statements for this purpose, and atom basic provides a second type of loop for greater flexibility.
5.1 FOR...NEXT Loops
The FOR statement, together with the NEXT statement, causes a set of
statements to be executed for a range of values of a specified
variable. To illustrate, the above example can be rewritten using a
FOR...NEXT loop as follows:
10 FOR I=1 TO 256
20 PRINT "!"
30 NEXT I
40 END
The FOR statement specifies that the statements up to the matching
NEXT statement should be executed for each value of I from 1 to 256
(inclusive). In this example there is one statement between the FOR
and NEXT statements, namely:
PRINT "!"
This statement has been indented in the program to make the loop
structure clearer; in fact the spaces are ignored by BASIC.
The NEXT statement specifies the variable that was specified in
the corresponding FOR statement. This variable, I in the above
example, is called the 'control variable' of the loop; it can be any
of the variables A to Z.
The value of the control variable can be used inside the loop, if
required. To illustrate, the following program prints out all
multiples of 12 up to 12*12:
10 FOR M=1 TO 12
20 PRINT M*12
30 NEXT M
40 END
The range of values specified in the FOR statement can be anything you
wish, even arbitrary expressions. Remember, though, that the loop is
always executed at least once, so the program:
_33_
10 FOR N=1 TO 0 20 PRINT N 30 NEXT N 40 END will print '1' before stopping.
The DO...UNTIL loop is most useful in cases where a program is to carry on until certain conditions are satisfied before it will stop. To illustrate, the following program prompts for a series of numbers, and adds them together. When a zero is entered the program terminates and prints out the sum: 10 S=O 20 DO INPUT J 30 S=S+J 40 UNTIL J=O 50 PRINT "SUM =", S 60 END Note that a statement may follow the DO statement, as in this example.
100 Q=s/2 110 DO Q=(Q+S/Q)/2 120 UNTIL (Q-1)*(Q-1)<S AND (Q+1)*(Q+1)>S 130 PRINT Q 140 END Description of Program: 20 Input number 100 Choose starting value 110 Calculate next approximation 120 Carry on until the square lies between the squares of the numbers either side of the root. 130 Print square root. Variables: Q - Square root S - Number Program size: 118 bytes Sample run: >RUN ?200000000 14142>
1 REM Reaction Timer 10 CLEAR 0 20 X=ABS(RND)%64; Y=ABS(RND)%48 30 FOR N=l TO ABS(RND)%600+300 40 WAIT; NEXT N 50 MOVE X,Y; DRAW X,Y 60 T=O 70 DO T=T+1; WAIT 80 UNTIL ?#Bool<>#FF 90 PRINT "REACTION TIME =" 100 PRINT T*10/6, "CSEC." 110 IF T>18 PRINT "WAKE UP!" 120 END Description of Program: 20 Choose random X,Y coordinates for point on screen. 30-40 Wait for random time between 6 and 9 seconds. 50 Plot point at X,Y 60-70 Count sixtieths of a second 80 #b001 is the address of the input port to which the SHIFT key is connected; the contents of this location are #ff until the SHIFT key is pressed. 90-100 Print reaction time converted to centiseconds. 110 If appalling reactions, print message. Variables: N - Counter for random delay T - counter in sixtieths of a second for reaction time X,Y - random coordinates for point on screen. Program size: 273 bytes _38_
6 Subroutines
As soon as a program becomes longer than a few lines it is probably more convenient to think of it as a sequence of steps, each step being written as a separate 'routine', an independent piece of program which can be tested in isolation, and which can be incorporated into other programs when the same function is needed.40 PRINT ”ENTER DEBITS"' 50 GOSUB 2000 60 PRINT "TOTAL IS" 70 GOSUB 3000 80 END Now all we have to do is write the subroutines at lines 1000, 2000, and 3000! The subroutines might be written as follows: 1000 REM Sum Credits in C 1010 REM Changes C,J 1020 C=O 1030 DO INPUT J; C=C+J 1040 UNTIL J=O 1050 RETURN 2000 REM Sum Debits in D 2010 REM Changes D,J 2020 D=O 2030 DO INPUT J; D=D+J 2040 UNTIL J=O 2050 RETURN 3000 REM Print Total in T 3010 REM Changes T; Uses C,D 3020 T=C-D; 0=5 3030 PRINT T/100," POUNDS",T%100," PENCE" 3040 RETURN Values are entered in pence, and entering zero will terminate the list of credits or debits. The two subroutines at 1000 and 2000 are strikingly similar, and this suggests that it might be possible to dispense with one of them. Indeed, the main part of the chequebook-balancing program can be written as follows, eliminating subroutine 1000: 10 REM Chequebook-Balancing Program 20 PRINT "ENTER CREDITS" 30 GOSUB 2000 40 C=D 50 PRINT "ENTER DEBITS" 60 GOSUB 2000 70 PRINT "TOTAL IS" 80 GOSUB 3000 90 END In conclusion, subroutines have two important uses: 1. To divide programs into modules that can be written and tested separately, thereby making it easier to understand the operation of the program. 2. To make it possible to use the same piece of program for a number of similar, related, functions. As a rough guide, if a program is too long to fit onto the screen of the VDU it should be broken down into subroutines. Each subroutine should state clearly, in REM statements at the start of the subroutine, the purpose of the subroutine, which variables are used by the subroutine, and which variables are altered by the subroutine. A few moments spent documenting the operation of the subroutine in this _40_
way will save hours spent at a later date trying to debug a program which uses the subroutine.
Variables: A - Lower starting value of X B - Upper starting value of X C - Value of Y for X=A D - Value of Y for X=B I - Iteration number S - Scaling factor; all numbers are multiplied by S and held as integers. X - Root being approximated Y - Value of equation for given,X Program size - 466 bytes Sample run: >RUN X1?1 X2?3 ROOT IS X= 1.618 NEEDED 7 ITERATIONS.
one disc at a time, with the restriction that no disc may be placed on top of a smaller disc. The number of moves required rises rapidly with the number of discs used; the problem was classically described with 64 discs, and moving one disc per second the solution of this problem would take more than 500,000 million years! A recursive solution to the problem, stated in words, is: To move F discs from peg A to peg B: 1. Move F-1 discs to peg C. 2. Move bottom disc to peg B. 3. Move F-1 discs to peg B. Also, when F is zero there is no need to do anything. Steps 1 and 3 of the procedure contain a reference to the whole procedure, so the solution is recursive. The following program will solve the problem for up to 13 discs, and displays the piles of discs at every stage in the solution: 1 REM Tower of Hanoi 10 PRINT$12 20 A=TOP;D=A+4 40 V=-3;W=-1 60 !D=$1020300;!A=0 70 INPUT"NUMBER OF DISCS "F 80 A?1=F;?D=F 85 N=64/3 90 CLEARO 100 FORQ=1TOF;MOVE(F-Q),(2*(F-Q));PLOTlg(2*Q-1),0;NEXT 110 GOSUBh;END 1000hIF?D=O RETURN 1010 D!4=!D-1;D?6=D?1;D?5=D?2;D=D+4;GOSUBh 1020 MOVE(F-D?-4+D?V*N-N),(D?V?A*2);PLOT1,(D?-4*2-1),0 1030 MOVE(D?W*N-N),(D?W?A*2-2);PLOT3,(F+D?-4),0 1040 A?(D?W)=A?(D?W)+W;A?(D?V)=A?(D?V)-W 1050 D?3=D?-2;D?2=D?W;D?1=D?V;GOSUBh 1060 D=D-4;RETURN Description of Program: 100 Draw starting pile of discs 110 Subroutine h is called recursively to move the number of discs specified in ?D. 1000 h: Subroutine to move ?D discs 1010 Recursive call to move ?D-1 discs 1020 Draw new disc on screen 1030 Remove old disc from screen 1040 Set up array A 1050 Recursive call to put back ?D-1 discs Variables: A?N - Number of discs on pile N D - Stack pointer ?D - How many discs to transfer D?1 - Destination Pile D?2 - Intermediate pile D?3 - Source pile F - Total number of discs N - One third of screen width V - Constant W - Constant Program size: 461 bytes Stack usage: (4 * number of discs) bytes _43_
7 Arrays and Vectors
So far we have met just 26 variables, called A to Z. Suppose you wanted to plot a graph showing the mean temperature for every month of the year. You could, at a pinch, use the twelve letters A to L to represent the mean temperatures, and read in the temperatures by saying: INPUT A,B,C,D,E,F,G,H,I,J,K,L However there is a much better way. A mathematician might call the list of temperatures by the names: t1, t2, t3, ..... t12. where the 'subscript', the number written below the- line, is the number of the month in the year. This representation of the twelve temperatures is much more meaningful than using twelve different letters to stand for them, and there is no doubt about which symbol represents the temperature of, for example, the third month. A similar series of variables can be created in ATOM BASIC, and these are called 'arrays'. Each array consists of an array 'identifier', or name, corresponding to the name 't' in the above example, and a 'subscript'. On most computers there is no facility for writing subscripts, so some other representation is used. Each member of the array can act as a completely independent variable, capable of holding a value just like the variables A to Z. The members of an array are called the array 'elements'. The total number of possible elements depends on how the array was set up; in the above example there were twelve elements, with subscripts from 1 to 12. In addition to the standard type of array, ATOM BASIC provides two other types of array called 'byte vectors' and 'word vectors'. Byte vectors are useful when only a small range of numbers are needed, and they use less storage space than word arrays. Word vectors use the same amount of storage as arrays, but can be manipulated in a more flexible manner.TOP, above the program text:
TOP: |
| ? | ? | ? | ? | ^ AA(0) | ^ AA(1) | ^ AA(2) | ^ AA(3) | ||||||||
? | ? | ? | ? | ||||||||||||||
^ AA(0) | ^ AA(1) | ^ AA(2) | ^ AA(3) |
The question marks represent unspecified values, depending on what the array contained when it was dimensioned. If now another array were dimensioned with the statement: DIM BB(3) space for the array BB would be reserved immediately following on from AA. Array elements can appear in expressions, and be assigned to, just like the simple variables A to Z. For example, to make the value of AA(3) become 776 we would execute: AA(3)=776 Then we could execute: AA(1)=AA(3)*2 AA(0)=AA(3)-6 and so on. The resulting array would now be:
TOP: |
|
140 FOR N=1 TO 12; J=11+4*N 150 MOVE J,12; DRAW J,(TT(N)*3/10+12) 160 NEXT N; END Description of Program: 20-30 Input 12 values 40 Clear screen 50-60 Draw axes 70-100 Label vertical axis 110-130 Label horizontal axis 140-160 Plot histogram bars Program size: 415 bytes Array storage: 52 bytes
are stored in an array AA, one digit per array element. Every power of 2 is obtained from the previous one by multiplying every element in the array by 2, and propagating a carry when any element becomes more than one digit. 5 REM Powers of Two 10 DIM AA(31) 20 0=1; P=O 30 AA(0)=1 40 FOR J=1 TO 31 50 AA(J)=0 60 NEXT J 70 DO J=31 80 DO J=J-1; UNTIL AA(J)<>0 85 PRINT'"2"" P "=" 90 FOR K=J TO 0 STEP -1 94 PRINT AA(K) 96 NEXT K 110 C=O 120 FOR J=O TO 31 130 A=AA(J)*2+C 140 C=A/10 150 AA(J)=A%10 160 NEXT J 170 P=P+1 180 UNTIL AA(31)<>0 190 END Description of Program: 40-60 Zero array of digits 80 Ignore leading zeros 85-96 Print power 110-160 Multiply current number by 2 180 Stop when array overflows. Variables: AA - Array of digits; one digit per element C - Decimal carry from one digit to next J - Digit counter K - Digit counter P - Power being evaluated Program size: 356 bytes Array usage: 124 bytes Total memory: 480 bytes.
90 END 1000pREM Plot Waveform 1005 MOVE 0,96 1010 FOR N=O TO 255 1020 PLOT13,N,(Z+AA(N)/H) 1030 NEXT N 1040 RETURN 2000sREM Make Sine Wave 2010 S=O;C=40000 2020 FOR N=O TO 255 2030 AA(N)=-S 2040 C=C-S/10 2050 S=S+C/10 2060 NEXT N 2070 RETURN 3000qREM Make Square Wave 3010 FOR N=O TO 255 3020 IF AA(N)<=0 AA(N)=40000 3030 IF AA(N)<0 aa(n)="-40000 3035 NEXT N 3040 RETURN 4000lREM Low Pass Filter 4010" b="O 4020 FOR" n="O TO 255 4030" b="AA(N)*360/1000+B*697/1000 4040" aa(n)="B; NEXT N 4050 RETURN Description of Program: 23 Calculate a square wave 25 Plot it at top of screen 28 Low-pass filter the square wave 30 Plot it in centre of screen 32 Calculate a sine wave 34 Plot it at bottom of screen 1000-1040 p: Plots waveform 2000-2070 s: Calculates a sine wave. 3000-3040 q: Squares-up the waveform 4000-4050 l: Low-pass filters the waveform Variables: AA(0...255) - Array of points, values between -40000 and 40000. B - Previous value for low-pass filter C - Cosine of waveform H - Scalinq factor for plotting waveforms N - Counter S - Sine of waveform Z - Vertical coordinate for centre of waveform. Program size: 564 bytes. Array storage: 1024 bytes Total memory: 1588 bytes _49_
Sample plot:
0 | 1 | 2 | 3 | 4 | 5 | |
0 | ||||||
1 | ||||||
2 | x |
|
|
|
65 DIM AA(I),CC(J),II(N) 70 0=0;FOR I=1TON;FOR J=1TO N+1 80 PRINT"C("I","J")=";INPUT C 90 CC((I-1)*(N+1)+J)=C;NEXT J;NEXT I 100 L=N+1;GOSUB c;E=D;M=l-2*(N%2) 110 PRINT'"SOLUTION:"' 112 IF E<0 e="-E;M=-M 115 IF" e="O;PRINTDEGENERATE!';END 120 FOR" l="1TON;GOSUB c 125" print"x("l")="130" a="M*D;B=E;DO" a="A%B 140 IF ABS(B)">ABS(A) THEN T=B;B=A;A=T 150 UNTIL B=O;A=ABS(A) 151 P.(M*D)/A;IF E/A<<1 PRINT"/"E/A 155 M=-M;PRINT';NEXT L;END 160cFOR I=1TON;FOR J=1TON;K=I*N-N+J 170 IF J<l aa(k)="CC(K+I-1) 180 IF J"<=L AA(K)=CC(K+I) 190 NEXT J;NEXT I 200dD=O;F=l;S=l 210 FOR J=1TON;II(J)=J;F=F*J;NEXT J 215 GOSUB f 220 FOR H=2TOF;GOSUB e;NEXT H;RETURN 230eI=N-1;J=N 240gIF II(I)>=II(I+1) I=I-1;GOTO g 250hIF II(I)>=II(J) J=J-1;GOTO h 260 GOSUB i;I=I+1;J=N;IF I=J GOTO f 270 DO GOSUB i;I=I+1;J=J-1;UNTIL I>=J 280fp=i;FOR K=1TON;P=P*AA(N*K-N+II(K)) 290 NEXT K;D=D+S*P;RETURN 300iK=II(I);II(I)=II(J);II(J)=K 310 S=-S;RETURN Description of Program: 50-60 Allocate space for matrix 70-90 Read in matrix of coefficients 120-155 Print solutions 130-150 Find GCD of solution, so it is printed in lowest terms 160-190 c: Permute terms to obtain next addition to determinant; i.e. for 5 equations, starting with (1,2,3,4,5) run through all permutations to (5,4,3,2,1). 280-290 f: Add in next product to determinant. 300-310 i: Swap terms in permutation. Variables: AA(1...N*N) - Matrix CC(1...N*N+N) - Matrix of coefficients S - Signature of permutation. Program Size: 932 bytes. Variable Space: (2*(N*N+N)+3)*4 bytes Sample run: <RUN NUMBER OF EQUATIONS=?2 C(1,1)=?1 C(1,2)=?2 C(1,3)=?1 C(2,1)=?4 C(2,2)=?5 C(2,3)=?2 _52_
SOLUTION: X(1)= 1/3 X(2)= -2/3
W!O, W!4, W!8, W!12, W!l6, W!20. Space can be dimensioned for word vectors by using the DIM statement, and allowing 4 bytes per element; for example, to provide storage for the above 6 elements, execute: DIM W(23) Note that the zeroth element of the vector, W!0, is equivalent to !W.
In this example subroutine p can be used ta print any array by passing its base address over in the variable P; this is known as a 'call by reference' because the subroutine is given a reference to the array, rather than the actual values in the array.
400-410 e: Unpack J into vector pointed to by R; store number of words in !R. Variables: D!0... - Workspace vector H - Radix for arithmetic P!1... - Vector for unpacked result !P - Number of elements used in P S!0... - Workspace vector T - Top of available memory Program size: 733 bytes. Additional storage: as available. Sample run: >RUN POWER PROGRAM COMPUTES Y"X, WHERE X>0 AND Y>0 VALUE OF Y?16 VALUE OF X?64 16^64=1157920892373161954235709850086879078532699846656405640394575 84007913129639936
8 strings
A 'string' is a sequence of characters; the characters can be anything letters, digits, or punctuation marks. They can even be control characters.
A:
A S T R I N G ~ ? ?
The question-marks indicate that the last two locations could contain anything. The character ' ' represents 'return' which is automatically stored in memory to indicate the end of the string. The DIM statement allocates one extra location to hold this terminator character, although you will not normally be aware of its presence. Note that it would be dangerous to allocate a string of more than 10 characters to A since it would exceed the space allocated to A.
A:
A S T R I N G ~ ? ? A S T R I N G ~ ^
A^
B
The value of the Nth. character in the string is then simply A?N. For example, A?7 is "G", etc. In general A?B is the value of the character stored in the location whose address is A+B; therefore A?B is identical to B?A. In other words, a string is being thought of as a byte vector whose elements contain characters; see section 7.2. The following program illustrates the use of the '?’ operator to invert all the characters in a string which is typed in: 1 REM Invert String 5 DIM Q(64) 10 INPUT $Q 20 FOR N=O TO LEN(Q)-1 30 Q?n=q?n fg 020 40 NEXT N 50 PRINT $Q 60 RUN _59_
A S T R I N G ~ ? ? 0
^
A1 2 3 4 5 6 7 8 9 10
HTJ I TYCU GQYFT FT SGGHH HJ FRP ELPHQMD, RW LN Qohd OQXSER CUAB. DKLCLDBCV.
Total memory: 563 bytes.
O N E ~ T W O ~ T H R E E ~ ? ? ? ^
VV(1)^
VV(2)^
VV3^
T
605 REM changes: A,Q,R 610 R=-1 620 DO R=R+1; A?R=Q?R 630 UNTIL A?R=CH"," OR A?R=0D 640 IF A?R=fD Q=Q+3 650 Q=Q+R+1; A?R=0D; RETURN 660* 800tONE,TWO,THREE,FOUR,FIVE 810 SIX,SEVEN,EIGHT,NINE,TEN 820 ELEVEN,TWELVE,THIRTEEN 830 FOURTEEN,FIFTEEN,SIXTEEN 840 SEVENTEEN,EIGHTEEN,NINETEEN 850 TWENTY Description of Program: 25 Find the text 30 Read in the next string 40 Print it out 500-550 f.: Search for label t and point Q to first string 600-660 r: Read up to comma or return and put string into $A 800-850 t: List of 20 strings Variables: $A - String J - Counter L - Label for text Q - Pointer to strings R - Temporary pointer Program size: 511 bytes String storage: 41 bytes Total memory: 552 bytes. The program can be moaified to read from several different blocks of text with different labels by changing the value of L. Also note that the character delimiting the strings may be any character, specified in the CH function in line 630.
Hex: Decimal: Character Printed: #00 - #1F 0 - 31 Control codes #20 - 05F 32 - 95 ASCII cnaracters #60 - #9F 96 - 159 Inverted ASCII characters #A0 - #DF 160 - 223 Grey graphics symbols 4EO - #FF 224 - 255 White graphics symbols Note that only half of the 64 possible white graphics symbols can be obtained in this way. The most useful control codes are specified in the following sections; for a full list of control codes see section 18.1.3.
_66_
9 Reading and Writing Data
The reader should now be familiar with the three types of data that can be manipulated using ATOM BASIC, namely: 1. Words i.e. numbers between -2000 million and 2000 million (approximately). Storage required: 4 bytes e.g. variables A to Z arrays AA(1) ... etc. word vectors A!4 ...etc. indirection !A ...etc. 2. Bytes i.e. numbers between 0 and 255, or single characters, or logical values. Storage required: 1 byte e.g. byte vectors A?1 ... etc. indirection ?A ...etc. 3. Strings i.e. sequences of between 0 and 255 characters, followed by a 'return'. Storage required: Length+1 bytes e.q. quoted string "A STRING" string variable $A ...etc. All these types of data can be written to cassette and read from cassette, making it very simple to make files of data generated by programs. The ATOM BASIC functions and statements for cassette input and output are designed to be fully compatible with the disk operating system, should that be added at a later stage. When the disk operating system is used, several files can be used by one program, and the individual files are identified by a 'file handle', a number specifying which file is being referred to. Although this facility is not available when working with a cassette system, the file handle is still required for compatibility.70 BPUT A,(N-1) 80 FOR M=O TO N-1 90 PUT A,VV(M) 100 NEXT M 110 END Description of Program: 30-50 Input numbers 60 Warn user to start tape 70 Output number of bytes 80-100 Save values on cassette Variables: A - Dummy file handle J - Temporary variable for values input M - Counter N - Counter for number of values VV(0...20) - Array of numbers The next program reads the values back in and plots a histogram of the values. The program automatically scales the values if they are too large to fit onto the screen. 1 REM Plot Histogram from Cassette 10 DIM VV(20) 20 A=FIN""; N=BGET A 30 FOR M=O TO N 4O VV(M)= GET A 50 NEXT M 60 REM X=Maximum, Y=Minimum 70 X=VV(0); Y=VV(0) 80 FOR M=1 TO N 90 IF X<vv(k) then x="VV(M) 100 IF Y"<VV(M) THEN Y-VV(M) 110 NEXT M 120 S=(X-Y+63)/64 130 REM Plot Histogram 135 CLEAR 0 140 FOR M=O TO N 150 MOVE O,M 160 DRAW ((VV(M)-Y)/S),M 170 NEXT M 180 GOTO 180 Description of Program: 20-50 Read values into array 70-110 Find maximum and minimum values in array 120 Calculate scaling factor 140-170 Plot scaled histogram 180 Wait for ESC key. Variables: A - Dummy file handle M - Counter N - Number of values in array S - Scale factor for array VV(0...20) - Array of values X - Maximum value Y - Minimum value _69_
160 K!Q=X-TOP; K!(4-Q)=L-TOP 170 UNTIL 0 1000qINPUT $T 1010 IF ?T=CH"Y"THEN Q=4; RETURN 1020 IF ?T=CH"Q"THEN END 1030 Q=O; RETURN 2000j$T=$R; A=i 2010 DO A=A+1 2020 V=T?(A+4); ST+A+4="" 2030 IF $T+A=" IT "UNTIL 1; GOTO k 2035 T?(A+4)=V 2040 UNTIL A=LEN T-5 2100 PRINT"WHAT WOULD THE ANSWER BE"' 2110 PRINT"FOR " $X 2120 RETURN 2150kT?(A+4)=V; $T+A+1="" 2160 PRINT $T,$X,$T+A+3 2170 RETURN 3000sT=T+LEN T+1; RETURN 9000 REM Set-Up File 9010 T=TOP; ST="*DOES IT HAVE FOUR LEGS" 9015 GOSUB s; P=T; T=T+8; !P=T-TOP 9020 $T="A CROW"; GOSUB s; P!4=T-TOP 9025 $T="A DOG"; GOSUB s 9100zREN Save Tree 9110 F=FOUT "1' 9112 BPUT F,OAA; WAIT 9115 PUT F,(T-TOP-1) 9120 FOR N=TOP TO T-1 9130 RPUT F, ?N 9140 NEXT N 9150 RETURN Description of Program: 20-30 Load previous tree 23 Look for start flag 35 Reset X to top of tree 50 Print next question 70 Carry on until not a question 75 Guess animal 90-95 Wait for a sensible reply 98 Find end of reply 1000-1030 q: Look for Y, N, or Q; set Q accordingly 2000-2120 j: Look for "IT "in question and print question with "IT" replaced by name of animal. 3000 s: Move T to end of string $T. 9000 Set up tree file 9100 z: Save tree file. Variables: F - Dummy file handle K - Pointer to addresses of next two branches of tree L - Pointer to animal typed in P - Pointer to address of next question or animal. Q - Value of reply to question; no=0, yes=4. R - Pointer to question typed in T - Pointer to next free location X - Pointer to current position on tree Program size: 1254 bytes Additional storage: as required for tree. _71_
Sample run: >RUN ARE YOU THINKING OF AN ANIMAL?Y DOES IT HAVE FOUR LEGS?Y CAN YOU RIDE IT?N DOES IT HAVE STRIPES?N IS IT A DOG?N WHAT WERE YOU THINKING OF?A MOUSE TELL NE A Question THAT WILL DISTINGUISH BETWEEN A MOUSE AND A DOG ?DOES IT SQUEAK DOES A DOG SQUEAK?NO ARE YOU THINKING OF AN ANIMAL?Y DOES IT HAVE FOUR LEGS?Y CAN YOU RIDE IT?N DOES IT HAVE STRIPES?N DOES IT SQUEAK?Y IS IT A MOUSE?Y HO-HO ARE YOU THINKING OF AN ANIMAL?N RECORD TAPE > _72_
10 More Space and More Speed
This chapter shows how to abbreviate programs so that they will fit into a smaller amount of memory, and how to write programs so that they will run as fast as possible.OLD OR PLOT PRINT P. PTR PUT REM RETURN R, RND R. RUN SAVE SA. SGET S. SHUT SH. SPUT SP. STEP S. THEN T. TO TOP T. UNTIL U. WAIT
5. If a calculation is performed every time around a loop, make sure that the constant part of the calculation is performed only once outside the loop. For example: FOR J=1 TO 10 FOR K=1 TO 10 VV(K)=VV(J)*2+K NEXT K NEXT J could be written as: FOR J=1 TO 10 Q=vv(j)*2 FOR K=1 TO 10 VV(K)=Q+K NEXT K NEXT J 6. Where several nested FOR...NEXT loops are being executed, and the order in which they are performed is not important, arrange them so that the one executed the greatest number of times is at the centre. For example: FOR J=1 TO 2 FOR K=1 TO 1000 . . NEXT K NEXT J is faster than: FOR K=1 TO 1000 FOR J=1 TO 2 . . NEXT J NEXT K because in the second case the overhead for setting up the inner loop is performed 1000 times, whereas in the first example it is only performed twice. 7. Choose the FOR...NEXT loop parameters so as to minimise calculations inside the loop. For example: FOR N=O TO 9 DRAW AA(2*N), AA(2*N+1) NEXT N could be rewritten as the faster: FOR N=O TO 18 STEP 2 DRAW AA(N),AA(N+1) NEXT N 8. Use word operations rather than byte operations where possible. For example, to clear the graphics screen to white it is faster to execute: _76_
FOR N=#8000 TO #9800 STEP 4; !N=-1; NEXT N than the following: FOR N=08000 TO #9800; ?N=-1; NEXT N 9. The IF statement containing several conditions linked by the AND connective, as, for example: IF A=2 AND B=2 AND C=2 THEN ..... will evaluate all the conditions even when the earlier ones are false. Rewriting the statement as: IF A=2 IF B=2 IF C=2 THEN ..... avoids this, and so gives faster execution. _77_
_78_
11 Advanced Graphics
The ATOM provides nine different graphics modes, up to a resolution of 256x192 in black and white, and 128x192 in four selectable colours. The graphics modes use the BASIC statements PLOT, DRAW, and MOVE in an identical way. All the black-and-white graphics commands are present in the unexpanded ATOM, although extra memory will be required for the higher-resolution graphics modes. Colour plotting requires the addition of an assembler routine, or the COLOUR statement provided in the extension ROM.This statement clears the screen and puts it into graphics mode. It is followed by a number, or expression in brackets, to specify the mode. The graphics screen is labelled as follows:
^ | Y | |
|
0,0 | -- X --> |
The smallest square which can be plotted on the display is referred to as a 'pixel' (or 'picture element').
K: Function: 0 Move relative to last position 1 Draw line in white relative to last position 2 Invert line relative to last position 3 Draw line in black relative to last position 4 Move to absolute position 5 Draw line in white to absolute position 6 Invert line to absolute position 7 Draw line in black to absolute position 8 Move relative to last position 9 Plot point in white relative to last position 10 Invert point relative to last position 11 Plot point in black relative to last position 12 Move to absolute position 13 Plot point in white at absolute position 14 Invert point at absolute position 15 Plot point in black at absolute position
Variables: C,D - Dimensions of rectangle E,F - Dimensions of safe part of screen to start drawing rectangle. H - Screen height Q - Counter S - Size of squares W - Screen width Z - Plot mode; draw relative. Program size: 278 bytes
1220 X=X+D;A=A+1;T=1;RETURN 1300 X=X-D 1305 PLOTS,X,Y 1310 X=X-D;Y=Y-D;PLOTS,X,Y 1320 Y=Y-D;B=B-1;T=2;RETURN 1400 Y=Y+D 1405 PLOTS,X,Y 1410 X=X-D;Y=Y+D;PLOTS,X,Y 1420 X=X-D;A=A-1;T=3;RETURN 2000oGOTO(2000+100*T) 2100 X=X+D;PLOTS,X,Y 2110 X=X+D;Y=Y+D;PLOTS,X,Y 2120 X=X+D;Y=Y-D;GOTO 1305 2200' Y=Y-D;PLOTS,X,Y 2210 X=X+D;Y=Y-D;PLOTS,X,Y 2220 X=X-D;Y=Y-D;GOTO 1405 2300 X=X-D;PLOTS,X,Y 2310 X=X-D;Y=Y-9;PLOTS,X,Y 2320 X=X-D;Y=Y+D;GOTO 1105 2400 Y=Y+D;PLOTS,X,Y 2410 X=X-D;Y=Y+D;PLOTS,X,Y 2420 X=X+D;Y=Y+D;GOTO 1205 Description of Program: 50 Plot five generations l000-1420 Plot centre square 2000-2420 Not a centre square Variables: A,B - Coordinates of current square D - Number of cells in a quarter of a square J - Number of squares in picture K - Resolution of screen 0 - Graphics mode S - Argument for PLOT statement T - Angle in units of 90 degrees. X,Y - Current drawing position Program size: 1047 bytes Sample plot: _82_
W - Function value Program size: 491 bytes Sample Plot
Description of Program: 10-12 Set up plotting statements for the 16 characters. 20 Scales for letters 30-50 Move X,Y around a circle 60-70 Plot random character 1000-1020 q: Plot low-order byte of B as two hex digits 2000-2060 p: Plot low-order hex digit of A in hex Variables: A - Hex digit to be plotted B - Byte to be plotted H - Horizontal scaling N - Vector containing character plotting statements Q - Next plot statement; low-order bit determines whether to draw or move. S - Slope factor V - Vertical scaling X,Y - Coordinates of point on circle. Program size: 457 bytes Vector: 16 bytes
170 NEXT M 175 A=X;B=Y;X=C;Y=D 180 GOSUBh;GOSUBi 200 NEXT H; UNTIL 0 399 REM 400h510VE E,F 410 V=X/2/Q;U=Y,¿2’Q;W=Vj5;T=U/5 415 WAIT 420 PLQT6,(V-T+E),(U+W+F) 430 PLOT6,(X/Q+E),(Y/Q+F) 440 PLOT6,(V4T+E),(U-W+F) 450 PLOT6,E,F;S=S+5;RETURN 500iWAIT;X=X+J*Y/K 510 Y=Y-J*X/K;S=S+1;RETURN 600sMOVE E,F 620pWAIT;PLOT6,(X/Q+E),(Y/Q+F) 630 S=S+1;RETURN Description of Program: 40-68 Draw clock face 80-84 Do hours and minutes 88 If shift key down miss out seconds 90-150 Do seconds 120 Use up remainder of each second 400-450 h: Draw hour/minute hand from centre of screen to X,Y 500-510 i: Increment X,Y one sixtieth of way around circle. 600 s: Draw second hand 620-630 p: Plot to point X,Y Variables: A,B - Coordinates of tip of minute hand C,D - Coordinates of tip of hour hand E,F - Coordinates of centre of screen H - Twelves of minutes counter J,K - Incremental variables; J/K = 2*PI/60 approx. L - Seconds counter M - Minutes counter N - Counter Q - Scaling factor R - Address of shift key S - Sixtieths of a second used out of current second X,Y - Coordinates on screen scaled by Q Program size: 806 bytes _86_
Sample Plot: To set the correct time hold the shift key down after typing RUN, and release it when the hour and minute hands are in the correct positions.
10 REM 4-Colour Plot 12 GOSUB 400 16 CLEAR4;?4BOOO=ODO 18 ?03FE=Q;?03FF=Q&OFFFF/256 30 FOR J=O TO 64 STEP 2 40 ?C=J%3*4;MOVE J,O 50 DRAW 127,J;DRAW(127-J),191 60 DRAW 0,(191-J);DRAW J,O 70 NEXT J 80 END 400 DIM V(11),C(0),P(-1),Q(-1) 420 !V=#01041040;V!4=002082080;V!8=#030C30CO 430 P.$21 508[ 510 LDA@O;STA #5F 520 LDA#5C;LSR A;ROR #5F 530 LSRA;ROR#5F;LSRA;ROR#5F 540 STA#60;LDA#5A;LSRA;LSRA 550 CLC;ADC#5F;STA05F 560 LDA#60;ADC@#80;STA#60 570\#5f AND #60 CONTAIN ADDRESS 580 LDA05A;AND@3;CLC;ADCC;TAY 590 LDX@0;LDAV,Y;ORA(#5F,X) 600 STA(#5F,X);RTS 610] 620 P.$6 630 RETURN Description of Program: 12 Assemble point plotting routine 16 Clear display; set mode 3a 18 Change point plotter vector 30-70 Demonstration program; curve stitching in 4 colours 400 Set up variable space 420 Vectors for three colours 430 Disable assembler listing 508-610 Assembler point-plotter program 620 Turn screen back on Variables: C - Colour: 0, 4, or 8. P - Location counter Q - Address of point-plotting routine V - Vectors for setting bits Program size: 558 bytes Vectors: 13 bytes Note that the routine only sets bits, and plots in three colours - the fourth colour being the background colour. It would be a simple matter to modify the routine so that it was able to set or unset bits; i.e. plot in the background colour. _89_
_90_
12 What to do if Baffled
This section is the section to read if all else fails; you have studied your program, and the rest of the manual, and you still cannot see anything wrong, but the program refuses to work. There are two types of programming errors; errors of syntax, and errors of logic.the application programs in this manual, to avoid this error. 3. Location counter P not set up when assembling. The value of P should be set before assembling a program to the address of an unused area of memory large enough to receive the machine code, and P should not be used for any other purpose in the program. 4. Graphics statements used without initialising graphics. The CLEAR statement must precede use of any graphics statements. 5. Assigning to a string variable and exceeding the allocated space. Care should be taken that enough space has been allocated to string variables, with DIM, to receive the strings allocated to them. 6. Assigning outside the bounds of an array or vector. Assigning to array or vector elements above the range dimensioned in the DIM statement will overwrite other arrays, vectors, or strings.
1 REM CHC Signature 10 INPUI' "LOOM ADDRESS", P 20 C=O;Z=OFFFI';Y=#2D 30 FOR 0=0 TO #FFF 35 A=P?Q 40 FOR B=l TO 8 60 C=C*2+A&1;A=A/2;IFC>Z C=C:Y;C=C&Z 80 NEXT B; NFXT Q 110 PRINT "SIGNATURE IS" &C' 120 END Program size: 213 bytes Sample run: >RUN PROM ADDRESS?#COOO SIGNATURE IS D67D >RUN PROM ADDRESS?#FOOO SIGNATURE IS E386 The program takes about 6 minutes to run, and if these signatures are obtained the ROM is correct. The Atom extension ROM, described in chapter 22, can be tested by giving the reply #DOOO to the prompt. It should give a signature of AAA1.