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.
ifless

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 AStores entered number
0002 0 IF< AIs number positive?
0005 GTL CCYes - go to label CC
0008LBL BBNo - executed label BB
0011 `NEGATIVE` PAUCreates message
0020 RCL ARecalls number
0022 ST+ B RCL BSums to B and recalls sum
0026 HLTStops program
0027LBL CCLabels segment
0030 `POSITIVE` PAUCreates message
0039 RCL ARecalls number
0041 ST+ C RCL CSums to C and recalls sum
0045 HLTStops 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 ]
D_4Ioc9B
Display RUN menu[ RUN ]D_mehN36
Enter a negative number6.4
[ +/- ]
{ PGM }
D_eamGST
D_rcWdCB
Enter a positive number10
{ PGM }
D_D9plwb
D_Ckdftl



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.
ifeq

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 CMSClears data registers
0001 DFN F1:ENT@ENDefines F1 for entry
0008LBL AALabels segment
0011 CLRNo - executed label BB
0012 `ENTER NUMBER`Creates message
0024 HLTWaits for entry
0025LBL ENLabels segment
0028 ST+ A STO CSums to A, stores in C
0032 / 6 =Divides number by 6
0035 FRC STO DStores fractional part
0038 0 INV IF= DIs number non-zero?
0042 GTL CCYes - go to CC
0045LBL BBLabels segment
0048 `DIVISIBLE BY 6`Creates message
0062 PAUPauses
0063 RCL C ST+ BSums original number to B
0067LBL CCLabels segment
0070 `SUM =`Creates message
0074 COL 16 MRG A PAUShows the sum
0079 GTL AARepeats 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 }
D_UmIHZs
Enter a number not divisible by 615D_FKvwB8
Continue running, display sum{ ENT }D_NJcVnr
D_UmIHZs
Enter a number divisible by 618D_4Voiol
Continue running{ ENT }D_LYZVEd
D_wkZrML
D_UmIHZs
Verify sum in register A[ RCL ] AD_jCZkdH
Verify sum in register B[ RCL ] BD_qCjQXZ



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.
progsub

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 CFClears flags
0001 0 STO A STO CClears registers A and C
0006 `SHOW SUBTOTAL?`Creates message
0020 Y/NWant subtotal?
0021 SF 00{ YES } - set flag 00
0023 DFN CLRClears Y/N definitions
0025LBL AALabels segment
0028 INC CIncrements item count
0030 CE `ENTER TERM`Creates message
0041 COL 14 MRG CMerges item count
0045 BRKWaits for entry
0046 ST+ ASums entry to A
0048 TF 00Subtotal requested?
0050 SBL BBYes - show subtotal
0053 GTL AARepeats loop
0056LBL BBLabels subroutine BB
0059 `TOT=`Creates message
0063 COL 16 MRG AMerges subtotal
0067 PAUPauses
0068 RTNReturns


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