Programmatically Preserving Last-X Stack Register

Post here to share useful tips and tricks, to ask questions about using your DM42 or to report software-related problems
Post Reply
J Peters
Posts: 13
Joined: Wed Apr 18, 2018 9:53 am

Programmatically Preserving Last-X Stack Register

Post 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?
dlachieze
Posts: 613
Joined: Thu May 04, 2017 12:20 pm
Location: France

Re: Programmatically Preserving Last-X Stack Register

Post 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"
DM42: 00425 - DM41X: β00066 - WP43: 00042
rprosperi
Posts: 1703
Joined: Mon Apr 24, 2017 7:48 pm
Location: New York

Re: Programmatically Preserving Last-X Stack Register

Post 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!
--bob p

DM42: β00071 & 00282, DM41X: β00071 & 00656, DM10L: 071/100
J Peters
Posts: 13
Joined: Wed Apr 18, 2018 9:53 am

Re: Programmatically Preserving Last-X Stack Register

Post 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.
Post Reply