TI-BASIC
From Wikipedia, the free 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 very 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.
[edit] 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).
[edit] Hello world
The following program, when run, will clear the standard output and print the phrase "HELLO WORLD!"
:
Z80 Series
PROGRAM:HELLOWLD :ClrHome :Disp "HELLO, WORLD!"
68000 Series
hellowld() :Prgm :ClrIO :Disp "Hello, World!" :EndPrgm
[edit] 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 A's contents:
PROGRAM:ADD1TO20 :DelVar A :For(X,1,20 :A+X→A :End :Disp A
Note that the following routine takes less RAM, and uses one fewer variable, for the same task (storing the sum of the integers from 1 to 20 inclusive to variable A and displaying the result:
PROGRAM:ADD1TO20 :Disp sum(seq(A,A,1,20→A
[edit] 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 :Repeat Ans :getKey :End :Disp Ans
[edit] Bouncing 'X'
This code will simply make an 'X' bounce around the screen. You can find 'less than', 'bigger than' and ' or ' under 2nd→Test
PROGRAM:BOUNCE :ClrHome :1→X:1→Y :1→S:1→T :While 1 :ClrHome :Output(Y,X,"X :X+S→X :Y+T→Y :If X<2 or X>15 :-S→S :If Y<2 or Y>7 :-T→T :End
[edit] Number guessing game
This program is a simple number guessing game.
PROGRAM:GUESSNUM :ClrHome :DelVar T :randInt(1,50→Y :Disp "START GUESSING! :Lbl 0 :Prompt X :If 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 :End
[edit] Another Simple Number Guessing Game
This program picks a number between 1 and 10, and makes you guess it.
PROGRAM:PICKNUMB :ClrHome :randInt(1,10)→A :Disp "PICK A NUMBER" :Input "BETWEEN 1 AND 10",B :If B=A :Then :Disp "YOU WIN!" :Else :Disp "HAHA! YOU LOSE! :Disp "NUMBER WAS",a
[edit] 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 pressed. 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 :0→X:0→Y :While 1 :If K>0: Pt-Off(X,Y :getKey→K :X+(Ans=26)-(Ans=24→X :Y+(K=25)-(K=34→Y :Pt-On(X,Y :End
[edit] Advanced Menu, Using GetKey
A more advanced example of a menu that can be used instead of the "Menu(" button.
PROGRAM:MENU :ClrHome :Output(1,1,"==== MENU ==== :Output(2,1,"CHOICE 1 :Output(3,1,"CHOICE 2 :Output(4,1,"CHOICE 3 :Output(5,1,"CHOICE 4 :Output(6,1,"CHOICE 5 :Output(7,1,"QUIT :Output(8,1,"==== MENU ==== :2→Y :Output(Y,4,"[ :Output(Y,13,"] :Lbl GK :DelVar K :Repeat K=25 or K=34 or K=45 or K=105 :getKey→K :End :If K=105:Goto DC :If K=45:Then :7->Y :Goto DC :End :Output(Y,4," " :Output(Y,13," " :Y+(K=34)-(K=25->Y :If Y>7:2->Y :If Y<2:7->Y :Output(Y,4,"[ :Output(Y,13,"] :Goto GK :Lbl DC :ClrHome :If Y=6:Then :Lbl QT :ClrHome :DelVar YDelVar K :Return :End :Disp "YOU PICKED","CHOICE :Pause Y :Goto QT
[edit] 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.
[edit] 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" much like a Windows popup box. It asks for the x and y coordinates for two points, and gives the distance and midpoint.
[edit] Mathematical Finance
It is possible to use the symbolic computations capabilities of TI-calculators to implement Mathematical Finance Programming in TI-Basic.
[edit] See also
[edit] External links
- http://education.ti.com/ Texas Instruments official website
- www.ticalc.org: TI Calculator Community and Downloadable Program Source
- BASIC Guru Online
- The TI-Tip List by Doug Burkett