( 8 )
Previous Table Of Contents Next


Programming The Calculator

Now that you know your way around the keyboard of the RPN calculator you're ready to start programming it. A program is simply a stored representation of the activities you want to take place at that later time when you decide to execute the program. Executing a program is also known as running the program. We will store our RPN calculator programs in plain-text ASCII files having a .TXT suffix and hence compatible with the Notepad word processor that Microsoft ships with every version of their Windows operating system.

Before we can execute a calculator program we must load it into the RPN calculator's program memory. We won't concern ourselves with the representation for the program used inside the calculator (this representation involves bits and bytes which we will study when we get to the next portion of our curriculum, an 8051 microprocessor). Instead we will just imagine that program memory can hold text (character strings) such as ENTER, 7, and *. Wherever possible we will use the text that is printed on the calculator for the representation of each keystroke. An example of where we can't do this is the key since we can't create a down arrow on a standard computer keyboard. So we use the text Rolldown as the representation of this keystroke. Text such as this is known as mnemonics because these character strings are designed to allow the human operator to remember the activities of each statement.

Let's imagine we wanted a program that would produce the sequence of integers starting from 1. Obviously this program would need to run forever. We can make a program run forever by including an infinite loop. An example of an infinite loop is the birthday card that says, "For your surprise, turn card over" on both of its sides. Or the instructions on the shampoo bottle that say:

These instructions would strand a computer programmer -- who is trained to be literal -- in his shower.

An RPN calculator program that creates an endless stream of integers starting from 1 is shown below. I will show you how to create this program and we will then save it in a plain-text ASCII file called "IncrementForever.txt".

       01   LBL 0
       02   1
       03   LBL 1
       04   1
       05   +
       06   GTO 1

You will notice that I have numbered the 6 statements in this program. Older computer languages required you to number your statements but most modern languages do not require statement numbers. In the case of the RPN calculator the statement numbers are mandatory but the calculator provides them for you.

In this program we see the mnemonics LBL, +, and GTO. You can read about each mnemonic in the section of this document entitled the instruction set but my plan is to introduce them to you a few at a time as we work through these help pages.

The first statement is a specific instance of the generic LBL n statement. The RPN calculator allows you to create up to 10 labels, numbered from 0 to 9. That is, the letter n in the generic LBL n statement indicates a 1 digit variable. We have chosen to start our program with a label numbered 0. We will see that having a label at the start of our program simplifies the task of running the program.

The next statement enters the number 1 into the X location of the operand stack (or, more precisely, it will have this action when we run the program). The prior contents of the stack lift but it makes no difference to our program what these prior contents were as we will never employ them. It is this statement that causes the sequence of integers to begin with 1. If you want your sequence to begin with a different number then change this statement.

The next statement is another label statement. We can't again use label #0 so this label is given the number 1. We need this label so we can use it as the destination of an unconditional branch that creates an infinite loop (more on this shortly).

The next statement enters the number 1 into the X stack location, thus causing the stack to lift. We now have two ones in our operand stack.

The next statement causes an addition operation (that is, it will cause an addition when execution reaches this statement). Because the X and Y stack locations both hold 1 this addition will result in the value 2 being placed in the X stack location.

The final statement is an unconditional branch statement which is a specific form of the generic GTO n statement. You can use however many GTO statements you want but you are limited to 10 destinations, labeled 0 through 9 since this is how many LBL statements you are allowed.

So our program is all designed and we want to get it into the RPN calculator. To accomplish this, first move the Prgm/Run slide switch over to Prgm (remember that you must click and drag the slide switches). This puts the calculator into its Program mode and causes the simulator's window to change from this:

to this:

The new pane that you see is called the Program window and it shows the RPN calculator's program memory. Any program that you wish to execute must first be placed into program memory. Using the scroll bar you can discover that program memory is limited to 98 statements (it is impossible to place a statement into the 00 slot and there is no 99 slot). You can also see that initially program memory is empty. Well, it's actually not empty: it is initialized with 98 repetitions of the R/S keystroke, which is the statement that stops a running program. But you can think of this configuration as being empty.

Anytime you want to return program memory to this reset condition just get into Program Mode and hit the key (this is called the ClearPRGM key). You are in Program mode anytime the Prgm/Run slide switch is set to Prgm.

Anytime you are in Program mode the current insertion point is highlighted in blue. To insert a new statement below the highlighted statement you merely need to press the necessary keys on the calculator. For example, immediately after performing a ClearPRGM operation the 00 numbered statement will be highlighted. This means we are ready to enter the calculator instruction we want to place into the 01 slot of program memory. To do this simply click on to create the LBL 0 statement. Note two things here:

Because this action inserts a new statement at program memory location 01, all subsequent statements are pushed down in program memory and the former statement at program memory location 98 is lost. This will not be obvious at the moment since program memory is populated with a bunch of R/S statements that all look identical. Furthermore the current insertion point moves down so now the 01 statement is highlighted.

You are now ready to add the 02 statement to the program. This merely requires you to click on the 1 key. This completes the 02 statement. Then type LBL 1 to complete the 03 statement. Continue in this manner until you have entered the entire program.

Before we learn how to run this program we should learn how to edit a program since you may have made a mistake while entering it. If there is something wrong with one of your program statements you need to delete the bad statement and then re-insert the correct statement. The DEL key () deletes whatever statement is at the current insertion point and moves all subsequent statements up in program memory and re-populates program memory location 98 with a new R/S statement. So before you can use the DEL key you must position the current insertion point on the bad statement.

There are two ways to move the current insertion point. You can move it by 1 statement at a time by clicking on the SST (single step) or BST (back step) keys. But if you have a large distance to move then you don't want to have to take baby steps to get there. The solution is the GTO .nn generic keystroke. The nn notation signifies that you must always provide a two digit statement number as part of this statement. As an example, to move the current insertion point to statement 05 you would use the keystroke sequence:


I should warn you that there is a subtle difference between the current insertion point and the statement that is highlighted. If you don't disturb things then the current insertion point will always remain highlighted. But via the mouse you can manage to highlight an arbitrary statement or just a portion of a statement or even nothing at all (you might choose to do this as part of a cut and paste operation). ALTHOUGH THESE ACTIVITIES CHANGE THE HIGHLIGHT THEY DO NOT CHANGE THE CURRENT INSERTION POINT. Only the SST, BST, and GTO .nn statements change the current insertion point (well, okay, adding a new statement or deleting a statement also moves the current insertion point). There is a special toolbar button () called the Highlight Next Statement button that will restore the highlight to the current insertion point. Click on this toolbar button any time you fear that the highlight might have gotten disturbed. The toolbar button doesn't move the current insertion point, it only re-highlights it (the menu choice that corresponds to this toolbar button is labeled "Repair highlight").

Once you are happy with your program you will want to save it in a plain text ASCII file having a .TXT filename suffix. This is accomplished by selecting File/Save from the menu bar (or clicking on the floppy disc icon visible on the toolbar). Save this 6 statement program in a file named "IncrementForever.txt". This is the first of the example programs that I provide with my RPN calculator. At any later time you can reload this program into the calculator via the File/Open choice in the menu (or the manila folder icon on the toolbar). You can load a program while the calculator is in Run mode or Program mode.

So let's summarize. The RPN calculator can, upon command, perform the sequence of operations that are stored in its program memory. Program memory can hold up to 98 operations, each of which is indicated by a textual mnemonic. While in Program mode your keystrokes do not cause computations to occur, instead they cause program memory to be populated with the corresponding mnemonic.

Remember that you can clear the calculator's program memory via the ClearPRGM keystroke anytime you are in Program mode. You can also clear the program via the blank piece of paper icon on the toolbar (this works in both Run and Program mode).


Previous Table Of Contents Next