![]() |
Previous | Table Of Contents | Next | ![]() |
Single Stepping a Program
So far we have only seen the consequences of the running program; that is, we have seen the changes in the operand stack. But this Windows simulator allows you to actually observe the execution of the program itself. To accomplish this halt the program and then click on the SST (single step) key. The SST key is employed for a type of debugging activity called single-stepping which is where you only allow your program to execute (perform) a single statement at a time. Which statement? The one pointed to by the PC of course. And because the SST key indicates that you wish to debug your program two new panes open up in the simulator's window. The Program window we have seen before. It is the same pane that is visible while we are in Program mode. The Registers window is new. It displays the contents of the 30 data memory locations available on this calculator. We will employ data memory in later programs but for now just ignore this pane. These new window panes are shown below:
Now that the Program window is visible let's continue our program by clicking on R/S. You can now observe the blue highlight constantly moving to identify the particular program memory instruction being executed. You will see that this program constantly cycles among the 4 consecutive statements in program memory locations 03 through 06. This is the infinite loop that we placed in our program.
Halt the program (by clicking any key) and then perform the following keystroke sequence:
![]() |
![]() |
![]() |
![]() |
Observe that the highlight moves to statement 00. Next click AND CONTINUE TO DEPRESS the SST key. Observe that while you hold the SST key depressed the highlight moves to statement 01 and the X location of the stack shows the same statement, which is the statement about to be executed. When you release the SST key this one statement is executed and the X location of the stack goes back to showing operands rather than statements. The highlight moves on to the 02 statement to show that the PC is identifying this as the next statement to be executed. Click on the SST key a few more times. Note that each program memory instruction is executed in sequential order. However the consequence of executing statement 06 is that the PC is reset to 03 (this is because statement 06 is a GTO statement).
Single stepping is just a slow way of running a program and hence is especially useful for debugging a faulty program. At any time you can click on the R/S key to re-initiate full speed execution of the program. Execution starts from wherever the PC happens to be. Click the R/S key a few times and observe how chance decides exactly where you interrupt the program.
Now let's do something devious. Halt the program on one of the statements within the infinite loop (that is, on statement 03, 04, 05, or 06). Now type the following keystroke sequence:
![]() |
![]() |
![]() |
![]() |
![]() |
This keystroke sequence has the consequence of initializing the X stack location with the number -5000. This shows that we are allowed to do whatever we want to the operand stack while the program is halted. Then continue the program from wherever it left off by clicking on R/S. The program is not smart enough to know that we have perturbed its counting sequence. It simply continues doing what it is told: endlessly adding 1 to the number in the X stack location. So you will observe that the program (which we haven't changed in any way) is now counting up from -5000. Eventually the count will pass 0 and then continue into the positive integers. Single-step the program if you are confused by this last paragraph.
As another experiment, halt the program and change the X stack location to the fraction 0.25 . As long as you continue rather than restart the program you will now observe the sequence 0.25, 1.25, 2.25, ... Remember, the R/S key continues a program while the keystroke sequence GTO.00 followed by R/S restarts a program from the beginning.
I said you had a couple of options for how to initiate your programs but thus far I have only shown you the R/S keystroke (which is usually used in conjunction with GTO .nn). A more conventional way to initiate a program is to use the GSB n generic keystroke sequence. The n indicates that you must supply a single digit to identify the label where you want execution to start. Since our program starts with a LBL 0 statement then the following keystroke sequence will start our program running from the top:
![]() |
![]() |
Since this keystroke sequence starts the program from the top it will be pointless to initialize the X stack location with -5000: the first (okay, second) statement of the program forces the count to begin from 1 (try it). However if you want to employ this program to count up from -5000 then initialize the X stack location with -5000 and then type:
![]() |
![]() |
This works because our program also includes a LBL 1 statement. If we start execution at LBL 1 we bypass the portion of the program that forces the count to begin from 1 (look at the program and check what is between the LBL 0 and LBL 1 statements).
As another experiment try the keystroke sequence:
![]() |
![]() |
This keystroke sequence causes an error since there is no LBL 2 statement anywhere in program memory. The calculator displays an error message which you must clear before you can do anything else. Click on ANY calculator key to clear the error (the normal function of the key is ignored).
The RPN calculator has a small number of error messages. Another way to elicit an error message is to attempt to divide by 0 via the keystroke sequence:
![]() |
![]() |
There is yet another way to initiate a program and it involves the GTO n generic instruction. When executed as part of a program the GTO n statement causes the program counter (PC) to be re-initialized to a new value and hence program execution continues at a new location. When executed from the keyboard the GTO n statement re-initializes the PC but does not initiate execution of the program.
Note the difference between GTO .nn and GTO n. The former keystroke sequence includes a decimal point and exactly two digits while the latter has no decimal point and exactly one digit. Whereas GTO .nn sets the PC to any value between 0 and 98 the GTO n instruction sets the PC to point to a LBL statement. So we can prepare to run our program from the top by clicking:
![]() |
![]() |
Whereas the GSB keystroke modifies the PC and then starts execution of the program, the GTO keystroke merely modifies the PC. We must then click on R/S to start the program running.
Before ending this section I want to warn you of a very common beginner's error. While you are in Program mode the PC is employed to define the current insertion point. When you throw the Prgm/Run slide switch back to Run the PC retains whatever value it last had (probably at the end of the program you just entered). If you simply click on R/S (or SST) your program will probably start running at the R/S statements that exist in program memory beyond the end of your program. But the calculator thinks that anything in program memory is part of your program. A R/S statement in a program causes that program to halt. So every time you click on R/S the program will execute another one of the R/S statements in program memory beyond the intended end of your program. Basically, nothing will be happening and you will wonder why. So always initialize the PC via the GTO .nn or GTO n statements before you click on R/S or else use the GSB n statement which accomplishes both activities.
![]() |
Previous | Table Of Contents | Next | ![]() |