Stack Save allows for easy saving and restoring of stack values from within other programs. Traditionally, one must perform tedious manual saving and restoring from within each program. Stack Save simplifies this process, requiring only a few options to be set, and calls to SAVE and RESTORE to be made at the beginning and end of the program.
When run, SAVE will save a copy of the entire stack, and optionally REGS and LAST X. The following options control the behavior of SAVE, and should be set prior to calling it:
Based on options set beforehand, RESTORE will restore the state of the stack in a fashion consistent with the behavior of built in-functions.
The reason global variables are used to control the number of inputs and outputs is that being forced to put these values on the stack before calling RESTORE would limit the number of values your program could output to only two. With global variables, they can be set at any point in the program when enough stack levels are available to do so without losing important values.
Program Listing - A plain text listing of the Stack Save programs. Suitable for entry on a real 42s or a simulator.
Raw program file - A .raw format file suitable for loading into a 42s simulator.
Following is a sample program with annotations demonstrating the usage of SAVE and RESTORE.
01 LBL "TEST" 02 STO "_O" 03 RDown 04 STO "_A"This section takes the values for _O and _A from the stack to allow experimenting with RESTORE behavior in different scenarios. (Normally you would set these to static values at some point in your program before calling RESTORE.) As an example, enter 2 and 1 on the stack before running TEST to see how the stack is saved and restored for a program with two inputs and one output.
05 1 06 2 07 3 08 4 09 SIZE 26Set up the test "before" state for testing. Some of these values will be considered inputs used by the program based on the value of _A. Normally your "before" state will be whatever is on the stack when the user executes your program.
10 "TEST"Set the ALPHA register so that "TEST" is added to the temporary variable names. (X_TEST, Y_TEST, etc.)
11 SF 91 12 CF 92Enable saving and restoring REGS, and use the value of X (4 in our "before" stack) as the new LAST X.
13 XEQ "SAVE"Here's where the magic happens - or at least the first part of it. Upon completion, the stack, ALPHA, and REGS will be in the same state as before executing SAVE, and your program can carry on with its business.
14 SIZE 10 15 CLST 16 10 17 20 18 30 19 40These are some sample operations to change the state of the calculator. Lengthy calculations could take place at this point. Some of these sample values placed on the stack will be considered legitimate output values, and some will be considered intermediate "junk" values to be overwritten with restored stack values depending on what _O is set to when RESTORE is called.
21 XEQ "RESTORE"The remainder of the magic. Using the values supplied for _A and _O, a number of stack values will be restored to what they were prior to calling SAVE. In this example program, REGS will be restored as well, and the value in X when SAVE was called will become the new LAST X.
To run the test program, enter 2 in the Y register, and 1 in the X register, then XEQ "TEST". The values 3 and 4 will be consumed as parameters and removed from the stack. You should see 40 in the X register as the one output value, and 1 and 2 will be moved down to Z and Y respectively, to fill the gap left by 3 which was consumed from the Y register. The value of T will remain 1, as it does when a built-in function causes the T register to fall down to Z and be copied in the process. Also, REGS will be restored to the 26 element size.
Try entering other combinations of values between 0 and 4 before running TEST to see the results. Insert STOP instructions into the TEST program at any locations where you would like to single-step through the program.