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: https://technical.swissmicros.com/decoders/dm42/

You can then copy/paste the listing and post it in "code" tags.
Thomas Okken
Posts: 1100
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: External stack

Post by Thomas Okken »

mcc wrote:
Sun Apr 14, 2019 3:26 pm
In this context I would like to suggest a "LMVAR" command, wich works similiar
to the LSTO-command and would create variables which evaporize into thin
air, when the program has ended.

Would this be possible?
Yes, that's possible, and you don't even need a new command for it.
Say you have a function FOO with MVAR parameters X, Y, and Z. You want to set X to 123 and Y to 456, and solve for Z with starting guesses 10 and 20.
In a program, you would do this:

Code: Select all

PGMSLV "FOO"
123
LSTO "X"
456
LSTO "Y"
10
LSTO "Z"
20
SOLVE "Z"
Note that this would not work in RUN mode, since the stack, including all local variables, is cleared when you execute GTO, XEQ, RTN, SOLVE, or INTEG from the keyboard.
mcc
Posts: 277
Joined: Fri Jun 23, 2017 5:10 am

Re: External stack

Post by mcc »

Hi Thomas,

if I understand it correctly, this would make the coding of the calling
program, which uses PGMSLV, dependant on the nameing and the count
of the program FOO.
So (for example) I had to reprogram a plotting program each time
the function to be plotted uses another set of variables.

And -- as you already mentioned -- MVAR called in a program directly
(RUN mode) will still "pollute" the memory with its leftovers.

My intention was just to prevent this.

Cheers,
mcc
DM 42 - SN: 00373, Firmware release v.:3.22. / DMCP 3.24. as compiled by SwissMicros
Thomas Okken
Posts: 1100
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: External stack

Post by Thomas Okken »

mcc wrote:
Sun Apr 14, 2019 7:24 pm
if I understand it correctly, this would make the coding of the calling
program, which uses PGMSLV, dependant on the nameing and the count
of the program FOO.
Correct. There's no way around that.
mcc wrote:
Sun Apr 14, 2019 7:24 pm
And -- as you already mentioned -- MVAR called in a program directly
(RUN mode) will still "pollute" the memory with its leftovers.
True. There's no mechanism for creating global variables that magically disappear when you are done with them. I guess that might take the form of directories, where you create a work area, and create the variables you need there, and finally delete the directory and all its contents once you are done with it.

Directories are on my "nice to have but still not convinced that the rather large effort is worth it" list.
User avatar
wiljea
Posts: 14
Joined: Tue Sep 04, 2018 6:20 pm

Re: Local variables & partial stack preservation

Post by wiljea »

For an interesting application of LSTO to realise a temporary partial preservation of the stack before any intermediate calculation see my working example of nov 12 at viewtopic.php?f=19&t=271#p12423.

For the purpose of calculating some new quantity (ending in the X register) derived from other physical constants (by internal calls in the same program), I had to developed simple Push (“PshST”) and Pop (“PopST”) subroutines to reinstate the unperturbed remaining stack (Y, Z, T and LastX registers) through the use of temporary local variables (“Ys”, “Zs”, “Ts”, “Ls” and “LBLxx”) where this last variable contains momentarily the numeric label xx referring precisely to a given target subroutine “LBLxx” where you can freely perform whatever complicated calculations you need, providing that it ends by a call to “PopST”. As it is explained, the calling order is important to respect the consecutive RTN order precedencies which allows the proper timely creation and disposal of these local variables. And what the example shows clearly is that within the very same program you can repeat this at different points without any risk of interference (side effect or memory corruption). You simply need to invoke “PshST” passing a different numeric label xx to address another subroutine invoked by the indirect call XEQ IND “LBLxx” and where a different calculation is performed also terminated by the same call to “PopST”. Note that in the actual implementation, there was no need or intent to preserve the content of the original T register (since it is pushed out of the stack by the introduction of the new X value), but that would be easy to include if needed eventually.

As said before, let us know about your initiatives on this subject of dynamic memory (and thanks again to Mr. Thomas Okken for the implementation of local variables)!

ADDED NOTE (Nov 17, 2019): for an interesting and more thorough implementation (using nested subroutines), look at my last version of this program (Const0) providing physical constants (including recent exact definitions and related calculations in compliance with the latest SI revisions of May 20 2019) with the new display (through alpha register) of the corresponding physical units, together with a brief description (an added NOTE describes also the approach): viewtopic.php?f=19&t=271#p12446.
DM42 SN: 03090
HP-29C, HP-41C, HP-15C, HP-33s, HP-35s, WP 34S, Free42, DM42
(Fortran, C++, Mathematica, Maple, Python, …) Physics aficionado
User avatar
Stefan Karlsson
Posts: 26
Joined: Thu Apr 04, 2019 10:26 pm

Re: External stack

Post by Stefan Karlsson »

mcc wrote:
Sun Apr 14, 2019 3:26 pm
/.../
In this context I would like to suggest a "LMVAR" command, wich works similiar
to the LSTO-command and would create variables which evaporize into thin
air, when the program has ended.
/.../
I would like a "LINPUT" command. I think it would make some programs cleaner, but I haven't thought it through.

Sorry for resurrecting an old discussion. ;)
Thomas Okken
Posts: 1100
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: External stack

Post by Thomas Okken »

Stefan Karlsson wrote:
Wed Mar 25, 2020 9:51 pm
mcc wrote:
Sun Apr 14, 2019 3:26 pm
/.../
In this context I would like to suggest a "LMVAR" command, wich works similiar
to the LSTO-command and would create variables which evaporize into thin
air, when the program has ended.
/.../
I would like a "LINPUT" command. I think it would make some programs cleaner, but I haven't thought it through.

Sorry for resurrecting an old discussion. ;)
You can just create a local variable using LSTO, then use INPUT to allow the user to enter a value. There's no need to create a new function to achieve the desired effect.
Netmage
Posts: 6
Joined: Sun Aug 20, 2017 11:08 pm

Re: External stack

Post by Netmage »

Thomas Okken wrote:
Mon Apr 08, 2019 5:00 pm
Walter wrote:
Mon Apr 08, 2019 2:36 pm
Ok, the WP 34S got local registers in 2011, Free42 got local variables now. Beyond the difference between registers and variables, is there anything else? Just for curiosity...
Well, Free42 is basically an HP-42S, with more memory, more speed, and higher precision, several extensions, and now, local variables and a bigger RTN stack. (LSTO isn't documented yet in the extensions page, I'll probably write that documentation next weekend.) I don't know enough about the WP34S to do a feature-by-feature comparison.
How do you enter LSTO in Free42 and on the DM42? Is it documented anywhere?
Thomas Okken
Posts: 1100
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: External stack

Post by Thomas Okken »

Netmage wrote:
Wed May 19, 2021 11:49 pm
How do you enter LSTO in Free42 and on the DM42? Is it documented anywhere?
Use XEQ and spell out "LSTO" using the ALPHA menu: XEQ "LSTO"

[XEQ] [ENTER] [√x] [√x] [LN] [1/x] [LN] [√x] [LOG] [1/x] [ENTER]

Or use the PRGM sub-catalog in the second row of CATALOG: [Shift] [+] [▼] [LOG] [XEQ]

See also: https://thomasokken.com/free42/#extensions
User avatar
OlidaBel
Posts: 58
Joined: Thu Mar 11, 2021 8:52 am
Location: Belgium

Re: External stack

Post by OlidaBel »

Thomas Okken wrote:
Thu May 20, 2021 2:26 am
Use XEQ and spell out "LSTO" using the ALPHA menu: XEQ "LSTO"
[XEQ] [ENTER] [√x] [√x] [LN] [1/x] [LN] [√x] [LOG] [1/x] [ENTER]

Or use the PRGM sub-catalog in the second row of CATALOG: [Shift] [+] [▼] [LOG] [XEQ]
See also: https://thomasokken.com/free42/#extensions
I don't see LSTO in catalog on DM42 (hosting Free42 2.5.20). LSTO seems to be running though... :?:
(I see the LSTO and extensions on the iphone app.)

Firmware updated, LSTO in Menu : OK
Last edited by OlidaBel on Fri May 28, 2021 4:08 pm, edited 1 time in total.
---
Olivier
48GX, Prime G2, 50G, DM15L, DM42, 28S, HP 15c CE
User avatar
OlidaBel
Posts: 58
Joined: Thu Mar 11, 2021 8:52 am
Location: Belgium

Re: External stack

Post by OlidaBel »

I could type the ACK given by Thomas.
Mr Ackermann, fear the recursion :lol: .
Tried ACK(4,1).... after about 30 sec, a :shock: message like "Insufficient memory" (on DM42).
I don't know well my calculator yet, I'm sure some bytes are gone, MEM gave me less memory than before, it is now only around 24000 bytes.
Is there a way to clean the occupied memory, or is there a garbage collector in DMCP or in Free42 ?

In Setup menu, I saved a State on the Disk, then Load a Clean State (=reset =memory lost), finally load the saved State. Memory is back > 64800 bytes.
Everything else is back. 8-)
---
Olivier
48GX, Prime G2, 50G, DM15L, DM42, 28S, HP 15c CE
Post Reply