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: 7
Joined: Wed Apr 18, 2018 7:53 am

Programmatically Preserving Last-X Stack Register

Post by J Peters » Thu Apr 26, 2018 9:16 am

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: 140
Joined: Thu May 04, 2017 10:20 am
Location: France

Re: Programmatically Preserving Last-X Stack Register

Post by dlachieze » Thu Apr 26, 2018 12:00 pm

J Peters wrote:
Thu Apr 26, 2018 9: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 SN: 00425

rprosperi
Posts: 375
Joined: Mon Apr 24, 2017 5:48 pm
Location: New York

Re: Programmatically Preserving Last-X Stack Register

Post by rprosperi » Thu Apr 26, 2018 1:40 pm

dlachieze wrote:
Thu Apr 26, 2018 12:00 pm
J Peters wrote:
Thu Apr 26, 2018 9: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

J Peters
Posts: 7
Joined: Wed Apr 18, 2018 7:53 am

Re: Programmatically Preserving Last-X Stack Register

Post by J Peters » Fri Apr 27, 2018 3:36 pm

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