Last modified on 11 March 2014, at 05:34

TI-Basic Z80 Programming/Tips, Tricks and Optimizations

Editing on the computerEdit

  • Edit on the computer. Calculator editing of programs is 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

This section assumes you already know TI-BASIC.

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 commands. 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 needed. 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 function. 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. Ex. INPUT "What number?",A
  • Leave off ending quotes and parentheses. 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 Equations. This can be a more difficult task to one who is unfamiliar with equation manipulation learned from many algebra II 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 your self a single byte by taking away a parenthesis.
    • Find the formula. Using for loops can be messy and buggy. A formula needs to be run once while for loops can take ages to finish.
    • Use constants that are available to you. Most should think this is a given, but due to the number of people I have seen using 3.14 for pi, this point should be brought up. Constants pi, e, and i are integrated into your calculator. Don't bother graphing using i, it usually won't work unless you change the mode to ax+i, but it usually isn't a good idea.
  • Use as few variables as you can. 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.
  • Convert to assembly-language. "Asm Programs" can run much faster, but are harder to edit and thus are not suitable for short, simple programs.
  • Use Subroutines. Often, in larger programs (on the scale of 5+ KB) there are repeating portions of the program (or very close bits). You can store these functions in a different program and have the original program call up the subroutine.
  • Watch out for Stack Overflows. 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 'Goto'ing out of 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' variable. Ans can store just about anything. If you have a big statement containing, say, 191 a number of times, you can do this: '191:(big statement)' and replace all 191 occurrences by 'Ans', which only takes up one byte, where 191 uses three. Be mindful of 'Ans' changing statements though.
  • Don't run code you don't use. 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 command: Simpy "If (some code), End" Meaning, if the condition is true, end the loop.
  • Calculate positions at display. 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 to. 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(). 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, simply code :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 ([1]).
    • 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

  • Make subroutines for complex tasks
    • 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):
PRGM SQRPLS1
:PROMPT A
:A
:prgmSQUARE
:Ans
:prgmSQUARE
:Disp Ans+1
PRGM SQUARE
:Ans^2
  • 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 "Insert" on the keypad, and then "Enter." Press "Rcl," or "Recall" and then the "PRGM" button. Navigate to the name of the program containing the code you want to copy. Then, press "Enter."
  • Draw Fast Circles
    • Insert a {i} parameter 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