![]() |
Previous | Table Of Contents | Next | ![]() |
Executing a Program
You can only edit your program while you are in Program mode (i.e., while the Prgm/Run slide switch is set to Prgm) and you can only execute a program while you are in Run mode (i.e., while the Prgm/Run slide switch is set to Run). So if you have fully entered the 6 statement IncrementForever.txt program that we created in the prior section then move the Prgm/Run slide switch back to Run in preparation for executing this program. The Prgm/Run slide switch controls the mode of the calculator but does not cause the stored program to begin executing.
Let me emphasize that while the calculator is in its Run mode a click on the Multiply key will cause a multiplication (of the numbers in the X and Y stack locations). But while the calculator is in its Program mode a click on the Multiply key will cause a * mnemonic to be inserted into program memory just below the current insertion point (the highlighted instruction). This mnemonic will cause a multiplication (of the numbers in the X and Y stack locations) to occur when we reach this statement while executing the program.
All computers execute their programs by performing the instructions found in program memory in sequential order. That is, each instruction is performed one after the other. The only way to get a program to jump to a different section of program memory is to employ a branch or call statement.
The hardware mechanism that is used to walk through the statements in program memory is a program counter (PC), which is a conventional counter that increments as each instruction is performed. If the PC holds the value 05 then the statement at program memory location 05 is executed next. Assuming this was not a branch or call statement then the PC will be incremented to 06 and the statement at program memory location 06 will be executed next. The way that branch and call statements alter the flow of control is that they load the PC will a new, non-sequential value (we will study branch and call statements in a later section).
You have a few alternatives when you wish to influence the PC manually. We will first learn about the GTO .nn ("goto") generic instruction. The nn portion of this instruction indicates that you must always include exactly 2 decimal digits indicating the new value you wish for the PC. Two digits are required because program memory can hold up to 98 statements and hence the PC can take on any value between 00 (which can't hold an instruction) and 98. We have already seen the GTO .nn statement in Program mode but this keystroke is available in Run mode as well. In Program mode the GTO .nn statement modifies the current insertion point and in Run mode the GTO .nn statement modifies the program counter.
From this you can conclude that the calculator employs the program counter to manage the current insertion point. You can observe that this is so because every time you switch from Program mode to Run mode the next instruction that will be executed (assuming you don't intervene and modify the PC) will be the instruction at the current insertion point. And every time you switch from Run mode to Program mode the current insertion point will be the last instruction that was executed. To summarize, in both Program mode and Run mode there is always a single, specially designated instruction among the 98 instructions in program memory and the PC points to this instruction. In Program mode it is the instruction that will be deleted or the instruction just prior to the next instruction that gets inserted. In Run mode it is the next instruction to be executed.
In the last section we wrote and edited our first program, which we stored in the file IncrementForever.txt . We now wish to execute this program. We must be in Run mode to execute a program so be sure the Prgm/Run slide switch is set to Run. One way to get a program to begin executing is to click on the R/S (Run/Stop) key. The R/S key causes execution to initiate at the instruction pointed to by the PC. Consequently you will almost always want to initialize the PC before you click on R/S to start a program. We can start our IncrementForever.txt program via the keystroke sequence:
![]() |
![]() |
![]() |
![]() |
![]() |
While a program is running the calculator's LED display is hard to read since the operand stack is constantly changing. Allow this program to run for a few seconds and then click any calculator key to halt the program. I always choose to use the R/S key to halt a program but any key can be used. In this situation the key that you select does not perform its normal function but simply halts the program. Once the program halts you will either see the number 1 in the X stack location or some other number, depending upon precisely where the IncrementForever.txt program was interrupted. If the X stack location holds the number 1 then the Y stack location will hold the number that we have counted up to. Otherwise the X stack location will hold the number that we have counted up to. After running this program for 2 seconds on a 400 MHz Pentium II computer the calculator will have counted up past 1000.
You can continue the program from wherever it left off simply by clicking the R/S key. Execution continues from where it left off because nobody has modified the PC. And hence the incrementing count will continue from where it left off. Alternatively, you can re-initiate the program from its beginning by using the same keystroke sequence as before:
![]() |
![]() |
![]() |
![]() |
![]() |
In this case we reset the PC to 0 and hence the program starts from the beginning and hence the incrementing count is re-initiated from the value 1. Note that the keystroke sequence GTO.01, R/S would be just as good as GTO.00, R/S since there is no instruction stored at program memory location 00.
![]() |
Previous | Table Of Contents | Next | ![]() |