The Museum of HP Calculators


HP-67/97 Programming

The HP-67 and HP-97 have different shift keys so shift keys are generally not shown below. Certain functions such as those involving printing and pausing and use of the indirect register (I) are also different. The HP-97 keys are generally shown with the HP-67 keys shown in [brackets].

Contents:

Features

Basic Programming

Entering And Running A Sample Program

A simple program is essentially just the keystrokes you would press to solve the program manually. The calculator remembers a sequence of keys and executes them in order at the touch of a key. In the simplest programs, the only additional steps are a name (LBL) and an end (RTN.)

To enter a program, slide the PRGM-RUN switch to PRGM. [W/PRGM-RUN on the HP-67.] Then press f CL PRGM to clear program memory and to set the calculator to the beginning.

With the PRGM-RUN switch set to PRGM (program) the number you see on the left side indicates the step number. As keystrokes are entered, their row/column codes will be displayed on the right. (On the HP-97, keys on the right keypad are prefixed by a dash. For example, the x⇔y key is shown as -41.) Step 000 is really just a placeholder. The first keys you press will go into step 001.

Enter the program below to compute the area of a circle. The program begins with LBL A which indicates that this program can be executed by pressing the A key on the calculator. (Remember that prefix keys are not shown, for example, you'll need to press f π on an HP-97 and h π on an HP-67.)

LBL A      [f LBL A on the HP 67] Gives the program a name
x2         These three instructions
π          are just the normal keystrokes
×          for computing the area given the radius in X
RTN        (return) Finishes the program

When the calculator encounters a RTN (Return) it halts execution unless it is in a subroutine in which case it returns to the caller. Programs can also be ended with R/S (Run/Stop) but RTN is more flexible because it allows your programs to be called as subroutines and makes the end of your program more obvious because R/S can also be used to stop for data entry.

Now set the PRGM-RUN switch to RUN. To run the program, key in a radius and press A. After the display stops flashing, the area is displayed. The program can be run as many times as you like by entering new values and pressing A. For many simple programs, that's all you need to know!

Stopping, Interrupting and Entering Data

Many programs only require data to be entered at the beginning as the one above did. Remember that you can use the stack and store values in registers before execution. You can also enter R/S instructions into the program to allow additional data to be entered. The user can enter the data and then press R/S to resume. If you create a program that runs for too long (possibly due to an infinite loop) you can stop it by pressing R/S and if necessary, resume it by pressing R/S again. If you press R/S in the middle of a program, you can see where the program is executing by pressing and holding SST (see below) or switching to PRGM mode.

Stepping And Editing programs

The following commands are used to edit or step through programs and are not recordable. (Prefix keys are not shown.)

Entering an instruction causes the new instruction to be inserted after the instruction displayed and all following instructions to be pushed down.

The following diagram when read from left to right shows the insertion of a multiply after the ENTER. Start with the display showing the ENTER instruction. The x is inserted after the ENTER and the display moves down to show it. Reading right to left, with the display showing the x, pressing g DEL, deletes the x, and leaves the display on the line above (ENTER.)

GTO lbl can be used in run mode to position the calculator to lbl. The calculator can then be switched to PRGM mode for editing at that label. Unlike the instructions above, this one is recordable so the position of the PRGM-RUN switch must be properly set. (GSB lbl in RUN mode causes the program at lbl to be executed. In RUN mode, pressing GSB A and GSB f d are equivalent to pressing just A and f d respectively.)

RTN can be pressed in RUN mode to reset the program counter to 000.

Saving and Loading Programs & Data

Programs

To save a program to magnetic card, make sure the PRGM-RUN is switch set to PRGM [W/PGRM on the HP-67] and place a card (always label side up) in the card reader/writer.

Half the program (or data) memory can be recorded on each side of a card. If the program is longer than 112 lines, the calculator displays Crd after passing side 1 through to prompt you for side 2. Pass the card through again in the opposite direction (still label side up.) You may cut off the corners of a magnetic card to prevent it from being overwritten. Cards can be marked with a permanent felt tip pen.

When programs are written, the calculator also records the states of the flags, trig mode and display format.

To load a program, make sure the PRGM-RUN switch is set to RUN and insert the first side of the card. If the program is longer than 112 steps, the calculator will display Crd to prompt you to insert the second side. If the checksum does not match the one recorded on the card, the calculator will display Error. Press any key to clear it and insert the same side again to retry. The registers and the stack are not changed when a program is written or read.

Data

Data can be saved to a magnetic card by making sure that the PRGM-RUN switch is set to RUN and pressing WRITE DATA [W/DATA] which causes the calculator to prompt you by displaying Crd. Insert the card and the calculator's primary registers R0-R9 and A-E are recorded on side 1. If all secondary registers contain 0, this is noted also on side 1 and you are finished. Otherwise, the calculator displays Crd to prompt you to insert the other side to record the secondary registers.

To load the data, simply pass the card through the reader. (Crd will then be displayed if you need to pass the 2nd side through.) The calculator marks cards as containing programs or data so there is no need to specify the contents when reading the cards. Flag settings, display mode etc. are not recorded on data cards. Program memory is not changed when data is written or read.

Merging Programs or Data

It is possible to merge some of the steps on a magnetic card with the current program memory. To merge a magnetic card with the current memory, set the PRGM-RUN switch to RUN. Use GTO . nnn to set the calculator to the last step of the loaded program that you want to preserve. Press MERGE and insert the card. (If the second side must be loaded, the calculator will display Crd.) Instructions after nnn will be replaced by those in the same positions on the card.

It is also possible to merge some of the data on a data card with the current data memory. First store a number between 0 through 25 in the I register. This is the highest register you want to overwrite from the card. Press MERGE and insert the data card. Registers 0-(i) inclusive will be replaced by the values on the card in the same register positions.

Reading Cards Under Program Control

These instructions are recordable so it's possible to design programs that use PAUSE to read or merge data or programs during their operation. If such a PAUSE is recorded into a program, you can insert the card as soon as the program begins -- the card will not be drawn into the reader until the running program pauses. (If the data or program exceeds one side, the calculator will prompt you with Crd to insert the second side before continuing. If the data or program is to be merged, a MERGE instruction should be recorded before the PAUSE. (For data, I should be set appropriately. For a merged program, the program in memory will be overwritten immediately following the PAUSE instruction.) Execution will resume after the card has been read. If a card is not available to be read within the 1 second time of the PAUSE, execution continues.

Because the F3 flag (see below) is set by data entry, you can write a loop that PAUSEs and tests F3 and won't continue until a data card has been entered. For example:

CLF 3       Clear flag 3 (data entry flag)
LBL 0
PAUSE       Allow data card to be inserted
F? 3        See if data was entered
GTO 1       Data entered - do something with it
GTO 0       No data entered Pause again

Writing Data Under Program Control

To write data to a card in a program, just insert a WRITE DATA [W/DATA] instruction. The calculator will prompt the user with Crd or the user can place a card in the slot ahead of time which will be drawn in and written when the WRITE DATA instruction is executed.

Programming Techniques

Addressing

The calculators use label addressing. Labels available are the upper-case letters A through E, the shifted labels f a through f e, and the digits 0 through 9. The calculator always searches downward through program memory from the current location. If there are multiple copies of a label it will execute the first one it finds. After it gets to the end of memory it resumes searching from the beginning.

Jumps and Subroutines

The program below calculates the volume of a cylinder by calling the first example as a subroutine. If you don't have the first example in memory, key it in now after pressing CL PRGM:

LBL A
x2
π
×
RTN

Now add the cylinder program: (If you already have program A loaded starting at step 001, you can press GTO . 005 to position the calculator to the end in order to add the new program or press GTO . 000 to insert routine B before routine A. You can also press GTO .100 to enter it at step 101 if you like -- Just be sure you don't insert the new code into the middle of the old.)

LBL B
GSB A
×
RTN

Now switch to RUN mode, type the cylinder length, press ENTER, then type the cylinder radius and press B to display the volume. The program uses the radius in X and calls the subroutine A to determine the area of the circle. When the subroutine returns, the area is in X and the length is still in Y so X and Y are multiplied to calculate the volume.

Conditional Tests and Flags

The calculators have instructions for comparing X to Y and X to 0. If the comparison is true the calculator executes the next instruction. If the comparison is false the calculator skips the next instruction.

The next instruction is most commonly a GTO or GSB like:

X=0
GTO 1      Go to label 1 if X is equal to zero
STO 2      The line above is skipped and execution continues here
           if x is not zero. 

The calculators also have four flags F0-F3 that can be set and tested in program execution. A flag is set with the STF n [SF n] instruction. The flag is tested by the F? n instruction and if the flag is set the next instruction is executed. Otherwise the next instruction is skipped.

Flags 0 in 1 are command-cleared flags. Once set they can only be cleared with the CLF n [CF n] instruction. Flags 2 and 3 are test-cleared flags that can be cleared by CLF n or by testing them with the F? n operation.

Flag 3 is also set by digit entry. When any digit on the keyboard is pressed it is set. It is also set when data is loaded into registers by magnetic card. The same label can be used to store or compute a value based on the state of this flag. Programs such as Time Value of Money use this. These programs use a fragment like:

LBL A
F? 3
GTO 1
...      No number pressed before pressing A - compute it
RTN
...
LBL 1    Number entered before pressing A - store it
STO A    Don't confuse register A with label A
RTN

All flags are cleared when the calculator is turned on or CL PRGM is pressed.

Indirect Addressing

The I register is used for indirect addressing. To store a number in I the STO I [ST I on the HP-67] instruction is used. To recall the contents of the I register, use RCL I [RC I]. The I register can also be exchanged with the X register with the X⇔I instruction.

Given some number in I, it can be used to affect the following instructions:

For STO and RCL, I map to register addresses as follows:

Reg.
Name
(i)
Value
      Reg.
Name
(i)
Value
      Reg.
Name
(i)
Value
0 0 S0 10 A 20
1 1 S1 11 B 21
2 2 S2 12 C 22
3 3 S3 13 D 23
3 4 S4 14 E 24
4 5 S5 15 I 25
5 6 S6 16
7 7 S7 17
8 8 S8 18
9 9 S9 19

See the looping constructs section below for an example of STO I and STO (i).

For GTO and GSB, I map to labels as follows:

Label (i)
Value
      Label (i)
Value
0 0 A 10
1 1 B 11
2 2 C 12
3 3 D 13
3 4 E 14
4 5 f a 15
5 6 f b 16
7 7 f c 17
8 8 f d 18
9 9 f e 19

For example:

11
STO I        [ST I on HP 67]
GSB (i)       

Would call the subroutine labeled B. That may look silly in such a trivial example but consider the GSB (i) combined with a DSZ (see below) that calls several subroutines in a loop. Or consider an example where I is calculated from some formula and/or user input.

The I register can contain a non-integer number but only the integer part is used. The looping constructs shown below also use the I register.

Rapid Reverse Branching

GTO (i) and GSB (i) can also be used with a negative number in the I register. In this case, the calculator jumps (i) steps backwards from the current position. This can save a label and is faster than normal branching because the calculator doesn't have to search. (Because the calculator always searches forward and wraps around, branching back a few steps is a relatively slow operation.)

Rapid Reverse branching can even be used to step forward by effectively stepping backward beyond step 000. (If the value in I would branch to a negative step, simply add 224 to the negative step number to find the real step the calculator would execute next.)

The next example shows a program that simply counts up from zero and displays each number. (Press R/S when you get tired of watching it.)

LBL A       Initialize
3 
CHS         We'll want to go back 3 steps
STO I       [ST I on the HP-67]
CLX         Start at zero
PAUSE       Beginning of the loop
1
+
GTO (I)     Go back 3 steps

Looping Constructs

Two looping functions are available:

These instructions increment or decrement I each time they're executed. As long as I is not equal to 0 the next instruction is executed. When I becomes 0 the next instruction is skipped. The I register can contain a non-integer number but only the integer part is used during the test (so n for -1 < n < 1 would be treated as zero.)

For example, here is a program that takes the number in the X register decrements it, pauses and loops until the I register goes to 0 (leaving 1 in the display.)

LBL A 
STO I       [ST I on the HP-67]
LBL a
RCL I       [RC I on the HP-67]
PAUSE
DSZ I       [DSZ on the HP-67]
GTO a
RTN 

Here's an example that combines looping and indirect addressing to store the squares of 0-9 in registers 0-9.

LBL A
9           Start from the top down
STO I
LBL 0       Top of the loop
RCL I
x2
STO (i)     Store x (i2) in register indicated by i
DSZ I       Decrement I and...
GTO 0       Go to 0 if I is not zero
RTN

The ISZ (i) and DSZ (i) instructions are similar except that they increment or decrement the register pointed to by the value of the I register.

Input/Output

R/S can be used as an instruction or pressed from the keyboard. If a program is stopped, pressing R/S starts it. If the program is running, pressing R/S stops it. An R/S can also be inserted into a program to allow the user to input data.

For example, the following program lets you key a percentage discount and calculate the cumulative cost of various quantities of items from which the discount has been subtracted. Slide the PRGM-RUN switch to PRGM, press CL PRGM if necessary*, and then press.

LBL a    (*Remember to press g LBL f A on the HP-67 and LBL f a on HP-97)
CL REG   (LBL f is a single keystroke on the HP-67.)
STO 0
LBL A
ENTER
R/S      Stop to key in price.
×
RCL 0
%
-
STO + 1  Add to running total in R1.
RCL 1    Recall running total for display.
RTN

Slide the PRGM-RUN switch to RUN, key in a percent discount and then press f a to initialize the program. Key in the first quantity and press A.

When the program stops key in the price for the first quantity and press R/S to resume execution. The calculator will then display the running total. Key in additional quantities and prices as needed. (Pressing A after quantities and R/S after prices.) Alternately you could have written the program to expect both values to be on the stack before the A key was pressed.

*Above it said "press CL PRGM if necessary". This program uses label A which was used in other examples. The calculators will allow you to use the same label again and this can be a useful feature but it can also lead to confusion as the calculators will always go to the next A downward in memory from the current position. For example:

LBL A
1
RTN
LBL A
2
RTN
LBL A
3
RTN

Would display 1, 2, 3, 1, 2, 3, 1,... on successive presses of A .

On both calculators the PAUSE instruction shows the X register for one second. Several instructions are printed on the HP-97 while they're displayed on the HP-67. These instructions are automatically converted when cards produced on one are read on the other. These instructions are shown below:

HP-97
instruction
HP-97 Effect Equivalent
HP-67
instruction
HP-67 Effect
PRINT x Prints X register -x- Pauses for 5 seconds displaying X register.
PRINT Stack Prints the Stack STK Displays each member of the stack in order T, Z, Y, X.
PRINT REG Prints all primary registers (R0-9, A-E and I) REG Displays each primary register preceded by its address.
PRINT SPACE Prints a blank line. SPACE None.

Advanced and Unusual Features

Registers S0-S9 are in the "secondary" bank. They can be accessed via the I register (values 10-19) or they can be swapped with the primary bank by using the P⇔S Key.

On the HP-97 the printer switch can be set to:

HP-97 users are encouraged to make heavy use of the Print Prgm function since programs are listed alphanumerically. Each printed line shows the step number, keystrokes and row/column codes.

Go back to the HP-67/97
Go back to the software library
Go back to the main exhibit hall