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 omitted 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 Fahrenheit to Celsius (Centigrade), giving the answer to the nearest degree:

   10 INPUT "FAHRENHEIT" F
   20 PRINT (10*F-315)/18 "CELSIUS" '
   30 END

The value, in Fahrenheit, 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 #8202 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.

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).

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=0 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 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:

 

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:

0,47   63,47
^
|
Y
|
 
0,0 -- X --> 63,0

4.7.1 CLEAR

To prepare the screen for graphics the statement CLEAR is used. It is followed by the graphics mode number. On the unexpanded ATOM the only legal option is:

      CLEAR 0

4.7.2 MOVE

Any point on the screen can be specified by moving the 'graphics cursor' to that point with the MOVE statement. The graphics cursor does not show on the screen, and it is different from the ordinary cursor which is visible in character mode. The format of the statement is:

      MOVE X,Y

where X and Y can be numbers, or arbitrary expressions provided they are enclosed in brackets. For example, to move the graphics cursor to the origin, X=0 Y=0, type:

      MOVE 0,0

The MOVE statement will normally be the first graphics statement of any program.

4.7.3 DRAW

The DRAW statement will plot a line anywhere on the screen. The line starts from the position of the graphics cursor, and ends at the point specified in the statement, and tie graphics cursor will be moved to that point. For example:

      DRAW 63,47

will draw a line to the top right-hand corner of the screen, and leave the graphics cursor at that point. It is quite legal, and safe, to draw off the screen; the line will just not appear.

4.7.4 Example

The following simple program will draw a rectangle, rotated by the amount entered for R. Try typing in numbers between 0 and 47 for R:

   10 REM Rotating Rectangle
   20 X=63; Y=47
   30 INPUT R
   40 CLEAR 0
   50 MOVE R,0
   60 DRAW X,R; DRAW (X-R),Y
   70 DRAW 0,(Y-R); DRAW R,0
   80 GOTO 30

4.7.5 Plotting Points

One way of plotting a single point at X,Y on the screen is to write:

      MOVE X,Y; DRAW X,Y

A more elegant way is given in Section 11.3.

4.8 Conditions - IF...THEN

One of the most useful facilities in BASIC is the ability to execute a statement only under certain specified conditions. To do this the IF...THEN statement is used; for example:

      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.1 Relational Operators

The part of the IF...THEN statement after the IF is the 'condition' which can be any two expressions separated by a 'relational operator' which compares the two expressions. Six different relational operators can be used:

          =  equal           <>  not equal
          >  greater than    <=  less than or equal
          <  less than       >=  greater than or equal

where each operator on the left is the opposite of the operator on the right.

The expressions on each side of the relational operators can be as complicated as required, and the order is unimportant. There is no need to put brackets around the expressions.

For example, the following program prints one of three messages depending on whether a number typed in is less than 7, equal to 7, or greater than 7:

   10 REM Guess a number
   20 INPUT"GUESS A NUMBER" N
   30 IF N<7 THEN PRINT "TOO SMALL" 
   40 IF N=7 THEN PRINT "CORRECT!" 
   50 IF N>7 THEN PRINT "TOO LARGE" 
   60 END

A flowchart for this program is as follows:

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 "BOTH"
   30 GOTO 10

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.

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

4.10.1 Cubic Curve

Perhaps a more useful example is the following program, which calculates the value of:

x3 - 600x

for 64 values of x and plots a graph of the resulting curve:

    1 REM Cubic Curve
   10 CLEAR 0
   20 MOVE 0,24; DRAW fi3,24
   30 MOVE 32,0; DRAW 32,47
   40 MOVE -1,-1
   50 X=-33
   55 Y=(X*X*X-600*X)/400
   60 DRAW (32+X),(24+Y)
   70 X=X+1
   80 IF X<33 THEN GOTO 55
   90 END
Description of Program:
10        Use graphics mode 0
20-30     Draw axes
40        Move graphics cursor off screen
50-80     Plot curve for values of X from -32 to 32
55        Equation to be evaluated divided by 400 to bring the
          interesting part of the cubic curve into range
60        Draw to next point, with origin at (32,24).
Program size: 190 bytes

Next chapter