Basic Memory Saving Techniques For TI83 Programs
Items covered in this
journal:
Disp, Labels, Quotes, Parentheses, For and While Loops, Advanced If-Then, A+1→A, Pause Statements, getKey, Turing strings into numbers, Output, If and Then Statements, Small Letters, Resetting Vars, Repeated actions,
Stop, and Ans.
I know this is big - But its great.
Disp
Most
people usually just have a pause to stop the screen to
display something like this:
:Disp "HELLO THIS IS"
:Disp "MY FIRST PROGRAM"
While this does what you want it to do, its not the best. Do the
following to save
memory:
:Disp "HELLO THIS IS","MY FIRST PROGRAM"
It's
smaller and
looks better in edit mode
Also, sometimes the Disp
command is assumed by the calculator. For example:
:Prompt B
:Disp B+1
is the same as
:prompt B
:B+1
------------------------
Parentheses
Parentheses are needed in almost all
programs. Usually used like the following:
:Output(4,5,"HELLO THIS IS")
To save memory simply drop the closing parentheses, therefore becoming:
:Output(4,5,"HELLO THIS IS
-------------------------
Pause Statements
Pause
statements can also be used just like the Disp function- simply
place what to display
behind it the following:
Pause "HELLO
--------------------
Output
Output is very useful, but I found out a way to make it much more
memory efficient. Take the following for example. (Taken from QUADPRO before and after
optimization).
Before:
:Output(4,1,"INVALID EQUATION
:Output(5,3,"DIVIDE BY ZERO
After:
:Output(4,1,"INVALID EQUATION DIVIDE BY ZERO
The
output command will display the text it can fit on the line given to it, if their is more text it will spill over to the next line. This can be an enormous
space saver.
-----------------------
Resetting Vars.
Never use this to put
0 into a
variable, instead use Delvar
Example:
Don’t do:
:0→A
Use:
:Delvar A
Now in the program use it as if A=0!
---------------------
Stop
Don’t put it in, that simple. Have a program simply go to a label that is the second to last
line of the program. Have the last line read:
:"
This serves three things, one it saves 1 byte, (because stop is not their), the program will return to the program the may have called it (even the Ti homescreen). (This is especially useful when making a
SOS compatible program because it will exit and run SOS, same with
Aurora,
Ashell, etc…), and lastly it will clear out ans. Ans will usually take up about 7
bytes, now it will only take up 2 tops.
-----------------------
Labels
To save memory try to use only one
character labels. This saves 2 bytes, the first in the Goto statement and the second in the actual label for the Goto
---------------------
For and While loops
This is a big problem that I see in many programs. I took me a while to master this concept, but in doing so you can save large amounts of memory (100+ bytes is easy!) Take the following For loop for example:
Don't Do this:
:Input "NUMBER? ",B
:B→LNUMB(1
:Input "NUMBER? ",B
:B→LNUMB(2
:Input "NUMBER? ",B
:BLNUMB(3
Do this:
:For(A,1,5
:Input "NUMBER? ",B
:B→LNUMB(A
:End
Now, If you don’t
understand this loop, READ THE
MANNUAL!
Another type of loop is the While loop, It will continue
running until its
conditions are meet, or a
goto gets it out of the loop. For example: (this is the program GETKEY):
:While A(not equal to)105
:getkey->A
:If A≠0:Disp A
:End
This program will continue to run, displaying the getkey for all the
keys pressed until you press
enter.
--------------------
getKey
Getkey is one nice
function if you know how to use it. Each key on your
Ti-83 has a key code for it, the getkey will get the code of the key being pressed. However it does have one
drawback, it will not wait for a key to be pressed. The sample following is a way to make it do just that.
:Repeat A(≠)0
:getKey→A
:End
---------------------
If and Then statements
This is used often in almost all programs, but I to often see the long way of doing them, take the following example and its shorter
version.
:If A=1
:Then
:Goto A
:End
Or
:If A=1:Goto A
That's It! (you can only place 1 command in this form though.)
--------------------
Repeated Actions
Try to put a repeatedly used code in to a subroutine, It allows for even less programming. Instead of having a program find the intersection point of two lines in 4 parts of the program. Place that code in another program and call it from the
master program with "progNAMEHERE". It will run the subroutine and return to the line
immediately after the one it was called from!
---------------------
Using ans
This is really
cool – and it will save 2 bytes each use!
Instead of having:
:A/BD+C→E
:Disp E
Do this:
:A/BD+C
:Disp ans
When doing anything that the program does the math or text will be put into ans (to display
text put it inside quotes) and you can display this like any
other variable.
----------------------
Quotes
Parentheses are needed in almost all programs. Usually used like the following:
:Disp "HELLO THIS IS"
To save memory simply drop the closing parentheses, therefore becoming:
:Disp "HELLO THIS IS
---------------------
Advanced If-Then A+1→A
This is all about the very common
If A=2:B+1→A statements. Use = to solve the problem. If the statement is true 1 is returned otherwise it will return 0.
This is a very short and fast example of
:If Z=2:A+1→A
:A+(Z=2)→A
See how it works? If Z=2 it becomes 1, otherwise it becomes 0. So it’s a short version of the example. This is also faster than if-then
logic!
----------------------
Turing strings into numbers
So, you're working on a program to turn a string into numbers? (an
encryption program maybe?) Well read this. I've looked at a few
encryption programs before and they are usually like this:
:Input "Message? ",Str1
:For(A,1,length(Str1
:sub(Str1,A,1→Str2
:If Str2="A":1→LMESS(A
:If Str2="B":2→LMESS(A
:If Str2="C":3→LMESS(A
....
:If Str2="Z":26→LMESS(A
:End
This is so slow and
inefficient!
Check this out:
:Input "Message? ",Str1
:For(A,1,length(Str1
:sub(Str1,A,1→Str2
:instring("ABCDEFG...Z",Str2→LMESS(A
:End
Isn’t instring great? This is how it works – It takes the letter (or Number) and looks for it in the string, when it finds it it returns its position in the string! So A is still 1, B is 2 et.
-----------------------
Small Letters
Want to know where the small letters are? Look in
VARS 5 (
statistics). In that menu there are numars small letters, but they each take up two
bytes instead of one for the large ones. Use them
wisely.
Hope you read and understood it all.
Have fun programming!!