External stack

Contributions to this software library are always welcome. Please ensure that you post program listings rather than .raw files. They give a reasonable idea of what your program does without having to load them into a DM42 and you can also include comments in your code. Check out the following link for a decoder/encoder: http://www.swissmicros.com/dm42/decoder/

You can then copy/paste the listing and post it in "code" tags.
hsilop
Posts: 33
Joined: Thu Mar 14, 2019 12:35 am
Location: Canberra, Australia

External stack

Post by hsilop » Thu Mar 14, 2019 6:30 am

Greetings fellow DM42 owners. I received mine only 2 days ago and have been feverishly porting my library of astrophysics programs from my HP41CV.

I soon discovered that programs do ugly things to the stack, and having such a lovely custom menu arrangement I though I'd like to make programs that operate like function *only* change the X register and programs that act like procedures push their result onto the stack. So I built an external stack. These programs will work even one program calls another ( as a lot of mine do ) since the external stack can hold many previous incarnations of the stack contents.

BTW - I am *very* new to HP42 style programming so be gentle with me. I am just trying to find ways to leverage the most out of this truly incredible machine

Read on ..

Stack is initialiased ( once ) like so:

Code: Select all

SIZE 100
99
STO "SP"

The stack starts at register 99 and grows downwards. SP is the stack pointer.

There are two top level progams "STKPSH" which pushes the X,Y,Z and T registers onto the stack, leaving the current stack undisturbed. And "STKPOP" which pops the previously pushed values for X,Y,Z and T.

Code: Select all

LBL "STKPSH"
XEQ "STKSAV"
XEQ "PUSH"
XEQ "PUSH"
XEQ "PUSH"
XEQ "PUSH"
XEQ "STKRES"
END

LBL "STKPOP"
XEQ "POP"
STO "STK_T"
XEQ "POP"
STO "STK_Z"
XEQ "POP"
STO "STK_Y"
XEQ "POP"
STO "STK_X"
XEQ "STKRES"
END

LBL "STKRES"
RCL "STK_T"
RCL "STK_Z"
RCL "STK_Y"
RCL "STK_X"
END

LBL "STKSAV"
STO "STK_X"
R↓
STO "STK_Y"
R↓
STO "STK_Z"
R↓
STO "STK_T"
R↓
END

LBL "PUSH"
STO IND "SP"
R↓
1
STO- "SP"
R↓
END

LBL "POP"
1
STO+ "SP"
RCL IND "SP"
END
In my functions I call STKPSH to the very start, perform whatever actions to calculate the return value then save X call STKPOP, ROLL and restore X just before returning.

Code: Select all

 XEQ "STKPSH"
 ... does something to X
STO "SAV_X"
XEQ "STKPOP"
R↓
RCL "SAV_X"
END
 
Procedures do a similar thing with the exception of the old X value not being rolled.

Code: Select all

 XEQ "STKPSH"
 ... computes a new X
STO "SAV_X"
XEQ "STKPOP"
RCL "SAV_X"
END
 
Here is a complete example. This function shows X light-years in metres. Register C holds the speed of light in a vacuum in metres per second.

Code: Select all

LBL "LYtoM"
XEQ "STKPSH"
RCL "C"
3600
×
24
×
365.25
×
×
STO "SAV_X"
XEQ "STKPOP"
R↓
RCL "SAV_X"
END
DM42 SN: 03223

HP11C, HP12C, HP15C, HP16C, HP25, HP32S, HP33C, HP41CV, HP46, HP65

User avatar
Walter
Posts: 1100
Joined: Tue May 02, 2017 9:13 am
Location: Close to FRA, Germany

Re: External stack

Post by Walter » Thu Mar 14, 2019 9:38 am

Looks like a very nice idea. I think that others being more competent in SW shall investigate for possible side effects though.
DM42 SN: 00041 --- Follower of Platon.

HP-35, HP-45, ..., HP-50, WP 34S, WP 31S, DM16L

hsilop
Posts: 33
Joined: Thu Mar 14, 2019 12:35 am
Location: Canberra, Australia

Re: External stack

Post by hsilop » Thu Mar 14, 2019 10:42 am

It was the side effects I was trying to eliminate. This scheme makes one-number functions operate the same as built-in function like SQRT and one value procedures work like PI or RCL. The X,Y,Z,T stack behaves as you would expect.
DM42 SN: 03223

HP11C, HP12C, HP15C, HP16C, HP25, HP32S, HP33C, HP41CV, HP46, HP65

User avatar
Walter
Posts: 1100
Joined: Tue May 02, 2017 9:13 am
Location: Close to FRA, Germany

Re: External stack

Post by Walter » Thu Mar 14, 2019 12:52 pm

Those are not the side effects I meant. Using your external stack, you eat memory - how do you care for (e.g. statistical) data competing for this memory?
DM42 SN: 00041 --- Follower of Platon.

HP-35, HP-45, ..., HP-50, WP 34S, WP 31S, DM16L

hsilop
Posts: 33
Joined: Thu Mar 14, 2019 12:35 am
Location: Canberra, Australia

Re: External stack

Post by hsilop » Thu Mar 14, 2019 8:58 pm

What registers are used by statistical functions? Would it help If I declared the external stack as a matrix perhaps? I haven't read much about those yet.

But, seriously, we have SO MUCH free memory on the DM42 surely I can find a quiet patch for my stack.
DM42 SN: 03223

HP11C, HP12C, HP15C, HP16C, HP25, HP32S, HP33C, HP41CV, HP46, HP65

dlachieze
Posts: 174
Joined: Thu May 04, 2017 10:20 am
Location: France

Re: External stack

Post by dlachieze » Thu Mar 14, 2019 11:20 pm

You can also see the PUSH/POP stack routines published earlier in this sub-forum.
DM42 SN: 00425

Thomas Okken
Posts: 619
Joined: Tue May 02, 2017 3:48 pm
Contact:

Re: External stack

Post by Thomas Okken » Thu Mar 14, 2019 11:39 pm

hsilop wrote:
Thu Mar 14, 2019 8:58 pm
What registers are used by statistical functions?
The statistical functions use numbered registers (which in turn are stored in the matrix variable REGS). By default it uses registers 11 through 23, as follows:

11: ΣX
12: ΣX↑2
13: ΣY
14: ΣY↑2
15: ΣXY
16: N
17: ΣLN(X)
18: ΣLN(X)↑2
19: ΣLN(Y)
20: ΣLN(Y)↑2
21: ΣLN(X)LN(Y)
22: ΣXLN(Y)
23: ΣYLN(X)

This applies to ALLΣ mode. In LINΣ mode, the sums involving logarithms are not accumulated, so from the list above, only registers 11 through 16 are used in that case. The default is ALLΣ.

Finally, this block of registers can be moved to a different address using the ΣREG function. The default setting, as shown above, is ΣREG 11.

ΣREG 11 plus LINΣ modes corresponds with the behavior of the HP-41 series.

hsilop
Posts: 33
Joined: Thu Mar 14, 2019 12:35 am
Location: Canberra, Australia

Re: External stack

Post by hsilop » Fri Apr 05, 2019 7:46 am

Forget this shit approach, the PUSH/POP stack thread by whuyse is a MUCH more elegant solution.

I'm just a noob ... matrices rock!
DM42 SN: 03223

HP11C, HP12C, HP15C, HP16C, HP25, HP32S, HP33C, HP41CV, HP46, HP65

Thomas Okken
Posts: 619
Joined: Tue May 02, 2017 3:48 pm
Contact:

Re: External stack

Post by Thomas Okken » Fri Apr 05, 2019 7:51 am

The next release of Free42 will have local variables, which will make it possible to preserve the stack with no side effects at all. Stay tuned...

User avatar
akaTB
Posts: 302
Joined: Tue May 02, 2017 11:56 am

Re: External stack

Post by akaTB » Fri Apr 05, 2019 11:08 am

Thomas Okken wrote:
Fri Apr 05, 2019 7:51 am
The next release of Free42 will have local variables, which will make it possible to preserve the stack with no side effects at all. Stay tuned...
:shock:
Greetings,
    Massimo

-+×÷ left is right and right is wrong :twisted: Casted in gold

Post Reply