The Museum of HP Calculators

# HP-29C/19C Programming

Contents:

## Features

• 98 Lines of fully merged keystrokes (multi-keystroke steps require just one line)
• Nonvolatile program memory
• 16 non-volatile primary storage registers (R0-9 & R.0-.5) Registers .0 - .5 are used by the statistical functions.
• 14 volatile indirectly addressable storage registers (R(16) - R(29))
• Label addressing with 10 Labels (0-9)
• Conditional and unconditional branching based on register values
• Convenient insertion and deletion of program steps
• Indirect addressing via the R0 register
• ISZ and DSZ looping
• Three levels of subroutines

## 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 OFF-PRGM-RUN switch (on the HP-19C) or the PRGM-RUN switch (on the HP-29C) to PRGM. (For simplicity, this switch is refered to as the PRGM-RUN switch from now on.) Then press f CLEAR 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. Step 00 is really just a placeholder. The first keys you press will go into step 01.

Enter the program below to compute the area the circle. The program begins with LBL 0 which indicates that this program can be executed by pressing the GSB 0 keys on the calculator. As you enter the first line of the program below, the HP-19C will display:  01     25 14 00. The HP-29C will display: 01     15 13 00 due to the different layout of its keyboard.

```g LBL 0      Gives the program a name
g x2         These three instructions
g pi         are just the normal keystrokes
x            for computing the area given the radius in X
g 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 GSB 0. 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 GSB 0. 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.)

• f CLEAR PRGM in PRGM mode clears the entire program memory by placing an R/S instruction in each step and sets the program counter to 00.
• SST steps one instruction ahead. In RUN mode the next instruction is displayed when the key is held down and then executed when it is released. The X register is then displayed making this instruction useful for debugging. It can also be used for moving forward without execution in PRGM mode.
• g BST backs up one instruction. This key may be used in RUN or PRGM mode but unlike SST, instructions are never executed.
• g DEL in 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. To delete several instructions, it is easier to start from the bottom since the g DEL key backs up one step each time it is pressed.
• GTO . nn is used to move to the instruction at line nn. This may be used in both PRGM and RUN mode. (No execution takes place in either mode.)

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.) If lbl doesn't exist, the calculator displays Error.

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

## Programming Techniques

The calculators use label addressing. Labels available are 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

• GTO lbl branches to label lbl.
• GSB lbl calls a subroutine lbl that ends with g RTN.
• g RTN returns from a subroutine. Subroutines can be nested up to three levels deep.

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 f CLEAR PRGM:

```g LBL 0      Gives the program a name
g x2         These three instructions
g pi         are just the normal keystrokes
x            for computing the area given the radius in X
g RTN        (return) Finishes the program
```

Now add the cylinder program: (If you already have program 0 loaded starting at step 01, you can press GTO . 05 to position the calculator to the end in order to add the new program or press GTO . 00 to insert routine 1 before routine 0. You can also press GTO .50 to enter it at step 51 if you like -- Just be sure you don't insert the new code into the middle of the old.)

```g LBL 1
GSB 0         Call the routine at LBL 0
x             Multiply by the length
g RTN
```

Now switch to RUN mode, type the cylinder length, press ENTER, then type the cylinder radius and press GSB 1 to display the volume. The program uses the radius in X and calls the subroutine 0 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

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

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

```g 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 R0 register is used for indirect addressing. Numbers can be stored and retrieved from R0 in the usual ways (STO 0, RCL 0) but once a number is placed in R0, it can be used to control other commands by using the i key.

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

• STO i: Store X in register indicated in R0. STO *, -, +, ÷ i are also allowed. R0 can contain 0-29 allowing easy access to all registers (see below.)
• RCL i: Recall the register indicated in R0.
• GTO i: Go to the label indicated in R0 (0-9).
• GTO i: If (-1 <= R0 <= -99) go backward R0 steps (see Rapid Reverse Branching)
• GSB i: Execute the subroutine labeled by R0 (0-9).
• GSB i: If (-1 <= R0 <= -99) call backward R0 steps (see Rapid Reverse Branching)

Note that the use of g is optional in the above functions (i.e. STO g i is the same as STO i).

For STO i and RCL i, values in R0 map to register addresses as follows: (Registers .0 - .5 are also used by the statistical functions as marked. Registers (16) - (29) are only accessible indirectly and are not preserved when the power is off.
 Reg. R0 (i) Reg. Stat R0 (i) Reg. R0 (i) Name Value Name Reg. Value Name Value 0 0 .0 n 10 (20) 20 1 1 .1 x 11 (21) 21 2 2 .2 x2 12 (22) 22 3 3 .3 y 13 (23) 23 3 4 .4 y2 14 (24) 24 4 5 .5 xy 15 (25) 25 5 6 (16) 16 (26) 26 7 7 (17) 17 (27) 27 8 8 (18) 18 (28) 28 9 9 (19) 19 (29) 29

For example:

```8
STO R0
GSB i
```

Would call the subroutine labeled 8. 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 R0is calculated from some formula and/or user input.See the looping constructs section below for an example of using STO R0 and STO i together.

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

### Rapid Reverse Branching

GTO i and GSB i can also be used with a negative number in the R0 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 00. (If the value in R0 would branch to a negative step, simply add 98 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.)

```g LBL 0     Initialize
3
CHS         We'll want to go back 3 steps
STO 0
CLX         Start at zero
f PAUSE     Beginning of the loop
1
+
GTO i       Go back 3 steps
```

### Looping Constructs

Two looping functions are available:

• g ISZ: increment R0, skip if 0
• g DSZ: decrement R0, skip if 0

These instructions increment or decrement R0 each time they're executed. As long as R0 is not equal to 0 the next instruction is executed. When R0 becomes 0 the next instruction is skipped. The R0 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.)

```g LBL 0
STO 0
g LBL 1
RCL 0
f PAUSE
g DSZ
GTO 1
g RTN
```

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

```g LBL 0
9           Start from the top down
STO 0
g LBL 1     Top of the loop
RCL 0
x2
STO i       Store x (R02) in register indicated by R0
g DSZ       Decrement I and...
GTO 1       Go to 1 if R0 is not zero
g 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.

f PAUSE may be used to show the value in the X register for about a second before continuing.

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 f CLEAR PRGM if necessary*, and then press.

```g LBL 0
f CLEAR REG   Initialization
STO 0         Using R0 as just a normal storage register
g LBL 1
ENTER
R/S           Stop to key in price.
x
RCL 0
g %
-
STO + 1      Add to running total in R1.
RCL 1        Recall running total for display.
g RTN
```

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

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 GSB 1 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 GSB 1 key was pressed.

*Above it said "press f CLEAR PRGM if necessary". This program uses labels 0 and 1 which were 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 label downward in memory from the current position. For example:

```g LBL 0
1
g RTN
g LBL 0
2
g RTN
g LBL 0
3
g RTN
```

Would display 1, 2, 3, 1, 2, 3, 1,... on successive presses of GSB 0.