Translating BASIC Programs

See all articles from CATS v4 n5
Author(s): Ken Brown
Publication: CATS Newsletter
Pub Details: CATS v4 n5 (Volume 4, Issue 5)
Date: Aug 1986
Pages: 8-10

Reprinted from The Computerist, Jan ’83 (Predecessor to the CATS Newsletter)

Sooner or later, as you browse through computer magazines and books, you will find a published program that you like – only to discover that it won’t work in Sinclair BASIC. This problem arises because there is no standard form of the BASIC language. At this point you have two choices: Try to write the program yourself, from scratch, or try to “translate” the program into Sinclair BASIC.

Some differences between BASIC languages are easily remedied. For example, some computers allow the use of multiple assignment statements such as:

10 A=B=C=D=5.4

Sinclair BASIC does not allow this type of statement, although it can be easily translated into:

10 LET A=5.4
11 LET B=A
12 LET C=A
13 LET D=A

Not as compact, but equally functional. [Ed note: Sinclair BASIC will accept the original line, as long as a LET precedes the numbers. Unfortunately, it will always assign A a value of zero, as it assumes that the multiple equals signs mean the writer wants a logical evaluation of the expression (((B=C)=D)=5.4). As logical values are either 0 or 1, they can NEVER equal 5.4, and thus A will always take the “not true” value of 0.]

Other differences, such as the lack of a READ or DATA statement in Sinclair BASIC, are more difficult. I’ll try to tackle READ DATA in this installment, and cover other differences in a later installment.

While READ and DATA are two separate BASIC commands, they are used together. A DATA statement contains a list of data items, either numbers or strings, usually seperated by commas. When the computer encounters a DATA statement it is ignored, much in the same way a REM statement is ignored. However, when a READ statement is encountered, the computer searches for the first DATA statement in the program and assignes the first data item in the DATA statement to the variable in the READ statement.

Here is an example:

10    DIM A(5)
20 DATA 5,6,-2,0,3
:
100 FOR I=1 TO 5
110 READ A(I)
120 NEXT I

The first time line 110 is executed the computer locates the DATA statement in line 20, locates the first data value (in this case 5), and assigns it to A(1). The computer remembers that the first value has been “used” and the next time line 110 is executed the computer assigns a value of 6 to A(2). Every time a READ statement is encountered the computer will take the next value from the DATA statement. Once the data items in the first DATA statement have been used the next READ will cause the computer to search further on in the program to find the next DATA statement. A RESTORE 20 command is used to move the computer’s “pointer” back to the start of line 20.

Now, can you translate the above example into Sinclair BASIC? One approach, the brute force technique, would be:

100   LET A(1)=5
110 LET A(2)=6
120 LET A(3)=-2
:
140 LET A(5)=3

This works for a small array, but suppose that the array is to contain 100 values. The following method is more elegant and tends to preserve the appearance of the original program:

10    DIM A(5)
20 LET D$=" 5, 6,-2, 0, 3"
:
95 LET POINTER=1
100 FOR I=1 TO 5
110 LET A(I)= VAL D$(POINTER
TO POINTER+1)
115 LET POINTER=POINTER+3
120 NEXT I

Study this example carefully to see how it works. First the DATA statement is replaced by a string variable D$. The commas are not required, though they do make the line easier to read. The blank spaces, on the other hand, are necessary. Because one of our data entries (-2) is two spaces long, making each entry two spaces long makes the “READ” part of our example easier to do. The VAL function takes the portion of D$ from POINTER to POINTER+1 and converts it to a number. [Ed note: for assignment of string variables, the above technique is easier to do than the cumbersome handling of string data in the “real” READ/DATA statements of the TS 2068.] Line 115 then moves the POINTER to the next item on the list, as the READ statement would do automatically. Note that because of the comma the pointer is incremented by three, even though each item is only two spaces long.

In this example the READ statement was used to assign values to an array. This is probably the most common use of the read statement. Occasionally, a program will use the READ statement in a slightly different way. However, it should still be possible to use the ideas I’ve presented here to translate the program into Sinclair BASIC.

Next chapter: Multiple statement lines and computed GOTOs.


from the Computerist #6 (Feb. ’83)

Multiple Statement Lines

Many computers allow more than one BASIC statement to be included in a single line of a program. Each statement, normally seperated by a colon or a slash (/), is executed in turn starting with the first statement after the line number. In most cases, translating such a program simply requires assigning each statement an individual line number.

Here is an example:

130  LET A=2:LET B=3:LET C=A/B

When this line is executed A is assigned a value of 2, then B is assigned a value of 3, and finally C is assigned a value of 2/3. Translated to Sinclair BASIC this line would look like:

130  LET A=2
131 LET B=3
132 LET C=A/B

One difficulty in translating this type of line occurs when the lines in the original program are sequentially numbered. In this case the line numbering in the Sinclair program may be very different than in the original. In this case GOTO and GOSUB statements must be examined carefully.

130  LET A=2:LET B=3:LET C=A/B
131 LET A=A+1:LET D=C/A
:
325 GOTO 131

In this program line 325 causes the computer to jump back to line 131 and begin execution with LET A=A+1. When this program is translated the line number of the statement LET A=A+1 will be changed, and the GOTO statement in line 325 must be changed to reflect this.

130  LET A=2
131 LET B=3
132 LET C=A/B
133 LET A=A+1
134 LET D=C/A
:
325 GOTO 133

In a short program it is easy to locate all of the GOTO and GOSUB statements and change them if necessary. However, in a program of several hundred lines just keeping track of this type of change can become a monumental task. If the lines in the original program are incremented by 10’s, or if only a few lines contain multiple statements then translation is probably possible. If, on the other hand, large portions of the program are sequentially numbered and contain multiple statement lines the program should probably be avoided.

There is one situation in which translating a multiple statement line becomes more difficult. This occurs when the line contains an IF…THEN statement. Unfortunately, computers which allow multiple statement lines handle this situation in two different ways. I believe that the method I will present first is the most common.

Here is an example of this type of line:

100  IF A=0 THEN PRINT "HELLO":LET A=1:LET B=-1
110 LET C=A

When the condition contained in the IF…THEN statement is true (in this case when A=0) then all of the statements after the IF…THEN statement will be executed. If, on the other hand, the condition is false then none of the statements after the IF…THEN statement will be executed, and the computer jumps immediately to the next line in the program.

In this example, if A=0 when the computer reaches line 100 then the message HELLO will be printed, A will be assigned a value of 1, and B will be assigned a value of -1. If A is not equal to 0 when the computer reaches line 100 the computer jumps immediately to line 110. No message will be printed, and the values of A and B will not be changed.

Translating this example to Sinclair BASIC requires some thought. Here is the same example translated to work properly:

100  IF A NOT =0 THEN GOTO 110
101 PRINT "HELLO"
102 LET A=1
103 LET B=-1
110 LET C=A

Although the IF…THEN statement in line 100 has been changed considerably these lines function in exactly the same way as the multiple statement lines in the previous example. When the computer reaches line 100 if A=0 then the new condition in the IF…THEN statement is now false. The computer jumps to line 101 and executes the statements which previously followed the IF…THEN statement. If A is not equal to 0 the new condition is now true and the statement GOTO 110 is executed. This skips the statements at lines 101 to 103.

Here is a general procedure for handling multiple line statements containing IF…THEN statements. If there are any statements in the line which proceed the IF…THEN statement these are treated in the normal way. The condition in the IF…THEN statement is changed so than the new condition is the opposite of the original (= becomes either NOT = or <>, etc.). Rewrite the IF…THEN statement so that it branches to the next line from the original program. Finally, place the original statement contained after the THEN clause, as well as all of the other statements to the right of the IF…THEN statement in the original line, on individual lines after the IF…THEN statement.

While most computers which allow multiple statements handle IF…THEN statements in this way, there are apparently exceptions. In these computers when the IF…THEN statement is false the computer jumps to the next statement in the line (the statement after the colon). If this is the case translating a program only requires writing each statement on a seperate line. However, unless the program description explains how this situation is to be handled, it is probably safest to assume that it is handled in the first way.

Translating programs containing multiple statement lines may be difficult at times. There is, however, a wealth of software avalible in books and computer magazines which is written for computers which allow multiple statement lines. In fact, the new Sinclair Spectrum allows multiple statement lines. Find a small program and try it. When you succeed you’ll be ready to tackle a large one.