Why Use Programs?
A program is simply a sequence of keystrokes that is stored in the calculator. Whenever you have to calculate with the same sequence of keystrokes several times, you can save a great deal of time by incorporating these keystrokes in a program. Instead of pressing all the keys each time, you press just one key to start the program: the calculator does the rest automatically!
Creating a Program
Creating a program consists simply of writing the program, then storing it: Write down the sequence of keystrokes that you would use to calculate the quantity or quantities desired. Press f P/R to set the calculator to Program mode. When the calculator is in Program mode, functions are not executed when they are keyed in, but instead are stored inside the calculator. The PRGM status indicator in the display is lit when the calculator is in Program mode.
Press f CLEAR PRGM to erase any previous programs that may be stored inside the calculator. If you want to create a new program without erasing a program already stored, then press g GTO . nn where nn is the program line of the last instruction of the previous program. (The last instruction of any program should be g GTO 00.) Begin keying in the new program. The new instructions are automatically stored starting after the last instruction of the previous program.
To run the program press f P/R to set the calculator back to Run mode. Key into the display or store into storage registers any data needed by the program. Press R/S to begin program execution.
Keystrokes entered into the calculator in Program mode are stored in program memory. Each digit, decimal point, or function key is called an instruction and is stored in one line of program memory--usually referred to simply as a program line. Keystroke sequences beginning with the f, g, STO, RCL, and GTO prefix keys are considered to comprise a complete instruction and are stored in only one program line.
When a program is run, each instruction in program memory is executed--that is, the keystroke in that program line is performed, just as if you were pressing the key manually--beginning with the current line in program memory and proceeding sequentially with the high-numbered program lines.
Whenever the calculator is in Program mode (PRGM status indicator is lit), the display shows information about the program line to which the calculator is currently set. At the left of the display is the number of the program line within program memory. The remaining digits in the display comprise a code that indicates what instruction has been stored in that program line. No code is shown for program line 00, since no regular instruction is stored there.
Identifying Instructions in Program Lines
Each key on the keyboard--except for the digit keys 0 through 9--is identified by a two-digit "keycode" that corresponds to the key's position on the keyboard. The first digit in the keycode is the number of key row, counting from row 1 at the top; the second digit is the number of the key in that row, counting from 1 for the first key in the row through 9 for ninth key in the row and 0 for tenth key in the row. The keycode for each digit key is simply the digit on the key.
The GTO 00 Instruction and Program Line 00
Whenever you run a program, the last instruction should be g GTO 00. Thus, after each time a program is run, the calculator automatically goes to program line 00 and halts, ready for you to key in new data and run the program again. Whenever you press f CLEAR PRGM (in Program mode), the instruction g GTO 00 is automatically stored in all program lines of program memory.
Setting the Calculator to a Particular Program Line
There will be occasions when you'll want to set the calculator directly to a particular program line--such as when you're storing a second program in program memory or when you're modifying an existing program. Although you can set the calculator to any line by using SST, you can do so more quickly as follows. In Program mode, pressing g GTO . followed by two digit keys sets the calculator to the program line specified. In Run mode, pressing g GTO followed by two digit keys sets the calculator to the program line specified by the digit keys. Since the calculator is not in Program mode, the line number and keycode are not displayed. The decimal point is not necessary if the calculator is in RUN mode, but it is necessary if the calculator is in Program mode.
Executing a Program One Line at a Time
Pressing SST while the calculator is in Run mode advances the calculator to the next line in program memory, then displays that line's number and the keycode of the instruction stored there, just as in Program mode. In Run mode, however, when the SST key is released the instruction in the program line just displayed is executed and the display then shows the result of executing that line.
Interrupting Program Execution
Occasionally you'll want a program to stop executing so that you can see an intermediate result or enter new data. The HP-12C provides two functions for doing so: PSE (pause) and R/S (
run/stop). When a running program executes a PSE instruction, program execution pauses for about 1 second, then resumes. During the pause, the calculator displays the last result calculated before the PSE instruction was executed. When R/S is executed as a program instruction, the calculator halts program execution indefinitely, presumably to display some intermediate result. The user can then resume program execution at the next consecutive program line by pressing R/S.
If you press any key during a pause, program execution is halted indefinitely. To resume program execution at the program line following that containing the PSE instruction, press R/S.
Program execution is halted automatically when the calculator overflows or attempts an improper operation that results in an Error display. Either of these conditions signifies that the program itself probably contains an error.
To determine at which program line execution has halted (in order to locate the error), press any key to clear the Error display, then press f P/R to set the calculator to Program mode and display that program line.
You may also want to display the current program line (by pressing f P/R) if your program has halted at one of several R/S instructions in your program and you want to determine which one that is. To continue executing the program afterward: Press f P/R to set the calculator back to Run mode (remember that when switching from Program mode to Run mode the calculator is automatically set to program line 00). If you want to resume execution from the program line at which execution halted rather than from line 00, press g GTO followed by two digit keys that specify the program line desired (that program line should be the one after the R/S instruction). Press R/S to resume execution.
Branching and Looping
Although the instructions in a program normally are executed in order of their program line numbers, in some situations it is desirable to have program execution transfer or "branch" to a program line that is not the next line in program memory. Branching also makes it possible to automatically execute portions of a program more than once--a process called "looping".
There are two conditional test instructions that are used in programs for conditional branching: x<=y tests whether the number in the X-register is less than or equal to the number in the Y-register. x=0 tests whether number in the X-register is equal to zero. If the conditional tests true, the calculator continues execution at the next program memory line. If false, the calculator skips the next line before resuming execution.