Olivetti M10 PORTABLE COMPUTER Operations Guide
                                                                   PART 2
                                       M10 BASIC Language Reference Guide

10. INTRODUCTION TO BASIC The next few chapters are entirely devoted to BASIC, Beginners All- purpose Symbolic Instruction Code, which is the hiqh-level programming language of the M10. Different versions of BASIC exist but all descrip- tions and references in this manual apply to the M10 version. In Chapter 4, a short description of the BASIC Application Program was given so the user will already be familiar with certain aspects of the M10 BASIC facility such as accessing SASIC, the three modes of operation, the concept of lines, the role nf the function keys etc. The fallowing chapters deal with writing and running programs, data and arithmetic, relational and logical operators and finally gives a Directory cf BASIC Commands for reference. NOTATION The convention given in Chapter 1 is maintained throughout the manual. However, for the purposes of BASIC syntax, it has to be somewhat expanded for this part of the manual. The following symbols occur in BASIC syntax and should be entered as they stand. Double quotation marks " Full stop . Comma , Colon : Semi-colon ; Addition symbol + Subtraction symbol - Multiplication symbol * Division symbol / Exponentiation symbol ^ Back slash \ Equals = Greater than > M10 Operations Guide 10-1
Less than < Question mark ? Percentage sign % Exclamation mark ! Number sign # Dollar sign $ Parentheses ( ) 10-2
11. BASIC PROGRAMS ORGANIZATION BASIC programs for the M10 are stored in memory. Up to 19 file names can be displayed on the menu and this determines the maximum number of pro- grams which can be stored in memory. Programs can also be stored on cassette tapes or other peripheral units. It is good practice to keep two separately stored copies of each program; one for normal use and one as a back-up. DOCUMENTING A PROGRAM It is most important that programs are created in a logical way and that they are well documented. A procedure which seemed obvious at the time of writing may well be incomprehensible at a later date, unless appropriate notes are added. This can be done with the REM statement, or an apos- trophe, ', can be used as an abbreviation. REM Remarks may use a complete line or they may be on the sane line as other statements. For the latter case the remark must be the last statement on the line. CREATING A PROGRAM The user creates a BASIC program on the M10 by entering the instructions via the keyboard; listing the instructions to confirm that they appear correct; and saving the program on tape so that it can be used repeti- tively. ENTERING A PROGRAM Each program line must start with a line number which is typed on the keyboard. Program lines may be entered in any order and the system re-arranges them into numerical order when the program is listed. Listing can be done at any time and is described below. The program can also be saved at any stage in its development; saving is described below. All the commands, statements and functions which can be used on the M10 are detailed in Chapter 15. M10 Operations Guide 11-1
LISTING A PROGRAM To list a program on the display the command LIST is available. The com- mand LLIST lists the program on the printer. LIST can be used to display one or more program lines on the screen as follows: LIST n -m <ENTER> where n is the first line to be listed and m is the last line to be displayed. If n is not specified all lines up to m are listed; and if m is not specified, only line n is listed. If neither n nor m are speci- fied the whole program is listed. LIST . will list the last line refer- enced by BASIC. A pcogram which has been listed is stored in memory but the syntax has not yet been checked. The consnand LLIST works in exactly the same way but the program lines are listed on the printer as well as the display. Details of LIST and LI.IST are given in Chapter 15. SAVING A PROGRAM A program stored in memory can be saved on a cassette or any other storage device connected to the M10. The SAVE conmand has a number of options available for different peripherals and these are outlined in Chapter 15. To store the current program on cassette the 'CSAVE tape filename' com- mand can be used, where tape filename is a name (of up to six characters) identifying the file to contain the program on the cassette. Tt is pos- sible to check that the program has been transferced correctly by using the command 'CLOAD? tape filename’ which compares the program on cassette with that in the memory. If there are any discrepancies the message Verify failed appears on the screen, and if the transfer is satisfactory the Ok prompt is displayed. To stop the program saving activity press <SHIFT + BREAK>. Details of the use of CSAVE and CLOAD? are given in Chapter 15. USING A PROGRAM To use a program it must first be loaded into memory, then executed. If it runs satisfactorily, results can be printed. If there are problems with the program which prevent its execution, e.g. syntax errors, they will be flagged by the system. The procedure for de-bugging is described below. 11-2
LOADING A PROGRAM The LOAD command has several options so that proqrans stored on different peripherals can be loaded into memory. These are detailed in Chapter 15. Programs stored on cassette using CSAVE can be loaded into memory using the command 'CLOAD tape filename' where 'tape filename' is the name of the file containing the program required. If the filename is omitted, the first program on the tape is loaded into memory. To stop the loading press <SHIFT + BREAK> and use the MOTOR OFF command to stop the cassette drive. It is possible to load and run a proqram from a cassette with one command and this feature is described below. EXECUTING A PROGRAM To execute a program use the RUN command. Typing the command RUN <ENTER> will attempt to execute the program last loaded into memory. If the pro- gram is not in memory and is stored on a cassette it can be loaded and then run by using RUN "CAS:tape filename" <ENTER> where tape filename is the name of the file containing the required pro- gram. If there is any problem with the program an error will be indicated by the system: the procedure for de-bugging is described below. If the program requires information from the user in the form of data, a prompt ? is displayed. Normally the prompt is preceded by a message explaining the data required. Refer to the INPUT statement in Chapter 15 for details of how this is written into a program. Type the required data and send it to the system by pressing <ENTER>. PRINTING Program results can be displayed by using the statement PRINT, or LPRINT may be used to output to the line printer or microplotter. It is possible to print data in a particular format by specifying it in a PRINT USING or LPRINT USING statement. The latter is useful for printing files or the results of programs in a form different from that in which they appear on the screen. If LPRINT is used the exact form shown on the display is reproduced on the line printer. To output data to a serial printer (connected to the RS-232C interface) the data must be written to a file. This is explained in Chapter 15. M10 Operations Guide 11-3
DE-BUGGING A PROGRAM Errors made in the writing of a program, e.g. incorrect syntax, are detected by the system when the user attempts to run the proqram. They are identified by line number so that the errors can be corrected. ERRORS A complete list of error codes is given in Appendix B. Each code has a two-character name which is displayed, and a number. The number is an integer in the range 1 to 255; many of the numbers are not allocated and they are available for system expansion in BASIC or for the user to define his own errors. In addition to the errors which can be made while writing a program there are a number of errors which may occur at other times, e.g. IO error meaning that an input ac output fault occurred when trying to read or write data. The user can define an error by using the statement ERROR n, where n is the specified error code integer. Details of this statement are given in Chapter 15. ERROR TRAPPING Error handling subroutines can be written into programs and the statement ON ERROR GOTO n is used to specify the first line, n, of such a routine. At any time after the ON ERROR GOTO statement any error will result in a jump (trap) to the subroutine. Error trapping will be cancelled after an ON ERROR GOTO 0 statement. MODIFYING A PROGRAM A program can only be modified when it is stored in the memory; if it is not currently in memory it must first be loaded, as described above. CHANGING A PROGRAM Program lines can be altered by retyping the complete line and pressing <ENTER>. The new line then replaces the old one in memory. A line can be deleted by typing the line number and pressing <ENTER>. 11-4
EDITING A PROGRAM For several small changes to the text of a program it is simpler to use the editing facility. The command EDIT transfers the system to the Text Mode where editing is done. Details of the format of the command are given in Chapter 15. Text Mode provides a screen editor which is used for creating and editing files. It has a number of commands which simplify the creation or modifi- cation of files, and, with the exception of the 'word-wrap’ feature, they are available for modifying programs. The full description of this mode is given in Chapter 4. MERGING TWO PROGRAMS A program on cassette can be merged with another in memory by using the command MERGE "CAS:filenane With this command the lines from the program on cassette are merged in numerical order with those of the program in memory. If a line in the cassette program has the same nunber as a line in memory the latter is replaced and the original line is lost. M10 Operations Guide 11-5
12. DATA AND ARITHMETIC DATA IN BASIC PROGRAMS Within programs data items can be constant or variable. A constant has the same value throughout program execution, whereas the value of a vari- able may be altered. Variables are, therefore, given names which are used in the writing of the program, e.g. the formula C + Z73 gives the temperature in degrees kelvin where C is a variable represent- ing the temperature in degrees celsius. A program using this expression can be used many tirades by using different values for C. Variable names can be longer than two characters, but only the first two are recognized and they must, therefore, be unique. A constant may also be given a name, e.g. it is easier to enter a short name than a long con- stant. A reserved word may not be used as a name or as part of a name. Therefore it is recommended that variable names are restricted to one or two characters, so that the only reserved words which must be avoided are IF, ON, OR and TO. Both constants and variables can be purely numeric or 'strings'. A string is a sequence of any characters except the double quotation marks ("), as these are used to define or delimit the string. An 'expression' is a general name for an item, or a combination of items, of data. Tt can contain constants or variables or both, but it cannot contain a mixture of numeric and string data. The formula above is a numeric expression and the numeric variable C must be assigned a value before the expression can be evaluated. STRING DATA String data are of variable length and BASIC allocates memory as it is required. The length of the string can by any number of printable charac- ters from 0 to 255. If the string contains no characters i.e. two double quotes without a space between them, it is called a 'null' string. M10 Operations Guide 12-1
NUMERIC DATA BASIC can handle numeric data of three different types depending on the precision required in calculations, viz. integer, single precision, or double precision data, All numeric data are entered in a decimal form (i.e. to the base 10). Integers These are most efficient in terms of the amount of memory used and the speed of computation. However, the range of values is limited to integers from -32768 to 32767. Single Precision Numbers These are used for general purpose applications in BASlC. The range of values is from ±10-64 to ±1062 and up to seven significant digits are used in computations. Up to six digits can be displayed, the least siqnificant being rounded up if the seventh digit is greater than or equal to 5. More memory is required to store single precision numbers and calculations take longer than for integers. Double Precision Numbers These are the most precise form for data in BASIC. The range of values is from ±10-64 to ± 1062 and up to 36 significant digits are used in computations. Up to 15 digits can be displayed, the least significant being rounded up if the 16th is greater than or equal to 5. Double preci- sion numbers use the most memory and calculations are the slowest. CLASSIFICATION OF DATA Normally a string is recognized by the enclosing double quotes. While data is being input, or in an instruction using the DATA statement, the characters need not normally be enclosed in double quotes. Numeric data constants ace classified by the minimum precision category which is appropriate, i.e: - if a number has more than seven digits it is a double precision number - if a number is a whole number in the range -32768 to 32767 it is an integer - if a number falls into neither of these categories it is a single precision number. Numeric variables are initially classified as double precision. 12-2
CHANGING CLASSIFICATION Both constants and variables can be given different classifications. The type of a constant can only be changed by adding a type declaration tag at the end of the data item. The four tags available are listed in Fig. 12-1. Variables can be defined at the start of a program as a particular type by using one of the fallowing statements: - DEFINT ident specifies integer variables - DEFSNG ident specifies single precision variables - DEFDBL ident specifies double precision variables - DEFSTR ident specifies string variables where, in each case, ident gives the letters which will identify a par- ticular type of variable when used as the first letter of a variable name, e,g: 10 DEFTNT A-C, M, X-Z states that all program variables starting with the letters A, B, C, M, X, Y and Z will be integers. The format of the DEF state nents is detailed in Chapter 15. It is possible to override these definitions using the type declaration tags listed in Fig. 12-1.
TAG MEANING
% Integer
I Single Precision
# Double Precision
$ String
Fig. 12-1 Type Declaration Tags Unless the nuneric variables and constants are specified as integer or single precision using DEF statements or type declaration tags, all cal- culations are carried out in double precision. M10 Operations Guide 12-3
CONVERSIONS Sometimes it is necessary to assign one type of constant to a different type of variable, or to convert one type of variable to another. This can be done using the form of a = b and some examples are illustrated below. Single or double precision to integer, and double orecision to single precision involve truncation or rounding, and nunbers outside the range of the converted variable will produce overflow errors. Integer to single precision produces no errors. Example 1 Convert the single precision variable A (= 23.56) to an integer. Follow the BASIC conmand sequence: A% = 23.56 <ENTER> Ok ?A% <ENTER> 23 Ok Note that the number is truncated and not rounded. Example Z Convert the double precision variable A (= 5.0069999999) to single precision. Follow the BASIC command sequence: A! = 5.0069999999 <ENTER> Ok ?A! <ENTER> 5.007 Ok In these examples the lines containing <ENTER> are commands to be entered by the user; the other lines are the output from the BASIC program. ARRAYS An array is a collection of variables of the same type which are grouped together. Each item is identified by the name of the array and a sub- script which is different for each element, e.a: A(0), A(1), A(2) are the three elements of a one dimension array, A, which has a maximum subscript of 2. This is defined as the upper bound of the dimension. An array may have any number of dimensions providing there is sufficient memory space available. Similarly each dimension can have any number of elements providing memory space is available. The appropriate amount of memory is allocated by BASIC for an array when it is first specified. The 12-4
following are elements of a two dimension array the maximum subscripts of which are n, m. B(0,0) B(0,1) B(0,2) - - - - - - - - B(O,m) B(1,0) B(1,1) B(1,2) - - - - - - - - B(1,m) B(2,O) B(2,1) B(2,2) - - - - - - - - B(2,m) | | | | B(n,O) B(n,1) B(n,2) - - - - - - - - B(n,m) This two dimension array has n + 1 rows and m + 1 columns. A single dimension is suitable for holding the contents of a list, and two dimen- sion arrays are typically used for tables. Defining Arrays An array can be defined using the DIM statement and the maximum sub- scripts to be used. 1f the array is to hold string variables this must be specified by the use of $ when it is defined, e.g.: 10 DIM A$(5,5), B(12) <ENTER> establishes a two dimensional string array with sub-scripts from 0,0 to 5,5 and a one dimensional array with sub-scripts from 0 to 12. if an array element is mentioned in a program and the array has not pre- viously been defined, one will be created with the number of dimensions required by the element and each dimension will have an upper bound of 10. The D1N statement sets all the elements of the specified arrays to zero or null string. There is no separate command for erasing an array. BASIC ARITHMETIC Operations within BASIC fall into three cateqories, viz. relational, numeric and logical. For each category there are a number of functions available, and each function is identified by a particular symbol which is referred to as an operator. M10 Operations Guide 12-5
RELATIONAL OPERATORS The simplest operation is a comparison between one expression and another. Only like expressions can be compared, i.e. it is not possible to make comparisons between strings and numbers. The following relational operators are available for both strings and numbers. Strings must be the same length or the longer one is truncated to match the shorter one. The 'value' of a string is determined by the ASCll value of each character, with the first character being most significant. The table in Fig. 12-2 lists the relational operators and their meanings.
OPERATOR MEANING
= Equal to
> Greater than
< Less than
= > or > = Greater than or equal to
= < or < = Less than or equal to
< > or > < Not equal to
Fig. 12-2 Relational Operators The result of a relational expression is given as -1 if it is true and 0 if it is false. NUMERICAL OPERATORS Numerical operations are possible only for numbers. The + symbol meaning addition can also be used with strings to join them together, but no other numerical operator may be used with strings. The table in Fig. 12-3 lists the available operators and their meanings. 12-6
OPERATOR MEANING
+ Addition
- Subtraction or Negation
* Multiplication
/ Division
\ Integer Division
^ Raising to a power
Fig. 12-3 Nunerical Operators The numbers used with an operator are called operands. For integer division the operands are counded to the nearest integer and the result of the division is truncated to the integer part. The remainder of such a division can be expressed to the nearest integer using the MOD function. The order of priorities for numerical operators is as follows: 1. Raising to a power 2. Negation 3. Multiplication and division 4. Integer division 5. Addition and subtraction For operations with the same priority, the operations are carried out in the order they are entered from left to right. Priorities can be altered by using parentheses to enclose the operation required first. This can be extended by using parentheses within parentheses as illustrated in the following examples: 1. For x + y + z enter (X + Y + Z)/2 --------- 2 2. For 2x + 5 enter 2 * X + 5 3. For 2(x + 5) enter 2 * (X + 5) 4. For (x + y)2 enter (X + Y) ^ 2 M10 Operations Guide 12-7
5. For x2(x + y)4 enter X ^ 2 * ((X + Y) / Z) ^ 4 ----- z4 Parentheses can be used even if they are not strictly necessary. This can be useful in helping to clarify an expression. If an expression contains a variable which has not been assigned a value, the variable is set to zero. Results