Page 1 of 1

PUSH/POP stack

Posted: Thu Dec 21, 2017 1:41 pm
by whuyse
As requested in another topic.
Before first use, create a 1x4 matrix and call it "STACK".
Of course, the stack may not hold complex numbers or matrices.

Code: Select all

00 { 74-Byte Prgm }
01>LBL "PUSHST"
02 INDEX "STACK"
03 INSR
04 R^
05 STOEL
06 J+
07 R^
08 STOEL
09 J+
10 R^
11 STOEL
12 J+
13 R^
14 STOEL
15 EXITALL
16 RTN
17>LBL "POPST"
18 INDEX "STACK"
19 STO ST L
20 RCLEL
21 ENTER
22 ->
23 ENTER
24 ->
25 ENTER
26 ->
27 DELR
28 EXITALL
29 X<> ST L
30 END
POPST will put back the original LXYZT as XNYZT, with N the X-register content before calling POPST; consistent with the way a 1-argument function works. To restore the stack mimicking a 2-argument function (thus returning XNZTT) use

Code: Select all

>LBL "POP2ST"
 INDEX "STACK"
 STO ST L
 RCLEL
 ENTER
 ENTER
 ->
 J+
 ENTER
 ->
 DELR
 EXITALL
 X<> ST L
 RTN
Cheers, Werner

Re: PUSH/POP stack

Posted: Sat Feb 03, 2018 10:34 pm
by tcab
Nice algorithm- thanks.
In the first version, modifying it slightly to leave out line 29 will restore XYZT as it was, which I prefer. Then if I happen to want what was in X prior to calling POPST, I can do a LASTX.

Re: PUSH/POP stack

Posted: Fri Mar 15, 2019 1:19 am
by hsilop
This looks brilliant!

I am trying to understand what you are doing here, but what instruction/key does the symbol

->

refer to ?

Thanks. I did a similar think using indirect addressing but I think a matrix would work better and not conflict with other memory hungry apps such as statistical operations.

Re: PUSH/POP stack

Posted: Fri Mar 15, 2019 1:30 am
by Thomas Okken
→ is one of the matrix editor's cell movement commands. It stores the contents of the X register in the current cell, moves the cell pointer one to the right, and puts the contents of that next cell in X. It is roughly equivalent to STOEL J+ RCLEL.

If all that is Greek to you, I recommend reading the documentation. Specifically, go to http://thomasokken.com/free42/, scroll down to Documentation, and start from there. In the HP-42S manual, specifically check out chapter 14, Matrix Operations.

Re: PUSH/POP stack

Posted: Fri Mar 15, 2019 5:46 am
by hsilop
Thanks dude. Don't worry, I'm a retired IT guy with a new DM42 and time to burn. I will work it out.

Re: PUSH/POP stack

Posted: Fri Apr 05, 2019 9:28 am
by hsilop
This is far superior to my "solution"!

Is there a way to test if "STACK" exists in the PUSH function? That way you could dynamically create it as needed. Just a thought. Matrices are cool in HP42S/DM42 land! Registers ... pfft.

Re: PUSH/POP stack

Posted: Fri Apr 05, 2019 3:24 pm
by budmur
As I understand it, one disadvantage of this method is that the stack can't contain a matrix. The other way (using registers) would be able to support a matrix on the stack.