Post Reply 
Free42 with big stack, test version
02-07-2021, 01:35 PM
Post: #61
RE: Free42 with big stack, test version
Despite the new Allow Big Stack setting, I'm still considering other options for peaceful coexistence of 4STK and NSTK. They will just be provided in addition to the current functionality, not instead of.

Disabling the big stack completely by default makes sense because HP-42S compatibility is a central aspect of Free42, and that is something which many users require. So that will stay in place regardless of whatever else I come up with.

But for people who are interested in using both modes, an additional "allow big stack, but automatically switch to four-level stack if..." mode would probably be useful. Details to be worked out. What should the "if" condition be? A program not tagged with BIGSTK in line 01, or a program containing LBL instructions instead of NLBL, or something else? And if the automatic switch takes place, should it be permanent or temporary, 4STK or L4STK?

I suppose there may not be one single best answer to this, it will depend on how one intends to use this feature, the specifics of each user's workflow. I'll hold off on implementing anything further until I have a better idea of what would actually work well in practice.
Visit this user's website Find all posts by this user
Quote this message in a reply
02-07-2021, 05:20 PM
Post: #62
RE: Free42 with big stack, test version
Like I said before, I think the NLBL/LBL/RTN/END is the safest way to distinguish between two programs and their stack mode usage.

This is what I would do if I was doing it:
  • at program startup
    • alloc a temporary n-levels stack (running-nstk) and running-nstk = empty
    • alloc a temporary 4-levels stack (running-4stk) and running-4stk = 0
    • alloc a temporary L register (running-lreg) and running-lreg = 0
    • if interactive mode is n-levels → push interactive n-levels stack (interactive-nstk) in running-nstk, running-nstk is activated
    • if interactive mode is 4-levels → copy interactive 4-levels stack (interactive-4stk) over running-4stk, running-4stk is activated
    • running-lreg = interactive-lreg
  • mode switch on LBL, NLBL, RTN and END
    • switch from 4-levels to 4-levels → nothing to copy, running-4stk stay active
    • switch from n-levels to n-levels → nothing to copy, running-nstk stay active
    • switch from 4-levels to n-levels → copy running-4stk to running-nstk (see note #1), running-nstk is activated
    • switch from n-levels to 4-levels → copy running-nstk to running-4stk (see note #2), running-4stk is activated
  • at program exit
    • if interactive mode is n-levels → apply mode switch if needed, flush interactive-nstk, push running-nstk in interactive-nstk, interactive-lreg = running-lreg, free running-nstk, free running-4stk, free running-lreg
    • if interactive mode is 4-levels → apply mode switch if needed, copy running-4stk over interactive-4stk, interactive-lreg = running-lreg, free running-nstk, free running-4stk, free running-lreg
Note #1: copy 4stk to nstk (running & interactive)
  • if nstk depth < 5 then flush nstk, push 4stk into nstk
  • if nstk depth > 4 then drop 4 data from nstk, push 4stk into nstk
Note #2: copy nstk to 4stk (running & interactive)
  • if nstk depth > 3 then copy nstk 4th lowest levels to 4stk
  • if nstk depth = 3 then copy nstk 3th lowest levels to 4stk, T=Z
  • if nstk depth = 2 then copy nstk 2th lowest levels to 4stk, TZ=Y
  • if nstk depth = 1 then copy nstk 1st level to 4stk, TZY=X
  • if nstk depth = 0 then TZYX=0
Find all posts by this user
Quote this message in a reply
02-07-2021, 10:34 PM
Post: #63
RE: Free42 with big stack, test version
That seems pretty similar to the behavior of LNSTK and L4STK, except the implementation is totally different. I'm only using one stack and extending or truncating it as needed. L4STK saves levels 5..N to a hidden local variable, and when used in conjunction with FUNC, it also saves the levels that will end up in the four-level stack but are not used as input parameters. So if you have a function with FUNC 21, i.e. two inputs and one output, L4STK will save levels 3..N.

The question of when to do the automatic switch is a bit difficult. In terms of implementation, it seems like there's only one way to do it: when execution starts, perform L4STK or LNSTK, as needed, and if execution ends with a RTN while the RTN stack is empty, then the normal L4STK/LNSTK cleanup mechanism will do the right thing. But if a program ends with STOP, i.e. without returning from the top-level routine, then the automatic L4STK or LNSTK cleanup will not happen right away, because the calculator has no way of knowing whether the program is just stopped for input or really done. The L4STK or LNSTK cleanup could still be done when the user performs an action that clears the RTN stack, like GTO or XEQ, but that might come as a bit of a surprise when it finally happens.
Visit this user's website Find all posts by this user
Quote this message in a reply
02-08-2021, 03:21 PM
Post: #64
RE: Free42 with big stack, test version
(02-07-2021 10:34 PM)Thomas Okken Wrote:  But if a program ends with STOP, i.e. without returning from the top-level routine, then the automatic L4STK or LNSTK cleanup will not happen right away, because the calculator has no way of knowing whether the program is just stopped for input or really done. The L4STK or LNSTK cleanup could still be done when the user performs an action that clears the RTN stack, like GTO or XEQ, but that might come as a bit of a surprise when it finally happens.
You have a point here, I did not think about that scenario, this is rendering my way of doing it flawed and useless.
I now see why keeping a single stack is a better solution. Thanks!
Find all posts by this user
Quote this message in a reply
03-10-2021, 08:18 AM
Post: #65
RE: Free42 with big stack, test version
(01-30-2021 01:18 AM)Thomas Okken Wrote:  I finished implementing the big stack logic in Free42. This isn't an official release; depending on the feedback I get, additions and changes to the instruction set are still possible. But I think this is useful enough as it is to share it around. Comments welcome!

https://thomasokken.com/free42/download/...indows.zip

I made some changes to the FCN catalog: because the number of additional functions in Free42, compared to the HP-42S, keeps getting larger, the FCN catalog became more and more messy. Because what to do: keep it strictly alphabetic, or group the new functions logically, but if the latter, how is that grouping obvious? So I split off the extensions into sections of their own, which are in a new row at the top level of the CATALOG menu.

The new functions in the STK section are:

NSTK N-level Stack. Turns on big stack mode.

4STK 4-level Stack. Turns off big stack mode, returning to the TZYX stack.

LNSTK and L4STK Local N-level / 4-level Stack. These switch to N-level or 4-level stack mode, respectively, but in addition, they automatically restore the previous stack mode when the current function returns. Further, L4STK saves levels 5 and up in a hidden list, and also restores them when the previous stack mode is restored. (The non-local 4STK function simply discards levels 5 and up.)

All of these functions are no-ops when the selected stack mode is already active.

DEPTH Returns the current stack depth.

DROP and DROPN n Drops level 1, or levels 1..n. In 4-level mode, the stack is replenished with zeroes from the top.

DUP and DUPN n Duplicates level 1, or levels 1..n. In 4-level mode, n must be 2 or less.

R↓N n and R↑N n Rolls levels 1..n down or up. Note that the standard R↓ and R↑ are equivalent to R↓N depth and R↑N depth.

PICK n and UNPICK n Gets the object from level n, or puts an object into level n. Note that UNPICK consumes the contents of level 1, unlike STO, and n refers to the stack level number after X has been dropped. The effect is that you can modify an object at level n by doing PICK n, <modify the object>, UNPICK n, without having to add 1 to the level number for UNPICK. Also note that this is similar to the behavior of PICK and UNPICK in RPL.

Here's a fairly useless but hopefully enlightening example:

Code:
00 { 31-Byte Prgm }
01▸LBL "FAC"
02 LNSTK
03 DUP
04 1
05 X<Y?
06 GTO 00
07 R↓N 3
08 DROPN 2
09 RTN
10▸LBL 00
11 -
12 XEQ "FAC"
13 ×
14 END

This uses the big stack to implement the factorial recursively, and because of how it uses LNSTK, you can call it even if you're in 4-level mode.

Although not shown here, the big stack is compatible with FUNC as well. Further, it is possible to combine FUNC and LNSTK or L4STK in one function; the only restriction is that FUNC must be called before LNSTK / L4STK.

Hi Thomas.
I found one minor improvement, at least to me.
As the big stack does not "purge" automatically old objects, in real life use the stack may became very deep.
In RPL models i use a lot the CLEAR function (on the 50g is Orange_Sht+bksp) to periodically purge the stack.
In Free42 the CLST command is buried in CLEAR menu and need a lot of keystrokes to be reached.
At present i mad a workaround by mean of custom menu.
In would be nice to have CLST mapped somewhere in the keyboard.
Find all posts by this user
Quote this message in a reply
03-10-2021, 09:45 AM
Post: #66
RE: Free42 with big stack, test version
(03-10-2021 08:18 AM)Marco Polo Wrote:  I found one minor improvement, at least to me.
As the big stack does not "purge" automatically old objects, in real life use the stack may became very deep.
In RPL models i use a lot the CLEAR function (on the 50g is Orange_Sht+bksp) to periodically purge the stack.
In Free42 the CLST command is buried in CLEAR menu and need a lot of keystrokes to be reached.
At present i mad a workaround by mean of custom menu.
In would be nice to have CLST mapped somewhere in the keyboard.

A lot of keystrokes for CLST? [Shift] [←] [LOG], three keystrokes, I think that's not so bad, and I use it all the time.

But I understand that not everybody feels that way. There are several skins in my collection that have CLST on the keyboard, take a look at the skins collection at my web site, there might be something there you like.
Visit this user's website Find all posts by this user
Quote this message in a reply
03-10-2021, 10:24 AM
Post: #67
RE: Free42 with big stack, test version
(03-10-2021 09:45 AM)Thomas Okken Wrote:  
(03-10-2021 08:18 AM)Marco Polo Wrote:  I found one minor improvement, at least to me.
As the big stack does not "purge" automatically old objects, in real life use the stack may became very deep.
In RPL models i use a lot the CLEAR function (on the 50g is Orange_Sht+bksp) to periodically purge the stack.
In Free42 the CLST command is buried in CLEAR menu and need a lot of keystrokes to be reached.
At present i mad a workaround by mean of custom menu.
In would be nice to have CLST mapped somewhere in the keyboard.

A lot of keystrokes for CLST? [Shift] [←] [LOG], three keystrokes, I think that's not so bad, and I use it all the time.

But I understand that not everybody feels that way. There are several skins in my collection that have CLST on the keyboard, take a look at the skins collection at my web site, there might be something there you like.

For sone odd reason (age? lockdown?) i did not see CLST is in the first page of CLEAR menu. So i cycled with arrow....
Totally equivalent to custom key.

Maybe i need some rest in a wood cottage far from work and family :-D
Find all posts by this user
Quote this message in a reply
Post Reply 




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