83Plus:Basic:Tricks
From WikiTI
Please format this article according to the guidelines and Wikification suggestions, then remove this {{Wikify}} notice from the article.
There is a whole list of tricks that one can implement, once one is comfortable programming in TI-Basic which allow for a whole onslaught of capabilities, such as using less memory or making the program run faster. There used to be a site called Kevtiva Inc. which had the largest list of TI-BASIC hacks I (Saibot84) have ever seen, including how to force a RAM reset (without the use of asm programs) but I've forgotten most of the meanest hacks. Here are some of the many tricks:
Memory Saving
- never write out closing ) parentheses at the end of a line or before the STO> arrow
- You save 1 byte. The reason for this is that TIOS closes all still-open parentheses when it encounters $3F, which is the new-line character that is input when you press enter in the Program Editor.
- never write out closing " quotes at the end of a line or before the STO> arrow
- You save 1 byte. The reason for this is that TIOS closes any still-open quotes when it encounters $3F, which is the new-line character that is input when you press enter in the Program Editor.
- never use a Pause right after a Disp, unless the Pause has text
- This is because both Disp and Pause can display text to the homescreen. Disp writes the text to the screen and then continues forward, but Pause waits for the [ENTER] key to be pressed. When Pause is used with text, such as in
:Pause "TEXT
- then it writes the text to the homescreen first, and then waits for the user to press [ENTER].
- CAPITALIZE
- For the most part, yes, it is nice to see that not EVERY character is in UPPERCASE on the calculator, but if you're looking to save space, don't use lower case letters. Each lowercase letter takes up twice as much space as the uppercase ones.
Speed Boosters
- use For( loops
- In light of research I once read online, as well as through personal research and experimentation, it is fairly simple to see that For( loops are the fastest of the looping options. To prove this, try running the following program on your calc, and see which one is the fastest: (btw, -> is the store token)
Program:LOOPTEST :Disp "FOR :For(X,1,10000 :End :Pause "FOR IS DONE :1->X :Disp "WHILE :While X<10001 :X+1->X :End :Pause "WHILE IS DONE :0->X :Disp "GOTO :Lbl XX :X+1->X :If X<10001 :Goto XX Pause "GOTO DONE
- You will notice, especially if using a stopwatch, that the For( loop, IIRC, should even run a FEW SECONDS faster than the While loop, and/or the Goto loop. Therefore, see if you can't change your loops to use For( instead. In the case of the While, change
:While X<Y
- to
:X-1->X :For(X,X,Y :X-1->X
- You most likely will be losing a few more bytes in memory, but it's very much worth it for the speed... and you've already saved much speed by doing the tricks above ;-)
- get Returns on your IfThens, Fors, etc.
- Each time you do an If-Then statement or a For( statement (among others), when the calc is "waiting" for an End statement, these conditions are taking up memory, which, after a while, slow down your BASIC programs. However, the benefit is that this is only the case, as long as your program is running... as soon as the calculator returns to the homescreen, these used memory is cleared again and you are ready to go at it again. The benefit of this is that when the calculator encounters a Return statement in a BASIC program, it "cancels" any conditions for its waiting for an End.
Graphical Touches
- use Text(-1,X,Y,value or text
- I learned this from Kevtiva Inc. Normally, you do something like this:
:Text(15,10,"MY TEXT
- following this, the text "MY TEXT" will be written on the 15th row, in the 10th column, in the small 3x5 font... however, try adding a "-1" (without the quotation marks) as the first argument...
:Text(-1,15,10,"MY TEXT
- This time, the text "MY TEXT" will be written on the 15th row in the 10th column, in the LARGE font
- write blank spaces to the graph screen when needing to erase stuff
- as long as what you are trying to erase is at least 6 pixels high, it is srongly recommeded that you use the Text( command to write blank spaces to the screen instead of creating a Line(A,B,C,D,0 loop. This is because to write a line, white or black, the calculator needs to do a lot of math to convert the X and Y coordinates into pixel coordinates, while writing text to the screen is almost at the same speed as it would take to draw a sprite to the screen in any asm program (note that for the calc, text chars are just sprites) and so writing (blank) text to clear the screen is much faster.
- try drawing graphics to the screen using text instead of lines
- With access to the lowercase letters, as well as the Catalog menu, it is possible to draw graphics to the screen using text. For example, to draw a heart for a Zelda game would be much faster if you do something like the following:
:"vQ6Qv " :For(X,1,7 :Text(10,9+X,sub(Ans,X,1 :End
- another typical example is that of "drawing" a status bar to the screen using something such as:
(P is the percent to be shaded in) (L is the length of the bar in pixels) (H is the horizontal position) (V is the vertical position, using the top-left corner of the screen as point 0,0) :Text(V,H,"( :For(X,0,PL/100 :Text(V,H+X+1,"8 :End :For(X,1,(100-P)L/100 :Text(V,H+PL/100+X,") :End
- Using this technique, small graphics such as these can be draw much faster than if we tried to draw them using lines and/or pixels and/or points
- to simplify your life, save a GDB at the beginning of your program
- Turn off the axes with AxesOff, turn off functions with FuncOff, set the Window to Xmin=0, Xmax=94, Xscl=8, Ymin=-62, Ymax=0, Yscl=8. When your program is exiting, have it reset the previous GDB. You might have to turn the axes back on. While debugging your graphics, moving your cursor on the graph screen will now tell you both the pixel coordinates, as well as the point coordinates since you have synchronized them. http://www.meebo.com/skin/default/img/emoticons/big_smile.gif
Cool Hacks
- have " and the store arrow in a string
- (IIRC, brought to you by Kevtiva Inc.) Type a " and the store arrow at the homescreen and press enter. An error message will appear, choose to Quit. Press [Y=] and go to Y1. Press [2nd] [ENTER] to paste the previous homescreen entry into Y1. Press [2nd] [MODE] to {ESC}. At the homescreen, type Equ>Str(Y1,Str1 (or whatever Str you want to store it to and press [ENTER]. You now have the " quote and the store arrow in your string. http://www.meebo.com/skin/default/img/emoticons/wink.gif
- If condition:End
- This will need a bigger explanation, but in short, you can have a conditional End statement that helps your loops run faster by only executing the End statement if needed. The reason this works is because you didn't use a Then statement. The calculator interprets the line following an If statement (as long as that following line is not a Then) as the consequence if the condition is met or not, automatically, so it skips it if the condition is not met, regardless of what that next statement is. You can therefore also insert comments into your code (which is not recommended for BASIC unless you're still debugging it) by doing If 0:Whatever. For instance:
(untested...)(<> is the not-equal-to sign)(lines are numbered) 01:For(X,0,1 02:getKey->K 03:K->X 04:If not(Ans 05:End 06:0->X 07:"- 08:If K=25 09:"LEFT 10:If K=26 11:"UP 12:If K=27 13:"RIGHT 14:If K=34 15:"DOWN 16:If K=45 17:"CLEAR 18:If Ans="- 19:End 20:Disp Ans 21:If K=45 22:1->X 23:End
- OK... here's an explanation: Lines 1-2 are normal. Line 3 resets X if there was no key, which will loop back to the For( statement, or allow the routine to continue if there WAS a key. The answer from the getKey is in Ans, so if no key was pressed, Line 5 will be executed, which loops back to Line 1 without wasting any more time. http://www.meebo.com//skin/default/img/emoticons/smile.gif ... If a key was pressed, X is reset, which will cause the routine to loop back to the For( statemtn the next time it encounters an End statement. If the condition in Line 4 is not met, it automatically skips to Line 6. The string "-" is stored in Ans. We then go into a series of checks looking for any of the arrow keys, and changing Ans to reflect that. Line 8 is executed, if the condition is not met, it skips to Line 10, if the condition is not met, it skips to Line 12, if the condition is not met, it skips to Line 14, if the condition is not met, it skips to Line 16, if the condition is not met, it skips to Line 18. Then, it checks the Ans var. If the Ans var is still unchanged, then none of the keys we are looking for have been found, so we End, which we've already said will loop back to the For( statement, essentially beginning the getKey loop once more. If Ans is different, then one of our keys was found, so we Disp the value in Ans (which is a String). We then check to see if it was the [CLEAR] key., and if it was, we set X so that the next End statement that is encountered will essentially close the loop and the routine will continue beyond this part of the code. We then End, which checks the value of X. If X is 0, it loops back to the For( statement, recommencing the whole loop, but if X is 1, it "closes" the loop, forgets about it completely, and moves on with it's life. Thus can you build in looping conditions into your programs without having to use Lbl nor Goto statements. http://www29.meebo.com/skin/default/img/emoticons/cool.gif
- Ans your answers
- the Ans var is, IMHO, the most useful yet most unsafe var on the whole calculator because it can be so many different variable types (real, complex, list, matrix, string)... One way to reduce the number of varables you're using in your program is to carefully structure your program so that it stores as much information into the Ans var as possible. One way to do this, is to setup Ans to be a list, say {3,1,4,2,5}. Doing Ans(4) is not going to multiply each value in the list by four, but rather going to give you the 4th value in the list:2. This happens because the OS will treat the Ans variable exactly the same as if it were L1 or any other list variable. I've already demonstrated that the Ans var can be used to keep track of whether one of the keys we were looking for was found or not, but other things you can do with the Ans var (as long as you're careful not to change the value in Ans. For instance, doing {Ans(2),Ans(4),Ans(1),Ans(3),Ans(5) will change Ans to be the list {1,2,3,4,5}... Note that the Ans var is changed AFTER the whole command has been executed. There will probably be more examples of using Ans in a program at a later date.