This is theprint version ofProgramming HP Calculators You won't see this message or any elements not part of the book's content when you print orpreview this page. |
The current, editable version of this book is available in Wikibooks, the open-content textbooks collection, at
https://en.wikibooks.org/wiki/Programming_HP_Calculators
This book is an unofficial guide to programming your HP calculator. It will teach you the basic commands mentioned in the official manual but will also include some other commands, such as Easter eggs and glitches.
The fact that HP calculators are programmable means that they are not calculators but computers. Although the language used by the calculators is simple it is similar to many of the complex languages used on PCs, making it a great starting point for people who want to learn more about programming.
1►L:DISP L; "Hello,":DISP 2; "World!":FREEZE:
This program basically does the following:
Type this example into a new program so you can see what it does.
BTW: 'Hello, World!' is a standard program for most programming tutorials and guides. It has no real significance that I know of.
On HP calculators you can store numbers into variables. This is done with the '►' or '1►L:DISP L;' symbol, like in the example above. In the example above we store 1 in the variable, 'L'. This means that 'L=1".
On HP calculators there are only prescribed variables, see the variable reference to see them all. Because all variables are prescribed there is no need to declare them at the start of the program like in most languages. Most of the time the A...Z and θ are the only variables used in programs.
Program Commands are instructions for the calculator. A program can contain any number of commands. When your calculator reads a command it will take the appropriate action; however, sometimes we might want that action to be slightly different each time we call it. This is why we have arguments after the command separated by a semicolon. Some commands have an unlimited number of arguments but most have somewhere between 0 and 8 arguments.
So that means that commands are formatted as:
COMMAND <arg1>; <arg2>; ... <arg#>
The arguments can be of various types. Sometimes they are numbers and sometimes they are strings (a collection of letters and symbols with quotation marks on each side). The 'DISP' command, for example, uses a number and a string for its arguments. It has the format:
DISP <line#>; <string>
The first argument is a number and the second argument is a string. This command basically tells the calculator to write 'string' on line 'line#'. If you want a more detailed description check the command reference.
Also, at the end of every command or variable assignment we need to put a colon so that the calculator knows that the command has ended.
Because it can be slow to type the commands and variables manually and they are easy to misspell you can select them from menus. These menus can be accessed by pressing theMATH button for commands and theVAR button for variables.
There are several subsets of the calculator commands. They are presented here along with a short summary of their features.
This section is currently incomplete. The status of each sub-section is written in italics below its link.
Current status: 0 of 4 commands completed.(0%)
The aplet commands are the commands used for buildingaplets (notapplets) on the calculator. Most of these are fairly advanced, and you should have mastered other programming skills before you use the aplet commands, as they work on existing programs.
Current status: 4 of 5 commands completed.(80%)
The branch commands are used to set the program to do different tasks depending on a certain situation. More information on branching (or "jumping") in programming can be found at Wikipedia:Jump instruction.
Current status: 7 of 7 commands completed.(100%)
The drawing commands are used for drawing images on the screen. They are differentiated from graphic commands by the fact that they interface with the display, not graphic objects (GROBs). That is to say, using a command such as ARC will draw on the screen, not in a GROB.
Current status: 6 of 12 commands completed.(50%)
The graphic commands are used for creating and modifying GROBs - GRaphic OBjects. These are essentially variables on the calculator from G0 to G9 that can contain graphic information, ie. a 1-bit bitmap.
| Commands included in this section | |||
|---|---|---|---|
| DISPLAY→ | →DISPLAY | →GROB | GROBNOT |
| GROBOR | GROBXOR | MAKEGROB | PLOT→ |
| →PLOT | REPLACE | SUB | ZEROGROB |
Current status: 0 of 4 commands completed.(0%)
The loop commands are used to set the calculator to keep performing an action until a certain condition is satisfied. For example, to display all the numbers from 1 to 100, you could use a loop command, rather than individually writing code to display each number.
Current status: 1 of 10 commands completed.(10%)
The matrix commands are used to edit and modify matrices, ie. the variables M0 to M9.
| Commands included in this section | |||
|---|---|---|---|
| ADDCOL/ROW | DELCOL/ROW | EDITMAT | RANDMAT |
| REDIM | REPLACE | SCALE | SCALEADD |
| SUB | SWAPCOL/ROW | ||
Current status: 0 of 3 commands completed.(0%)
The print commands are used to send information to HP's separately-sold infrared printer. They are generally not very useful.
Current status: 7 of 10 commands completed.(70%)
Prompt commands are used to ask the user for information, or alert the user to some fact. These are amongst the most important commands available for your use.
Current status: 0 of 7 commands completed.(0%)
The stat commands are actually two groups of commands, Stat-One and Stat-Two. These two groups represent one-variable and two-variable statistics modes, respectively. They are used for editing statistics and performing statistical functions.
Current status: 8 of 8 commands completed.(100%)
An "easter egg" is a common term used to refer to "hidden" or undocumented features in a program or operating system, in this case, the calculator. The commands documented here are not in the CMDS menu, and they are not documented by HP. Most of them don't serve any useful purpose.

Syntax
IF <test> THEN <clauses>:ELSE <clauses>:END:
Or for "nesting" IF statements:
IF <test> THEN <clauses>:ELSE IF <test> THEN <clauses>:ELSE <clauses>:END:ELSE <clauses>:END:
Or for running IF such that no action is performed should the test be false:
IF <test> THEN <clauses>:ELSE END:
Detail
Example
PROMPT A:IF A==0 THENMSGBOX "You entered zero.":ELSE MSGBOX "You didn't enter zero.":END:
This is a basic example, only checking for one result. Here's a more complex one, although multiple IF...THEN statements are usually best replaced with CASE.
PROMPT A:IF A==0 THENMSGBOX "You entered zero.":ELSE IF A>0 THENMSGBOX "You entered a positive number.":ELSE END:ELSE IF A<0 THENMSGBOX "You entered a negative number."ELSE END:ELSE END:
Note that the additional "ELSE IF" commands displayed are not actually commands per se. What is actually happening is that the result of the previous test, when evaluated as "false", moves on to the ELSE result, where it encounters IF, telling it to startanother IF statement. This is what the "ELSE END" commands do - they close off the secondary statements nested within the original statement.


Syntax
CASE IF <test> THEN <clauses>: END ... END:
Detail
CASE is the command that is used in the place of multiple statements.
The best way of describing this is with a flowchart, seen right. Compare to the lower IF flowchart, and you can see how the CASE command is more efficient when there is more than one option.
Example
CASE is especially useful when accepting input from a CHOOSE list. For example:
1?A:CHOOSE A;"FAVOURITE APPLE?";"Granny Smith";"Golden Delicious";"Pink Lady":CASEIF A==1 THENMSGBOX "Granny Smiths are my favourite, too!":ENDIF A==2 THENMSGBOX "Golden Delicious apples are OK, too.":END IF A==3 THENMSGBOX "Bleh, I can't stand Pink Ladies!":ENDEND:MSGBOX "Program finished.":
The first five lines initiate the CHOOSE menu. Of particular note is the 1?A at the start, this sets the default value of the menu to 1 (ie. the first option) and thus ensures that the menu operates correctly (if the value of A begins as outside the acceptable value for the menu, the highlighting will not operate correctly).
CASE initiates the CASE statement. You can then see three branches of this statement; one for each of the three menu options specified in CHOOSE. The basic syntax is:
CASE IF <clause 1> THEN <action set 1> END IF <clause 2> THEN <action set 2> END ... END:
Importantly (and counter-intuitively), the END at the end of an IF/THEN/END statement within CASE doesnot contain a full-colon at the end of it! However, the final END completing the CASE does require one.
You should be familiar with the MSGBOXs in each action set, these are a simple way of displaying text in a dialog and FREEZEing at the same time. The final MSGBOX is included to demonstrate what happens once the command completes; it skips to the final END:, so if you had somehow tricked the program into giving a non-valid answer (ie. changing the input for A) the program would just display the final MSGBOX. The same happens after the appropriate clause has been run for A==?.
Syntax
RUN "<program-name>":
Detail
Dangers
Example
This program is called "INFINITE".
MSGBOX "You can't escape!":RUN "INFINITE":
It should be noted that you can "escape", just press ON - this usually cancels the current command from running and this feature is designed specifically to allow users to cancel programs stuck in an infinite loop due to a programming error or malicious intent by the software author.
Syntax
STOP:
Detail
Example
This program is called "EXAMPLE".
MSGBOX "Want to exit? 1 = yes, 0 = no.":PROMPT A:IF A==1 THEN STOP:ELSE RUN "EXAMPLE":
This command is practically self-explanatory.
The drawing commands are somewhat confusing in that they reference parts of the 'plot', not parts of the physical screen.
It is intuitive to assume that (knowing that the screen is 131x64 pixels wide, and that the calculator references points by a Cartesian-like system) a pixel at (1,1) willalways be the most bottom-right pixel on the screen.
However, this is not the case. (1,1) is the pixel at (1,1) on the infinite 'plot'. This means that it willonly be the bottom-right pixel, as expected, when the plot is set to the dimensions 1?X?131 and 1?Y?64. This can be done by setting the (X/Y)(MIN/MAX) commands to appropriate values:
1?XMIN:131?XMAX:1?YMIN:64?YMAX:
Adding this code to the start of your program will set the calculator's plot size to these specifications automatically, ensuring smooth operation of your program. However, upon the program's completion, any graph the user might have set up will be displayed on a (presumably) incorrect plotsize, which may frustrate the user. You could work around this like follows:
XMIN?A:XMAX?B:YMIN?C:YMAX?D:1?XMIN:131?XMAX:1?YMIN:64?YMAX:<program code goes here>A?XMIN:B?XMAX:C?YMIN:D?YMAX
Of course, this adds bloat to the program somewhat, but will resolve the problemas long as you don't use the variables you selected in the program for other purposes. An alternate method would be to store the values in a list.
Syntax
ARC <cent-x>;<cent-y>;<radius>;<start>;<end>:
Detail
Dangers
1?HAngle:
Example
ARC 65;32;15;0;360:
Draws a full circle of radius 15px, from the center of the screen.
ARC 65;32;15;10;80:ARC 65;32;15;100;170:ARC 65;32;15;190;260:ARC 65;32;15;280;350:
Draws a full circle comprised of four arcs, but the circle is missing a arc of 20° at each cardinal point.Represented more concisely using a loop:
FOR X=0 TO 270 STEP 90;ARC 65;32;15;10+X;80+X:END:
Syntax
BOX <x1>;<y1>;<x2>;<y2>:
Detail
Example
BOX 1;1;131;64:
Draws a box around the border of the screen.
BOX 1;1;131;64:BOX 2;2;130;63:
Draws a border 2 pixels thick around the screen. More concisely:
FOR X=0 TO 1;BOX 1+X;1+X;131-X;64-X:END:
This allows you to easily adjust the thickness of your border by simply adjusting the FOR command; for "FOR X= TO Z" the border will always be (Z-1) pixels thick.
Syntax
ERASE:
Detail
Syntax
FREEZE:
Detail
Syntax
LINE <x-start>;<y-start>;<x-end>;<y-end>:
Detail
Example
LINE 1;1;131;64:LINE 1;64;131;1:
Draws a cross across the screen.
Syntax
PIXON <x>;<y>:PIXOFF <x>;<y>:
Detail
Dangers
Example
FOR X=1 TO 131 STEP 1;FOR Y=64 TO 1 STEP -1;PIXON X;Y:END:END:FOR X=131 TO 1 STEP -1;FOR Y=1 TO 64 STEP 1;PIXOFF X;Y:END:END:
Fills the screen with pixels from left-to-right, top-to-bottom, then reverses the process.
Syntax
TLINE <x-start>;<y-start>;<x-end>;<y-end>:
Detail
Example
ERASE:DISPLAY? G1:GROBNOT G1:?DISPLAY G1:TLINE 1;1;131;64:TLINE 1;64;131;1:
Draws a cross in white across the screen, after turning on all the pixels. Therefore, it acts the same as:
ERASE:LINE 1;1;131;64:LINE 1;64;131;1:DISPLAY? G1:GROBNOT G1:?DISPLAY G1:
Syntax
DISPLAY→ <name>:
Detail
Example
DISPLAY→ G1:ERASE:WAIT 2:→DISPLAY G1:FREEZE:
Stores the display in GROB G1, clears the screen, waits two seconds, then reloads the display on the screen and pauses. (Effectively useless without other graphic commands).
Syntax
→DISPLAY <name>:
Detail
Example
See example for DISPLAY→.
Syntax
GROBNOT <name>:
Detail
Example
ERASE:DISPLAY→ G0:GROBNOT G0:→DISPLAY G0:FREEZE:
This program will erase the screen (ie. turn all pixels OFF), then invert the colours (ie. turn all pixels ON), giving you a black screen.
Syntax
MAKEGROB <name>; <width> <height><hex-data>:
Detail
| 0: 0000 or □□□□ | 1: 1000 or ■□□□ | 2: 0100 or □■□□ | 3: 1100 or ■■□□ |
| 4: 0010 or □□■□ | 5: 1010 or ■□■□ | 6: 0110 or □■■□ | 7: 1110 or ■■■□ |
| 8: 0001 or □□□■ | 9: 1001 or ■□□■ | A: 0101 or □■□■ | B: 1101 or ■■□■ |
| C: 0011 or □□■■ | D: 1011 or ■□■■ | E: 0111 or □■■■ | F: 1111 or ■■■■ |
Example
MAKEGROB G1; 12 12FFF10810899999910850A999168108FFF:
■■■■■■■■■■■■■□□□□□□□□□□■■□□□□□□□□□□■■□□■■□□■■□□■■□□■■□□■■□□■■□□□□□□□□□□■■□■□□□□□□■□■■□□■■□□■■□□■■□□□□■■□□□□■■□□□□□□□□□□■■■■■■■■■■■■■
The above MAKEGROB code translated into the creation of the GROB roughly approximated by the ASCII sketch above.
Syntax
EDITMAT <matrix>:
Detail
Example
MSGBOX "This program multiplies two matrices.":MSGBOX "Please enter the first matrix.":EDITMAT M1:MSGBOX "Please enter the second matrix.":EDITMAT M2:M1*M2?M3:MSGBOX "The result of the multiplication is stored in M3."
This program will multiply two matrices, as detailed in the MSGBOX commands.
Syntax
RANDMAT <matrix>;<#rows>;<#columns>:
Detail
Example
RANDMAT M1;50;50:
Makes a really big matrix filled with random integers that wastes a fair bit of your calculator's memory, as well as drains the batter power writing all that info to memory. Useful!
Note: This information was obtained via trial and error and may be incorrect, pending verification.Splintax 12:57, 8 February 2006 (UTC)
Syntax
SCALE <matrix>;<factor>;<row>:
Detail
Example
[[1,2],[3,4]] |STO| M1:SCALE M1;5;1:EDITMAT M1:
Generates the 2x2 matrix with [1 2] on the top and [3 4] on the bottom row, then multiplies the top row (row 1) by 5 to make it [5 10].
Note: This information was obtained via trial and error and may be incorrect, pending verification.Splintax 13:03, 8 February 2006 (UTC)
Syntax
SCALEADD <matrix>;<factor>;<rowa>;<rowb>:
Detail
Example
[[1,2],[3,4]] |STO| M1:SCALEADD M1;5;1;2:EDITMAT M1:
Generates the 2x2 matrix with [1 2] on the top and [3 4] on the bottom row, then adds the top row multiplied by 5 (that is, [5 10]) to the bottom row to produce [8 14].
Syntax
BEEP <freq>;<time>:
Detail
Example
INT(RANDOM*9+1)?R:MSGBOX "Guess the number between 1 and 10."WHILE A?R REPEATPROMPT A:IF A==R THENBEEP 400;.2:BEEP 600;.5:MSGBOX "You win!":STOP:ELSE BEEP 600;.2:BEEP 400;.5:IF A>R THEN MSGBOX "Lower..":ELSE MSGBOX "Higher..":ELSE END:ELSE END:END:
This program generates a random number from 1 to 10, and prompts the user for a guess. If the user guesses correctly, the calculator beeps twice with rising pitch (typical "correct" sound). Otherwise, it beeps twice with lowering pitch (typical "wrong" sound), and tells the user if the answer is higher or lower than what they specified.
Syntax
CHOOSE <variable>;"<title-name>";"<option-1>";"<option-2>";..."<last-option>":
Detail
Dangers
Syntax
DISP <line>;"<text>":
Detail
Dangers
Example
ERASE:DISP 1;"Hello World!":FREEZE:
Displays the ubiquitous "Hello World" on the first line on the screen, after erasing the screen, and freezes the program at the end so that the user has time to read the message.
1?A:1?B:ERASE:FOR X=1 TO 500;A+B?C:B?A:C?B:B/A?P:DISP 1;B":"A:DISP 2;P:
Advanced: Approximatesphi using the ratio between increasingFibonacci numbers. The ability of DISP to erase the line before displaying its payload creates an interesting-looking "reiterative" effect.
Syntax
DISPTIME:
Detail
Example
This program is called "TimeUtils".
1?M:CHOOSE M;"Display time";"Set time":CASEIF M==1 THENDISPTIME:STOP:ENDIF M==2 THENMSGBOX "Hour? (24h time)":PROMPT H:MSGBOX "Minute?":PROMPT M:H+M/100?TIME:MSGBOX "Set. Date?":PROMPT D:MSGBOX "Month?":PROMPT M:MSGBOX "Year?":PROMPT Y:D+M/100+Y/1000?DATE:MSGBOX "Set.":RUN "TimeUtils":ENDEND:
This program is a multi-function application that allows you to both display and edit (correct) the current time. Most of it should be self-explanatory, given the information about the DATE and TIME variables in Details.
SeeEDITMAT under thematrix commands section for information on this command.
Syntax
FREEZE:
Detail
Syntax
MSGBOX "<message>":
Detail
Example
PROMPT A:MSGBOX "You entered "A".":
Syntax
AMIGOS:
Details
Syntax
DEMO:
Details
Syntax
HELPWITH <cmd>:
Details
Syntax
LIBEVAL <library number>; <object number>:
Details
Syntax
RULES:
Details
Syntax
SYSEVAL <address>:
Details
Dangers
Syntax
VERSION:
Details
Syntax
WSLOG:
Details
Real variables are the letters A through Z, plus θ (Greek lowercasetheta). They act just like regular numbers stored in the calculator.