Difference between revisions of "83Plus:Basic:Tricks"

From WikiTI
Jump to: navigation, search
(added a few more routines... will come back later to separate it into different pages)
m (Pause affects the Ans variable, another case when Pause after Disp is valid.)
 
(21 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{wikify}}
 
 
[[Category:83Plus:Basic|Tricks]]
 
[[Category:83Plus:Basic|Tricks]]
 +
[[Category:83Plus:Basic:Routines|Tricks]]
  
 
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: (Please use common sense in deciding whether a "-" is a minus or a negative sign. When the use is un-common-sense-ical or possibly confusing, it will be specified.)
 
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: (Please use common sense in deciding whether a "-" is a minus or a negative sign. When the use is un-common-sense-ical or possibly confusing, it will be specified.)
Line 7: Line 7:
  
 
==Memory==<!-- →≠≤≥ -->
 
==Memory==<!-- →≠≤≥ -->
===Memory Management===<!-- →≠≤≥ -->
+
===Memory Management===<!-- →≠≤≥ -->
 
;Number values:In order to be able to save memory on the calculator, it helps to have a bit of an understanding as to how the calc manages the memory, especially in regards to numbers. Because TIOS is designed for school, and so is oriented for precision, it saves all real number values as 14-digits and an exponent of 10. In other words, it saves the number &pi; as 3.1415926535898 &times; 10<sup>0</sup>, but in memory, it is something more similar (not exact) to <code>0 31415926535898</code> where the 0 tells TIOS what the exponent of 10 is that need to be applied to the 31415926535898.  However, this also applies to smaller values, such that a 13 is saved as <code>1 13000000000000</code>.  For small, rational, terminating values, it can be seen that a good deal of memory is being wasted.  I've created the following formula which, when saved to Y0 (or any other Y function, works with numbers in the same way that the sub( function works with strings:
 
;Number values:In order to be able to save memory on the calculator, it helps to have a bit of an understanding as to how the calc manages the memory, especially in regards to numbers. Because TIOS is designed for school, and so is oriented for precision, it saves all real number values as 14-digits and an exponent of 10. In other words, it saves the number &pi; as 3.1415926535898 &times; 10<sup>0</sup>, but in memory, it is something more similar (not exact) to <code>0 31415926535898</code> where the 0 tells TIOS what the exponent of 10 is that need to be applied to the 31415926535898.  However, this also applies to smaller values, such that a 13 is saved as <code>1 13000000000000</code>.  For small, rational, terminating values, it can be seen that a good deal of memory is being wasted.  I've created the following formula which, when saved to Y0 (or any other Y function, works with numbers in the same way that the sub( function works with strings:
  int(fPart(X/10^(int(2-A+log(X))))*10^(F))
+
  int(10^(F)fPart(X/10^(int(2-A+log(X
 
:translated as a sub( statement, it would be the equivalent of sub(X,A,F) regardless of the position of the decimal point in X.  If you don't understand what the formula is doing, it's dividing X by 10<sup>value</sup> so that the digit we're looking for (identified in A) is "moved" to be the first digit to the right of the decimal point, then ignores anything to the left or the decimal piont, then multiplies the value left over by 10<sup>F</sup> and ignores anything to the right of the decimal point, thus providing the sub(X,A,F).
 
:translated as a sub( statement, it would be the equivalent of sub(X,A,F) regardless of the position of the decimal point in X.  If you don't understand what the formula is doing, it's dividing X by 10<sup>value</sup> so that the digit we're looking for (identified in A) is "moved" to be the first digit to the right of the decimal point, then ignores anything to the left or the decimal piont, then multiplies the value left over by 10<sup>F</sup> and ignores anything to the right of the decimal point, thus providing the sub(X,A,F).
 
;use more than 27 vars:If you need to use more than the 27 vars TIOS gives you with A-Z, other than using lists, matrices, strings, or Ans, you can go look in the [VARS] menu under Window.  Of these, being that the calculator is usually in function mode, you can use any of the window vars under T/theta (which are for parametric mode) and/or under U/V/W (which are for polar).  It is recommended that you prefer those under U/V/W because this mode is usually never used, whereas parametric has been used on more than one occasion by programs, such as for drawing X in terms of Y. You can also go into the Finance menu under [APPS] and go to Vars, but IIRC, some are read-only vars, so you will need to experiment to see if a var is accessible or not, but it is certain, that at least the '''N''' can be used problemlessly in your programs. You can also use the ''n'', which is found by going to the catalog and pressing "N" as a variable, as well as the sequential functions u,v, and w.
 
;use more than 27 vars:If you need to use more than the 27 vars TIOS gives you with A-Z, other than using lists, matrices, strings, or Ans, you can go look in the [VARS] menu under Window.  Of these, being that the calculator is usually in function mode, you can use any of the window vars under T/theta (which are for parametric mode) and/or under U/V/W (which are for polar).  It is recommended that you prefer those under U/V/W because this mode is usually never used, whereas parametric has been used on more than one occasion by programs, such as for drawing X in terms of Y. You can also go into the Finance menu under [APPS] and go to Vars, but IIRC, some are read-only vars, so you will need to experiment to see if a var is accessible or not, but it is certain, that at least the '''N''' can be used problemlessly in your programs. You can also use the ''n'', which is found by going to the catalog and pressing "N" as a variable, as well as the sequential functions u,v, and w.
Line 15: Line 15:
  
 
===Memory Saving===<!-- →≠≤≥ -->
 
===Memory Saving===<!-- →≠≤≥ -->
;Never write out closing ''')''' parentheses at the end of a line: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, or when it encounters the store arrow.
+
;Never write out closing ''')''' or '''"''' (parentheses/quotes) at the end of a line: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, or when it encounters the store arrow.
;Never write out closing '''"''' quotes at the end of a line: 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, or when it encounters the store arrow.
+
;Never use a Pause right after a Disp, unless the Pause has text or you need to preserve Ans: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
;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"
 
  :Pause "TEXT"
 
:then it writes the text to the homescreen first, and then waits for the user to press [ENTER].
 
:then it writes the text to the homescreen first, and then waits for the user to press [ENTER].
Line 35: Line 34:
 
  Archive XArchive YArchive ZUnarchive ZUnarchive TDelVar ADelVar BFor(X,0,1
 
  Archive XArchive YArchive ZUnarchive ZUnarchive TDelVar ADelVar BFor(X,0,1
 
:this doesn't work with Lists, or labels, and presumably neither with the prgm token.
 
:this doesn't work with Lists, or labels, and presumably neither with the prgm token.
 +
 +
===Avoiding Memory Leaks===
 +
Memory leaks can pose a serious problem to TI-Basic developers writing more advanced programs.  Specifically, TI-Basic doesn't allow you to effectively Goto out of a loop.  Take the following code:
 +
<nowiki>While A?3
 +
If A=7
 +
Then
 +
0?A
 +
Goto B1
 +
End
 +
A+1?A
 +
End
 +
Lbl B1
 +
Disp "Done."</nowiki>
 +
This code does work as intended, but it causes a memory leak.  When the "Goto B1" line executes, the loop technically never ends, which causes a memory leak.  What you can do instead is place more End statements after your loop.  Here's the alternate code, with no memory leak:
 +
<nowiki>While A?3
 +
If A=7
 +
Then
 +
0?A
 +
Goto B1
 +
End
 +
A+1?A
 +
End
 +
Goto C1
 +
Lbl B1
 +
End
 +
End
 +
Lbl C1
 +
Disp "Done."</nowiki>
 +
This code executes an extra End statement for every skipped one if you jump out of the loop early.  When the loop exits normally, extra End statements are skipped.  This can be extended several times, such that you execute as many end statements as you originally skipped.  Here's a more complex example (Note that in the example, ";" denotes a comment, which is not valid TI-Basic syntax):
 +
<nowiki>While 1
 +
If 1
 +
Then
 +
Goto E2 ; End twice
 +
End
 +
 +
If 1
 +
Then
 +
If 1
 +
Then
 +
Goto E3 ; End 3 times
 +
End
 +
End
 +
 +
End ; Normal loop exit
 +
Goto E0 ; End none
 +
Lbl E3
 +
End
 +
Lbl E2
 +
End
 +
End
 +
Lbl E0
 +
Disp "Done."</nowiki>
 +
This code executes all the remaining End statements that would have been missed upon exiting the loop.
  
 
==Speed Boosters==<!-- →≠≤≥ -->
 
==Speed Boosters==<!-- →≠≤≥ -->
Line 92: Line 144:
 
:Using this technique, small graphics such as these can be drawn much faster than if we tried to draw them using lines and/or pixels and/or points.
 
:Using this technique, small graphics such as these can be drawn much faster than if we tried to draw them using lines and/or pixels and/or points.
 
;Simplify your Life:Save a GDB at the beginning of your program. "Graph Databases (GDBs) are useful storage centers of graph informaiton. This means that it saves not only the Zoom settings, but also Axes on/off, coord on/off, and the Y= equations. Using these can reduce the size of your program." (quoted from http://web.archive.org/web/20021020121625/www.kevtiva.com/calc/gdb.htm) Turn off the axes with AxesOff, turn off functions with FuncOff, and 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 so that you don't mess up the gamer's graph settings.  <!--You might have to turn the axes back on. but at 5:22 in the morning, I don't remember anymore... -->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
 
;Simplify your Life:Save a GDB at the beginning of your program. "Graph Databases (GDBs) are useful storage centers of graph informaiton. This means that it saves not only the Zoom settings, but also Axes on/off, coord on/off, and the Y= equations. Using these can reduce the size of your program." (quoted from http://web.archive.org/web/20021020121625/www.kevtiva.com/calc/gdb.htm) Turn off the axes with AxesOff, turn off functions with FuncOff, and 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 so that you don't mess up the gamer's graph settings.  <!--You might have to turn the axes back on. but at 5:22 in the morning, I don't remember anymore... -->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
;Get rid of the "Done":use the following right before your program exits to avoid having the calc display the "Done" text on the homescreen
+
;Convert Pts into Pxls:If you have not used the previous tip, it is important to know that the coordinates you give to any Pt command will be different from those you give to any Pxl command (if you want to use, say Pt-On/Pt-Off, with Pxl-Test).  The reason for this is that Pt commands take your window settings into account, while Pxl commands don't.Here is one set of formulas you can use to convert your Pt coordinates into Pxl coordinates:
 +
<nowiki>round(94X/(Xmax-Xmin)+47,0)->A
 +
round(-62Y/(Ymax-Ymin)+31,0)->B
 +
-RobbieMc</nowiki>
 +
:With this formula, the Pxl command is Pxl-XX(B,A), where XX is On, Off, Test, or Change. Another formula can be found in the Ans your answers section of Cool Hacks. http://meebo.com/skin/default/img/emoticons/smile.gif
 +
;Get rid of the "Done":Use the following right before your program exits to avoid having the calc display the "Done" text on the homescreen
 
  <nowiki>:ClrHome
 
  <nowiki>:ClrHome
:Output(1,1,"</nowiki>
+
:"</nowiki>
 +
:When used immediately before a "Stop" token, "Done" is still displayed after the program quits. This also moves the cursor down one line before quitting. To avoid both these, use <code>Output(1,1,"</code>.
 
;Get rid of the RunIndic:On the homescreen, do <code>Output(1,16,"_</code> in the loop that is displaying the run indicator. On the graph screen, do <code>Text(-1,0,90,"_</code> in the loop that is displaying the run indicator. (_ in this context refers to the space character.)
 
;Get rid of the RunIndic:On the homescreen, do <code>Output(1,16,"_</code> in the loop that is displaying the run indicator. On the graph screen, do <code>Text(-1,0,90,"_</code> in the loop that is displaying the run indicator. (_ in this context refers to the space character.)
  
Line 125: Line 183:
 
23:End</nowiki>
 
23:End</nowiki>
 
: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( statement 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, and if the condition is not met, it skips to Line 10; if that condition is not met, it skips to Line 12; if that condition is not met, it skips to Line 14; if that condition is not met, it skips to Line 16; if that 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 its life. Thus can you build in looping conditions into your programs without having to use Lbl or Goto statements. http://www.meebo.com//skin/default/img/emoticons/cool.gif
 
: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( statement 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, and if the condition is not met, it skips to Line 10; if that condition is not met, it skips to Line 12; if that condition is not met, it skips to Line 14; if that condition is not met, it skips to Line 16; if that 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 its life. Thus can you build in looping conditions into your programs without having to use Lbl or Goto statements. http://www.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 there are other things you can do with the Ans var (as long as you're careful not to change the value in Ans unintentionally).  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 esamples of using Ans in a program at a later date.
+
;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 there are other things you can do with the Ans var (as long as you're careful not to change the value in Ans unintentionally).  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. Therefore, you can do something like this:
 +
<nowiki>(50% tested)
 +
(to convert from Pt coordinates to Pxl coordinates)
 +
A is the X-coord
 +
B is the Y-coord
 +
 
 +
{63/(Ymax-Ymin),95/(Xmax-Xmin
 +
 
 +
;{pixels per Y, pixels per X
 +
 
 +
{abs(Ymax-B),abs(Xmin+A),int(Ans(2))+(0=fPart(Ans(2))),int(Ans(1))+(0=fPart(Ans(1
 +
 
 +
;{Y,X,int ppY,int ppX
 +
;Ans(1)Ans(3) is now the Pxl-Y
 +
;Ans(2)Ans(4) is now the Pxl-X</nowiki>
 
;Autodetect Degree or Radian Mode:Robert Maresh states that this is one thing he learned from  James Matthew's Asmguru.hlp
 
;Autodetect Degree or Radian Mode:Robert Maresh states that this is one thing he learned from  James Matthew's Asmguru.hlp
 
  <nowiki>:If 0<cos(9)
 
  <nowiki>:If 0<cos(9)
Line 133: Line 205:
 
:Disp "Radian Mode
 
:Disp "Radian Mode
 
:End</nowiki>
 
:End</nowiki>
 +
Similarly, you can also use sin(&pi;), which only returns 0 if in radian mode, to do something like
 +
<nowiki>:If sin(&pi;
 +
:Then
 +
:Disp "Degree Mode
 +
:Else
 +
:Disp "Radian Mode
 +
:End</nowiki>
 +
;Ans as a list:It is possible to use the Ans variable as a list in replacement to using any of the real or complex variables.  The trick is knowing how to use the augment( and the seq( commands so that you won't be destroying Ans and inadvertedly converting it into something other than a list. '''NOTE: '''extensive usage of this, as in the case of replacing all variables from a program can/will slow down the program, as well as use up more memory than if you just used variables. A shellsort program using variables A,B,C,D,L,Z, and &theta; used up 143 bytes, while its variable-free counterpart used up 645 bytes and was significantly slower. However, this doesn't mean the technique is completely useless.
 +
:*augment({1,2},{3,4}  will return {1,2,3,4}... it can only augment 2 lists at a time, so repeated usage will be necessary to maintain the Ans variable as a list
 +
:*seq(formula,tempvar,start,end will return a list of what the formula evaluats to when the tempvar is (start-to-finish).  I think an example is necessary: seq(X^2,X,1,5 will For(X,1,5) evaluate X^2 and store that value to a list, thus producing {1,4,9,16,25}, without changing the value of X ;)
 +
:We can therefore use seq(Ans(X),X,start,end as a list-equivalent to sub(String,start,len... using a sequence of augment( and seq( commands, we can therefore store values into specific Ans-list elements.
 +
<nowiki>{0,0,0,0,0,0,0,0}:augment(seq(Ans(X),X,1,3),augment({2,3},seq(Ans(X),X,6,8</nowiki>
 +
:That returns the list {0,0,0,2,3,0,0,0}.
 
;Autodetect if the Calculator is an 82 or 83/+:Robert Maresh states that  
 
;Autodetect if the Calculator is an 82 or 83/+:Robert Maresh states that  
 
  <nowiki>abs -1+1 would return 2 on a TI-82
 
  <nowiki>abs -1+1 would return 2 on a TI-82
 
abs(-1+1 would return 0 on a TI-83 (this is because the abs is followed by a parenthese)</nowiki>
 
abs(-1+1 would return 0 on a TI-83 (this is because the abs is followed by a parenthese)</nowiki>
and that one can therefore use the following to display whether the calc is an 82 or 83
+
:and that one can therefore use the following to display whether the calc is an 82 or 83
 
  Text(0,0,"YOU HAVE A TI-",83-.5(abs -1+1
 
  Text(0,0,"YOU HAVE A TI-",83-.5(abs -1+1
 
==ExecAsm==<!-- →≠≤≥ -->
 
This section is organized such that first comes the "name" of the routine, then the sequence of hexadecimal codes, and then (usually) comes the asm instructions that were used to get the sequence of hexadecimal codes, in case anyone wishes to attempt to edit anything in the routine. In order to use these, you can enter these as a string argument in Omnicalc's ExecAsm( function, or you can create a new program, put AsmPrgm (found in the Catalog) as the first instruction, type the hexadecimal code sequence, and then (VERY IMPORTANT) add C9 to the end of the hex code sequence. If you do this, you will have to do Asm(prgmYOURPRGM in order to run your program, replacing YOURPRGM with whatever you named the program in which you entered this sequence. You should also note that any routine ending with "BCALL _StoAns" will be saving something to the Ans variable.
 
::FloppusMaximus wrote: Looks like the ExecAsm function *always* returns 1?! Probably the best thing to do in this case is change the StoAns to a StoX (EFBF4A → EFD14A) and read the result from X rather than Ans.If you really wanted to use Ans, you could also just reset the numOP1 flag yourself (FDCB0786).
 
:::The code to reset the numOP1 flag should be put at the end of the sequence when using Omnicalc, if you still wish to use Ans. These changes can be ignored if putting the hexcode sequences in a separate program.
 
::'''Warning'''
 
:::You should ensure that you type in these hexcode sequences as ACCURATELY as possible as the alteration of a SINGLE character CAN and most likely WILL cause the calculator to CRASH. Use these at your own risk.
 
===Screen===<!-- →≠≤≥ -->
 
;Increase the Contrast:2147847EC604FE28D077C6D8CD0B00D310
 
<nowiki>ld hl,contrast ; 21478477C6D8CD0B00D310
 
ld a,(hl)      ; 7E
 
add a,4        ; C604 (or whatever)
 
cp 28h        ; FE28
 
ret nc        ; D0
 
ld (hl),a      ; 77
 
add a,D8h      ; C6D8
 
call 0bh      ; CD0B00
 
out (10h),a    ; D310
 
-FloppusMaximus</nowiki>
 
;Decrease the Contrast:2147847ED604D877C6D8CD0B00D310
 
<nowiki>ld hl,contrast ; 214784
 
ld a,(hl)      ; 7E
 
sub 4          ; D604 (or whatever)
 
ret c          ; D8
 
ld (hl),a      ; 77
 
add a,D8h      ; C6D8
 
call 0bh      ; CD0B00
 
out (10h),a    ; D310
 
-FloppusMaximus</nowiki>
 
;Shift screen right (no LCD update):2140930E40060CB7CB1E2310FB0D20F5
 
:To update the LCD, add EF6A48 at the end (before adding C9 if in a prgm)
 
<nowiki> 21 40 93 -  ld hl,gbuf
 
0E 40 -  -  ld c,64
 
-  -  -  -  loop:
 
06 0C -  -  ld b,12
 
B7 -  -  -  or a
 
-  -  -  -  smalloop:
 
CB 1E -  -  rr (hl)
 
23 -  -  -  inc hl
 
10 FB -  -  djnz smalloop
 
0D -  -  -  dec c
 
20 F5 -  -  jr nz,loop
 
-Jim e</nowiki>
 
;Shift screen left (no LCD update):213F960E40060CB7CB162B10FB0D20F5
 
:To update the LCD, add EF6A48 at the end (before adding C9 if in a prgm)
 
<nowiki> 21 3F 96 -  ld hl,gbuf+767
 
0E 40 -  -  ld c,64
 
-  -  -  -  loop2:
 
06 0C -  -  ld b,12
 
B7 -  -  -  or a
 
-  -  -  -  smalloop2:
 
CB 16 -  -  rl (hl)
 
2B -  -  -  dec hl
 
10 FB -  -  djnz smalloop2
 
0D -  -  -  dec c
 
20 F5 -  -  jr nz,loop2
 
-Jim e</nowiki>
 
;Shift screen up one line (no LCD update):214C9311409301F402EDB0EB010C00EF304C
 
:To update the LCD, add EF6A48 at the end (before adding C9 if in a prgm)
 
<nowiki>ld hl,plotSScreen+12  ; 214C93
 
ld de,plotSScreen    ; 114093
 
ld bc,756            ; 01F402
 
ldir                  ; EDB0
 
ex de,hl              ; EB
 
ld bc,12              ; 010C00
 
BCALL _MemClear      ; EF304C
 
-FloppusMaximus</nowiki>
 
;Shift screen down one line (no LCD update):213396113F9601F402EDB823010C00EF304C
 
:To update the LCD, add EF6A48 at the end (before adding C9 if in a prgm)
 
<nowiki>ld hl,plotSScreen+767-12  ; 213396
 
ld de,plotSScreen+767    ; 113F96
 
ld bc,756                ; 01F402
 
lddr                      ; EDB8
 
inc hl                    ; 23
 
ld bc,12                  ; 010C00
 
BCALL _MemClear          ; EF304C
 
-FloppusMaximus</nowiki>
 
;Set temporary contrast to X (0 <=X <= 63):EFE04AEFEF4AC6C0CD0B00D310
 
<nowiki>BCALL _RclX    ; EFE04A
 
BCALL _ConvOP1 ; EFEF4A
 
add a,C0h      ; C6C0
 
call 0bh      ; CD0B00
 
out (10h),a    ; D310</nowiki>
 
;Black graph (no LCD update):2140930100033EFFEF334C
 
:To update the LCD, add EF6A48 at the end (before adding C9 if in a prgm)
 
<nowiki> ld hl,plotsscreen ;214093
 
ld bc,768        ;010003
 
ld a,$FF          ;3EFF
 
bcall(_memset)    ;EF334C
 
-RobbieMc</nowiki>
 
;Invert graph (no LCD update):2140930100037E2F77230B78B120F7
 
:To update the LCD, add EF6A48 at the end (before adding C9 if in a prgm)
 
<nowiki> ld hl,plotsscreen ;214093
 
ld bc,768        ;010003
 
ld a,(hl)        ;7E
 
cpl              ;2F
 
ld (hl),a        ;77
 
inc hl            ;23
 
dec bc            ;0B
 
ld a,b            ;78
 
or c              ;B1
 
jr nz,$-7        ;20F7
 
-RobbieMc</nowiki>
 
;Enabling Fast Circles:FDCB3CE6
 
set useFastCirc, (iy+plotFlag3) FD CB 3C E6
 
-Michael Cimino
 
;Toggle the Screen On and Off:DB10CB6F20053E03D310C93E02D310
 
<nowiki> in a,(10h)    ; DB10
 
bit 5,a        ; CB6F
 
jr nz,turnoff  ; 2005
 
ld a,3        ; 3E03
 
out (10h),a    ; D310
 
ret            ; C9
 
turnoff:
 
ld a,2        ; 3E02
 
out (10h),a    ; D310
 
-FloppusMaximus</nowiki>
 
;Clear Graph (no LCD update):EFD04B
 
:To update the LCD, add EF6A48 at the end (before adding C9 if in a prgm)
 
<nowiki>B_CALL _GrBufClr ; EFD04B</nowiki>
 
;Invert HomeScreen:FDCB05DEEF7945FDCB059E
 
<nowiki>set textInverse,(iy+textFlags)  ; FDCB05DE
 
B_CALL _RstrShadow              ; EF7945
 
res textInverse,(iy+textFlags)  ; FDCB059E
 
-FloppusMaximus</nowiki>
 
;Invert Screen (Any):F33E20CD0B00D3103E80CD0B00D310060EC5063F2E7FCD0B00DB112C
 
:CD0B00DB112FF57DCD0B00D311CD0B00DB11F1CD0B00D31110E5C110D8
 
:Note: This should be one LONG sequence. The line-break is inserted for readability only.
 
<nowiki>    DI                              ;F3
 
    LD A, $20                        ;3E20
 
    CALL $B                          ;CD0B00
 
    OUT ($10), A                    ;D310
 
    LD A, $80                        ;3E80
 
    CALL $B                          ;CD0B00
 
    OUT ($10), A                    ;D310
 
    LD B, 14                        ;060E
 
loop:
 
    PUSH BC                          ;C5
 
    LD B, 63                        ;063F
 
    LD L, $7F                        ;2E7F
 
    CALL $B                          ;CD0B00
 
    IN A, ($11)    ;dummy read      DB11
 
loop1:
 
    INC L          ;row counter      2C
 
    CALL $B                          ;CD0B00
 
    IN A, ($11)                      ;DB11
 
    CPL                              ;2F
 
    PUSH AF                          ;F5
 
    LD A, L        ;reset the row    7D
 
    CALL $B                          ;CD0B00
 
    OUT ($11), A                    ;D311
 
    CALL $B                          ;CD0B00
 
    IN A, ($11)    ;dummy read...    DB11
 
    POP AF                          ;F1
 
    CALL $B                          ;CD0B00
 
    OUT ($11), A                    ;D311
 
    DJNZ loop1                      ;10E5
 
    POP BC                          ;C1
 
    DJNZ loop                        ;10D8
 
-Taricorp</nowiki>
 
 
===Other===<!-- →≠≤≥ -->
 
;Enable Alpha Lock:FDCB12E6FDCB12F6
 
<nowiki>set shiftAlpha,(iy+shiftFlags) ; FDCB12E6
 
set shiftALock,(iy+shiftFlags) ; FDCB12F6
 
-FloppusMaximus</nowiki>
 
;Get Amount of Free Ram:EFE542EF9247EF5641EFBF4A
 
<nowiki>BCALL _MemChk    ; EFE542
 
BCALL _SetXXXXOP2 ; EF9247
 
BCALL _OP2ToOP1  ; EF5641
 
BCALL _StoAns    ; EFBF4A
 
-FloppusMaximus</nowiki>
 
;Battery state (0 = good, 1 = bad):EFBF41EFB3502803EF9B41EFBF4A
 
<nowiki>  BCALL _OP1Set0      ; EFBF41
 
  BCALL _Chk_Batt_Low ; EFB350
 
  jr z,good          ; 2803
 
  BCALL _OP1Set1      ; EF9B41
 
good:
 
  BCALL _StoAns      ; EFBF4A
 
-FloppusMaximus</nowiki>
 
;Amount of Free ROM → X
 
:This routines is still being discussed in the Detached Solutions forum.  The final version will be posted here as soon as all parties have come to an agreement.
 
<!--EF1450219F8356235E2346234EC5EBEF9247EF5641210080EF9247EF8440EF6640E1EF9247F7EFBF4A
 
<nowiki>BCALL 5014h        ; EF1450
 
ld hl,839Fh        ; 219F83
 
ld d,(hl)          ; 56
 
inc hl            ; 23
 
ld e,(hl)          ; 5E
 
inc hl            ; 23
 
ld b,(hl)          ; 46
 
inc hl            ; 23
 
ld c,(hl)          ; 4E
 
push bc            ; C5
 
ex de,hl          ; EB
 
BCALL _SetXXXXOP2  ; EF9247
 
BCALL _OP2ToOP1    ; EF5641
 
ld hl,32768        ; 210080
 
BCALL _SetXXXXOP2  ; EF9247
 
BCALL _FPMult      ; EF8440
 
BCALL _Times2      ; EF6640
 
pop hl            ; E1
 
BCALL _SetXXXXOP2  ; EF9247
 
rst 30h            ; F7
 
BCALL _StoAns      ; EFBF4A
 
-FloppusMaximus</nowiki>
 
:EF1450ED4B9F83ED437884ED4BA183ED437A84219584DB02CB7F20012B36002B0607
 
:111000E5C5EFB180C1E13A8684C630772B10EDEF3E41EFD14A
 
:Note: This should be one LONG sequence. The line-break is inserted for readability only.
 
<nowiki> bcall(_arcchk)    ;EF1450
 
ld bc,($839F)    ;ED4B9F83
 
ld (op1),bc      ;ED437884
 
ld bc,($83A1)    ;ED4BA183
 
ld (op1+2),bc    ;ED437A84
 
ld hl,op3+7      ;219584
 
in a,(2)          ;DB02
 
bit 7,a          ;CB7F
 
jr nz,$+3        ;2001
 
dec hl            ;2B
 
ld (hl),0        ;3600
 
dec hl            ;2B
 
ld b,7            ;0607
 
loop:
 
ld de,10          ;111000
 
push hl          ;E5
 
push bc          ;C5
 
bcall(_div32by16) ;EFB180
 
pop bc            ;C1
 
pop hl            ;E1
 
ld a,(op2+3)      ;3A8684
 
add a,$30        ;C630
 
ld (hl),a        ;77
 
dec hl            ;2B
 
djnz loop        ;10ED
 
bcall(_op3toop1)  ;EF3E41
 
bcall(_stox)      ;EFD14A
 
-RobbieMc</nowiki>-->
 
;OS Version:EF6F4CF578EF8C47EF6742EF6742F1EF8F47F7EFBF4A
 
<nowiki>BCALL _GetBaseVer ; EF6F4C
 
push af          ; F5
 
ld a,b            ; 78
 
BCALL _SetXXOP1  ; EF8C47
 
BCALL _DecO1Exp  ; EF6742
 
BCALL _DecO1Exp  ; EF6742
 
pop af            ; F1
 
BCALL _SetXXOP2  ; EF8F47
 
rst 30h          ; F7
 
BCALL _StoAns    ; EFBF4A
 
-FloppusMaximus</nowiki>
 
;Hardware Version:EFBA80EF8C47EFBF4A
 
<nowiki>BCALL _GetHWVer  ; EFBA80
 
BCALL _SetXXOP1  ; EF8C47
 
BCALL _StoAns    ; EFBF4A
 
-FloppusMaximus</nowiki>
 
;Automatic Power Down (APD):210101224884FDCB08DEFB76
 
<nowiki>ld hl,0101h                  ; 210101
 
ld (apdSubTimer),hl          ; 224884
 
set apdRunning,(iy+apdFlags) ; FDCB08DE
 
ei                          ; FB
 
halt                        ; 76
 
-FloppusMaximus</nowiki>
 
;Turn Off the Calc:3E01D30376FDCB09A6
 
<nowiki>ld a,1                        ; 3E01
 
out (3),a                    ; D303
 
halt                          ; 76
 
res onInterrupt,(iy+onFlags)  ; FDCB09A6
 
-FloppusMaximus</nowiki>
 
;Toggling Lowercase:21148A3E08AE77
 
<nowiki>ld hl,flags+appLwrCaseFlag ; 21148A
 
ld a,8                    ; 3E08
 
xor (hl)                  ; AE
 
ld (hl),a                  ; 77
 
-FloppusMaximus</nowiki>
 
;Remove "Done":
 
<nowiki>res donePrgm,(iy+doneFlags) ; FDCB00AE
 
-FloppusMaximus</nowiki>
 
;GetKey:EF7045FB76EF4447B728F8EF5641EFD14A
 
<nowiki> B_CALL _RunIndicOff ; EF7045
 
again:
 
ei                  ; FB
 
halt                ; 76
 
B_CALL _GetK        ; EF4447
 
or a                ; B7
 
jr z,again          ; 28F8
 
B_CALL _OP2ToOP1    ; EF5641
 
B_CALL _StoX        ; EFD14A
 
-FloppusMaximus</nowiki>
 
 
 
==Miscellaneous==<!-- →≠≤≥ -->
 
==Miscellaneous==<!-- →≠≤≥ -->
 
;Have conditions built into your formulas:This will probably need a better explanation, but I have found it useful to build certain conditions into my formulas, instead of having to write all those If statements... for instance, for piece-wise graphing, you can do Y=(X^2)(X>0)+(2X)(X<1), which is the same as If X>0:Y=X^2:If X<1:Y=2X ...right now, I can't really say what you'll be saving by doing this (memory/speed/etc) because I don't remember, but I can assure you this come quite in handy because you can then have one formula solve a variety of different problems, without the hassle of dealing with a lot of If-Then statements.  For example, if you were to save that formula as Y1, then you'd only need to do Y1(number) to have it do the whole sequence of conditionals. I consider this one of the more complicated tricks to implement (because one can easily get confused as to how to build it as well as to what conditions one is looking for) but I have found it to be extremely useful.  For example, let us say you write a text editor program in BASIC that allows the user to edit Str1, as displayed on the homescreen using Output(1,1,Str1.  However, you run into a problem when Str1 is longer than the 96 chars that fit on the homescreen, so you can, using the sub( and length( functions, you could have one line of code something like
 
;Have conditions built into your formulas:This will probably need a better explanation, but I have found it useful to build certain conditions into my formulas, instead of having to write all those If statements... for instance, for piece-wise graphing, you can do Y=(X^2)(X>0)+(2X)(X<1), which is the same as If X>0:Y=X^2:If X<1:Y=2X ...right now, I can't really say what you'll be saving by doing this (memory/speed/etc) because I don't remember, but I can assure you this come quite in handy because you can then have one formula solve a variety of different problems, without the hassle of dealing with a lot of If-Then statements.  For example, if you were to save that formula as Y1, then you'd only need to do Y1(number) to have it do the whole sequence of conditionals. I consider this one of the more complicated tricks to implement (because one can easily get confused as to how to build it as well as to what conditions one is looking for) but I have found it to be extremely useful.  For example, let us say you write a text editor program in BASIC that allows the user to edit Str1, as displayed on the homescreen using Output(1,1,Str1.  However, you run into a problem when Str1 is longer than the 96 chars that fit on the homescreen, so you can, using the sub( and length( functions, you could have one line of code something like
Line 430: Line 228:
 
:Output(1,1,sub(Str1,1(Ans<96)+16frac(Ans/16)(Ans>95)+(16int(Ans/16)-80)(Ans>95),Ans+80-16int(Ans/16</nowiki>
 
:Output(1,1,sub(Str1,1(Ans<96)+16frac(Ans/16)(Ans>95)+(16int(Ans/16)-80)(Ans>95),Ans+80-16int(Ans/16</nowiki>
 
:should make the Output scroll up one line whenever the screen has been filled (untested, please verify).  What this line is saying is: give me from Str1, starting at the first byte if Str1 is smaller than 96 charachters, otherwise, calculate the beginning to be one "row" less than the total, and give me all the rest of the chars till the end.  If you could not follow that, don't worry, as that is what I meant when I said this is one of the harder tricks. If you did follow with what I was trying to do, Kudos to you!  Keep in mind that the conditions within the () can hold any of the condition-elements of If statements, i.e. and, not, or, =, ≠, >, <, ≥, ≤.  BTW, this BASIC trick  does '''NOT''' work on the 89. I tried, but there I could not get the 89 to convert a binary operation into a numerical value, the way the z80s do. (I could be wrong about this working on all z80s... I've tested it on an 82 and an 83+)
 
:should make the Output scroll up one line whenever the screen has been filled (untested, please verify).  What this line is saying is: give me from Str1, starting at the first byte if Str1 is smaller than 96 charachters, otherwise, calculate the beginning to be one "row" less than the total, and give me all the rest of the chars till the end.  If you could not follow that, don't worry, as that is what I meant when I said this is one of the harder tricks. If you did follow with what I was trying to do, Kudos to you!  Keep in mind that the conditions within the () can hold any of the condition-elements of If statements, i.e. and, not, or, =, ≠, >, <, ≥, ≤.  BTW, this BASIC trick  does '''NOT''' work on the 89. I tried, but there I could not get the 89 to convert a binary operation into a numerical value, the way the z80s do. (I could be wrong about this working on all z80s... I've tested it on an 82 and an 83+)
;See program in MirageOS
+
;See program in MirageOS:If you want to see your program in a shell, like MirageOS, you must type the following code in the first line of the program:
If you want to see your program in a shell, like MirageOS, you must type the following code in the first line of the program:
+
 
  <nowiki>::"Description</nowiki>
 
  <nowiki>::"Description</nowiki>
That will show the program name with the description you typed.
+
:That will show the program name with the description you typed.
 +
;User-friendly Error Messages:(brought to you by Kevtiva Inc.) Write an error message to the screen before calling any subroutine programs, and have the subroutines program erase the error message. This way, if the subroutine program is missing, the user will be informed about the specifics of the problem. Example:
 +
<nowiki>PROGRAM:MYSHELL
 +
:program code
 +
:Output(1,1,"ERR:prgmMYSUB MISSING
 +
:prgmMYSUB
 +
:more program code
 +
 
 +
PROGRAM:MYSUB
 +
:Output(1,1,"                    "
 +
:The rest of program MYSUB.</nowiki>
 +
 
 +
==Further Reading==<!-- →≠≤≥ -->
 +
'''The Complete TI-83 BASIC Optimization Guide, Version 2''' [http://www.ticalc.org/archives/files/fileinfo/145/14542.html]
 +
 
 +
'''TI-Basic Developer'''
 +
[http://tibasicdev.wikidot.com]
  
 
==Special Thanks==<!-- →≠≤≥ -->
 
==Special Thanks==<!-- →≠≤≥ -->
 
Special thanks to:
 
Special thanks to:
  
'''Kevtiva Inc.''' http://web.archive.org/web/20021010114152/www.kevtiva.com/calc/index.html © 2000 Kevtiva Interactive
+
'''Kevtiva Inc.''' [http://web.archive.org/web/*sr_1nr_315/http://kevtiva.com/* © 2000 Kevtiva Interactive]
 +
 
 +
'''BASIC Guru Online''' [http://bgo.netfirms.com/ Copyright © 2000-2006 BASIC Guru Online. All Rights Reserved.]
  
'''BASIC Guru Online''' http://bgo.netfirms.com/ Copyright © 2000-2006 BASIC Guru Online. All Rights Reserved.
+
'''Detached Solutions''' [http://www.detachedsolutions.com/forum/ © 2000-2006 Detached Solutions]

Latest revision as of 15:47, 13 September 2018


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: (Please use common sense in deciding whether a "-" is a minus or a negative sign. When the use is un-common-sense-ical or possibly confusing, it will be specified.)

Memory

Memory Management

Number values
In order to be able to save memory on the calculator, it helps to have a bit of an understanding as to how the calc manages the memory, especially in regards to numbers. Because TIOS is designed for school, and so is oriented for precision, it saves all real number values as 14-digits and an exponent of 10. In other words, it saves the number π as 3.1415926535898 × 100, but in memory, it is something more similar (not exact) to 0 31415926535898 where the 0 tells TIOS what the exponent of 10 is that need to be applied to the 31415926535898. However, this also applies to smaller values, such that a 13 is saved as 1 13000000000000. For small, rational, terminating values, it can be seen that a good deal of memory is being wasted. I've created the following formula which, when saved to Y0 (or any other Y function, works with numbers in the same way that the sub( function works with strings:
int(10^(F)fPart(X/10^(int(2-A+log(X
translated as a sub( statement, it would be the equivalent of sub(X,A,F) regardless of the position of the decimal point in X. If you don't understand what the formula is doing, it's dividing X by 10value so that the digit we're looking for (identified in A) is "moved" to be the first digit to the right of the decimal point, then ignores anything to the left or the decimal piont, then multiplies the value left over by 10F and ignores anything to the right of the decimal point, thus providing the sub(X,A,F).
use more than 27 vars
If you need to use more than the 27 vars TIOS gives you with A-Z, other than using lists, matrices, strings, or Ans, you can go look in the [VARS] menu under Window. Of these, being that the calculator is usually in function mode, you can use any of the window vars under T/theta (which are for parametric mode) and/or under U/V/W (which are for polar). It is recommended that you prefer those under U/V/W because this mode is usually never used, whereas parametric has been used on more than one occasion by programs, such as for drawing X in terms of Y. You can also go into the Finance menu under [APPS] and go to Vars, but IIRC, some are read-only vars, so you will need to experiment to see if a var is accessible or not, but it is certain, that at least the N can be used problemlessly in your programs. You can also use the n, which is found by going to the catalog and pressing "N" as a variable, as well as the sequential functions u,v, and w.
Debugging
One of the easiest ways to help you debug your program is through the use of the Pause command. You can use the Pause command to display the value of a var, or just to track the progress of your program. During any of the Pause commands, you can press [ON] to break the program and choose Goto so that the calc will automatically bring you to the point in your program where it was Pause-ing when you interrupted it with the [ON] key. Some common bugs include using the Y var or the X var while drawing to the screen (TIOS tends to change the value of Y and/or X when doing certain things on the graph screen, so avoid using Y or X when working on the graph screen.

Memory Saving

Never write out closing ) or " (parentheses/quotes) at the end of a line
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, or when it encounters the store arrow.
Never use a Pause right after a Disp, unless the Pause has text or you need to preserve Ans
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.
Read-Only Lists and Matrices
If you have a list or matrix that will be read-only, it behooves you to save it to a string or Y function (Y1-Y0). Instead of doing
:{1,2,3,4,5,6,7,8,9→L1
:L1(5 (to retrieve the 5th element of the list)
try doing
:"{1,2,3,4,5,6,7,8,9→Str1
:expr(Str1
:Ans(5
or
:"{1,2,3,4,5,6,7,8,9→Y1
:Y1:Ans(5
The benefit is that you are now no longer wasting the space, as explained in the Number Values. When choosing between storing your list to a string and storing it to a function, keep in mind that string values are somewhat editable, while functions are completely read-only. If you needed to edit something in the function, you'd have to convert it to a string first before editing, and then overwrite the original function. It is therefore recommended that you save your lists to strings.
No NewLine Required
The following commands do not require that you neither begin a new line, nor use a colon before the next command: Archive, Unarchive, DelVar. Example:
Archive XArchive YArchive ZUnarchive ZUnarchive TDelVar ADelVar BFor(X,0,1
this doesn't work with Lists, or labels, and presumably neither with the prgm token.

Avoiding Memory Leaks

Memory leaks can pose a serious problem to TI-Basic developers writing more advanced programs. Specifically, TI-Basic doesn't allow you to effectively Goto out of a loop. Take the following code: While A?3 If A=7 Then 0?A Goto B1 End A+1?A End Lbl B1 Disp "Done." This code does work as intended, but it causes a memory leak. When the "Goto B1" line executes, the loop technically never ends, which causes a memory leak. What you can do instead is place more End statements after your loop. Here's the alternate code, with no memory leak:

While A?3
If A=7
Then
0?A
Goto B1
End
A+1?A
End
Goto C1
Lbl B1
End
End
Lbl C1
Disp "Done."

This code executes an extra End statement for every skipped one if you jump out of the loop early. When the loop exits normally, extra End statements are skipped. This can be extended several times, such that you execute as many end statements as you originally skipped. Here's a more complex example (Note that in the example, ";" denotes a comment, which is not valid TI-Basic syntax):

While 1
If 1
Then
Goto E2 ; End twice
End

If 1
Then
If 1
Then
Goto E3 ; End 3 times
End
End

End ; Normal loop exit
Goto E0 ; End none
Lbl E3
End
Lbl E2
End
End
Lbl E0
Disp "Done."

This code executes all the remaining End statements that would have been missed upon exiting the loop.

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:
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.
Avoid storing to vars in a loop
Brandon Green writes in his BASIC Guru Online that his experiments have led to the conclusion that storing variables is a prime cause of slowdowns in TI-BASIC programs. Therefore, try to restructure your code so as to avoid using the → as much as possible.

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 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 (since 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 drawn much faster than if we tried to draw them using lines and/or pixels and/or points.
Simplify your Life
Save a GDB at the beginning of your program. "Graph Databases (GDBs) are useful storage centers of graph informaiton. This means that it saves not only the Zoom settings, but also Axes on/off, coord on/off, and the Y= equations. Using these can reduce the size of your program." (quoted from http://web.archive.org/web/20021020121625/www.kevtiva.com/calc/gdb.htm) Turn off the axes with AxesOff, turn off functions with FuncOff, and 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 so that you don't mess up the gamer's graph settings. 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
Convert Pts into Pxls
If you have not used the previous tip, it is important to know that the coordinates you give to any Pt command will be different from those you give to any Pxl command (if you want to use, say Pt-On/Pt-Off, with Pxl-Test). The reason for this is that Pt commands take your window settings into account, while Pxl commands don't.Here is one set of formulas you can use to convert your Pt coordinates into Pxl coordinates:
round(94X/(Xmax-Xmin)+47,0)->A
round(-62Y/(Ymax-Ymin)+31,0)->B
 -RobbieMc
With this formula, the Pxl command is Pxl-XX(B,A), where XX is On, Off, Test, or Change. Another formula can be found in the Ans your answers section of Cool Hacks. http://meebo.com/skin/default/img/emoticons/smile.gif
Get rid of the "Done"
Use the following right before your program exits to avoid having the calc display the "Done" text on the homescreen
:ClrHome
:"
When used immediately before a "Stop" token, "Done" is still displayed after the program quits. This also moves the cursor down one line before quitting. To avoid both these, use Output(1,1,".
Get rid of the RunIndic
On the homescreen, do Output(1,16,"_ in the loop that is displaying the run indicator. On the graph screen, do Text(-1,0,90,"_ in the loop that is displaying the run indicator. (_ in this context refers to the space character.)

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: an If followed by a single non-Then statement is interpreted as an If with no Else clause and the single statement as the equivalent of the Then clause, even if the statement is End. 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...)(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
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( statement 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, and if the condition is not met, it skips to Line 10; if that condition is not met, it skips to Line 12; if that condition is not met, it skips to Line 14; if that condition is not met, it skips to Line 16; if that 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 its life. Thus can you build in looping conditions into your programs without having to use Lbl or Goto statements. http://www.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 there are other things you can do with the Ans var (as long as you're careful not to change the value in Ans unintentionally). 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. Therefore, you can do something like this:
(50% tested)
(to convert from Pt coordinates to Pxl coordinates)
A is the X-coord
B is the Y-coord

{63/(Ymax-Ymin),95/(Xmax-Xmin

;{pixels per Y, pixels per X

{abs(Ymax-B),abs(Xmin+A),int(Ans(2))+(0=fPart(Ans(2))),int(Ans(1))+(0=fPart(Ans(1

;{Y,X,int ppY,int ppX
;Ans(1)Ans(3) is now the Pxl-Y
;Ans(2)Ans(4) is now the Pxl-X
Autodetect Degree or Radian Mode
Robert Maresh states that this is one thing he learned from James Matthew's Asmguru.hlp
:If 0<cos(9)
:Then
:Disp "Degree Mode
:Else
:Disp "Radian Mode
:End

Similarly, you can also use sin(π), which only returns 0 if in radian mode, to do something like

:If sin(π
:Then
:Disp "Degree Mode
:Else
:Disp "Radian Mode
:End
Ans as a list
It is possible to use the Ans variable as a list in replacement to using any of the real or complex variables. The trick is knowing how to use the augment( and the seq( commands so that you won't be destroying Ans and inadvertedly converting it into something other than a list. NOTE: extensive usage of this, as in the case of replacing all variables from a program can/will slow down the program, as well as use up more memory than if you just used variables. A shellsort program using variables A,B,C,D,L,Z, and θ used up 143 bytes, while its variable-free counterpart used up 645 bytes and was significantly slower. However, this doesn't mean the technique is completely useless.
  • augment({1,2},{3,4} will return {1,2,3,4}... it can only augment 2 lists at a time, so repeated usage will be necessary to maintain the Ans variable as a list
  • seq(formula,tempvar,start,end will return a list of what the formula evaluats to when the tempvar is (start-to-finish). I think an example is necessary: seq(X^2,X,1,5 will For(X,1,5) evaluate X^2 and store that value to a list, thus producing {1,4,9,16,25}, without changing the value of X ;)
We can therefore use seq(Ans(X),X,start,end as a list-equivalent to sub(String,start,len... using a sequence of augment( and seq( commands, we can therefore store values into specific Ans-list elements.
{0,0,0,0,0,0,0,0}:augment(seq(Ans(X),X,1,3),augment({2,3},seq(Ans(X),X,6,8
That returns the list {0,0,0,2,3,0,0,0}.
Autodetect if the Calculator is an 82 or 83/+
Robert Maresh states that
abs -1+1 would return 2 on a TI-82
abs(-1+1 would return 0 on a TI-83 (this is because the abs is followed by a parenthese)
and that one can therefore use the following to display whether the calc is an 82 or 83
Text(0,0,"YOU HAVE A TI-",83-.5(abs -1+1

Miscellaneous

Have conditions built into your formulas
This will probably need a better explanation, but I have found it useful to build certain conditions into my formulas, instead of having to write all those If statements... for instance, for piece-wise graphing, you can do Y=(X^2)(X>0)+(2X)(X<1), which is the same as If X>0:Y=X^2:If X<1:Y=2X ...right now, I can't really say what you'll be saving by doing this (memory/speed/etc) because I don't remember, but I can assure you this come quite in handy because you can then have one formula solve a variety of different problems, without the hassle of dealing with a lot of If-Then statements. For example, if you were to save that formula as Y1, then you'd only need to do Y1(number) to have it do the whole sequence of conditionals. I consider this one of the more complicated tricks to implement (because one can easily get confused as to how to build it as well as to what conditions one is looking for) but I have found it to be extremely useful. For example, let us say you write a text editor program in BASIC that allows the user to edit Str1, as displayed on the homescreen using Output(1,1,Str1. However, you run into a problem when Str1 is longer than the 96 chars that fit on the homescreen, so you can, using the sub( and length( functions, you could have one line of code something like
:length(Str1
:Output(1,1,sub(Str1,1(Ans<96)+16frac(Ans/16)(Ans>95)+(16int(Ans/16)-80)(Ans>95),Ans+80-16int(Ans/16
should make the Output scroll up one line whenever the screen has been filled (untested, please verify). What this line is saying is: give me from Str1, starting at the first byte if Str1 is smaller than 96 charachters, otherwise, calculate the beginning to be one "row" less than the total, and give me all the rest of the chars till the end. If you could not follow that, don't worry, as that is what I meant when I said this is one of the harder tricks. If you did follow with what I was trying to do, Kudos to you! Keep in mind that the conditions within the () can hold any of the condition-elements of If statements, i.e. and, not, or, =, ≠, >, <, ≥, ≤. BTW, this BASIC trick does NOT work on the 89. I tried, but there I could not get the 89 to convert a binary operation into a numerical value, the way the z80s do. (I could be wrong about this working on all z80s... I've tested it on an 82 and an 83+)
See program in MirageOS
If you want to see your program in a shell, like MirageOS, you must type the following code in the first line of the program:
::"Description
That will show the program name with the description you typed.
User-friendly Error Messages
(brought to you by Kevtiva Inc.) Write an error message to the screen before calling any subroutine programs, and have the subroutines program erase the error message. This way, if the subroutine program is missing, the user will be informed about the specifics of the problem. Example:
PROGRAM:MYSHELL
:program code
:Output(1,1,"ERR:prgmMYSUB MISSING
:prgmMYSUB
:more program code

PROGRAM:MYSUB
:Output(1,1,"                     "
:The rest of program MYSUB.

Further Reading

The Complete TI-83 BASIC Optimization Guide, Version 2 [1]

TI-Basic Developer [2]

Special Thanks

Special thanks to:

Kevtiva Inc. © 2000 Kevtiva Interactive

BASIC Guru Online Copyright © 2000-2006 BASIC Guru Online. All Rights Reserved.

Detached Solutions © 2000-2006 Detached Solutions