![]() |
Previous | Table Of Contents | Next | ![]() |
Branch And Call Operations
A computer runs its program by executing the statements of that program in sequence. The statements are normally executed in the order they appear in the program. This is accomplished via a program counter (PC) which selects the next statement to be executed. After most statements the program counter is simply incremented and hence the subsequent statement in the program will be the next statement to be executed.
However this flow of control can be altered by branch and call statements. A branch statement permanently jumps to a new section of the program. By "permanent" I mean no preparations are made for a later return to the original location. To accomplish a jump we simply re-initialize the program counter to a new value instead of merely incrementing it.
A call statement temporarily jumps to a new section of the program. This change in locality is temporary because the original program location is remembered and ultimately returned to. Call statements are further discussed in the section entitled subroutines.
Branch statements are either conditional or unconditional. A conditional branch is a jump that is taken only if a certain condition is satisfied at the moment the conditional branch instruction is executed. A conditional branch can be constructed by pairing a conditional statement such as x<0 with a GTO n statement.
An unconditional branch statement does not depend upon any conditions at the time the statement is encountered. Hence an unconditional branch is always taken. An example of an unconditional branch is the GTO n statement.
The list of branch and call operations supported by the RPN Calculator is shown below. Each of these operations is identified by both a textual instruction statement and a keystroke sequence. The former is used when you prepare your program via a word processor such as Notepad and the latter is used when you prepare your program by clicking on the calculator's keys. In a generic instruction statement such as GSB n the letter n is a placeholder for a single digit; i.e., a member of the set {0,1,...9}.
GSB n | ![]() ![]() |
Gosub meaning enter and execute the subroutine having the label n. Execution will return to the original location when the RTN statement is encountered in this subroutine. An error message will be displayed if the specified label does not exist. Note that subroutines can only be nested 3 levels deep (this is further discussed in the section entitled subroutines). |
---|---|---|
GSB i | ![]() ![]() ![]() |
When the value held in register #0 is positive this statement
causes the calculator to enter an indirectly addressed subroutine.
That is, the calculator will enter the subroutine indicated by
the (integer portion of the) value currently in register #0.
An error message will be displayed if this subroutine does not exist.
When the value held in register #0 is negative this statement causes the program counter (PC) to be decremented by the integer portion of the absolute value of the number currently in register #0. Execution then continues from that location in the program. This is exactly what the GTO i statement does when the value in register 0 is negative (see the IncrementForever4.txt program for an example). |
RTN | ![]() ![]() |
Return from the subroutine. Execution resumes at the statement following the GSB statement that entered the subroutine. It is the subroutine return address stack that remembers the location where execution should resume at the completion of the subroutine. This is further discussed in the section entitled subroutines. Either a R/S or a RTN statement can be employed at the very end of your program to halt execution and return control to the keyboard. |
GTO n | ![]() ![]() |
Goto label n. Unconditionally branch to the label identified by the number n. An error message is displayed if this label does not exist. If executed from the keyboard, this statement modifies the program counter (PC) but does not execute any instructions. If part of a program, this statement causes execution to continue at the new location. |
GTO i | ![]() ![]() ![]() |
When the value held in register #0 is positive this statement
causes the calculator to unconditionally branch to the indirectly
addressed label.
That is, the calculator will jump to the label indicated by
the (integer portion of the) value currently in register #0.
An error message will be displayed if this label does not exist.
When the value held in register #0 is negative this statement causes the program counter (PC) to be decremented by the integer portion of the absolute value of the number currently in register #0. If this statement is part of a program then execution continues at the new location. If this statement is entered from the keyboard then the PC is reset to this value but the program does not begin executing. See the IncrementForever4.txt program for an example. |
GTO .nn | ![]() ![]() ![]() ![]() |
Goto program step nn. This means the program counter (PC) is re-initialized to the value given by nn, a two digit value which can range from 00 to 98 (the value 99 has the same consequence as the value 01). The program counter always points to the next statement to be executed. Note that it is impossible to include this statement in a program since it is used to edit your program (so use the GTO n statement instead). |
LBL n | ![]() ![]() ![]() |
Creates the label having the number n which can range from 0 thru 9. Label statements must be used for the initial statement in subroutines and are also useful as destinations for branch statements. |
DSZ | ![]() ![]() |
"Decrement and Skip if Zero". If executed from the keyboard this statement decrements the value held in register #0. In a program, this statement decrements the value held in register #0 and then skips over the next instruction if register #0 now holds the value zero. It is very common to use an unconditional branch as the "skipped over" instruction thus creating a conditional branch. |
ISZ | ![]() ![]() |
"Increment and Skip if Zero". If executed from the keyboard this statement increments the value held in register #0. In a program, this statement increments the value held in register #0 and then skips over the next instruction if register #0 now holds the value zero. |
x<=y | ![]() ![]() |
If this condition is false (that is, the contents of the X stack location are not less than or equal to the contents of the Y stack location) then the next program statement is skipped. It is very common to use an unconditional branch as the "skipped over" instruction thus creating a conditional branch. |
x>y | ![]() ![]() |
If this condition is false then the next program statement is skipped. |
x!=y | ![]() ![]() |
If this condition is false then the next program statement is skipped. |
x=y | ![]() ![]() |
If this condition is false then the next program statement is skipped. |
x<0 | ![]() ![]() |
If this condition is false (that is, the contents of the X stack location are not less than zero) then the next program statement is skipped. |
x>0 | ![]() ![]() |
If this condition is false then the next program statement is skipped. |
x!=0 | ![]() ![]() |
If this condition is false then the next program statement is skipped. |
x=0 | ![]() ![]() |
If this condition is false then the next program statement is skipped. |
![]() |
Previous | Table Of Contents | Next | ![]() |