TECHNICAL NOTES
Beginner Level

WRITING YOUR THIRD HP 48 PROGRAM

New Commands used:
IP, STD, DISP, WAIT, CLLCD, FREEZE, SUB, SIZE

This is the third in a series of EduCALC HP 48 Programming class handouts used during the first class period. Here is our problem.

Write a program that takes the integer part of the number
on level one and appends it to the word Lucky and displays
it in the center of the screen. Use 7 for test purposes.

It is good practice to write the program in stages, testing as you go.

'LUK1'  << IP STD "Lucky " SWAP + >>
           31 Bytes # EC9Ah            Test;  7  => 1:      "Lucky 7"

The IP command insures that only the integer part of the input number is used. All digits to the right of the decimal point are dropped. STD is used to show the number without a decimal point. 0 FIX will show a decimal point after the number. The simple integer is desired. It is small details like this one that makes programming fun, challanging, and an activity not appealing to everyone - because of the intense detail. The fine detail of each command is NOT covered in most books and it takes endless hours of experimenting and testing to learn the exact nature of each HP 48 command. That is why we allow at least four to six hours for each class. The more time you spend writing programs of all kinds, the better you will be as a programmer.

'LUK1' works well. It places the desired text as a text object on level one of the display. We, however, want to show it as the only text on the screen. And, we don't want double quotes around Lucky 7. To accomplish this we use the DISP command. DISP requires two objects on the stack. Level two is the text string to be displayed. Level one is the screen line number 1 to 7. Show the text on line four.

'Luk2'  << IP STD "Lucky " SWAP + 4 DISP >>
           36 Bytes  # 23A1h<

This works as expected except for one slight problem. The display appears on the left part of the screen as a flash on, then off. The DISP command only displays the text as long as the program is running. Once the program stops it goes away. Add 2 WAIT to the end of the program. You can see that the text for the DISP command overwrites the existing screen as long as the program is running. How can we make the screen freeze instead of clearing? By using the FREEZE command, of course. Replace the 2 WAIT in your program with 0 FREEZE. Run the program. Now you have the display retained after the program stops. Pressing ON clears the display created by DISP. Type "88...88" , 17, eights, and fill the stack by pressing ENTER four times. Run 'Luk3'

'LUK3'  << IP STD "Lucky " SWAP + 4 DISP 0 FREEZE >>
           41 Bytes  # CF3h

Observe that DISP clears the line across the display. Also notice that DISP uses character size two and overwrites the existing display of character size three. Add eight spaces in front of "Lucky " in the program. You now have the text approximately centered but it is still cluttered with what may be on the stack. Next you need to clear the display with CLLCD. Make these changes and call the program 'LUK4'. 'LUK4' now meets the requirements of the problem.

  
'LUK4'  << IP STD "  (8 spaces)      Lucky " SWAP + CLLCD 4 DISP 0 FREEZE >>
           51.5 Bytes  # 8FE8h

An important part of programming is testing. Run 'LUK4' with 12345678 as an input. Does this look nice? What about -153? Do you want negative numbers? If not, you could add ABS, absolute value, to the front of the program to insure that only positive numbers are used. Dealing with variable length numbers is more involved.

A good way to make the display deal with any size number is to place a variable number of spaces in front of Lucky to make it centered. To do this we must know the length of the text to be centered and the width of the line it is to be centered on. The display width is 22 characters. The length of the text string will depend on the number length. Lucky plus a space is six characters. The test input number makes the text line seven characters long. Subtract seven from 22 to get 15. Divide 15 by 2 to get 7.5 spaces to place in front of the text string to center it. You used 8 in 'LUK4'. The process described above may be done by the program. Write a program starting with a text string and adding the calculated number of centering spaces.

'CEN1'  << DUP SIZE 22 SWAP - 2 / >>
           35.5 Bytes  # 27F6h

'CEN1' makes a copy of the input text string, determines its length with SIZE, which consumes the copy, and subtracts the length from 22 with: 22 SWAP -. The number of desired spaces is then computed by: 2 / to put equal number of spaces on each side. With an input of "Lucky 7" 'CEN1' produces the text string on level two and 7.5 on level one. This works as expected. Next you need 7, or 8 spaces. One way to get the spaces is to provide a text string of the maximum number of spaces needed and SUB (extract a sub string from a source string) the desired number from it. SUB requires three inputs. The string on level three, the start position of the SUB string, and the end position of the SUB string. SUB start and end numbers are rounded "up" if the inputs are not integers. Rounding is 5 and up, and 7.5 becomes 8, 7.4 becomes 7.

'CEN2'  << DUP SIZE 22 SWAP - 2 / "    (8 spaces)    " 1 ROT SUB SWAP + >>
           61 Bytes  # 564Eh

Starting with a string "Lucky 7", spaces magically appear in front of the string when 'CEN2' is run. If you integrate 'CEN2' into 'LUK4' without the spaces in front of Lucky, you have 'LUK5'.

'LUK5'  << IP STD "Lucky " SWAP + DUP SIZE 22 SWAP - 2 / "        " 1 ROT SUB 
        SWAP + CLLCD 4 DISP 0 FREEZE >>
           94.5 Bytes  # F4A0h

This program appears to work as expected. It is not easy to count the spaces on the screen so let's add a line to our screen that will "count" the spaces for us. Following 4 DISP, add: "1234567890110987654321" 5 DISP. See 'LUK6' below.

'LUK6'  << IP STD "Lucky " SWAP + DUP SIZE 22 SWAP - 2 / "   (8 spaces)     " 1 
        ROT SUB SWAP + CLLCD 4 DISP "1234567890110987654321" 5 DISP 0 FREEZE >>
           127.5 Bytes  # 8C73h

Run 'LUK6' with 77 as an input. It is clear that there are seven spaces on each side of the text. Try a single 7. You have eight on the left and seven on the right. You cannot split a space - without using graphics techniques. You can, however, determine which side of the text the odd space should appear. In 'LUK6' the extra space always appears on the left side because of the rounding up of SUB inputs. Test with various input number lengths. What could you do to insure that the extra space is on the right instead of the left?

The problem is to insure when N.5 results with the division by two that N.5 becomes N instead of N + 1 when SUB is used. This may be done with the IP, Integer Part command. IP insures that an extra space is not SUBed when odd number lengths are input. Add IP following: 2 /. See 'LUK7' below.

'LUK7'  << IP STD "Lucky " SWAP + DUP SIZE 22 SWAP - 2 /  IP "     (8 spaces)    " 1 
        ROT SUB SWAP + CLLCD 4 DISP "1234567890110987654321" 5 DISP 0 FREEZE >>
           129 Bytes  # Ch

'LUK7' insures that odd number lengths places the 'extra' space on the right. Note the unusual single digit HEX check sum. There is one more change that could be made. Can you spot it? Making the change saves one byte. Hint: The odd space. Question. Are both DISP's necessary?

The method of using 0 FREESE to hold a display until ON is pressed is useful for many applications. Many computer users are used to the response "Press any key to continue". Is it possible to program the HP 48 to display Lucky N until any key is presed? YES! See "LUK8" below.

'LUK8'  << IP STD "Lucky " SWAP + DUP SIZE 22 SWAP - s / IP "  (8 spaces)  "
           1 ROT SUB SWAP + 4 DISP "1234567890110987654321" 5 DISP 0
           WAIT DROP >>
           131.5 Bytes  # D137h

''LUK8' uses the special argument of zero for the WAIT command as discussed in TN51C example six for the DO...UNTIL...END loop structure. The display in "frozen" until any key is pressed. This is almos true. Which keys do not perform as advertised? Is this correctible? 'LUK9' corrects this problem. The command 0 WAIT is very useful for diplaying a message that needs to be cleared with any key except - the prefix keys. They, of course, must be completed with one or two additional keys in the normal manner. Tis is also true with your desktop computer. Pressing any key such as Shift, Ctrl or Alt doesn't meet the requirement of pressing "any key".

'LUK9'  << IP STD "Lucky ' SWAP + DUP SIZE 22 SWAP - 2 / IP " (8SPACES "
           1 ROOT SUB SWAP + 4 DISP "1234567890110987654321" 5 DISP IFERR 0
           WAIT THEN END DROP >>
           149.0 Bytes  # 431Bh

Both 'LUK8' and 'LUK9' do not requere the keycode normally returned by 0 WAIT. The last command, DROP remove this from the stack. The HP 48 is in a low battery drain state while the display is showing what you want. This is much better solution than using KEY whichkeeps the machine will not automatically time out and turn off either. With 0 WAIT it will. Adding the IFERR test allows ON to be pressed to clear the display.

The new commands used in this exerc ise will help you get started in your own programming. Having specific examples will help, but it is very important for you to spend the time necessary to understand how the commands work. The rounding of the SUB command is an example. What happens if a negative number is used? If the start and end values are the same, or both zero? 0 FREEZE is not the only way FREEZE may be used. The input number controls the "frozen" part of the screen. Programming is an open ended time sink activity. Even a "simple" routine may take many hours of testing and rewriting to get it right.

The object orientation of the HP 48 allows programs to be written in small modules and combined when each module is finished. The fourth and last part of this series will cover this concept.

REV D 940925

<-- Back to Technical Notes Index