Trident Help

RPN Advanced 42 manual · Chapter 16 of 18

Programming

Record keystrokes as a program: labels, running, loops, tests, subroutines, input and output, flags, and program memory.

16.1Program mode and how lines are recorded#

Shift R/S turns PRGM mode on or off. In PRGM mode keys are not executed; they are recorded as numbered program lines. The display shows the current line and the next line, for example 01▸LBL "CIRC". Line 00 is a header showing the program size, and .END. marks the end of memory. All programs share one memory.

PRGM mode on an empty memory. The display shows: 00▸{ 0-Step Prgm } / 01 .END..
PRGM mode on an empty memory.
After recording a label: the line number, a pointer and the instruction. The display shows: 01▸LBL "FX" / LBL RTN INP VEW AVW XEQ.
After recording a label: the line number, a pointer and the instruction.

Numbers are recorded as typed. Two numbers in a row need ENTER between them. Press ← to delete the current line and ▲ / ▼ to move through the program. Shift ▲ and shift ▼ (BST and SST) also step through lines.

Example 16.1

Record and read a program

  1. Turn on PRGM mode and record a label, then x squared, then pi, then multiply and return.

    ⇧PRGMR/S⇧PGM.FCN3Σ+⇧ALPHAENTERΣ+√x1/xLOGLNΣ+Σ+√xENTER⇧x²√x⇧πR↓×⇧PGM.FCN31/x
    After step 1: Turn on PRGM mode and record a label, then x squared, then pi, then multiply and return. The display shows: 05▸RTN / LBL RTN INP VEW AVW XEQ.
  2. Press EXIT to close the menu, then up twice to move back through the lines.

    EXIT▲▲
Result of the example "Record and read a program" The display shows: 03▸PI / 04 ×.
You should see03▸

16.2The PGM.FCN menu#

Shift 3 opens PGM.FCN, the program functions, over three pages.

PGM.FCN page 1: LBL, RTN, INPUT, VIEW, AVIEW and XEQ. The display shows: x: 0.0000 / LBL RTN INP VEW AVW XEQ.
PGM.FCN page 1: LBL, RTN, INPUT, VIEW, AVIEW and XEQ.
PGM.FCN page 2: the test menus, PROMPT, PSE, ISG and DSE. The display shows: x: 0.0000 / X?0 X?Y PMT PSE ISG DSE.
PGM.FCN page 2: the test menus, PROMPT, PSE, ISG and DSE.
ItemWhat it does
LBLMarks a place in the program: a name ("CIRC") or a two-digit number.
RTNEnds the program or returns from a subroutine.
INPUTAsks for a value for a variable; the program stops until you press R/S.
VIEWShows a variable, as name=value.
AVIEWShows the ALPHA register.
XEQRuns a label or calls a subroutine.
X?0Opens the six comparisons with zero.
X?YOpens the six comparisons of X with Y.
PROMPTShows the ALPHA register and stops.
PSEA pause. Programs run instantly in this app, so PSE just leaves X on the display (like VIEW X); it is shown if nothing else is displayed afterwards.
ISG, DSECounting loops (increment or decrement and skip).
MVAR (page 3)Declares a variable of the solver or integrator.

16.3Write and run a program#

A global label has a name such as "CIRC", typed in ALPHA. Run it with XEQ and the name, or press XEQ and choose it from the menu. A program continues until RTN. Local labels are two-digit numbers used inside a program.

Example 16.2

Circle area program

A=πr2A=\pi r^{2}
  1. Record CIRC: label, square, pi, multiply, return.

    ⇧PRGMR/S⇧PGM.FCN3Σ+⇧ALPHAENTERΣ+√x1/xLOGLNΣ+Σ+√xENTER⇧x²√x⇧πR↓×⇧PGM.FCN31/x⇧PRGMR/SEXITEXIT
    After step 1: Record CIRC: label, square, pi, multiply, return. The display shows: y: 0.0000 / x: 0.0000.
  2. Run it with radius 5: XEQ, then the name.

    5XEQ⇧ALPHAENTERΣ+√x1/xLOGLNΣ+Σ+√xENTER
Result of the example "Circle area program" The display shows: y: 0.0000 / x: 78.5398.
You should see78.5398
Example 16.3

Hypotenuse from two legs

  1. Record HYP: square, swap, square, add, square root. (EXIT closes the PGM.FCN menu so that the square-root key is not read as a menu key.)

    ⇧PRGMR/S⇧PGM.FCN3Σ+⇧ALPHAENTER1/x√xXEQ√xLOG√xENTER⇧x²√xx⇄y⇧x²√x+EXIT√x⇧PGM.FCN31/x⇧PRGMR/SEXITEXIT
  2. Enter 3 and 4 and run it.

    3ENTER4XEQ⇧ALPHAENTER1/x√xXEQ√xLOG√xENTER
Result of the example "Hypotenuse from two legs" The display shows: y: 0.0000 / x: 5.0000.
You should see5.0000
Example 16.4

Choose the program from the XEQ menu

  1. Record CIRC, then press XEQ and the first menu key.

    ⇧PRGMR/S⇧PGM.FCN3Σ+⇧ALPHAENTERΣ+√x1/xLOGLNΣ+Σ+√xENTER⇧x²√x⇧πR↓×⇧PGM.FCN31/x⇧PRGMR/SEXITEXIT5XEQΣ+
Result of the example "Choose the program from the XEQ menu" The display shows: y: 0.0000 / x: 78.5398.
You should see78.5398
Example 16.5

Running from the CATALOG

  1. Open CATALOG, PGM, and press the program name.

    ⇧PRGMR/S⇧PGM.FCN3Σ+⇧ALPHAENTERΣ+√x1/xLOGLNΣ+Σ+√xENTER⇧x²√x⇧πR↓×⇧PGM.FCN31/x⇧PRGMR/SEXITEXIT2⇧CATALOG+1/xΣ+
Result of the example "Running from the CATALOG" The display shows: x: 12.5664 / CIRC                   .
You should see12.5664

See also Record a keystroke programNamed programs with variables

16.4Loops with DSE and ISG#

DSE and ISG use a control number of the form ccccc.fffiiccccc.fffii: the counter, a final value and an increment. ISG adds the increment; DSE subtracts it. After the change, if the counter has passed the final value, the next program line is skipped. This is how a loop ends.

The program below sums the numbers from N down to 1. It stores N in register 00 and a running total in register 01.

  1. LBL "SUMN"
  2. STO 00
  3. 0
  4. STO 01
  5. LBL 01
  6. RCL 00
  7. STO+ 01
  8. DSE 00
  9. GTO 01
  10. RCL 01
  11. RTN
Example 16.6

Sum 1 to 5

∑k=15k=15\sum_{k=1}^{5}k=15
  1. Record SUMN, then run it with 5.

    ⇧PRGMR/S⇧PGM.FCN3Σ+⇧ALPHAENTERLN1/xLNLOG√xLOGLOGΣ+ENTERSTO000STO01⇧PGM.FCN3Σ+01RCL00STO+01⇧PGM.FCN3▼XEQ00⇧GTOXEQ01RCL01⇧PGM.FCN31/x⇧PRGMR/SEXITEXIT5XEQ⇧ALPHAENTERLN1/xLNLOG√xLOGLOGΣ+ENTER
Result of the example "Sum 1 to 5" The display shows: y: 1.0000 / x: 15.0000.
You should see15.0000
Example 16.7

Sum 1 to 10

  1. The same program with 10.

    ⇧PRGMR/S⇧PGM.FCN3Σ+⇧ALPHAENTERLN1/xLNLOG√xLOGLOGΣ+ENTERSTO000STO01⇧PGM.FCN3Σ+01RCL00STO+01⇧PGM.FCN3▼XEQ00⇧GTOXEQ01RCL01⇧PGM.FCN31/x⇧PRGMR/SEXITEXIT10XEQ⇧ALPHAENTERLN1/xLNLOG√xLOGLOGΣ+ENTER
Result of the example "Sum 1 to 10" The display shows: y: 1.0000 / x: 55.0000.
You should see55.0000

16.5Decisions with tests#

A test line (such as X<0?) makes the next line run only if the test is true. If it is false, the next line is skipped. The test menus are X?0 and X?Y on PGM.FCN page 2. Each has six comparisons: =, ≠, <, >, ≤, ≥.

The X?0 menu: x=0, x≠0, x<0, x>0, x≤0, x≥0. The display shows: x: 0.0000 / x=0 x≠0 x<0 x>0 x≤0 x≥0.
The X?0 menu: x=0, x≠0, x<0, x>0, x≤0, x≥0.
Example 16.8

Absolute value with a test

  1. Record AV: if X < 0 then change its sign.

    ⇧PRGMR/S⇧PGM.FCN3Σ+⇧ALPHAENTERΣ+Σ+LNLNENTER⇧PGM.FCN3▼Σ+√x+/−⇧PGM.FCN31/x⇧PRGMR/SEXITEXIT
  2. Try it on −4.

    4+/−XEQ⇧ALPHAENTERΣ+Σ+LNLNENTER
Result of the example "Absolute value with a test" The display shows: y: 0.0000 / x: 4.0000.
You should see4.0000
Example 16.9

Test from the keyboard

  1. From the keyboard a test answers Yes or No: is 5 less than 3? (Y=5, X=3, X<Y? is the third key of X?Y).

    5ENTER3⇧PGM.FCN3▼1/x√x
Result of the example "Test from the keyboard" The display shows: Yes / x=y x≠y x<y x>y x≤y x≥y.
You should seeYes

16.6Subroutines and local labels#

XEQ with a two-digit number calls a local label in the same program; RTN returns to the line after the call. GTO with a label jumps without returning. GTO then . and three digits moves the pointer to a line number; GTO . . goes to the end.

Example 16.10

Call a local subroutine

  1. Record SB: call 05, add 1, return; label 05 multiplies by 10.

    ⇧PRGMR/S⇧PGM.FCN3Σ+⇧ALPHAENTERLN1/xΣ+1/xENTERXEQ051+⇧PGM.FCN31/x⇧PGM.FCN3Σ+0510×⇧PGM.FCN31/x⇧PRGMR/SEXITEXIT
  2. Run with 3: (3 × 10) + 1.

    3XEQ⇧ALPHAENTERLN1/xΣ+1/xENTER
Result of the example "Call a local subroutine" The display shows: y: 0.0000 / x: 31.0000.
You should see31.0000

16.7Ask for input and show results#

INPUT asks for a variable: the screen shows the name and its current value, for example R?0.0000. Type the value and press R/S to continue. VIEW shows name=value. AVIEW shows text from the ALPHA register. PROMPT shows the ALPHA text and stops; press R/S to continue.

Example 16.11

Ask for a radius

  1. Record IN: INPUT R, then compute the circle area.

    ⇧PRGMR/S⇧PGM.FCN3Σ+⇧ALPHAENTER1/xLOGLOGΣ+ENTER⇧PGM.FCN3√x⇧ALPHAENTERLNΣ+ENTERRCL⇧ALPHAENTERLNΣ+ENTER⇧x²√x⇧πR↓×⇧PGM.FCN31/x⇧PRGMR/SEXITEXIT
  2. Run it: the program stops and asks for R.

    XEQ⇧ALPHAENTER1/xLOGLOGΣ+ENTER
    After step 2: Run it: the program stops and asks for R. The display shows: R?0.0000 / x: 0.0000.
  3. Type 2 and press R/S.

    2R/S
Result of the example "Ask for a radius" The display shows: y: 2.0000 / x: 12.5664.
You should see12.5664
Example 16.12

Show a variable

  1. Record VW: store 42 in Z and VIEW it.

    ⇧PRGMR/S⇧PGM.FCN3Σ+⇧ALPHAENTERLNLNXEQΣ+ENTER42STO⇧ALPHAENTERXEQLOGENTER⇧PGM.FCN3LOG⇧ALPHAENTERXEQLOGENTER⇧PGM.FCN31/x⇧PRGMR/SEXITEXIT
  2. Run it.

    XEQ⇧ALPHAENTERLNLNXEQΣ+ENTER
Result of the example "Show a variable" The display shows: Z=42.0000 / x: 42.0000.
You should seeZ=42.0000
Example 16.13

Prompt with a message

  1. Record PR: type HELLO in ALPHA, then PROMPT, then 7.

    ⇧PRGMR/S⇧PGM.FCN3Σ+⇧ALPHAENTERLOG√xLNΣ+ENTER⇧ALPHAENTER1/x√xΣ+LN√x√x√x√xLOG1/xENTER⇧PGM.FCN3▼√x7⇧PRGMR/SEXITEXIT
  2. Run it; it shows HELLO and waits.

    XEQ⇧ALPHAENTERLOG√xLNΣ+ENTER
    After step 2: Run it; it shows HELLO and waits. The display shows: HELLO / x: 0.0000.
  3. Press R/S to continue.

    R/S
Result of the example "Prompt with a message" The display shows: y: 0.0000 / x: 7.0000.
You should seex: 7.0000
Example 16.14

Show text with AVIEW

  1. Record AW: HELLO text then AVIEW.

    ⇧PRGMR/S⇧PGM.FCN3Σ+⇧ALPHAENTERΣ+Σ+XEQΣ+ENTER⇧ALPHAENTERLOG1/x√x1/xENTER⇧PGM.FCN3LN⇧PRGMR/SEXITEXITXEQ⇧ALPHAENTERΣ+Σ+XEQΣ+ENTER
Result of the example "Show text with AVIEW" The display shows: OK / x: 0.0000.
You should seeOK

16.8Flags in a program#

Example 16.15

Set a flag and branch on it

  1. Record FL: set flag 01, then if it is set run the next line (100).

    ⇧PRGMR/S⇧PGM.FCN3Σ+⇧ALPHAENTER1/xΣ+√x√xENTER⇧FLAGS6Σ+01⇧FLAGS6√x01100⇧PRGMR/SEXITEXIT
  2. Run it.

    XEQ⇧ALPHAENTER1/xΣ+√x√xENTER
Result of the example "Set a flag and branch on it" The display shows: y: 0.0000 / x: 100.0000.
You should see100.0000

16.9Stop, step and interrupt#

Press R/S in PRGM mode to record a STOP line. When it runs, the program stops there and R/S resumes it. Outside PRGM mode, shift ▼ (SST) runs one program line at a time. A program that loops forever stops itself with the message Interrupted after about a million steps.

Example 16.16

Stop and resume

  1. Record ST: double X, stop, then add 3.

    ⇧PRGMR/S⇧PGM.FCN3Σ+⇧ALPHAENTERLN1/xLN√xENTER2×R/S3+⇧PRGMR/SEXITEXIT
  2. Run it with 5. It stops with 10 in X.

    5XEQ⇧ALPHAENTERLN1/xLN√xENTER
    After step 2: Run it with 5. It stops with 10 in X. The display shows: y: 0.0000 / x: 10.0000.
  3. Press R/S to resume: 13.

    R/S
Result of the example "Stop and resume" The display shows: y: 0.0000 / x: 13.0000.
You should see13.0000
Example 16.17

An endless loop is interrupted

  1. Record IL: label 01 then GTO 01.

    ⇧PRGMR/S⇧PGM.FCN3Σ+⇧ALPHAENTER1/xLOG√x√xENTER⇧PGM.FCN3Σ+01⇧GTOXEQ01⇧PRGMR/SEXITEXIT
  2. Run it.

    XEQ⇧ALPHAENTER1/xLOG√x√xENTER
Result of the example "An endless loop is interrupted" The display shows: Interrupted / x: 0.0000.
You should seeInterrupted

16.10Delete a program#

Use CLP in the CLEAR menu (second key) and choose the label name. It removes that program up to the next global label. Deleting a single line in PRGM mode is done with ←.

Example 16.18

Delete a program by name

  1. Record CIRC, then CLEAR, CLP and its name.

    ⇧PRGMR/S⇧PGM.FCN3Σ+⇧ALPHAENTERΣ+√x1/xLOGLNΣ+Σ+√xENTER⇧x²√x⇧πR↓×⇧PGM.FCN31/x⇧PRGMR/SEXITEXIT⇧CLEAR←1/xΣ+
  2. Try to run it: the label is gone.

    EXIT5XEQ⇧ALPHAENTERΣ+√x1/xLOGLNΣ+Σ+√xENTER
Result of the example "Delete a program by name" The display shows: Label Not Found / x: 5.0000.
You should seeLabel Not Found

16.11Read and paste a program listing#

The Data and programs sheet shows the program memory as a numbered listing with one instruction per line, for example:

  • 01 LBL "CIRC"
  • 02 X↑2
  • 03 PI
  • 04 ×
  • 05 RTN

You can copy this text or paste a listing to replace the program memory. Lines it cannot read are reported so you can fix them.

See also Import and export programs

HP-42S is a trademark of HP Inc.. Trident Calculator is an independent product by South View Studios and is not affiliated with, sponsored by or endorsed by HP. The name is used only to describe the kind of calculator this model resembles; Trident's names, keys, display, fonts and software are its own clean-room design.