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.
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:
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.
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.
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>
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"
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*T
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 the 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.
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, "POUNDS", R%100, "PENCE"
Numbers are normally printed out right-justified in a field of 8 character spaces. If the number needs more than 8 spaces the field 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.
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.
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
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 '#' symbol, called 'hash', is used to introduce a hexadecimal number. Thus #E9 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. #8000 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
will print:
8000>
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:
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
Try typing the following:
PRINT 0 & 1 PRINT 1 \ 1 PRINT 1 : 1
and verify that the results agree with the table.
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 #C000 type:
PRINT ?#C000
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:
?#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:
?#E1=0
which will turn the cursor off. To turn the cursor back on again type:
?#E1=#80