Programmatically Preserving Last-X Stack Register
Posted: Thu Apr 26, 2018 11: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 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?
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?