Previous Section
Cover Page
Table of Contents
Index
Exercises for this Section
Next Section

Section 2 - Stack Manipulation

When working on the HP49G+ in RPN mode we sometimes make use of the stack manipulation commands that are available in the STACK menu. These tend to be much more important in programming. With the calculator in RPN mode, the STACK menu can be found by pressing TOOL F3-STACK or LS PRG F1-STACK. There are 19 stack manipulation commands in this menu. These are listed and explained in the table below because they don't seem to be explained anywhere in UG.

DUP Duplicates the item on level 1 of the stack, puts it on level 1 of the stack and moves everything else up on the stack.
SWAP Interchanges the items on levels 1 and 2 of the stack
DROP Deletes the item on level 1 of the stack and drops everything else down one level.
OVER Makes a copy of the item from level 2 of the stack, puts it on level 1, and moves everything else up one level.
ROT Rotates the item in level 3 of the stack to level 1, the item from level 1 to level 2, and the item from level 2 to level 3.
UNROT Reverses the process of ROT.
ROLL With an integer n in the command line, ROLL works like ROT, but on the first n levels of the stack, so 3 ROLL has the same effect as ROT. If the command line in empty but there is an integer n on level 1 of the stack, ROLL takes n from the stack, drops the rest of the stack down one level, then performs the ROLL. Thus, 4 ROLL and 4 ENTER ROLL have the same effect.
ROLLD Reverses ROLL
PICK With an integer n in the command line, PICK works like OVER, but selects the item from level n of the stack, 2 PICK is the same as OVER. If the command line is empty and an integer n is on level 1 of the stack, PICK removes the integer from the stack, moves everything down one level, then performs the PICK, thus 4 PICK and 4 ENTER PICK have the same effect.
UNPICK Is NOT quite the reverse of PICK. With an integer n in the command line, PICK removes the item in level 1 of the stack, moves everything in the stack down one level, then replaces the item in level n with the item that was originally removed from level 1. If the command line is empty and there is an integer n on level 1 of the stack, UNPICK removes the integer from the stack, moves everything in the stack down one level, then performs the UNPICK, thus 4 UNPICK and 4 ENTER UNPICK have the same effect.
PICK3 The same as 3 PICK
DEPTH Counts the number of elements in the stack, puts the number on level 1 of the stack and moves everything else up one level.
DUP2 Duplicates the items in levels 1 and 2 of the stack and moves everything up 2 levels.
DUPN With an integer n in the command line, DUPN duplicates the items in levels 1 through n and moves everything up n on the stack. If the command line is empty and there is an integer n on level 1 of the stack, DUPN takes the n from the stack, drops everything down one level, then executes the DUPN. Thus, 3 DUPN and 3 ENTER DUPN have the same effect.
DROP2 Drops the items on levels 1 and 2 of the stack and moves everything else down two levels.
DROPN With an integer n in the command line, DROPN drops the items on levels 1 through n from the stack and moves everything else down n levels. If the command line is empty and there is an integer n on level 1 of the stack, DROPN removes the n, drops everything down one level, then executes the DROPN. Thus, 4 DROPN and 4 ENTER DROPN have the same effect.
DUPDUP The same DUP DUP, that is, the same as pressing DUP twice.
NIP Drops the item in level 2 of the stack and moves anything above level 2 down one level.
NDUPN With an integer n in the command line, NDUPN puts n copies of the item on level 1 into levels 2 through n + 1, and n is put on level 1. Everything else on the stack is moved up n levels. If the command line is empty and there is an integer n on level 1, NDUPN removes n, drops everything on the stack down one level, then executes NDUPN. Thus, 3 NDUPN and 3 ENTER NDUPN have the same effect.

Suppose the bowling league of Section 1 computes a bowler's handicap for the next week as 80% of (200 minus this week's average). Let's write a program to take three scores from the stack and leave the total on level 3, the average truncated to an integer on level 2, and next week's handicap truncated to an integer on level 1.

Type this program into your calculator and store it as BWL2 then try it with several sets of data. With 172, 177 and 186, the output should be 535 on level 3, 178 on level 2, and 17 on level 1. Notice that the commands 0 MAX are needed to make sure that a bowler whose average is over 200 will not have a negative handicap. The FLOOR function (LS MTH F5-REAL NXT NXT F3-FLOOR) is described on page 3-14 of UG.

EXERCISE SET 2

1. Change Problem 3 of Exercise Set 1 so that the distance is left on level 2 of the stack and the time on level 1. Save this new version as TMR2.

2. Change Problem 1 of Exercise Set 1 so that the original amount is left on level 3 of the stack, the tax on level 2, and the total on level 1. Call this new version TAX2.

3. Write a program called LIN1 to do linear interpolation. That is, given the two points (x1, y1), (x2, y2) and a number x between x1 and x2, find The program should begin with x1 in level 5, y1 in level 4, x2 in level 3, y2 in level 2, and x in level 1; and should end with y in level 1 and the rest of the stack empty. (First strive for a solution which works, but then try to do it in as few steps as possible.)

Previous Section
Cover Page
Table of Contents
Index
Top of Page
Next Section