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 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?

## Programmatically Preserving Last-X Stack Register

### Re: Programmatically Preserving Last-X Stack Register

To preserve the stack I would do:

X<> ST L

X<> "ORIGX"

X<> ST L

CLV "ORIGX"

DM42: 00425 - DM41X: β00066

### Re: Programmatically Preserving Last-X Stack Register

Clever and elegantly simple Didier, thanks!

--bob p

DM42: β00071 & 00282, DM41X: β00071 & 00656, DM10L: 071/100

DM42: β00071 & 00282, DM41X: β00071 & 00656, DM10L: 071/100

### Re: Programmatically Preserving Last-X Stack Register

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.

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.