360 Assembly/Branch Instructions (original) (raw)

The branch instructions for the 360 Series mainframe computer come in two types: instructions which branch where a return address is provided (such as a subroutine call) and one-way branches where no return address is provided.

All branch instructions come in 3 forms: No Branch At all, otherwise known as No-Operation or NO-OP, Conditional Branch, and Unconditional Branch

In the examples below, it is presumed that R0,R1,R14,R15 are labels "equated" to the equivalent registers in these examples (0, 1, 14 and 15, respectively) to provide a cross reference listing. The examples would function equally well without the R prefixes, but it is common practice for programmers to use a label equated to each of the numbers, because references to registers do not show up in the cross-reference listing, but references to symbols do.

The following instructions provide a branch to another address, where a register is provided to store the address of the instruction following the branch to provide a means to return to the calling point.

The following instructions are available for all models of the 360 series: 360, 370, 390, 390/ESA and z/System.
* BAL - Branch And Link * BALR - Branch and Link Register
The following instructions are available only on 370 and above: 370, 390, 390/ESA and z/System.
* BAS - Branch And Save * BASR - Branch and Save Register

The following instructions are available for all models of the 360 series, including the 360, 370, 390 and z/System.

* BC - Branch on Condition[1] * BCR - Branch on Condition Register[1] * BCT - Branch on CounT * BCTR - Branch on CounT Register

[1] Extended mnemonics provide condition tests, including BR and B for unconditional branch; BNO, BE, BNE, BH, BL and BM, among others

The general use of these is explained below.

Certain types of branch instructions are treated as a no branch, or no-operation (NO-OP). Generally they involve the use of Register 0 or a mask value of 0. These are:

Label1 BR R0 No branch BC 0,Label1 Branch not taken; mask is 0 BALR R14,R0 Branch not taken; this is used at the start of a

An unconditional branch instruction causes the Program location counter (PSW) to be set to the address specified in the register or the register plus a 12-bit offset, or the register & offset plus the value of an additional "index" register.

The branch does not occur, and is treated as a no-op, for a BR instruction using register 0. The branch does not occur, and is treated as an instruction to load the address of the following instruction into the left register, if the right register in a BALR instruction is 0. No conditional branch - including unconditional branch - will occur if the index register is 0.

These instructions may be one of the following types:-

Example1 BR R15 Branch to the location whose address is in Register 15 Example2 BR R0 No Branch - Acts like no-op Example3 BALR R14,R15 Branch to the location whose address is in Register 15, put

Example4 BALR R12,R0 Load Register 12 with the address of the next instruction,

Example1 B 4(R15) Branch to the location in R15 plus the (12 bit)

Example2 B X'010'(R15) Branch to the location in R15 plus the (12 bit) hex

Example3 B LABEL1 Branch to the location with the specified address (Base &

Example4 BAL R14,X'010'(R15) Branch to location in (R15 plus displacement), put

**LABEL1 EQU *** A location (within the range of the base register

Example1 B 4(R15,R1) Branch to location whose address is calculated from R15

Example2 B 10(R12,R0) No branch because index register is 0; treated as NO-OP Example3 B 10(R0,R12) This is standard, and will branch to the address at the

A conditional branch instruction causes the location counter in the PSW to be set to the address specified in the register or the register plus a 12-bit offset, if a condition is satisfied (and the register is not 0). There are two types, condition by mask and condition by index.

Conditional branches may be one of the following types:-

Example BE 4(R15) Branch to the location in (R15 plus 4), if previous

Example BE 12(R0) As specified earlier, branch on R0 is treated as a NO-OP

Example BC 8,4(R15) Branch to the location in (R15 plus 4), if previous

Example BC 7,X'010'(R15) Branch to the location in (R15 plus X'010') if previous

Example BCT R1,4(R15) Reduce value in R1 by 1 and, if it is not zero, branch

Example BCT R1,12(R0) The register is reduced by 1, but the branch will never occur

Example BCTR R1,R15 Reduce value in R1 by 1 and, if it is not zero, branch

Example BCTR R1,R0 Reduce R1 by 1 but do not branch. This instruction is often

A branch table is a literally a set of contiguous unconditional branch instructions which are of equal length (usually 4 bytes), that are used to very efficiently branch directly to one of this set, using an index. This index is often generated from some source input value that may itself be non-sequential as in the example below. This method is considerably faster than using either a binary search or sequential table lookup for example. Lookups involve compare instructions and a subsequent conditional branch. Only 5 instructions are used in the example (2 of which are unconditional branch) that perform the following:-

Example Consider an input variable that is a single byte character in the range A-Z , where specific values such as A,S,M,D decide the processing logic within the program. In this case A=Add,S=Subtract,M=Multiply and D=Divide.

In the following two examples, the time taken to perform validity and go to the appropriate label is fixed, irrespective of the number of different valid one byte input characters.

         SR    R15,R15             Clear index register to zero  (32 bits)
         IC    R15,INPUT           Insert input byte into low order bits of R15

TABLE1 EQU * ---Start of Branch table--- (each branch instruction

ERROR EQU *

SUBTRACT EQU *

Another very similar technique to the above branch table can be used. Instead of a table of branch instructions, a table of absolute or relative addresses (offsets) can be built by the Assembler. This requires just one extra instruction but increases the branch range to 64K without need for additional base register coverage and halves the size of the Table.

         SR    R15,R15             Clear index register to zero  (32 bits)
         IC    R15,INPUT           Insert input byte into low order bits of R15 

TABLE1 DS 0H ---Start of Offset table--- (each is 2 bytes long) DC Al2(ERROR-TABLE1) 00 = Invalid input value
DC AL2(ADD-TABLE1) 02 = Input value was "A" DC AL2(SUBTRACT-TABLE1) 04 = Input value was "S" DC AL2(MULTIPLY-TABLE1) 06 = Input value was "M" DC AL2(DIVIDE-TABLE1) 08 = Input value was "D"

ERROR EQU *

SUBTRACT EQU *

An alternative method of defining TABLE2 above, letting the Assembler automate the placement of the index bytes, is as follows (example shown for "A" and "D" only for brevity).

TABLE2 DC 256AL1(0) define 256 bytes of nulls ORG TABLE2+C**'A'** repeat these two lines for each

A table of absolute addresses can be built by the Assembler. This requires just one extra instruction but increases the branch range to 2 gigabytes (i.e. the whole address space for 31 bit processors).

         SR    R15,R15             Clear index register to zero  (32 bits)
         IC    R15,INPUT           Insert input byte into low order bits of R15 

TABLE1 DS 0H ---Start of Offset table--- (each is 2 bytes long) DC A(ERROR) 00 = Invalid input value
DC A(ADD) 04 = Input value was "A" DC A(SUBTRACT) 08 = Input value was "S" DC A(MULTIPLY) 12 = Input value was "M" DC A(DIVIDE) 16 = Input value was "D"

ERROR EQU * Label for Errors - could be in a

SUBTRACT EQU * Label for 'Subtract' - could be in different

INPUT DS C The input character is in this byte. TABLE2 DC Al1(00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00) X'00'-X'0F' DC Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) X'10'... DC Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) DC Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) DC Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) DC Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) DC Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) DC Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) DC Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) DC Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) DC Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) DC Al1(00,00,00,00,00,00,00,00,00,00)00,00,00,00,00,00) DC Al1(00,04,00,00,16,00,00,00,00,00,00)00,00,12,00,00)

The following branch instructions are available in the 370 and zSeries machines—To be added later--

360 Assembly Language
360 Family Introduction · Basic FAQ · 360 Family · 360 Architecture
360 Instruction Set 360 Instructions · Branch Instructions · Data Transfer Instructions · Control Flow Instructions · Arithmetic Instructions · Logic Instructions · Shift and Rotate Instructions · Privileged Instructions · Other Instructions
Syntaxes and Assemblers 360 Assemblers · Pseudo Instructions
Instruction Extensions Floating Point · High-Level Languages