Page 1 of 1

Programmatically Preserving Last-X Stack Register

Posted: Thu Apr 26, 2018 11:16 am
by J Peters
This is a question about programming practice/methodology.

I have written a some programs which take a numeric input from the X-register and perform some function on it, leaving the result(s) on X (and sometimes on Y if two parameters need outputting).

After the program has run, the Last-X register is left in a fairly meaningless state, since it will show the last X from the most recent intermediate calculation which the program performed. It strikes me that it would be better programming practice, and more useful, to leave the original input in the Last-X register when the program exits, just as it would be for an internal function.

For example, the processing steps for converting Celsius to Fahrenheit might be:
9
x
5
/
32
+

This would leave the result in X, but Last X would be showing '32' which is pretty pointless. I would rather Last X showed the original input (Celsius).

It's occurred to me to use something like:
STO "ORIGX"
9
x
5
/
32
+
RCL "ORIGX"
STO ST L
R↓ (roll stack down)

This does the trick but it shifts the stack up and back down again, potentially losing something off the top that you may have wished to keep. It also looks to me like a clunky and fairly hideous piece of programming :D Oh and I'd probably want to add a CLEAR "ORIGX" on the end as well so we don't clutter the variables menu with dross.

Are there any more elegant solutions?

Re: Programmatically Preserving Last-X Stack Register

Posted: Thu Apr 26, 2018 2:00 pm
by dlachieze
J Peters wrote:
Thu Apr 26, 2018 11:16 am
RCL "ORIGX"
STO ST L
R↓ (roll stack down)
To preserve the stack I would do:

X<> ST L
X<> "ORIGX"
X<> ST L
CLV "ORIGX"

Re: Programmatically Preserving Last-X Stack Register

Posted: Thu Apr 26, 2018 3:40 pm
by rprosperi
dlachieze wrote:
Thu Apr 26, 2018 2:00 pm
J Peters wrote:
Thu Apr 26, 2018 11:16 am
RCL "ORIGX"
STO ST L
R↓ (roll stack down)
To preserve the stack I would do:

X<> ST L
X<> "ORIGX"
X<> ST L
CLV "ORIGX"
Clever and elegantly simple Didier, thanks!

Re: Programmatically Preserving Last-X Stack Register

Posted: Fri Apr 27, 2018 5:36 pm
by J Peters
Very many thanks for the replies. If I use a register variable then there is no need to clear it, so this gets the code down to 3 lines, and either of these work. Assumes original input value is in R00:

X<> ST L
X<> R00
X<> ST L

or

X<> R00
X<> ST L
X<> R00

Edit: Actually I think this is better:

X<> R00
STO ST L
X<> R00

This leaves the original input value in R00 which might serve some purpose on occasion. Thanks it might have taken me a while to hit on the idea of register swapping without your original suggestions.