HP Forums
Accessing / Manipulating RPN Stack from PPL - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Accessing / Manipulating RPN Stack from PPL (/thread-724.html)



Accessing / Manipulating RPN Stack from PPL - John R. Graham - 02-20-2014 04:56 PM

One of the most natural programming activities on prior programmable HP calculators is to write a program that, when invoked, takes arguments from the stack and does something with them. Presumably stack access from PPL is with some set of (currently undocumented) functions. Does anyone know how to do this?

- John


RE: Accessing / Manipulating RPN Stack from PPL - Han - 02-20-2014 05:05 PM

(02-20-2014 04:56 PM)John R. Graham Wrote:  One of the most natural programming activities on prior programmable HP calculators is to write a program that, when invoked, takes arguments from the stack and does something with them. Presumably stack access from PPL is with some set of (currently undocumented) functions. Does anyone know how to do this?

- John

It works the same way as with regular commands. In RPN mode, the "argument" of a command is the number of arguments, and the true arguments sit on the stack. For example, if you create a program of the form MYPROG(a,b,c) which takes three inputs from the stack as parameters a, b, c, then in RPN you would have a, b, and c on the stack, and call MYPROG(3).

Your program is created the same was as in non-RPN mode.


RE: Accessing / Manipulating RPN Stack from PPL - John R. Graham - 02-20-2014 05:10 PM

Okay, thank you. I couldn't find a single example in the officical documentation that described that, although perhaps I missed something else that has been written.

Stack maipulation functions would still seem to be necessary, unless the RETURN statement also has some not fully elucidated syntax to return multiple results. As far as I can easily tell, functions only return one object, right?

- John


RE: Accessing / Manipulating RPN Stack from PPL - Han - 02-20-2014 06:40 PM

(02-20-2014 05:10 PM)John R. Graham Wrote:  Okay, thank you. I couldn't find a single example in the officical documentation that described that, although perhaps I missed something else that has been written.

Stack maipulation functions would still seem to be necessary, unless the RETURN statement also has some not fully elucidated syntax to return multiple results. As far as I can easily tell, functions only return one object, right?

- John

Since programs are still "regular" (in the sense of non-RPL), then there really is no immediate need for stack manipulations within a program. This would only be necessary if there were no mechanisms for storing intermediate values -- but we have local variables for that (and they can be created as-needed). If you have multiple results to return, just return a list: RETURN({a,b,c,d}) and the list can contain elements of different types.


RE: Accessing / Manipulating RPN Stack from PPL - Miguel Toro - 02-20-2014 09:05 PM

(02-20-2014 06:40 PM)Han Wrote:  
(02-20-2014 05:10 PM)John R. Graham Wrote:  Okay, thank you. I couldn't find a single example in the officical documentation that described that, although perhaps I missed something else that has been written.

Stack maipulation functions would still seem to be necessary, unless the RETURN statement also has some not fully elucidated syntax to return multiple results. As far as I can easily tell, functions only return one object, right?

- John

Since programs are still "regular" (in the sense of non-RPL), then there really is no immediate need for stack manipulations within a program. This would only be necessary if there were no mechanisms for storing intermediate values -- but we have local variables for that (and they can be created as-needed). If you have multiple results to return, just return a list: RETURN({a,b,c,d}) and the list can contain elements of different types.

Hi,

And there is where a command like OBJ-> would be logical to have. Of course, you can always extract, one by one, the elements from the list but it is more cumbersome. If there is already ->LIST, why not OBJ->?

Regards,

Miguel


RE: Accessing / Manipulating RPN Stack from PPL - Stevetuc - 05-14-2018 09:08 PM

Here is a workaround to allow writing back to multiple stack locations from PPL
It uses the fact that a key definition returns a string to the command line without quotes. So RETURN "stack1 stack2 stack3 stack4" will place those vars onto the lowest 4 stack levels on pressing <ENTER>

In the code below , global vars are created to hold the stack values.
Ans(1) to Ans(4) are used to pop 4 values from the stack.
a sample routine then takes the factorial of the stack values.
on pressing SHIFT User 0, stack4 stack3 stack2 stack1 is placed on the command line, and <ENTER> pushes them onto stack.

try this:
Home screen RPN mode
place on stack:
2 <ENTER>
4 <ENTER>
6 <ENTER>
8 <ENTER>
Shift User 0
stack4 stack3 stack2 stack1 is returned
<ENTER>

2
24
720
40320
is returned to the stack

Code:

EXPORT stack1,stack2,stack3,stack4; //global stack vars
KEY K_0()
BEGIN
//pop 4 items from stack
Ans(1)▶stack1;Ans(2)▶stack2;Ans(3)▶stack3;Ans(4)▶stack4;

//routines go here
stack1!▶stack1; 
stack2!▶stack2; 
stack3!▶stack3;
stack4!▶stack4;

//push 4 items to stack. Detect Entry and return array if not RPN mode
RETURN  IFTE(Entry==2,"stack4 stack3 stack2 stack1","{stack4,stack3,stack2,stack1}");
END;