Ways to Use Tests with Transfer Instructions
By following a test with a transfer instruction such as GTL or SBL, you can use the test to conditionally execute entire program segments. You can use such a combination, for example, to conditionally execute one of two segments or to conditionally call a subroutine.
Executing One of Two Segments
You may want to execute only one of two program segments according to the result of a test.In the following illustration, an IF< test is used with a GTL transfer instruction to determine which of the two segments BB or CC will be executed.
- If the test is true, the program skips the segment labeled BB and transfers control to label CC.
- If the test is false, the program executes the segment labeled BB. Because the BB segment ends with a HLT instruction, the program does not execute label CC.
Example
Write a program that uses two labeled program segments: one to sum a negative number into data register B, and a second to sum a positive number into register C. A test of the number determines which segment is executed.PC = | Program Mnemonics | Comments |
---|---|---|
0000 | STO A | Stores entered number |
0002 | 0 IF< A | Is number positive? |
0005 | GTL CC | Yes - go to label CC |
0008 | LBL BB | No - executed label BB |
0011 | `NEGATIVE` PAU | Creates message |
0020 | RCL A | Recalls number |
0022 | ST+ B RCL B | Sums to B and recalls sum |
0026 | HLT | Stops program |
0027 | LBL CC | Labels segment |
0030 | `POSITIVE` PAU | Creates message |
0039 | RCL A | Recalls number |
0041 | ST+ C RCL C | Sums to C and recalls sum |
0045 | HLT | Stops program |
Running the Example
Test the program by entering a negative number and a positive number.Procedure | Press | Display |
---|---|---|
Clear display and data registers | [ CLEAR ] [ 2nd ] [ CMS ] | |
Display RUN menu | [ RUN ] | |
Enter a negative number | 6.4 [ +/- ] { PGM } | |
Enter a positive number | 10 { PGM } |
Skipping a Segment of a Program
You may want to skip a segment of a program depending on the result of a test. For example, if the result of a calculation is negative, you may want the program to skip the next five instructions.In the following illustration, an IF= comparison test is used with a GTL instruction to determine whether the segment labeled BB will be skipped. (The main difference between this and the previous example is the removal of the HLT instruction that preceded label CC.)
- If the test is true, the program skips the segment labeled BB and transfers control to label CC.
- If the test is false, the program executes the segment labeled BB and continues executing through the segment labeled CC.
Example
This program sums each number you enter to data register A. The segment labeled BB sums a number to data register B, but is executed only for numbers that are evenly divisible by 6.PC = | Program Mnemonics | Comments |
---|---|---|
0000 | CMS | Clears data registers |
0001 | DFN F1:ENT@EN | Defines F1 for entry |
0008 | LBL AA | Labels segment |
0011 | CLR | No - executed label BB |
0012 | `ENTER NUMBER` | Creates message |
0024 | HLT | Waits for entry |
0025 | LBL EN | Labels segment |
0028 | ST+ A STO C | Sums to A, stores in C |
0032 | / 6 = | Divides number by 6 |
0035 | FRC STO D | Stores fractional part |
0038 | 0 INV IF= D | Is number non-zero? |
0042 | GTL CC | Yes - go to CC |
0045 | LBL BB | Labels segment |
0048 | `DIVISIBLE BY 6` | Creates message |
0062 | PAU | Pauses |
0063 | RCL C ST+ B | Sums original number to B |
0067 | LBL CC | Labels segment |
0070 | `SUM =` | Creates message |
0074 | COL 16 MRG A PAU | Shows the sum |
0079 | GTL AA | Repeats loop |
Running the Example
Run the program and enter a number that is not divisible by 6. The program sums the number to register A, but skips label BB and does not sum the number to register B.Now enter a number that is divisible by 6. The program sums the number to register A, displays an additional message, and sums the number to register B.
Procedure | Press | Display |
---|---|---|
Run the program | [ RUN ] { PGM } | |
Enter a number not divisible by 6 | 15 | |
Continue running, display sum | { ENT } | |
Enter a number divisible by 6 | 18 | |
Continue running | { ENT } | |
Verify sum in register A | [ RCL ] A | |
Verify sum in register B | [ RCL ] B |
Conditional Execution of a Subroutine
Your program may have a subroutine that you want to call only under specific conditions. By preceding an SBL or SBR instruction with a test instruction, you can use the test to control whether the subroutine will be called.In the following illustration, a TF test is used with SBL to determine whether the program will execute the subroutine labeled BB.
- If flag 00 is set, the program executes subroutine BB before returning control to the segment labeled CC.
- If flag 00 is reset, the program proceeds to the segment labeled CC without executing the subroutine.
Example
This program contains a subroutine labeled BB that displays the subtotal of numbers as you sum them to data register A. The test instruction at program step 0048 ensures that the subroutine is called only if you select { YES } at the start of the program.
PC = | Program Mnemonics | Comments |
---|---|---|
0000 | CF | Clears flags |
0001 | 0 STO A STO C | Clears registers A and C |
0006 | `SHOW SUBTOTAL?` | Creates message |
0020 | Y/N | Want subtotal? |
0021 | SF 00 | { YES } - set flag 00 |
0023 | DFN CLR | Clears Y/N definitions |
0025 | LBL AA | Labels segment |
0028 | INC C | Increments item count |
0030 | CE `ENTER TERM` | Creates message |
0041 | COL 14 MRG C | Merges item count |
0045 | BRK | Waits for entry |
0046 | ST+ A | Sums entry to A |
0048 | TF 00 | Subtotal requested? |
0050 | SBL BB | Yes - show subtotal |
0053 | GTL AA | Repeats loop |
0056 | LBL BB | Labels subroutine BB |
0059 | `TOT=` | Creates message |
0063 | COL 16 MRG A | Merges subtotal |
0067 | PAU | Pauses |
0068 | RTN | Returns |
Running the Example
Run the program and select { YES } when it asks if you want to show the subtotal. After the program prompts you for each item to be summed, subroutine BB shows the current subtotal.Run the program a second time and select { NO }. The program does not call subroutine BB to display the current subtotal as you enter numbers.
☚ Back