›› Controlling Program Flow
Index
Instead of writing one very large program you should consider breaking the program up into smaller functional
sections or subprograms. Subprograms can be internal or external to your program. External subprograms are standard programs
and will be assembled and linked as individual programs. You move around in your program by using branch instructions.
In addition you can repeat certain sections in a loop without having to code the same piece of code repeatedly.
We will discuss external subprograms in the advanced sections. For now lets look how to use the branch instructions.
Branch and Link Register (BALR) instruction
The BALR instruction will branch to the address in R2. Before the branch takes place the address of the next instruction in your program following the BALR instruction in placed in R1.
If the second operand (R2) contains 0 no branch will take place and the only action that would take place would be to load the next instruction address into the first operand. >
With the current tri modal design R1 will contain different information depending if you are in 24, 31 or 64 bit address mode. Refer to the z/Architecture Principles of Operation manual mentioned at the home page for 64 bit addessing information.
In 24 bit addressing the address is the last 3 bytes of the register.
In 31 bit addressing the left most bit will contain a 1. This is from bit 32 in the PSW and it indicates 31 bit addressing mode. The rest of the register contains the address of the return address.
After your subprogram is done it can return to the calling section by using a BR (branch register) instruction.
Instruction format
+--------+-------+
| op-code| R1| R2| 4 bytes long x'05'
+--------+-------+
0 8 15
Format:
BALR R1,R2
LA R6,MOON
BALR R4,R6 /* Go to the MOON and do some*/
EARTH SR R4,R4 /* stuff. */
.
.
.
MOON EQU * /* */
.
.
BR R4 /* Return to EARTH */
Branch and Link (BAL) instruction
This is the same as the BALR instruction but insread of a register containing the address to link too the effective address is a relocatable area defined by a label in the program.
Again if the second operand contains 0 no branch will take place and the only action that would take place would be to load the next instruction address into the first operand. >
After your subprogram is done it can return to the calling section by using a BR (branch register) instruction.
Instruction format
+--------+-------+---+---+---+---+
| op-code| R1| X2| B2| D2 | 4 bytes long x'45'
+--------+-------+---+---+---+---+
0 8 16 20 31
Format:
BAL R1,D2(X2,B2)
BAL R4,MOON /* Go to the MOON and do some*/
EARTH SR R4,R4 /* stuff. */
.
.
.
MOON EQU * /* */
.
.
BR R4 /* Return to EARTH */
Branch on Condition (BCR) instruction
With this instruction you set a mask value. This mask is a 4 bit area numbered from left to right 0, 1,2, and 3.
This mask is compared to the condition code in the PSW. If you have a bit on and it matches the corrent condition code the branch will take place.
If no match is found no branch will be taken. If you have all bits set on a unconditional branch will be taken.
As an example if you set the mask as B'1000' the branch will be taken if the condition code is zero.
If the mask is set to B'0110' a branch will be taked of the condition code is 1 and 2.
Instruction format
+--------+-------+
| op-code| M1| R1| 2 bytes long x'07'
+--------+-------+
0 8 12 15
Format:
BCR M1,R1
BCT B'1111',R2 /* branch unconditional to */
/* address in R2 */
BCT X'B',R2 /* Branch to R2 if mask */
/* is '1011'
BCT 5,R2 /* Branch on mask '0011' */
Branch on Count (BCT) instruction
BCT Instruction format
+--------+---+---+---+---+---+---+
| op-code| R1| X2| B2| D2 | 4 bytes long x'46'
+--------+---+---+---+---+---+---+
0 8 12 16 20 31
Format:
BCT R1,D2(X2,B2)
Branch on Count Register (BCTR) instruction
This a fairly easy and often used instruction. You need to load a value into the first operand. When the instruction gets executed this value is tested. If it's zero it does not branch. If it is bigger than zero 1 will be subtracted from this value and the branch will take place. The address of the branch will either be in the register on the second operand or the determined effective address using the base displacement method.
The BCTR instruction can be used to subtract 1 from a register and no condition code would be set. Using a 0 as the will also cause no branching to occur.
BCTR Instruction format
+--------+---+---+
| op-code| R1| R2| 2 bytes long x'06'
+--------+---+---+
0 8 12 15
Format:
BCT R1,D2(X2,B2)
You can get in trouble when you set the instruction sequences up wrong. As stated before if the first operand contains zero the branching stops. If you set this value to zero the very first time 1 would be subtracted from this value. It will now not be zero when the test happens. It's -1. This loop will continue until -2,147,483,648. Thi number minus one will be become the largest positive number 2,147,483,647. From this number 1 would be subtracted continuesly until you get to zero. Now the branch would stop.
BCTEX1 LA R5,6 /* Load R5 with the value 6 */
LOOP . /* start of loop */
.
.
BCT R5,LOOP /* Do loop again until R5 = 0 */
DONE . /* we are done with the loop */
BCTEX2 LA R5,6 /* Load R5 with the value 6 */
LA R6,LOOP /* Load address of LOOP */
LA R6,LOOP /* into R6 */
* BALR R6,0 /* We could have done this */
* /* instead. What? See BALR */
* /* earlier. */
LOOP . /* start of loop */
.
.
BCTR R5,R6 /* Do loop again until R5 = 0 */
DONE . /* we are done with the loop */
BCTEX3 BCTR R5,0 /* Subtract 1 from R5 and */
. /* continue with next inst. */
You now know about how to set a mask to branch if the condition code has a certain value.
This if very error prone and an easier way exist to do this. It's called extended mnemonics.
Extended mnemonics is a short way to say if a condition code is a certain value branch.
Here is a table showing some of the mnemonics:
Description Instruction Meaning Equal instruction
Branch unconditional B moon B b'1111' BC 15,moon
BR R4 BCR 15,R4
Branch on Equal BE moon B b'1000' BC 8,moon
BER R4 BCR 8,R4
Branch on now equak BNE moon B b'0111' BC 7,moon
BNER R4 BCR 7,R4
Branch on low BL moon B b'0100' BC 4,moon
BLR R4 BCR 4,R4
Branch on not low BNL moon B b'1011' BC 11,moon
BNLR R4 BCR 11,R4
Branch on high BH moon B b'0010' BC 2,moon
BHR R4 BCR 2,R4
Branch on not high BNH moon B b'1101' BC 13,moon
BNHR R4 BCR 13,R4
No branching takes place NOP moon B b'0000' BC 0,moon
NOPR R4 BCR 0,R4
After arithmatic instructions:
Branch on Zero BZ moon B b'1000' BC 8,moon
BZR R4 BCR 8,R4
Branch on not Zero BNM moon B b'0111' BC 7,moon
BNMR R4 BCR 7,R4
Branch on Positive BP moon B b'0010' BC 2,moon
BPR R4 BCR 2,R4
Branch on not Positive BNP moon B b'1101' BC 13,moon
BNPR R4 BCR 13,R4
Branch on Minus BM moon B b'0100' BC 4,moon
MBR R4 BCR 4,R4
Branch on not Minus BNM moon B b'1011' BC 11,moon
BNMR R4 BCR 11,R4
Branch on Overflow BO moon B b'0001' BC 1,moon
BOR R4 BCR 1,R4
Branch on not Overflow BNO moon B b'1110' BC 14,moon
BNOR R4 BCR 14,R4
Links