C. Regena
Translating
Programs
Into TI BASIC
Into TI BASIC
I have had several requests to explain how to translate a BASIC program from another brand of microcomputer to TI BASIC. For example, you may see a program that fits your needs, but it's written for the Apple, or Atari, or TRS 80. How can you rewrite it so that it works on your TI-99/4A?
All the main microcomputers use a programming language called BASIC. However, each brand of computer has its own form of BASIC which may not be compatible with other computers.
To "convert" programs, you first need to be familiar with your TI's language idiosyncrasies: what syntax and spelling to use; where to put spaces, commas, colons, and semicolons; what type of numbers to put in parentheses; and what the limits of parameters are.
The command module for TI Extended BASIC makes conversion easier because Extended BASIC increases programming power by allowing multistatement lines, PRINT AT or DISPLAY AT features, and more versatile IF-THEN-ELSE logic. This column, however, concerns conversions to the built-in console TI BASIC.
You cannot load a program from cassette or diskette from another brand of microcomputer to your own. In general, the baud rates (the rates at which information is transferred from one place to another) are different, and each computer has special character codes which may not be recognized by another computer. Graphics are-especially machine-specific.
Games With Graphics And Sound
Action games are probably the most challenging programs to translate because they use graphics and sound. You could run the program on the computer for which it was written (to see what it looks like) and then write your own TI graphics. It's really easier to design your own graphics and sound than to try to convert line by line. If you see a command in another program with the word SOUND or PLAY, the command is for noises or music; and TI BASIC will require a CALL SOUND statement.
Typical graphics statements in other versions of BASIC contain PRINT with special characters in quotes, or such words as LINE, DRAWTO, HLIN, VLIN, CIRCLE, PAINT, COLOR, SETCOLOR, SET, PSET, RESET, PRESET, INVERSE, GRAPHICS, GR, PMODE, SCREEN, DRAW, or PLOT. Many POKE statements also display graphics or play sounds. Also numbers for graphics commands may be contained in DATA and READ statements.
Common Statements
Many general-purpose programs can be easily converted from a printed listing for another brand of computer. Below are examples of common statements and the translations. The left column contains examples you may see in listings for other computers. The right column gives the TI BASIC equivalent.
Other computers that allow multi-statements often use a colon to separate commands. For the TI simply separate the statements with new line numbers. If there are any branching statements, be especially careful of proper logic and program flow.
100 X=1:Y=1:Z=2 100 X=1
110 PRINT Y:GOTO 400 102 Y=1
104 Z=2
110 PRINT Y
112 GOTO 400
Spaces which are required in TI BASIC may be omitted in other versions of BASIC:
200FORX=1T05:PRINTX: 200 FOR X=1 TO 5
NEXTX
202 PRINT X
204 NEXT X
A NEXT statement requires the name of the loop variable, and NEXT statements may not be combined.
200 FOR D=1 TO 500:NEXT 200 FOR D=1 TO 500
202 NEXT D
300 FOR I=1 TO 10 300 FOR I=1 TO 10
310 PRINT I 310 PRINT I
320 FOR J=1 TO 100:NEXT J,I 320 FOR J =1 TO 100
322 NEXT J
324 NEXT I
Some computers have special function keys to clear the screen, or they may use the command CLS.
100 PRINT {CLEAR} 100 CALL CLEAR
100 CLS
CLS with a number following the command clears the screen with a certain color number. TI BASIC can use CALL CLEAR then CALL SCREEN(C) for the color C.
INPUT statements in TI BASIC may have a prompt which is followed by a colon. Other computers may use a semicolon or a comma.
200 PRINT "NUMBER?"; 200 INPUT "NUMBER?":N
210 INPUT N
300 INPUT "ENTER 300 INPUT "ENTER
COST ";C COST":C
TI BASIC allows colons in the PRINT statements to indicate blank lines or to start a new line.
200 PRINT:PRINT:PRINT X 200 PRINT ::X
300 PRINT "JOHN":PRINT 300 PRINT "JOHN":"JACK"
"JACK"
400 FOR L=1 TO 5 400 PRINT:::::
410 PRINT
420 NEXT L
An IF statement must contain a line number rather than a command after THEN or ELSE. Some computers do not have the ELSE option, but in your translations you may notice it would be appropriate to use an ELSE.
200 IF X=20 THEN X=1 200 IF X<>20 THEN 210
210 PRINT X 202 X=1
210 PRINT X
300 IF A=B THEN C=1: 300 IF A < > B THEN 310
GOTO 100
310 A=A+1 302 C=1
304 GOTO 100
310 A=A+1
400 IF N<10 THEN N= 400 IF N>=10 THEN 600
N+1:GOTO 100
410 GOTO 600 402 N=N+1
404 GOTO 100
500 IF I>J THEN 250 500 IF I>J THEN 250 ELSE 700
510 GOTO 700
Random numbers may be generated in a variety of ways. The TI BASIC command RND yields a decimal from 0 to 1 (which may then be multiplied by another number). The INT command is used to get random integer numbers (whole numbers). For example, INT(10*RND) yields a random number from 0 to 9, so INT(10*RND) + 1 or INT(10*RND + 1) will give a random number from 1 to 10. INT(5*RND) + 10 will give a random integer from 10 to 14 -10, 11, 12, 13, or 14.
TI BASIC also has the command RANDOMIZE to mix up the random selection. Other computers may not have this function or may use the words RANDOM or RAND.
To get a random number from 1 to 6, the following statements are equivalent.
VIC-20 X=INT(6*RND(0))+1
TRS-80 CC X=RND(6)
Apple X=RND(6)+1
Atari X=INT(6*RND(1))+1
TI X=INT(6*RND)+1
GET and INKEY$ check to see which key has been pressed on the keyboard for a single keystroke answer. Some computers may "buffer" several keys. The equivalent TI statement is CALL KEY.
200 GET A$:IF A$="" 200 CALL KEY (0,K,S)
THEN 200
210 IF A$="Y" THEN 300 210 IF K=89 THEN 300
220 IF A$="N" THEN END 220 IF K<>78 THEN 200
ELSE 200
230 END
200 A$=INKEY$:IF A$="" THEN 200
210 IF A$="Y" THEN 300
220 IF A$="N" THEN END
230 GOTO 200
How Variables Vary
String variables are handled differently in different computers, so it helps to know what the other computer is doing to be able to convert to the TI. For example, the Atari requires a DIMension statement for the string length. The TI uses a DIMension statement when the string is in an array. The TRS-80 Color Computer may have a statement such as PCLEAR 2000 to clear more memory for strings.
LEN(A$) returns the length of the string variable A$. Some computers give the length of the null string ,"", as 1, but the TI says the length is zero.
To combine strings in TI BASIC, use the ampersand symbol.
200 D$=A$+B$+"XYZ" 200 D$=A$&B$&"XYZ"
LEFT$, MID$, and RIGHT$ are functions that refer to part of the string. The TI BASIC equivalent function is SEG$.
200 B$=LEFT$(A$,5) 200 B$=SEG$(A$,1,5)
(Left five characters of A$ starting with the first character)
210 C$=MID$(A$,7,3) 210 C$=SEG$(A$,7,3)
(Three characters of A$ starting with the 7th character)
220 D$=RIGHT$(A$,2) 220 D$=SEG$(A$,LEN(A$)
-1,2)
(Right or last two characters of A$)
230 E$=RIGHT$(A$,R) 230 E$=SEG$(A$,LEN(A$)
(Right R characters of A$) -R+1,R)
The PRINT AT or PRINT @ statement is another statement you may wish to convert. The PRINT AT statement is followed by one or two numbers which indicate a position on the screen to begin printing. There are two main ways to write this procedure in TI BASIC.
200 PRINT TAB(COL);
'HELLO":::::::
(where the colons scroll the printing up to the proper row.)
To print without scrolling:
200 M$="MESSAGE"
210 FOR I=1 TO LEN(M$)
220 CALL HCHAR(ROW,COL-1+I,ASC(SEG$(M$,I,1)))
230 NEXT I
(where the colons scroll the printing up to the proper row.)
To print without scrolling:
200 M$="MESSAGE"
210 FOR I=1 TO LEN(M$)
220 CALL HCHAR(ROW,COL-1+I,ASC(SEG$(M$,I,1)))
230 NEXT I
Lines 210-230 graphically place one letter at a time for the length of the message on the screen. Several microcomputers distinguish between integers (whole numbers) and floating point numbers (numbers which may contain a decimal). Often the symbol % is used to designate an integer in a variable name (as in B%). This is similar to the way we use $ to designate a string variable such as S$. In TI BASIC all numbers are able to contain a decimal (they are floating point numbers). TI BASIC programmers also do not need to worry about single precision and double precision designations.
A function you may see in other listings is FIX. FIX(N) is the same as INT(N) which returns the integer or whole number portion of a number N.
Two symbols you may see in listings are "tokens" or abbreviations: ? (question mark) is the abbreviation for the word PRINT, and' (apostrophe) is the abbreviation for REM or REMARK.
You may also need to adjust DATA and READ statements because TI BASIC contains the command RESTORE. Other computers might not have it. RESTORE means to start at the beginning of the data list with the next READ statement. You may also RESTORE data beginning with a certain line number. RESTORE 430 indicates that the next READ statement should start with the first data item in line 430.