TI-BASIC

From Free net encyclopedia

This article is about the calculator programming language. For the TI 99/4A home computer programming language, see TI BASIC (TI 99/4A).

TI-BASIC is the unofficial name of a BASIC-like language built into Texas Instruments (TI)'s graphing calculators. (TI only refers to it as the calculators' "extensive programming capability".) It is the simplest way to program any TI calculator; assembly language (often referred to as "asm") can also be used, and C compilers exist for translation into assembly: TIGCC for Motorola 68000 based calculators, and Z88DK for Zilog Z80 based calculators. However, both of them are in fact cross-compilers, not allowing on-calculator programming. Although TI-BASIC is considerably slower than assembler and consequently is not much good for programming fast applications and games, it is very useful for quickly writing programs to solve math problems.

Although it is lacking in features and usability, TI-BASIC is nonetheless an important factor in the programming community. Because TI graphing calculators are required at nearly all high schools, TI-BASIC is often the first glimpse many students have into the world of programming. Learning to program in TI-BASIC is a relatively easy task, encouraging potential programmers to explore the field of computer science.

Contents

Examples

These examples are slightly TI-83-series-biased. For example, "ClrHome" would be "ClLCD" on the TI-85 and TI-86.

An entirely different command is required to clear the screen in the TI-89. Since output is generally displayed on the ProgramIO screen, the "ClrIO" command is required to clear the output screen. There exists a "ClrHome" command on the TI-89, and it performs its function - namely, clearing the Home screen. For the purpose of programs, however, this command is essentially useless (yet it is invaluable in the programming of functions).

Hello world

The following program, when run, will clear the standard output and print the words "HELLO WORLD":

Z80 Series

PROGRAM:HELLOWLD
:ClrHome
:Disp "HELLO, WORLD!"

68000 Series

hellowld()
:Prgm
:ClrIO
:Disp "Hello, World!"
:EndPrgm

Add integers and display sum

The following program will add the integers from 1 to 20 inclusive, and store the result to variable "A". It will then display the contents of A on a blank screen:

PROGRAM:ADD1TO20
:ClrHome
:0→A
:For(X,1,20)
:A+X→A
:End
:Disp A

Note that it is easier to do:

PROGRAM:ADD1TO20
:ClrHome
:Disp sum(seq(x,x,1,20))

Which finds the sum of the sequence between 1 and 20.

Await keypress, display keycode

This program will loop indefinitely, waiting for a key to be pressed and informing the user of its keycode. Note that the "On" key will stop execution of a TI-BASIC program, and therefore does not have a keycode. (Some assembly subroutines can be called from a TI-BASIC program to clear this interrupt.)

PROGRAM:KEYCODE
:ClrHome
:0→A
:While A=0
:getKey→A
:End
:Disp A

Bouncing 'X'

This code will simply make an 'X' bounce around the screen.

PROGRAM:BOUNCE
:1→X:1→H
:1→Y:1→V
:0→K
:while(K=0)
:output(H,V,"X
:if(H>15 or H<1
:-X→X
:if(V>7 or V<1
:-Y→Y
:H+X→H
:V+Y→Y
:for(i,0,99:end
:clrHome
:getKey→K
:end

Number guessing game

This program is a simple guessing game.

PROGRAM:GUESSNUM
:ClrHome
:0→T
:randInt(1,50)→Y
:Disp "START GUESSING!"
:Lbl 0
:Prompt X
:If not(X=Y):Then
:T+1→T
:If X>Y:Then
:Disp "TOO LARGE!"
:Goto 0
:Else
:Disp "TOO SMALL!"
:Goto 0
:End
:Else
:ClrHome
:Disp "CORRECT!","TRIES:"
:Output(2,8,T)
:Stop
:End

Move dot using arrow keys

A common feature of TI-BASIC game programming is programming the arrow buttons to move an object on the screen. This program will draw a dot on the screen that will move according to which arrow buttons you press. The lines up to While 1 set up the graph mode screen for use with pixel-by-pixel movement.

PROGRAM:MOVEDOT
:ClrDraw
:-47→Xmin
:47→Xmax
:-31→Ymin
:31→Ymax
:0→X:0→Y
:While 1
:Pt-Off(X,Y)
:getKey→C
:If C=24:X-1→X
:If C=25:Y+1→Y
:If C=26:X+1→X
:If C=34:Y-1→Y
:Pt-On(X,Y)
:End

However, this code is both large and slow. Attempts have been made to optimize TI-Basic. The program below does the same thing as the program above, yet is far smaller, and somewhat faster. Note that the end parentheses for commands such as "Pt-On" (point-on) are not needed; often ending quotes, parentheses, brackets, and braces can be eliminated to save space.

PROGRAM:MOVEDOT
:ZStandard
:ZInteger
:DelVar X
:DelVar Y
:While 1
:Pt-Off(X,Y
:getKey→K
:X+(Ans=26)-(Ans=24→X
:Y+(K=25)-(K=34→Y
:Pt-On(X,Y
:End

Exec: calling OS routines

In addition, some of the more advanced TI graphing calculators have something called "Exec", which gives the user some control of the 68k Motorola microprocessor by calling built-in subroutines in the calculator's operating system. However, because this gives the user full control of the microprocessing unit, a minor mistake could lead to crashes and loss of data.

Example on 68k Series Calculators: scrnoff()

:Prgm
:Exec "4E444E754E750000"
:EndPrgm

This is a simple (somewhat pointless) program that turns the calculator off from the home screen.

Dialog boxes

Here's another example that shows the 68k Calculators' capability of "dialog" boxes:

:dist2d()
:Prgm
:Local xc1,yc1,xc2,yc2,distance,midxc,midyc
:
:Dialog
:Request "X for first point",xc1
:Request "Y for first point",yc1
:Request "X for second point",xc2
:Request "Y for second point",yc2
:EndDlog
:expr(xc1)→xc1
:expr(yc1)→yc1
:expr(xc2)→xc2
:expr(yc2)→yc2
:
:√((xc2-xc1)^2+(yc2-yc1)^2)→distance
:(xc1+xc2)/2→midxc
:(yc1+yc2)/2→midyc
:
:Dialog
:Title "ANSWERS"
:Text "Distance is "&string(distance)&"."
:Text "Midpoint is ("&string(midxc)&","&string(midyc)&")."
:EndDlog
:EndPrgm

This program makes a "dialog box" which is like a windows popup box. It asks for the x and y coordinates for two points, and gives the distance and midpoint.

Mathematical Finance

It is possible to use the symbolic computations capabilities of TI-calculators to implement Mathematical Finance Programming in TI-Basic.

See also

External links

de:TI-Basic es:TI-Basic fr:TI-Basic