TI-Basic Z80 Programming/Tips, Tricks and Optimizations

This chapter will focus on writing good practice code, including various tips, tricks, and optimizations you can apply.

Editing on the computerEdit

Editing programs on the calculator can be extremely cumbersome; scrolling is slow, and program structure is hard to visualize. Also, calculators can lose power or freeze, which will destroy the current program and all other programs not archived. TI provides freely-downloadable "Graph Link" software, which is difficult to use, but is easier than manually programming on the calculator. It allows you to display messages in lowercase and write-protect programs. Unfortunately, the Windows version is not integrated into the TI Connect software, which is necessary to transfer your programs to the calculator.

    • Graph Link allows you to type strings with lowercase letters, which results in a better user interface.
    • You can "lock" a program, which prevents display of the code on the calculator, so you (or your friend) cannot accidentally mess up the program.
    • Fortunately, the Mac OS X version of TI Connect is much more powerful than its Windows counterpart, and allows for editing programs.

Program OptimizationEdit

As in optimization with other program languages, most TI-BASIC programs can be optimized in two ways: size optimization (makes the program take up less room on the calculator) and speed optimization (makes the program run faster). Often, improving the size of the program helps increase its speed and vice versa. Some basic tips that apply on BASIC programs follow:

Learn more commandsEdit

TI graphing calculators have many commands built in. To access them go to the catalog. On the 83+/84+ the catalog can be displayed by pressing 2ND [CATALOG]. Some commands allow you to drastically shorten and speed up your TI-BASIC programs. For example, the abs( function automatically chops of the negative sign of a number. abs(-2) will return 2. This is faster and more compact then writing out if/then statements that chop off negative signs.

Use Lbl/Goto loops only when neededEdit

Whenever you can, replace them with While, For( and other loops. This makes your code smaller, faster and a lot easier to read. Sometimes you absolutely need labels; do not be afraid to use them, but replace them whenever practical.

Use text with the Input functionEdit

You can place a single line of text after the input function using quotation marks. Follow this by a comma, then a variable. This saves you from having to use the Disp function and saves space (e.g., :INPUT "What number?",A instead of :Disp "What number?" :Input A).

Leave off ending quotes and parenthesesEdit

At the end of every line in a TI-BASIC program, the calculator does not care if you leave off your ending quote or your parenthesis. So, instead of typing :A+abs((2X+3)Y) you should type :A+abs(Y(2X+3 to save space. However, you should do this only when releasing a program (keep the original!) since it is more difficult to understand and edit. Not all commands or functions may allow this.

Simplify equationsEdit

This can be a more difficult task to one who is unfamiliar with equation manipulation learned from many algebra classes. A smaller, more simplified equation usually can run faster and be smaller in size. It is an important note that parentheses also count as a single byte.

(x + y) / z can be z-1(x + y. This is because there is a -1 function right on the calculator that takes up a single byte, so in theory, you are saving a single byte by taking away a parenthesis. The * command is not necessary, since it can replaced with nothing: A*B becomes AB and A/(B*(C+4*F)) becomes A/(B(C+4F.

It may not sound like a lot, but it adds up quickly!

Find the functionEdit

Whenever possible, use a built in function instead of using a loop. For example, instead of writing:

:0→A
:For(X,1,len(L1
:A+L1→A
:End

Write:

:sum(L1)→A

Use constants that are available to youEdit

Instead of typing out 3.14 or 2.72, use π and e, respectively. The variables have much more precision and allow you to save space by using the token instead of the value.

Use as few variables as you canEdit

Variables take up 18 bytes. If you use all 27 variables (not including available string and list spaces), this uses 486 bytes. This is sometimes more than the program itself. Simplify formulas as best you can. You can always use the Ans variable. Simply state the formula in your code without saving it into a variable. This puts the answer/result into the Ans variable itself. The Ans variable does not take up extra space because it always exists and always changes.

Use SubroutinesEdit

Often, in larger programs (on the scale of 5+ KB) there are repeating portions of the program (or very close bits). Make small programs that perform complex, repeated tasks. For example, suppose you have a program that performs a complex calculation multiple times. Instead of rewriting the complex calculation multiple times, write it once as a different program. Then, you can mention the program name in one line multiple times instead of writing out something long and complicated multiple times. The way to do this is to treat the separate program as a "black box." The "black box" must manipulate values from the Ans variable. For example, consider these paired sets of code, which square a number twice and add one to it (prgmSQUARE does the repeated task of squaring the number):

:Prompt A
:A
:prgmSQUARE
:Ans
:prgmSQUARE
:Disp Ans+1

prgmSQUARE:

:Ans^2

Watch out for stack overflowsEdit

Any use of If Then Else, While, or other statements that are ended by End causes the program to use some more memory, because it has to remember that that statement has to be closed by an End. Avoid using Goto to escape these blocks, or end them at your destination, otherwise if you do this often, you can get a memory error and you slow down your program fast.

Make good use of the 'Ans' variableEdit

Ans can store just about anything. If you have a big statement containing a number that you frequently reference, for example 100, it is possible to use Ans to save space. For example:

:Pxl-On(100,10
:Pxl-On(100,20
:Pxl-On(100,30
:Pxl-On(100,40

can be reduced to:

:100
:Pxl-On(Ans,10
:Pxl-On(Ans,20
:Pxl-On(Ans,30
:Pxl-On(Ans,40

which could be further reduced to (it is actually more efficient to use 100 since it is only needed once):

:For(I,10,40,10
:Pxl-On(100,I
:End

Having a single value on one line of code is, surprisingly to those who write in other languages, a valid statement. It stores the value into Ans.

Don't run code you don't useEdit

This is one optimization that can speed up your games tremendously. Instead of letting the program run through an entire For( / While / Repeat loop, you can end it prematurely with an If:

:For(A,1,len(L1
:If L1(A)=4
:End
:End

The above example will break the For( loop if the value is 4.

Calculate positions at displayEdit

Instead of displaying text or a glyph for a program after its X,Y coordinates have been calculated, put the formulas where you would normally put the X,Y variables. For example, instead of going through a bunch of If statements to find the string length of a number, you can use the log( operator inside your Output( or Text( commands to dynamically find its string length and display it at the correct position.

Only clear what you need toEdit

To save time for drawing graphics for games, don't use clear draw or clear home commands. The calculator takes a long time to clear the entire screen. Instead, you can output blank spaces to clear. For example, you have a symbol representing a spaceship. Instead of redrawing the whole screen when the spaceship's position changes, just draw blank spaces where its old position was and draw the new spaceship on top.

Pause calculations using For(Edit

An easy workaround to pause the current state of your program for a small amount of time would be to use a blank For( loop. For example, to pause the game when a message pops up:

:For(Ans,1,30
:End

Use this wisely, however, since it will pause all user input as well. (unless you put some code in the for loop)

Nearly always optimization requires creativity to be implemented properly and successfully.

TipsEdit

If you must program on your calculator, there are several ways to make editing easier.

  • Use CtlgHelp. Download and install the CtlgHelp app on your calculator. It requires 32 KB free space. This app is a useful syntax reference. To access help, highlight a command in a menu or the Catalog and press the + key.
  • Use a Plus Silver Edition calculator. If you can afford it, buy a "Plus Silver Edition" calculator, which is faster and has more memory.
  • Backup your programs. When you finish writing or using a program, immediately archive it (this saves the program from RAM to an internal flash disk). Programs which are archived will survive a power loss and most crashes, but those in RAM will disappear. This is only recommended if you only have to protect a single program. Use groups to backup multiple programs. You may find CalcUtil useful because it will allow you to edit and run archived programs, thus protecting everything from accidental deletion.
    • If your program saves permanent data, such as a notepad or to-do list, archive the file after reading or writing. The data file will thus be protected against power loss or crashes. However, this will significantly degrade performance, so it should not be used for large quantities of replaceable data that must be accessed quickly (such as a list of random numbers).
    • Alternatively, you can back up multiple programs in one group, which can be created in the memory menu. Groups must contain at least two objects (programs or variables).

TricksEdit

  • Copy-and-paste code. Let's say you have a program with code you don't want to rewrite in your main program. First navigate to the place in your main program where you want to insert code. Then, press 2ND [INS] 2ND [RCL] PRGM PRGM. Navigate to the name of the program containing the code you want to copy. Then, press ENTER.
  • Draw fast circles. Insert {i} as the last argument in your circle command. For example, this quickly draws a circle of radius 5 centered at (0,0): :Circle(0,0,5,{i}).



Previous: Errors
Next: Advanced Programming
Table of Contents: TI-Basic Z80 Programming