Post Reply 
Accessing / Manipulating RPN Stack from PPL
02-20-2014, 04:56 PM
Post: #1
Accessing / Manipulating RPN Stack from PPL
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
Find all posts by this user
Quote this message in a reply
02-20-2014, 05:05 PM
Post: #2
RE: Accessing / Manipulating RPN Stack from PPL
(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.

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
02-20-2014, 05:10 PM
Post: #3
RE: Accessing / Manipulating RPN Stack from PPL
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
Find all posts by this user
Quote this message in a reply
02-20-2014, 06:40 PM (This post was last modified: 02-20-2014 06:41 PM by Han.)
Post: #4
RE: Accessing / Manipulating RPN Stack from PPL
(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.

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
02-20-2014, 09:05 PM
Post: #5
RE: Accessing / Manipulating RPN Stack from PPL
(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
Find all posts by this user
Quote this message in a reply
05-14-2018, 09:08 PM (This post was last modified: 05-15-2018 05:24 PM by Stevetuc.)
Post: #6
RE: Accessing / Manipulating RPN Stack from PPL
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;
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 2 Guest(s)