0% found this document useful (0 votes)
615 views

TI 89 Programming Advanced Guide

TI 89 programming advanced guide

Uploaded by

kalimbalo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
615 views

TI 89 Programming Advanced Guide

TI 89 programming advanced guide

Uploaded by

kalimbalo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 29

Advanced Programming Guide – TI-89, -92, and –92+ Graphing Calculators

Well, folks, you asked for it, and here it is. I’ve gotten some questions from literally all over the

world (California, Germany, and even the United Arab Emirates), and I decided that, in order to follow up

on the heels of the insanely successful basic BASIC programming guide, I would write a guide that delved

a little deeper into the world of BASIC programming.

Before I begin, let me preface my work with this: I wrote the last manual assuming the reader

threw away the manual for his calculator shortly after he got it. That’s fine – all he probably wanted out of

it was a good grade in Algebra II, anyway. Here, though, I start assuming. I’m going to assume several

things: 1.) you’ve already read the beginner’s programming guide that I wrote and want more; 2.) if you

haven’t read my earlier guidebook, you have at least done some basic-level programming on TI calculators;

and 3.) you’ve read the manual that came with the calculator. (Please note that any quotes or examples that

I take out of the TI manual will come out of the PDF version of the 89/92+ manual posted on Texas

Instrument’s website.)

Here’s another qualifier – don’t take offense if you don’t read a single “she” in here. According to

MLA English guidelines, when the gender of a noun is either unimportant or unknown, then the masculine

form is preferred. This is not sexist; I am merely saving time. Think about it: depending on how many

times the basic pronoun “he” is used instead of “she,” I stand a chance of saving a lot of time by skipping

that one little letter. Cool, huh?

Having said all that, let’s get right down to it. We’ll start off with an intro to advanced

programming.

Preface: An Overview of Advanced Programming

To most people, the original guidebook that I wrote would have sufficed. From what I said, it’s

entirely possible to write more or less compact programs that perform a variety of different uses. Armed

with a general knowledge of functions and programs, the reader is now equipped to go forth into his math

or science class and write programs to save himself time and calculation errors.

But to some, that’s not good enough.


Some people require a little more. It’s not good enough that they know how to create a function

that can calculate everything but the weight of the kitchen sink or write a program that will give the orbit of

the stars for the next three thousand years. No, some people require a little more, and it’s for those people

that this manual is written.

Questions have come up recently – how do you store variables in a function? How do you convert

between units in a program? How do you create menu-driven programs? Can I display the contents of a text

file from my program? How do I scramble strings on my calculator so that people can’t read them? All of

these are very good questions. I hope to give the answers to them in this manual.

At the end of my last project, I listed a number of frequently asked questions (FAQs) that people

had emailed me. Some questions were fairly simple to answer. Some had to send me the code they had

written and let me look through it. Some questions took more than a few minutes to answer. But in the end,

I got a glimpse at the questions that other people had. I’d like to start this manual off with a rehash of some

of those questions.

Chapter One: Previously asked questions

Questions about error messages came up. Specifically, people were asking about an error that

appeared within an If…Then…EndIf statement. Therefore, I want to spend some time looking at error

messages and how to resolve them. I mentioned debugging in my earlier manual, but I didn’t go into great

detail about fixing the error. I intend to change all that. Later.

I don’t remember whether someone actually emailed me about this or if it was just a question that

came up in my mind. The question was this: can you tell which key was pressed to get out of a dialog box?

The answer is yes. I’ll go into some more depth dealing with hidden variables and how you can set system

options by changing those variables.

One big hurdle that I overcame myself a few weeks ago was the issue of converting between units

within a program block and using the converted unit for calculations. I’ll definitely go into detail dealing

with that, but I’ll also explain alongside that all the ins and outs of string manipulation and how a few

simple commands can open your program up to new levels.


Something that TI actually has over Visual Basic for Applications 1 is the indirection feature,

signified by the pound symbol (#). I’ll get more into it later. For now, it’s enough to know that the feature is

incredibly powerful.

Something cropped up that I, along with a sharp up-and-coming college freshman I know named

Nick, have become interested in is the issue of encryption. I’ll get into how that interest came about, then

I’ll explain how you can password-protect your programs as well as turn any string into a bit of

unintelligible garbage that only you can make right.

Chapter Two: Common Error Messages

Let’s pick up where the last manual ended. You’ve just written the program that will be used to

debug the Space Shuttle and solve the world peace crisis. As you’re showing your genius to the leaders of

the world in the UN General Assembly, it suddenly hits you – you just got an error message, and you have

no idea how to fix it. Oops.

As a friend of mine would say, you just pulled a major brick. Sadly, it’s a brick that I see

happening more and more often. Young programmers eager to get their name out decide to throw caution to

the wind and post something that works on paper but not necessarily on the calculator. Allow me to use a

personal example: I programmed Vectors-89, and it went like wildfire the first few weeks. Then the emails

started coming like nothing else. “I get this error!” “This doesn’t work right!” “I lost points because of your

program! Die!!!!” Well, I didn’t actually get the third one, but if I had let it go, I probably would have

eventually. One thing I can’t stress enough is: make sure your program works before you let other people

touch it. Don’t even consider letting people on the Internet download your program before you’re

absolutely sure that there are no bugs in it. If you know of bugs and you can’t fix them, then do your

audience a favor: write a read-me file and explain known bugs. Warn them ahead of time so you don’t get

death threats in your inbox.

Probably the worst misconception that people have regarding programs on the Internet is that

anything posted works perfectly all the time. That’s about as dumb as believing that the newspaper would

1
I know – I’m really cheap. But, hey, I’m a college student, and I don’t have scads of
money laying around. If you really like this documentation, my birthday is early in May. I’ll give
you my address and you can send me a copy of VB6 for my birthday!
only print true, unbiased reports. To those that download with that expectation, I have these words: reality

check.

But you know, there seems to be that mentality among programmers, too. Just because you can

make a perfect flowchart describing how the program runs, it doesn’t necessarily follow that you wrote it

into the calculator perfectly. You need to check to make sure your program is as bug-free as possible. But,

sometimes, the error messages you get when running your program are downright cryptic. 2 Thankfully,

none of the errors that TI has are as cryptic as mentioned in the footnote, but they can be terribly confusing.

So, I’ll list several error messages, what they mean in plain simple English, and try to give a

section of code that would generate such an error message.

Error What it What it means Code example (will return How to fix it
Numbe says an error)
r
20 A test did You goofed Assuming there’s nothing in Make sure the variable being
not somewhere in the variable x: tested exists.
resolve to your If…EndIf
TRUE or statement.
FALSE.

40 Argument You supplied too If your function was defined Look at how many arguments
Error many or too few as f(x,y) and you simply the function requires and
arguments for typed in f(x), you would get supply it with that number.
the function. this error.
161 ASAP or Not good. This occurs only in the Read up on the TI website for
EXEC Contact the assembly environment, so it’s information regarding the
string too programmer. possible to get this if you use EXEC command.
long the EXEC command in a
program.
230 Dimensio Error in
n whatever
matrix/vector/lis
t operation you
just tried.
Usually when
trying to read
outside the
number of
elements

2
My personal favorite comes from MS Windows: “Error: An unknown error has occurred.”
Following that one is the Macintosh error that simply lists a memory address where the error
occurred, along with some stupid number like (–48066) that’s supposed to tell you what the error
was.
Error What it says What it means Code example (will return How to fix it
Number an error)
345 Inconsistent Units You’re trying to When you
convert between two select the units
units that can’t be from the list,
converted between choose only
in one step. ones from the
same list
(time, length,
etc.)
450 Invalid in a Fairly self- Change to a
function or current explanatory program or
expression use a Local
statement for
variable
storage in a
function.

That pretty much covers the usual errors. If you find another one that’s really useless, then let me

know and I’ll include it in the next installment of this document.

Chapter Three: Hidden Variables

TI did something very sneaky when they programmed their calculators. Several variables were

hidden from regular view. These are variables that were only intended to be modified by the calculator

itself, and of course, advanced programmers like you.

There’s a list of these variables on the PDF version of the TI-89/92 Guidebook (p. 567), but you

can’t really tell what the variables are for just by looking at the book. Most of these variables are hidden

from the average user’s eyes, but they’re easy to find. Go into the VAR-LINK screen , hit F2 (View), and

select “System Variables” from the drop down box. See? Some of them can be quite useful, especially if

you need to manipulate the graph screen inside your program. Let’s look at an example of graph axis

manipulation.

Hit [DIAMOND] [F2] to bring up the Window area. You have some settings listed (these settings

determine what you see on the graph screen).


See the variables listed (xmin, xmax, xscl, ymin, ymax, yscl, and xres)? Every single one of those

can be changed within a program to play around with the graph screen. For example – take a look at this

program and see how the graph changes.

That’s the program – now here’s the output.

Did you follow that?

Since this is a programming manual, not a graphing guidebook, I won’t go into detail about what

happened mathematically; you can look in your guidebook or the PDF version I’ve included. However, I

would like to look at how the scales were changed around within the program. (Note: it may be a good idea

to have the guidebook out right now if you’re not familiar with the ins and outs of graphing.)

On line four (I count from the very first line in the program, not the first command), I used the

ZoomStd command. This is the exact same command as you would see in the Window page under F2

(Zoom). It automatically sets all the scales. But what if you want to set them yourself?

When I was writing the Vectors program, I realized that the preset zoom settings weren’t going to

work at all. So, I had to play around and set some axis manually. I did that by first looking at whatever

graph mode I was in and determining what needed to be changed (x-axis min value, x-axis max value, etc.).

From there, it was a simple matter to change those variables by storing numbers to them as I would any
other regular value. One catch – don’t expect to delete or Local-ize those variables. With a few rare

exceptions (mostly the functions like y1-y99), you can’t delete them, nor can you trick the calculator into

deleting them for you with a Local statement.

Setting system variables can be used outside the world of graphing as well. If you’re still in the

stone ages and using tables, you can set the table start and increment values by setting the system values in

the program. Again, not too many people even use tables at all anymore, and when they do, it’s not in the

realm of a program. However, there really aren’t very many uses for setting system variables outside of

graphing.

However, there’s a very useful application for reading system variables. I’ve gotten several emails

asking me how to “trap” (determine) the key pressed at the end of the dialog box. Here’s why: just

canceling the dialog box doesn’t end the program. It used to be that I used a Try…Else…EndTry statement

after every dialog box. I still do, but not for the same reason. I use the Try block to make sure all the

variables were entered. However, if you’re sure all the variables are going to be entered, then all you need

is a few simple lines at the end of your dialog box. I’ve created this example; enter it in your calculator and

run it (note that the program is too long to fit on one screen, so it’s wrapped; ignore the identical

statements):

See? A nice, clean way to trap whether or not the user completed the dialog box. Some ideas to try

when using this feature:

 If OK wasn’t pressed, loop back to just before the dialog box (use a line label to specify
where to go)
 Inside a Loop…EndLoop structure, multiple values can be taken, with an event trap set
up to exit the loop when CANCEL is pressed.
 A clean alternative to setting up a blank I/O screen, accepting keystrokes for numbers
with the getKey() function, and finishing when ESC or ENTER is pressed.

When you think about it, you can most likely come up with other applications.
Chapter Four: Converting units in programs

I think I briefly touched on this when I last put out a programming manual; I’m going to go more

in-depth here. One of the major problems I had with the conversion feature was that it was inaccessible

inside a program or function. You don’t know how many times I wanted to convert numbers in programs or

set up traps in functions to detect what kind of unit I was inputting. The only units I could convert between

were temperature units. Anything else, I had to sit down and actually write slope-intercept equations to

transform between. And, if I wanted to go from any length unit to another, I had to set up each and every

situation (inches to centimeters, inches to meters, meters to inches, meters centimeters, centimeters to

inches…and so on). That took up truckloads of space. But, thanks to a one-line function, I can do this

easily. All it takes is a dialog box or inputted string (note: anytime I have three or more formatting tags on a

word, chances are that word has some sort of significance).

By now, I’m sure you’re all drooling to find out how I did this, because it would make your

programs so much stinking better. All right, fine, if you insist. But, because the one-line 3 function is too

long to be intelligible with the arbitrary line breaks, I’m going to write it out.

:a()
:Prgm
:Dialog
: Request “Value”,v
: Request “Starting unit _”,s
: Request “End unit _”,e
:EndDlog
:string(expr(v&“_”&s&“►_”&e))→temp
:mid(temp,1,instring((temp,“*”)-1)&“_”&e→t
:Text t
:EndPrgm

Let’s analyze what’s going on in the program. First, there’s a dialog box that amounts to this:

3
Ok, I lied. It’s actually two lines. Originally, I had the entire expression that gets stored
into temp written out both times it appears in the last line. You can feel free to do that, but for sake
of explanation, it’s easier for me to write it out.
In the first box, enter the value to be converted, without the units (remember that importance

thing?). The “Starting unit _:” input box gets the unit that you’re starting out with, and the last box, the

“End unit _:” box, gets the unit you want to convert to. Let’s do an example. I want to convert 200

centimeters to inches. Now I know that I could very easily do this by hand on the home screen, but bear

with me. I’ll get to the uses in a minute.

Hit enter. Now, you should see the result:

Here’s the nifty thing – the result is stored as a string. That means you can use the expr( function

and turn it into a variable for calculations!

OK, now let’s look at the guts of the program.

Notice that some of the lines overlap in the example screenshots.

The Dialog routine should be old hat to you by now. I’m going to break down the next line into a

C-like structure.

:string(
: expr(
: v & “_” & s & “►_” & e
: )
:)
OK, here we go. The middle line (after the expr line) takes the number you entered (stored as a

string by virtue of how it was entered) and plays around with concatenation until you’re left with a string

that, if you were to stop the program here and call it up on the home screen, would look like this:

“200_cm►_in”. Look familiar? Yeah, that’s right…it’s what you would enter on the home screen. Now

look at the lines immediately before and after that: expr(“200_cm►_in”). The expr function evaluates the

string and returns it’s numerical answer. So, for example, expr(“1+2”) would return the integer 3. The

conversion is evaluated much in the same way. So what’s the string function there for? Well, 78.740_in

suddenly becomes “78.740*_in”. Not much of a change, but it makes a world of difference. Because of the

way it displays, the next line in the program strips out the asterisk and everything following in the string,

leaving just “78.740” behind. Then, it adds on the “_in”. That’s strictly for display purposes. For

calculations, you could leave the line out and it wouldn’t make any difference. Finally, the “Text t” line

displays the result in a text box.

Now, as I mentioned before, this seems like I’m reinventing the wheel. There’s not too much point

in writing a program to convert numbers if I can do the same thing on the home screen. It’s like creating a

full-featured dialog box to add two numbers, right?

Wrong. So stinking wrong.

This has a lot of good applications, like creating something to interactively see the relationship

between miles per hour and kilometers per hour. Check out the program I’ve included named speeds.89p.

It’s a little slow, and it could use some optimization (frankly, I don’t really care; it’s not being released, and

I just came up with it on the fly), but it demonstrates the principles I was just talking about. Use the Graph-

Link program to send it to your calculator. (Yes, the file’s in the public domain, so if you’re a physics

teacher and you think the program’s wonderful, feel free to distribute it around. Just give credit to the fact

that I came up with this program.) Try interactively exploring that relationship on the home screen. It just

ain’t gonna happen.

Please note that the program is useless when it comes to temperature. I wouldn’t have to go

through all that crap about converting a number into a string, back into a number, then back into a string

again if TI had done everything the same way and set up all conversions like tmpCnv(). Oh well.
Chapter Five: String manipulation

Sooner or later, you’re going to need to manipulate strings within your program. This may include

trimming off numbers from a user’s input, breaking down a string into a list of the characters and finding

out the frequency of each letter used (I think I did this once, but I’m not sure why anyone would need to;

maybe in some kind of Cryptogram game…), and a number of other things that I don’t feel like typing out.

There are a number of functions that modify, examine, or otherwise look at strings. They are:

 string(
 expr(
 dim(
 &
 mid(
 inString(
 left(
 right(
 format(
 char(
 ord(

Let’s analyze each one.

String(expr)

This takes whatever number you supply for expr and converts it into a straight string. Look at
these examples:

Note that the format( function is a whole lot more powerful. We’ll get there in a minute.

Expr(string)

Reverse operation from the string( function. Takes a string and converts it into a number.
Examples:
Note the last two lines. The last one shows the ability of expr( to take a string with mathematical
operations and evaluate it into a number. The one above shows the ability to use a variable instead of a
string.

Dim(string)

Stands for “dimension”. Counts the number of characters (spaces and non-display characters
included) in the specified string.

string&string

Concatenation. Puts two strings together.

Mid(string,start[,count])

Probably the most useful string function there is. Mid( can extract one character or whole words.
Very useful with the inString( function. Extracts from string starting at character start and going to the end
of the string, unless count is specified. Check it out:

inString(source string,string to search for[,count])


Searches source string for string to search for, starting at count, if specified; otherwise begins
search at first character. Returns the character position of the first matching string. Can also be used for
multiple letters.

left(string[,count]) and right(string[,count])

So similar, I put them together. Returns everything to the left (or right) of and including count.
Note that while count is not listed as a vital section of the function, it is when dealing with strings. You can
get away without specifying a count, but the function will simply return the whole string. Now that I think
about it, the same thing’s true when using this function on a list. Let’s leave it at this: always specify a
count.

format(expr[,string])

Formats expr into a string based upon styles. The available styles are:
 F[n] – Fixed format. N indicates the number of decimal places.
 S[n] – Scientific format. N indicates the number of decimal places.
 E[n] – Engineering format. N is the number of decimal places after the first significant digit.
 G[n][c] – Same as fixed format, but breaks apart everything to the left of the decimal point
into clusters of three. N is again the number of decimal places, and c indicates the character
used to separate the groups of three (comma is the default).
 R[c] – can be tagged into any of the above letters (F, S, E, and G). c indicates the character to
use instead of a decimal point.

This is a really powerful function, and I didn’t really find out about it until I did this manual. With
the variety of tags, you can really go to town playing around with numbers.

Char(integer) and ord(string)


Again, these two are very close together. The char( function converts an integer into its
corresponding value, while the ord( function does the same thing in reverse – it takes a single character or
the first character in the string you specified and finds its key code value. Look on page 555 (if you’re
using the PDF version, go to page 572) and following for a list of the key codes for just about any key
stroke you can imaging, including those with modifiers (2nd or diamond keys).

I debated putting down examples, but since you’re supposed to be an advanced programmer, you

can probably do whatever you want without my help. Pretty much every one of these functions can be

nested – used in the same line.

Chapter Six: Indirection

Quite possibly one of the least-known functions, but one of the most powerful, is the indirection

feature. If you were to look around on the catalog, chances are you wouldn’t find it right off. In fact, go

look there now. Hit the [Catalog] key, then press 9 (“I”). See the one four lines down – the pound (#) key?

That’s the indirect key. Ridiculously powerful if you want to play around with other files.

Use the # symbol immediately followed by a string that is the name of a file. After that, you can

do anything you like. If the variable is a matrix or a list, you can extract elements out of it. If it’s a string,

you can play around with the string functions listed above.

How is this useful? Well, let’s look at a little file info box I created:

Here’s where this comes in handy. This is a function called isVar(). I made this to test and see if

the variable supplied actually existed. If it did, it would return the type of variable it was. If not, it returns

“NONE”. So, this function returns “true” unless the getType value is “NONE”, in which case it returns

“false”.
You know, despite the hype that you heard about this, there’s really not too much else to say about

it. This is great for the password entry programmers, because they can check to see if a file to be decoded

actually still exists. Or, they can check to see if the file where a password may be stored exists. This is

invaluable for someone making a new interface. (NO – don’t ask me to give an example. I swore off

making file managers in BASIC. I know a guy that had no life to speak of who made a pretty decent rip-off

of Windows 95 in Dos BASIC. The source code was multiple megabytes long. Try doing something like

that on the TI and you have problems. Someone can, but I’m not.)

There’s really no use for indirection in basic-level programs. (At this point, I feel it necessary to

point out that “BASIC” is a programming language and “basic” denotes something simple.) Let’s face it: if

you’re writing a program to multiply two numbers, you don’t need to access a file. If you’re making your

program global (that is, not confined to a set of variables; the user supplies the name of the variables),

indirection is a marvelous tool, one that I’m shocked TI included.

Chapter Seven: Menu-driven programs

I debated for a few minutes over where this section would go. I thought about putting the section

on encryption before this, but then I realized that I might use menus in the encryption; then I decided that

the encryption section would make a good “final exam” to see if you really understand all the concepts I

put out. So, here goes nothing…

In my experience, none but the best programmers use menus in their programs. I got to thinking,

“Why is that?” Well, to start, advanced programmers are the only ones that actually write programs large

enough to need subdivisions that menus offer. Please, please, PLEASE (remember the thing about the three

formatting tags?) don’t use menus unless your program is large enough that it needs to be broken down.

Nothing is more arrogant than showing off your skills when your skills aren’t called for. You may be a

good, even great programmer, but you don’t need a menu to add two numbers. You just don’t.
I suppose that I should start off explaining the two different types of menus. The first type is what

I’ll call a paste menu. A paste menu simply contains text entries that, when selected, are pasted to the

current text entry point on the home screen. Please note that these kinds of menus don’t have much

application in programming. Hence, we won’t spend a whole lot of time with them. However, because the

Custom Menu can be configured within a program, I’ll touch on it.

If you were to execute the Restore custom defaults command (F6, item 3 on the 89), you would

get a line of characters that, in program form, would turn out like this:

Note that the three screenshots do not represent the entire command sequence. I’ve just included a

few things that should help to clear things up.

The paste menu follows this form:

Custom
Title string-title
Item string-item
Item string-item
Title string-title
EndCustm

It can be thought of as a Custom…EndCustm block. The Title entries specify the title of the menu

tab; that is, it is the same thing as the text you see for the F1-Tools, F2-Algebra, etc. on your home screen.

The Item keywords contain the text to paste. As you can see from the third screenshot, to turn the paste

menu on from inside a program, use the CustmOn command.

That’s really all there is. Like I said, there’s not a whole lot you can do with this inside a program.

It’s great for storing things like units that you use often, but has little bearing on the programming world.

The other menu type, however, has everything to do with programming.


The second menu type is the command menu. This is different from the paste menu because the

items listed actually carry out different commands. You’ll find this listed in the manual as a toolbar, but

why confuse the issue? Let me start out by showing you a code example, then walking you through it.

The line before the start of the screen shot is the beginning of the program; the line after “Lbl d” is

the end of the program.

You’ll notice right off (or at least you should) that this is a block statement – the Toolbar…

EndTBar statement. It looks very similar to the Custom…EndCustm block, but it’s just a little different. To

begin, it’s led off and finished with a different block indicator. Also, you’ll notice that while the nested Title

and Item commands are the same, the Item keyword has a comma after it, followed by a label (I know

they’re just single letters; I can use full strings if I want). These are label indicators, similar to saying

“when this item is selected, goto that label.” Afterwards, I have the Lbl… statements. If “Clear Home” was

selected, it goes to label b. If “Show Graph” was selected, it goes to label c. Here’s a screenshot of what

happens when this is run:

Because the program is set to run a command when something is selected, it will not paste

anything to the home screen. Now, if you were to run the program as shown and select something, because

of the way the program branches, you’ll be kicked out of the program after the line is finished running.

Here’s a way around it:


Notice that after the program does what it’s supposed to do, it goes back to the beginning of the

Toolbar statement. Please note that although the Toolbar statement is the beginning of the program, get it in

your head that you need to put the label directly before the toolbar statement. If you put it at the beginning

of the program, you’ll just keep on running everything over again. I shouldn’t have to tell you that, but

some people…well, you just never know.

We still have one slight problem. Well, two, depending on how you define “problem.”

Aesthetically, it doesn’t look very good to have just the two Title bars sitting out there scrunched up. That

can be fixed, but it’s not as important a thing. Let me clear up the worse problem first. If you were to stick

this in your calculator and run it, you would find that there was no way of getting out. Suppose you had to

clear the home screen and then show the graph? It can be done by running the program twice, but that’s no

good. And, after you choose to show the graph screen, you can’t exit out. It’ll go to the graph screen, and

then you’re stuck. Well, there’s one little thing that I haven’t shown you yet, and I think now is a good time

to pull the coves off it. Remember how I said that the Item statement could have a label attached to it? Well,

so can the Title statement. Check this out:

Now, we have a third choice – Exit. Notice that there’s no drop down arrow on it. It’s just a

command menu. Press F3, and the program exits (more accurately, the Toolbar block ends, which in this

case means the program’s done).

I mentioned that the things scrunched together didn’t look all that nice. There is a way around it,

and it can be easy of difficult, depending on how much time you want to spend on it. I’d take the easy way

out and include a few more spaces after your label. Just don’t go past 18 characters on the line. Also, be
aware that different characters take up more room in the small font, whereas the regular type has a fixed

letter width. So, for example, the letter “x” takes up substantially more room than does the negation symbol

(-). I mentioned that it could be difficult – I’ve seen someone (actually it was Nick, who I referred to at the

beginning of the documentation) actually write a program in forty minutes that would calculate how much

space was needed and center-justify the text on the menu. Now you see why I said he’s sharp. If he wasn’t

800 miles north right now, he and I would be working on this together. As it is, he helped me out last year

when I put out the basic documentation. Sometime or other I may email him and get the code for that. It’s

neat, and it makes a good study of the mechanics of string manipulation.

Chapter Eight: Encryption

Now we get to the juicy part. This is the culmination of pretty much everything I was just talking

about. If done correctly, encryption will allow you to store pretty much anything in a string and ensure that

no one can break into it. Encryption is also the cornerstone upon which program locks4 are written. Now,

this may sound strange, but people do actually store sensitive stuff on their calculators. In my opinion, these

people have a very slim semblance of a life, but I’m not one to judge. In this section, I’ll discuss a little

about how string encryption works, then I’ll dive into how to program encryption.

To fully understand the mechanics of string encryption, it is necessary to recognize that the

characters on the front of your calculator are not all the characters you can access. In reality, there are 255

separate characters that can be accessed, with the first thirteen as non-printing characters (like carriage

returns and backspaces). The programmer can take advantage of this by shifting the character codes (see

page 555 in the PDF guidebook) around and making it unintelligible. The operable range of characters is

from 32 to 122. That is, pretty much any character that needs to be encrypted will have a code of 32 at least

to 122 at most (remember the char( function?). You can scramble the characters by subtracting no more

than 31 or adding no more than 133. By using the mid( function along with the char( and ord( functions,

you can change around the strings.

4
A program lock is a separate program that is on your calculator, or better yet, included as a Local
program within your program. It is a simple password setup that will not allow a program to be run unless
the password is entered. Encryption comes in if you want to store the password in an external file and
scramble the string with the password in it. That’s where indirection comes in, as well as string
manipulation…you get the idea.
Here’s how I would do something like this: I’d set up to read one string into another, then delete

the overwrite the original string with the new string, then delete the new string. So, say my original string

was “Hello, world!” If I was just going to transpose this string one character at a time and subtract one from

the character code along the way, this is what I’d do:

:“Hello, World!” -> s1


:“” -> s2
:dim(s1) -> L
:For x,1,L
: s2&char(ord(mid(s1,x,1))-1) -> s2
:EndFor
:s2 -> s1
:Delvar s2

That’s a relatively painless way of doing it. It’s unfortunate that you can’t replace characters in a

string using the mid( function. By that, I mean that one should be able to write out something like this:

:“Heu there” -> s


:“y” -> mid(s,3,1)

The above example would store the “y” in the third position of the string s, resulting in string s

containing “Hey there” instead of “Heu there”. That’s my two cents, and I think that since the rest of the

programming world is doing it, TI should as well. Again, that’s just me. (You can disregard what I just said,

because it’s not important, and the example provided won’t work anyway. I was just merely off on a

tangent.)

Oh, by the way, the string returned after the encryption example is “Gdkkn+ЄVnqkc ”. Notice that

the comma changed to a plus sign, the space changed to something that I could only come close to with the

Cyrillic subset of Times New Roman, and the exclamation mark changed to a space. See? That’s already

pretty nicely encrypted. No one would guess that the above-displayed garbage would be “Hello, World!”

Oh, but it can get much better. How about subtracting two? Three? Fifteen?!? Nor is that all. If

someone sat down (like I did when Nick programmed his first encryption routine) and mapped out what

characters were represented by what characters, he would be done in short order and figure out how to

make a hack. Not so if you do what Nick did. Rather than simply displace the text characters and store the

string, he decided to get sneaky. Instead of doing a straight scale, he did a little algebra with the scrambling,

then on top of that scrambled the first three characters. In other words, for the sake of simplicity, he did

something like subtract one from the first character code, two from the second character code, and three
from all the rest. That threw me off for a few minutes until I caught on (I don’t really remember how I

caught the sneakiness).

When you put your mind to it, the possibilities of encryption are endless. No two codes

(technically called ciphers, unless I miss my guess) have to be the same. The only catch is making sure you

know how to decrypt the string after it’s been encrypted.

Let’s get to the design of the actual cipher key (the way the string is scrambled). The first step is to

decide what mathematical change you want done on the numbers and then determine a function rule for it.

Please note that you have to be able to do to things with it: 1.) have it display a character (no negative

numbers, zeros, or numbers higher than 255) and 2.) be able to turn it back (in other words, no ambiguous

references; each number must be completely unique). Here’s what I’m going to do. I’m going to make a

cipher key that swaps the position of the character in the numerical order. That is, character number 1

becomes character 255, 2 becomes 254, and so on. My function rule therefore is this: y(x) = 256 – x. Not

too hard to break, but it serves a purpose.

Now it’s time (perhaps) to reread the section on string manipulation, because here’s where it gets

tricky. I’m going to take an arbitrary string (“I’m #1”) and encrypt it. You’ll notice that the string has six

characters. To make it easy on myself, I’m going to use a program that was inspired elsewhere but is my

work (the last line is, of course, “EndFunc”):

After running through the “I’m #1” string, we are returned a list that looks like this:

Now, a simple command-line instruction will change this around:


To finish, we run it back through CharList, this time with the list (pasted from the entry line) as the

argument:

“˙ùΩàï”. I am, of course, missing the slashed v. I don’t seem to have that particular character. I

suppose, if you wanted, this could all be put as one command:

Charlist(256 – charlist(“I’m #1”))

You know, that’s looking an awful lot like a program! So, let’s reword CharList to make it return

an encrypted string:

Nifty, huh?

Since you’ve made it to this point in the manual, I’m not going to describe what each line does. I

think you can figure it out on your own.

Now, we need to move on to the decrypt stage. You already know that the character code was

subtracted from 256 to make the new code. I’m going to do a little algebra here; bear with me.

y  256  x

y  x  256

x  256  y
If you don’t understand what I just did there, don’t worry about it. I just solved for the opposite

variable. All that scribble means this in this context: in order to get the codes back to where they belong, we

need to take the new codes (the scrambled ones) and subtract them from 256. Logically, this makes sense.

Now, since it’s just the reverse of what we did there, we should be able to run it through the encrypt routine

again.

Wow. What do you know? It worked.

As I mentioned, Nick threw in his little string offsetting roadblock, and it took me about 45

minutes to figure out how to get around it. Remember what he did? He subtracted two from the first

character code, one from the second, and none from the rest. Now, the little slope-intercept cop-out doesn’t

work. Let’s take an example string and walk through the encryption, assuming that everything I just

showed you stayed the same with the exception of the offsetting.

Say the string is “abcde”. (Note to English majors – I know that terminal punctuation comes inside

the quotation marks, but this isn’t a thesis paper, it’s not being graded, and I don’t want to throw anyone off

by making them think the period is part of the string.) Look at this table to see what would happen:

Original number Encryption one Encryption two


(new symbol) (new symbol)
“a” 97 159 () 157 (
“b” 98 158 () 157 (
“c” 99 157 () 157 (
“d” 100 156 ( 156 (
“d” 101 155 (y) 155 (y)

See how that works? It can get complicated really quick. But how is this done inside the program?

The secret lies in putting a few IF statements inside your For…EndFor. It’s not that hard to do

(what you’re seeing is the guts of the program. I left out the Func and the EndFunc statements):
That’s all good and well, except it still isn’t very secure. Again, I can run a string through twice

and come out with the same thing I started with. How do I make it secure?

There are a few ways, but they make the whole process terribly long. The first way is the one I like

– put a password inside the string itself. Specifically, make a program that requires you to get the password

right before you decrypt the string. This requires a tangent:

Program Locks

This isn’t as painful as it sounds. It just takes a little creative skill. If you want, you can even do

something that I’ve seen in Visual Basic: the programmer can specify that an input box is a password input

box; that is, any character that shows up is displayed as an asterisk (*). It’s not so easy in TI-BASIC.

To start off, it requires the use of the I/O screen, as well as the getKey() function. I know you hate

using getKey() (frankly, so do I), but it’s necessary. We want our program to loop until the enter key is

pressed. While it’s looping, it needs to record every key pressed (making provision for the backspace key),

store the key pressed in a string, concatenating as it goes, and (possibly) display the star for every key

pressed. This isn’t as difficult as it sounds. Really.

First, I’m going to set up by defining constants for my key values. I’m not going to have one for

every letter and number, but I am going to have one for the backspace, clear, and enter keys, as well as an

escape constant so I know if the program is to be cancelled. Here are the constants you need: escape (264),

backspace (257), clear (263), and enter (13):


Now, I need to set my loop to exit when the value of getKey(), in my case ‘kp’, is equal to the

escape value:

Now we fill in the guts of the loop. I’m paranoid, so I always repeat myself in a While…EndWhile

loop (note that I inserted a 0->kp and “”->t line before the While statement and a ->t line after the last thing

you see on the screenshot):

Now, this doesn’t address the whole asterisk thing. I want to lay the groundwork for the entry first.

Try this program for yourself and see. It’ll just sit there with a busy screen. That’s fine; just start typing,

remembering what you typed in. When you’re done, press enter, then type “t” (no parens) on the home

screen. It comes out to exactly what you typed. From here, it’s easy to make the asterisk thing. (Advanced

advanced note – you could actually stop here. Someone peering over your shoulder may be able to figure

out how many characters your password is, but if it’s just a blank screen, they’ll never know!)

Here’s the screenshots from the program:

Note the overlap.


If it’ll make you feel better, you can put a Disp “” statement after your Local statement. It won’t

bother me.

Now, if you’re really into this, you can create a whole faux dialog box on your drawing screen and

have the asterisks entered there. Like I said earlier, you don’t have to have any asterisks displaying.

Whatever.

OK. Now that we’ve covered discreet entry, it’s time to add on to the program. I’m going to go

back to what I had, so everything will be entered “silently.” The next thing you need to do is encrypt what

was entered to compare to the encrypted password, or decrypt the password that you have stored and

compare it to what was entered. By way of preference, I’m going to encrypt the just-entered string. It

actually saves me from having to re-encrypt the original after I decrypt it. Again, your preference may be

different. You’ll just get examples from my way, that’s all. I’m going to stick with the offset I described at

the beginning of the chapter. First, I need to have a string encrypted and stored. I’m going to use my name,

tom, as the string. So, after encrypting it, we find it comes out to:

Notice I stored it to the variable “oikt” – my shorthand for “OnlyIKnowThis. Now, I need to add

the encryption routine to the entry program. I’ll just extract from e2() what I need.

Note the indirection in the second shot. By the way, all the new variables were Local-ized. I also

included a catch to make sure the file exists. Remember the isFile() example earlier?

There! You now have a complete program lock. Also, you control the file that the password is in.

Just make sure you don’t lose the password. My suggestion would be to archive it as soon as you create it.

Now, it’s possible to create a fully functional box, one that includes options for changing the file. Here’s the
problem with doing that: size. I thought it would be neat to do something like that – create a full box to

control every aspect of the lock. But, a problem arises when the program used to control entry to another

program is twice the size of the program you want to protect! I would suggest a simple program that allows

you to enter the password; then create a separate program to act as the “file manager” – a program that will

allow you to change existing passwords, create new ones, delete old ones, etc. For example, the program on

my page, PWPro, is only 640 bytes. Short enough that it could be put inside the program itself, or used as a

called program (the Return keyword at the end allows me to do that).

Obviously, you’ll want to create your own cipher key. Play around. Try different things. That’s the

only way that you’ll make the program truly unique. It won’t come without some frustration; however, that

comes with the territory.

That should just about do it for the password entry and encryption thing. Not too many people are

going to use this, as it can show a little paranoia on the part of the programmer. But, if you’re one of the

people in the world that likes to store stuff on your calculator that shouldn’t be general knowledge, then

you’ll want to go into more depth. Sadly, time and space doesn’t permit me to go into more detail. Frankly,

I wasn’t entirely sure that I needed to do this section in the first place, but I wanted a place to show off….

Seriously, while you may not use anything in this section, the thought process behind it is key. I’ll get off

my soap box now.

Chapter Nine: Loose ends

As you might could see from the title, this chapter won’t have a whole lot to do with

programming. I’m going to use this as my forum to write a few closing thoughts.

First, I’d like to thank the thousands of people that downloaded my first manual and those who

gave me constructive feedback. You’d be surprised – sometimes things that you take for granted, things that

you think people already know, are things that people haven’t read or heard anything about. Goodness

knows I tried to make my first manual comprehensive, but I left some really basic stuff out because I

figured people already knew it. Imagine my surprise when I found out that not everyone knew all that I did!

(Take that as sarcastic as possible.)


I’d also like to say hey to the guys at TICalc.org. I don’t think that I’ll ever upload to another TI

website (except mine). I’ve gotten scads of great programs from there, and it’s a quick and easy place to

upload stuff to share with the community. Keep up the good work, guys.

Also, I’d like to thank God for giving me the time to finish this beast. That may sound overly

“religious” of me, but if I was really worried about what other people thought of me, I wouldn’t step out of

the door most of the time. Some people think that mentioning God in something automatically makes the

writer some pious religious nut, and honestly, being branded as one doesn’t bother me in the least. Who and

what I am is between me and God, not me and the rest of the world. OK, off my soap box again.

Finally, this manual wouldn’t have even come under consideration without the support of people

like you that want to learn. To those who wish to broaden their horizons, I salute you. To those that could

care less and just want to figure out a cool new game, I salute you, too. I mean, we’ve got to have

something to do in those boring lectures, right? Besides (and this may come as a shock), those that program

math functions and those that program games really aren’t all that far apart. It’s still a creative expression,

and I’m all for that in an age of sterile, pre-packaged ideas and shows. It’s about time someone started

thinking outside the box. Congratulations.

One last loose end to tie up – for those of you that have Linux or excessively slow download

speeds and want to read my first manual, you can find it at

http://usa.internations.net/cyber/calcti/default.htm. I just put the site up yesterday. The HTML isn’t all that

fancy, just some exports from MS Word. Drop by – it’s got all my programs, plus some that haven’t made it

to TICalc.org, the PDF version of the 89/92/92+ manual from TI, and both programming manuals. It’s

worth the time, trust me.

Tom Mount
Programming Manual ©2001 by Mountaintop Productions. All rights reserved. If you found this useful,
then put a link to my website and email ([email protected]) in your documentation. Thanks!

You might also like