The Museum of HP Calculators

# HP-65 Programming

Contents:

## Features

• 100 Lines of partially merged keystrokes (some multi-keystroke steps require just one line)
• 9 storage registers (R1-R9, R9 is overwritten by some built-in functions.)
• Label addressing with 15 Labels (0-9, A-E). Pressing A - E from the keyboard in run mode allows one-key access to 5 programs.
• 2 Flags.
• Unconditional and conditional branching based on register values and flag settings.
• Convenient insertion and deletion of program steps.
• DSZ looping (using the R8 Register).
• Subroutines (one level).

## 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 W/PRGM-RUN switch to W/PRGM. Then press f PRGM to clear program memory and to set the calculator to the beginning. The Display will show 00 00 which is the top of memory marker. Note that the HP-65 powers up with programs entered to give default functions to the A-E keys (1/x etc.). Pressing f PRGM removes these programs.

With the W/PRGM-RUN switch set to W/PRGM (write/program) as keystrokes are entered, their row/column codes will be displayed. (There are no step numbers.)

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.

```LBL
A         Gives the program the name A
f-1       These instructions are just the normal keystrokes
√x        for computing the area given the radius in X
g π       (f-1 means inverse of shifted function so f-1 √x means x2)
×
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 W/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 switching to W/PRGM mode.

### Stepping And Editing programs

The following commands are used to edit or step through programs and are not recordable.

• f PRGM in W/PRGM mode clears the entire program memory by placing a g NOP instruction in each step and sets the program counter to the top of memory. Remember that the HP-65 powers up with default programs supplied for the A-E keys (you can SST through them if you like) so it is important to press f PRGM to remove those programs before creating your own. If you don't, successive uses of the A-E keys could alternate between using your programs and the default.
• SST steps one instruction ahead without execution in W/PRGM mode. In RUN mode, SST executes the next instruction (the instruction is not displayed).
• g DEL in W/PRGM mode deletes the instruction currently displayed and moves all following instructions up. Because the calculator addresses by labels rather than line numbers, this movement does not disturb program branches and subroutines.

Note that since the g prefix key is first inserted into memory before the DEL key is pressed, if memory is full, deleting a step in the middle will cause the loss of an instruction at the end. In this case, you will find two g NOPs at the end - one because of the deleted instruction and one because of the room that was needed to store the g prefix before the deletion. You'll need to go to the end and reinsert the last instruction. To delete multiple steps, start at the last step to be deleted because the program counter is backstepped on each deletion. Then you can press g DEL as many times as needed.

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.)

The bottom of memory is displayed by dashes at the right and left of the display. For example, if you pressed SST repeatedly with the default programs loaded, you would eventually reach a g NOP/end of memory marker line that would look like:

```-       35 01 -
```

If the last line of memory contains an instruction that is not g NOP, the display in W/PRGM mode shows a dash at the right side to indicate that memory is full. For example, if you pressed the ENTER key somewhere in a program and this filled the memory, the ENTER keycode would appear as:

```           41 -
```

GTO lbl can be used in run mode to position the calculator to lbl. The calculator can then be switched to W/PRGM mode for editing at that label (after the LBL instruction). Unlike the instructions above, this one is recordable so the position of the W/PRGM-RUN switch must be properly set. (If the label was not found, the calculator will be positioned at the top of memory when you enter W/PRGM mode.)

RTN can be pressed in RUN mode to reset the program counter to the top of memory marker (00 00).

### Merged Keycodes

Only some key combinations are merged. They are: g LST x, g x=y, g x≠y, g x>y, and g x≤y, g NOP, g R↑, g R↓, STO 1-8 and RCL 1-8. STO 9 and RCL 9 were not merged to remind you that register 9 is overwritten by trigonometric functions, polar/rectangular conversions and comparison tests.

To save a program to magnetic card, make sure the W/PRGM-RUN is switch set to W/PRGM and place a card (label side up) in the card reader/writer. You may cut off the corner of a magnetic card to prevent it from being overwritten. Cards can be marked with a permanent felt tip pen.

To load a program, make sure the W/PRGM-RUN is switch set to RUN and insert the card.

The registers and the stack are not changed when a program is written or read. If the checksum does not match the one recorded on the card, the display will blink and the memory will be filled with g NOPs. Press any key to clear it and insert it again to retry.

Note that as on the HP-67, it's possible to insert the card in the other direction (still label side up) to record a backup or a different program on a second track. There is no easy way to label the second program and you must be very careful if you wish to protect it by clipping its corner since you have no guide to follow. HP also warned that the motor roller is over the second track so after heavy use, the 2nd track may not read properly.

## Programming Techniques

The calculator uses label addressing. Labels available are the upper-case letters A through E and the digits 0 through 9. Only labels A-E can be directly executed from the keyboard. The numeric labels are for use internal to the programs. 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

• GTO lbl branches to label lbl.
• alphalbl calls a subroutine alphalbl where alphalbl is A-E. (Note that only A-E may be called as subroutines and that there is no GSB instruction preceding them.)
• RTN returns from a subroutine.

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 switching to W/PGRM mode and pressing f PRGM:

```LBL
A          Gives the program the name A
f-1        These instructions are just the normal keystrokes
√x         for computing the area given the radius in X
g π        (f-1 means inverse of shifted function. f-1 √x means x2)
×
RTN        (return) Finishes the program
```

Now add the cylinder program: (If you already have program A loaded use SST in W/PRGM mode to step past it or press RTN in RUN mode to move to the top of memory and insert this routine in front of it.)

```LBL
B
A          call A as a subroutine
×          multiply result of A times length.
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. The HP-65 only allows a single level of subroutines. In this example, if A had called another subroutine C, C would have executed but instead of returning to A, it would have returned to B.

### Conditional Tests and Flags

The HP-65 has instructions for comparing X to Y and if the comparison is true the calculator executes the next two steps. If the comparison is false the calculator skips the next two steps.

The next two steps are most commonly a GTO like:

```g X=Y
GTO        Go to label 1 if X is equal to Y
1          otherwise skip over these two steps
STO 2      The two steps above are skipped and execution continues here
if the contents of the X and Y registers are not equal.
```

This example computes the arc sine of a value in X (-1 ≤ x ≤ 1). If the result is less than or equal to zero, it adds 360 to the angle. Switch to W/PRGM mode, press f PRGM and then enter:

```LBL
D       The program is started with the D key
f-1
SIN     Arc Sine
0
g x⇔y   Swap the angle and the 0
g x≤y   Test the angle for being ≤ 0
3       Execute these two steps only
6       if the angle is ≤ 0
0
RTN
```

Switch back to RUN mode and .5 D will display 30.00 and .5 CHS D will display 330. To verify that you've entered the program correctly, press RTN while in RUN mode and then switch to W/PRGM mode which will display 00 00. Stepping through the program with SST you should see:

```00 00     top of memory marker
23        LBL row/column code
14        D
32        f-1
04        SIN (04 is the digit code for the 4 key)
00        0
35 07     g x⇔y
35 22     g x≤y
03        3
06        6
00        0
61        +
24        RTN
35 01     g NOPS fill the space after your program
```

Note that in cases where only a single step needs to be executed or skipped based on a condition, you should place a g NOP in the 2nd skippable step.

The calculator also has two flags that can be set and tested in program execution. Flag one is controlled by the functions:

f SF 1
Set flag 1 (turn it on.) This can be entered from the keyboard or in a program.
f-1 SF 1
Clear flag 1 (turn it off.) This can be entered from the keyboard or in a program.
f TF 1
Test if flag 1 is on. This function is used in programs only. If flag 1 is on, the next two program steps are executed, otherwise, they are skipped.
f-1 TF 1
Test if flag 1 is cleared. This function is used in programs only. If flag 1 is off, the next two program steps are executed, otherwise, they are skipped.

There is a similar set of instructions (f SF 2 etc.) for flag 2. Both flags are command-cleared meaning that testing doesn't alter their settings.

### Looping

The calculator provides the DSZ (decrement and skip on zero) instruction which decrements register 8 each time it's executed. As long as R8 is not equal to 0 the following two steps are executed. When R8 becomes 0 the following two steps are skipped. The R8 register should contain only integers.

For example, here is a program that takes the number in the X register, saves it in R8, displays it and waits for the user to press R/S. Then it decrements R8, and loops back to the display portion for the user to press R/S again. This continues until register 8 is equal to zero.

```LBL
A
STO 8
LBL
0
RCL 8
R/S
g
DSZ
GTO
0
RTN
```

### 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. (Pressing any key will stop a running program.)

For example, the following program calculates the cumulative cost of various quantities of differently prices items at a 15% discount. Slide the W/PRGM-RUN switch to W/PRGM, press f PRGM if necessary*, and then press.

```LBL
A          Program is named A
f
STK        Initialize by clearing the stack
LBL        Label for looping
3
R/S        Stop so user can type quantity
ENTER      Copy quantity to Y
R/S        Stop so user can type price
×          Multiply quantity by price
.
8
5          (1 - 15% discount)
×          Discounted price
GTO
3          Go back for more data
```

There is no need for a RTN on this program since it is an infinite loop with R/S instructions to allow data input.

Slide the W/PRGM-RUN switch to RUN and press A to begin. You will then enter a quantity, press R/S, a price at that quantity and press R/S again which will cause the running total to be displayed. You can start a new problem by pressing A again. For example, if you make the following purchases: 5 @ \$2 each, 7 @ \$4 each you would press: A 5 R/S, 2 R/S (which would display the running total of 8.5 at the 15% discount) 7 R/S, 4 R/S which would display a total of 32.30.

*Above it said "press f PRGM if necessary". This program uses label A which was used in other examples. The calculator 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 calculator 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 .

When a subroutine is called, the HP-65 keeps track of the return address by inserting a marker instruction into the program memory. If you stop a program while in a subroutine, you can switch to PRGM mode, move to the return address and actually see the marker. (Which displays as a 41 keycode - same as Enter.) Only one level of subroutine is allowed.

It is possible to use R8 and DSZ as a self-clearing flag. Just set R8 to 1 and then use DSZ as the test instruction. On the first pass, R8 will be decremented to zero and the next two steps will be skipped. On future passes, the next two steps will be executed because R8 will no longer be zero.