New Commands used:
, INPUT, OBJ
, SWAP, DUP, DUPN, ROT, LASTARG
Your "first" HP 48 program (TN 52C) calculates the area of a circle assuming the radius input is on the stack. Pressing the AREA menu key runs the program. The output is "labeled" Area: N using a Tag. The problem here is similar except you use three inputs instead of one. This problem illustrates how to make a simple prompt so the user knows what to input and in what order. The examples 'RSA1' thru 'RSA5' in this handout solve the problem described in the box below using different methods. The last program was written by Joseph in response to the question "Can you inprove this ('RSA4') program?" Making programs faster and using less memory are two of the most important goals of our class. Programs are usually presented in order of increasing efficency.
Write a program to calculate the surface area of a rectangular box of dimensions h, w , d. (Height, Width, Depth) Test using 1, 2, 3. |
The equation for the Rectangular box Surface Area is: 2(hw + hd + wd). Each of the three variables is used (multiplied) twice. One method of using variables as needed, is to store them as local variables using the v << . . . >> structure. Any number of variables may be stored, v, in temporary memory and recalled to the stack by name as needed. 'RSA1' utilizes a varient of this method. Inputs h, w, & d are assumed to be on the stack.
'RSA1' << h w d '2*(h*w+h*d+w*d)' "SArea"
TAG >>
The method is straightforward and the local variables h, w, d, are available while the algebriac is (automatically) evaluated. Using local variable storage is convenient, but not especially efficient. The actual equation is solved to calculate the answer. 'RSA2' uses a more conventional calculation process instead of solving an expression or equation.
'RSA2' << h w d << h w * h d * + w d * + 2 * "SArea"
TAG >> >>
Suppose the user doesn't know what the inputs are, or how many? You may display a prompt for each input to guide the user through the program. The INPUT command provides a convenient means for this. 'RSA3' adds prompting messages to the front of 'RSA2'.
'RSA3' << "Height, ENTER" "" INPUT OBJ "Width, ENTER" "" INPUT OBJ
"Depth, ENTER" "" INPUT OBJ
h w d << h w * h d * + w d * + 2 * "SArea"
TAG gt;> >>
When the INPUT command is executed the program halts and displays the first string at the top of the LCD with an active command line. You press the required keys followed by ENTER. INPUT will cause the key sequence you press to be converted to a text string. The second (empty) text string ahead of INPUT is used for other features of the INPUT command. You want real numbers not a text string and this conversion is made by the object out, OBJ, command. After the third input is entered the three numbers are on Levels one thru three of the stack and the rest of the program is identical to 'RSA2'.
'RSA2' takes 33 Milliseconds to calculate "SArea: 22" using 1, 2, & 3 as inputs. The program requires 88 bytes of memory. 'RSA3' can not be timed because of the prompts. Adding the prompts, nearly doubles the program size. Often the core of a program, the part that does the real work, is only a small part of the total because the user interface requires memory hungry text.
'RSA3' provides a prompting, user dialog, type of interface. This is most useful for first time users. Experienced users know what the inputs are and don't need the prompts. The intended user is an important aspect of designing programs. The HP 48 G series added dialog boxes as a means of promting the user. The S series stack-input-menu-key user interface is still included and the user has the convenience of using either of the two types of interfaces.
If you test 'RSA3' (or 'RSA1' or 'RSA2') you will find that any order of the inputs give the same results. You could also observe this from the mathematics of the equation. It is observations like this that programmers study to make the program as simple as possible.
'RSA2' may be improved if the stack is used to store the variables. This method will usually save time, and often shorten the program. 'RSA4' does the same calculations using only the stack. Can you improve this program?
'RSA4' << 3 DUPN * SWAP ROT * + SWAP ROT * + 2 * "SArea" TAG >>
'RSA2' and 'RSA4' contrasts convenience with efficiency. Each have their uses. Our class programming philosophy, however, favors faster (27%) and shorter (38%) methods. Here is how 'RSA4' works.
The sequence: 3 DUPN makes a copy of the three inputs on the stack. SWAP exchanges levels one and two. ROT rotates the bottom three levels up moving level three to level one and pushing the objects on levels one and two up one level. Make stack diagrams, see example below, until you have a good working knowledge of the full set of stack commands. Two of the most useful and powerful stack commands are ROT and OVER - practice these. Can 'RSA4' be improved ?
Indeed 'RSA4' may be improved.
'RSA5' << * LASTARG + ROT * + DUP + "SArea" TAG >>
LASTARG, if executed from the keyboard, will return the last argument. If, for example, you enter 3 4 * the answer 12 will be returned to the stack. If you now execute LASTARG, the arguments of the multiplication are returned, 3 4. The stack now contains 12 3 4. The LASTARG command requires memory to store the arguments. In some cases these arguments may be very large and some users may deactivate LASTARG to save memory. We assume that LASTARG is active when 'RSA5' is run. LASTARG is very important for error recovery and you should keep it active, if not you will get an error message.
Joseph uses the LASTARG command to save memory and make the program run faster. To see how the program works start with the equation for the surface area:
In this form the problem should be clear from the stack diagram below.
d d hw d hw w d w hw h+w hw hw d(h+w)+hw h hw h h+w d d(h+w) d(h+w)+hw d(h+w)+hw 2(d(h+w)+hw) ----- --- ------- --- --- ------ --------- --------- ------------ START * LASTARG + ROT * + DUP +
This is the fastest and shortest program to meet the requirements stated by the problem. Compared to 'RSA1', 'RSA5' is: 88 Vs 42.5 bytes (52% less memory) and 35 Vs 19 milliseconds (46% faster). 'RSA5' was written in class and added here to an earlier (2 page) version of this handout. Making stack diagrams is a useful way to manage the stack effectively for good optimum programs in terms of efficiency. Note: DUP + is slightly faster than 2 *.
Several basic programming commands have been used in these first two programming execises. The INPUT command helps with prompts which are important for first time uses of the program. The tagged output, however, doesn't provide complete control of the LCD for more elaborate displays. "Writing Your Third HP 48 Program" uses the DISP command which provides a wide range of input and output displays.
A vital part of any program is testing. Try all possible inputs. Allow for the unusual cases. This an important part of the programming job.
REV C 940925