TECHNICAL NOTES
Beginner Level

WRITING YOUR FOURTH HP 48 PROGRAM

New Commands/ideas Used: STO, STO+,
CLEAR, DROP, FIX, OVER, modules

Marci, the owner of a small fast food restaurant, wants to use her HP 48 to display/print orders. Since she only offers four food items she plans on a single menu with her items plus a menu to start an order and a menu to total the order. Here is her menu layout.

ORDR DCOK BRGR FRYS CBRG TOTL

Where:

Marci decides that the program should work as follows. ORDR should initialize variables and display the words N E W O R D E R. The four food menus supply a description and price. A quantity is input and the desired food menu key is pressed. The display should show a running "invoice" as shown in figure 1.


                           3 Frys @1.29=$3.87       Display right
                            3 DCok @.79=$2.37       justified.
                           4 CBrg @2.05=$8.20
Figure 1 - Invoice form building in HP 48 display

The display should be right justified and as each item is added a new line should be shown. Marci decides that the four food menus should have a program for each with the, easily changed, form:

<< " DESC" PRICE P >>

Where:

When the order is finished the TOTL key is pressed and three lines are added as shown in figure 2. Only four food items per order are able to fit in the display. This is acceptable to Marci.

                           3 Frys @1.29=$3.87           Note:
                            3 DCok @.79=$2.37           Show all
                           4 CBrg @2.05=$8.20           display lines
                            Order Total=$8.20           right justified.
                              Sales Tax=$1.12
                            Amount Due=$15.56
Figure 2 - Finished Order Invoice Display

The program is now designed and it is time to start writing code. Since Marci is a beginner at programming, and she is expects lots of problems debugging and changing the program later, she decides to write the program in modules. The modules will have single letter names to keep everything as simple as possible. See Table 1.

The first module she calls 'M' for make the input a string. The input is the quantity ordered. Having difficulty keeping everything coordinated on the stack she decides to use local variable storage.

'M'  << STD ROT -> p q << q SWAP + 2 FIX " @" + p + "=$" + p q * DUP 'T' STO+ + >> >>
        100.5 Bytes  # CA66h

The input quantity is to be displayed as an integer and STD does this. ROT puts the stack in order for local variable storage - p is the price from the food menu program, q is the input quantity. The sub program brings the quantity to the stack, SWAP orders the food menu provided description for the first + to start building the string. The next part of the string requires dollars and cents so the display mode is changed with 2 FIX. The rest of the string is built in a straight forward manner. When p and q are multiplied for the dollar total for an item it is also added to a variable T (TOTAL) with STO+. T is to be used later when the order is complete and totaled.

This is actually the most difficult part of the program. During the debugging process Marci used the food menu program:

<< " DESC" PRICE M >>

Using FRYS with 3 as an input the result is: "3 Frys@1.29=$3.87" The next step is to add spaces in front of the string to make it 22 characters long to right justify the text. See "Writing Your Third HP 48 Program" for a discussion of these techniques. This Module is called 'R'.

                               
'R'  << 22 OVER SIZE - 1 SWAP "      (11 SPACES)     " ROT ROT SUB SWAP + "  " + >>
        70 Bytes  # B16Bh     Note:     above, is the end line character.

A new technique used in 'R' is the addition of the end line character," " in the program, (above the decimal point key) at the end of the 22 character string. This permits a single DISP to show all lines with a single string input.

Marci's next module is the string storing module, 'S'.

'S' << D SWAP + 'D' STO >> 31.5 Bytes # BC4Bh

'D' is a variable used to store the display string. 'S' simply takes the text on level one and appends it to the string stored in 'D'.

At this point Marci's test food menu program looks like:

<< " DESC" PRICE M R S >>

With a quantity input the string is Made, Right justified, and Stored. The next step is to display the input. This is done with module 'I'.

'I' << D CLLCD 0 DISP 0 FREEZE >> 27 Bytes # BBABh

Now each food menu looks like:

<< " DESC" PRICE M R S I >>

Next Marci makes the process module 'P'.

'P' << M R S I >> 28 Bytes # AE6Bh

Now the food menu is as it was specified in the design description. The individual modules that make up the process were written one by one and tested step by step.

<< " DESC" PRICE P >>

Now Marci keys in the specific food menu data for the four food menus. Next the two programs 'ORDR' and 'TOTL' are written.

'ORDR'  << ""  'D' STO 0 'T' STO CLEAR "  N E W  O R D E R" CLLCD 3 DISP 0 FREEZE >>  
           80.5 Bytes  # 5BFAh

'D' needs to be initialized to an empty string because 'I' assumes a string exists to append the current input string. A zero is stored in the Total 'T'. CLEAR keeps the stack clear to avoid confusion. The NEW ORDER text is displayed on line 3.

'TOTL'  << "Order Total=$" T + R S T .0775 * "Sales Tax=$" 
           OVER + R S T + "Amount Due=$" SWAP + R S I >>
           134 Bytes  # FAA0h

A quick look at 'TOTL' and the repeating sequence, R S T, suggests one last module. R S T is used twice, and, if T is eleminated, a third time. Module 'O' is added.

'O' << R S T >> 23.5 Bytes # E140h

Module 'O' in 'TOTL' makes it a little simpler:

'TOTL'  << "Order Total=$" T + O .0775 * "Sales Tax=$" OVER + O + "Amount Due=$" SWAP 
           + O DROP I >>
           114 Bytes  # 16Ch

Everything checks out OK. After seeing the program run several times Marci decides that the display would look better if it were not right justified but having two spaces on the right. This is an easy change with modules. Simply change the 22 in 'R' to 20.

Making changes to the program is one of the advantages of using modules. Since some modules are used many times in the program they act like subroutines and often memory is saved. Multiple programs in the menu causes unpleasent menu clutter and this is a drawback. In this application the required six menus are ordered together. The others are "hidden" from normal view. Here is a summary of the modules.

TABLE 1 Module Summary
NameDescriptionBytesSum
ORDR New Order, initializes variables.80.55BFAh
DCOKDiet Coke @ .7935.0EE39h
BRGRHamburger @ 1.7535.0D1E7h
FRYSFrench Frys @ 1.2935.039D4h
CBRGCheezeburger @ 2.0535.088EDh
TOTLtotal and display tax and amounts114.016Ch
MMake input a string100.5CA66h
R Right justify text70.0B16Bh
SStore display string in D31.5BC4Bh
D Display string storage**
IInput display27.0BBA8h
PProcess input, display, etc.28.0AE6Bh
O Output process, display, etc.23.5E140h
TTotal storage* *
Total Bytes615.0
* variable.

This programming exercise illustrates some basic ideas. While the program works it is not adaquate for real world use. To make a bullet proof program more advanced programming techniques are required. Here is a list of improvements that should be addressed.

The basic ideas of this series provide some insight into HP 48 programming. Making this program "real world bullet proof" requires more advanced programming techniques such a libraries, key assignments, KEY, and others. This is the last of the series. Congratulations! You have finished your first EduCALC HP 48 Programming class! It probably took six hours of intensive work to get this far.

RJN REV B 960925

<-- Back to Technical Notes Index