BASIC - Language features

Like many who grew up in the 1980s, my first programming language experience was on BASIC (on the VIC-20, in my case) and although I haven’t used it for 30 years I felt right at home.

The Wikipedia article defines three broad generations of BASIC

MTS BASIC falls firmly in the first camp, with a limited number of keywords, restrictions on numbers and names of variables, but with support for matrix operations that was not present in later versions of the language.

The below is a quick look at the main features in MTS BASIC; see the Further Information section for more complete guides.

Program format

Each line of the program must start with a line number - note this is different from line numbers in MTS native files and only integers are allowed.

Full line comments can be introduced with REM or *. To add a comment that lasts until the end of the line you can use /* (very confusing to C programmers!).

10 rem This is a comment
20 * This also
30 let x = 3 /* Line comment
40 let y = 4

Types and variables

BASIC has two data types (only one less than Javascript!) - floating point numbers and strings. Numeric variable names must start with a letter followed by one or two numbers; string variables names must have two identical letters followed by an optional number (ie AA - ZZ, AA1, AA2 etc). Maximum length of strings is 127 characters. Variables can be initialised with LET:

10 let A = 1.23
20 let A1 = 3
30 let AA = "Hello world"

You can have arrays and matrices of numbers which can be indexed using (), eg a(1) or b(2,3). The size can be set using the DIM statement but if not given they are size 10. Indices start at 0. The below will print 100.

10 dim a(2,2)
20 a(1,0) = 100
30 a(1,1) = 200
40 print a(1)

Strings are implemented as vectors of cells containing strings, so AA is the same as AA(0) and you can also access AA(1), AA(2) etc.


INPUT will prompt for the user to enter a value which will be assigned to a variable; PRINT will display a variable or expression, using ; or , to control formatting.

  10 input a, b
  20 print a; b, (a+b)/2

: run
? 10
? 20
  10 20          15

INP and OUT are like INPUT and PRINT but also return the value assigned to the variable.

GOTO x will jump unconditionally to line x. Note you can’t GOTO a non-executable line like a comment. There is also a rather hairy computed GOTO where they program will jump to a line based on the result of an expression.

10 goto 40
20 print "This will be printed second"
30 stop
40 print "This will be printed first"
45 let a=2
50 goto (60,60,20,60) a+1
60 print "This will not be printed"

The IF .. THEN statement will calculate an expression and then either jump to another line or execute a statement. Note that there is no ELSE.

10 let a = 5
20 if a > 1 then 40
30 print "This will not be printed"
40 if a <> 5 then: print "This will not be printed"
50 if a = 5 then: print "This will be printed"

Simple loops can be done with FOR .. NEXT with an optional STEP.

10 for i = 1 to 10
20 for j = 0 to 10 step 2
30 x = x + (i*j)
40 next j
50 next i
60 print x

GOSUB x will call a subroutine at line x; execution will resume from the current line when a RETURN statement is found.

It’s possible to temporarily transfer execution to another program with CALL or permanently transfer with CHAIN.

Simple functions can be defined with DEF, eg DEF FNS(x)=x*x for a square function. Only one parameter is allowed and the function name can only be a single character A-Z.

STOP will end a program and PAUSE will prompt the user for input before continuing.

Library functions

BASIC comes with a number of library functions. Note that you have to assign results of these functions to variables rather than printing them directly - so in the example below I can’t do PRINT CLS().

String operations include conversion to numbers (STN) and back (NTS) along with access to the surrounding environment such as UID for user ID and CLS for time of day.

10 let ss = cls()
20 print ss
30 let ss = uid()
40 print ss
: run

Numeric functions include mathematical support functions like ABS() and SIN().

Matrix and vector support is especially powerful, with the ability to enter, print and do matrix operations. The below will prompt for a 2x2 matrix, add it to its transpose and then print it.

10 dim x(2,2), y(2,2), z(2,2)
20 mat input x
30 mat let y = trn(x)
40 mat let z = x + y
50 mat print z
: run
? 1,2
? 3,4
  2              5             
  5              8             

There is also limited support to read and write files - but these are special data files attached to the BASIC environment rather than general MTS files.

And that’s pretty much it for BASIC. In the next post we’ll look at a larger example of a MTS BASIC program.

Further information

MTS Volume 10 offers a complete description of the BASIC language and environment.


comments powered by Disqus