# 360 Assembly/360 Instructions/BC

BC - Branch on Condition - RX type instruction - Opcode 47 / Decimal 71

## Availability

The BC instruction is available on all models of the 360, 370 and z/System.

## Format

    BC 2,LABEL
    BH LABEL - Special opcode, same as BC 2,LABEL
    BC 1,L1
    BO L1 - Same as BC 1,L1
    BC 15,106(0,10)
    B 106(0,10) - B opcode is unconditional branch and is same as BC 15
    BC 3,256(7,6)
The specific syntax is
    BC mask,offset(index register,base register)
Where the offset, index register and base register values will be determined automatically by the assembler if the USING pseudo-instruction has been used, and the target address label used is within 4096 bytes of the value of some base register.
RX Instruction (4 bytes) :
| Byte 1 | Byte 2 | Byte 2 | Bytes 3 and 4 | Bytes 3 and 4 | Bytes 3 and 4 |
| Byte 1 | mask | Target Address | Target Address | Target Address | Target Address |
| (8 bits) Opcode 47 | (4 bits) 0..F | (4 bits) index register 0..F | (4 bits) base register 0..F | (12 bits) offset 0..FFF | (12 bits) offset 0..FFF |
* The first argument is a mask which the condition code is compared against.
* The second argument is the location to transfer to if the mask matches the currently set condition code. The offset value is added to the values in the base and index registers to form the target address. Most instruction usage tends to use one base register with an index register of zero, but if two non-zero registers are used, the target address will be the same no matter which register is the base register and which is the index register.
* The mask, base_register and index_register values are 0 to 15. The offset value is 0 to 4095.
* If the mask or base_register is zero, the instruction is a no-operation, and does not branch.
* If the mask is 15 the branch is unconditional (unless the base register is 0, in which case it does not branch).
* The value in the index_register is not used if the index_register specified is 0.

## Mask

After executing an instruction which changes the condition code, the computer sets the CC flag bits in the "Condition Code" field in the PSW. This instruction can then branch if the selected condition code matches the mask in this instruction. For example suppose we are using arithmetic add register "AR". Then in following conditions the value of "Condition Code" will be as follows:
| Condition | Symbol | Condition code in PSW |
| Result is zero | Z | 0 |
| Result is negative | N | 1 |
| Result is positive | P | 2 |
| Result overflows | O | 3 |
Then consider instruction "BNZ" (Branch Not Zero). It's mask is 7. What does that mean? This instruction branches if the result is not zero, which means it branches if the value of condition code is not zero! So it is 1,2 or 3. Consider the following table
| Z | N | P | O |
| 0 | 1 | 1 | 1 |
The example above gives us mask (0111) = 7. For each of following opcodes you can construct the table ZNPO and find the corresponding mask.

## Other Opcodes

The assembler provides the mask as part of several optional opcodes. These simply require the target address. These opcodes are:
| Opcode | Mask | Usage | Purpose | Equivalent to | Usage |
| NOP | 0 | NOP LABEL | No Operation | BC 0,LABEL | Any place where a no-operation/filler is wanted |
| BO | 1 | BO LABEL | Branch on Overflow/Ones | BC 1,LABEL | After an arithmetic operation or arithmetic comparison, if arithmetic overflow or result is all ones occurred |
| BH | 2 | BH LABEL | Branch (a High) | BC 2,LABEL | After any comparison, branch if first value in comparison is higher than second value (A > B) |
| BP | 2 | BP LABEL | Branch on Plus | BC 2,LABEL | After an arithmetic operation or arithmetic comparison, branch if result is positive |
| BL | 4 | BL LABEL | Branch (a Low) | BC 4,LABEL | After any comparison, branch if first value is lower than second (A < B) |
| BM | 4 | BM LABEL | Branch on Minus/Mixed | BC 4,LABEL | After an arithmetic operation or arithmetic comparison, branch if the result is negative or is ones and zeroes |
| BNE | 7 | BNE LABEL | Branch Not Equal | BC 7,LABEL | After any comparison, branch if first value is not equal to the second value (A <> B or A ~= B or A != B) |
| BNZ | 7 | BNZ LABEL | Branch Not Zero | BC 7,LABEL | After an arithmetic operation or arithmetic comparison, branch if result is not zero |
| BE | 8 | BE LABEL | Branch (a Equal b) | BC 8,LABEL | After any comparison, branch if first value equals the second value (A = B or A == B) |
| BZ | 8 | BZ LABEL | Branch on Zero | BC 8,LABEL | After an arithmetic operation or arithmetic comparison, branch if result is zero |
| BNL | 11 | BNL LABEL | Branch (a Not Low) | BC 11,LABEL | After any comparison, branch if first value is not lower than the second value (A >= B) |
| BNM | 11 | BNM LABEL | Branch Not Minus | BC 11,LABEL | After an arithmetic operation or arithmetic comparison, branch if result is zero |
| BNH | 13 | BNH LABEL | Branch Not High | BC 13,LABEL | After any comparison, branch if first value is not higher than the second value {A<=B) |
| BNP | 13 | BNP LABEL | Branch Not Plus | BC 13,LABEL | After an arithmetic operation or arithmetic comparison, branch if result is not positive |
| BNO | 14 | BNO LABEL | Branch Not Ones | BC 14,LABEL | After an arithmetic operation or arithmetic comparison, branch if result is not all ones |
| B | 15 | B LABEL | Branch (unconditional) | BC 15,LABEL | Branch in all cases (unless index register is 0; then treat as no-op) Equivalent to GOTO in high-level languages |

## Optional formats

For the example machine code shown on the left, the address LABEL is presumed to be at offset 106 (06A hex) from register 10, and the address X1 is presumed to be at offset 256 (0100 hex) from the sum of base register 6 and index register 7.

### Used after Test Under Mask instructions
## Operation

Upon performing an arithmetic operation or a comparison, certain bits in the Program Status Word called the Condition Code are set or cleared. In the case of comparison of two fields, the left value is considered the "A" value, and the right value is considered the "B" value, and the result of the comparison of A to B tests how A compares to B, either low, high, equal or not-equal.
In the case of an arithmetic operation, the result being plus, minus, zero or having had an overflow is tested.
In the case of the test under mask instructions, the result of the test being all ones, all zeroes, or mixed ones and zeroes.
The Branch on Condition instruction is used following such a test to compare the condition code bits to the mask value. If bits set in the mask match bits which are set in the condition code (or all of the bits in the mask are set), and the base register of the target address is not 0, the target address is placed into the PSW as the new address of the current instruction and the branch is taken. Otherwise execution continues with the next instruction following the branch on condition instruction.
The target address is constructed by adding the contents of the base and index registers to the offset address. If the index register is zero its value is not used.

## Purpose of Instruction

The 'Branch on Condition instruction is the general purpose branch within a program. It has three variations: no branch or No Operation, conditional branch depending upon a test, or unconditional branch.

### No Operation

The 'no branch' - BC 0 - or 'NOP' is typically used to create a label which is not tied to an existing instruction. It may be used by a macro for alignment to force an instruction or data onto a specific boundary but without causing a program exception if the instruction is branched to. It can also be used to provide 'slack' space to allow later patching of the binary without having to reassemble the program. A NOP will also occur, regardless of the mask value, if the base register of the branch is also zero.

### Conditional Branch

The bits in the mask are compared to the bits in the condition code. If the bits in the mask match the bits in the condition code, (and the base register of the target address is not zero) the branch is taken.

### Unconditional Branch

A branch to another location (the equivalent of a GOTO in high-level languages) is performed by setting all the bits in the mask, e.g. BC 15, or using the B instruction. As long as the base register of the target address is not zero, the branch will always be taken.

## Typical Usage

The branch on condition and optional formats are typically used after performing a comparison or arithmetic operation. In the following code, a question is asked, the response is compared to yes or no, and the question is re-issued if not either. The 'CALL' macro is used to create a standard subroutine linkage.

## Exceptions and Faults

* The target address contained within the branch register must not be odd, or an operation exception occurs
* The target address contained within the branch register must be within the range of valid memory or an operation exception occurs.
* The storage key for the target address must be the same as the current process (or this process must have a key of 0) or a memory protect violate exception occurs.

## Alternative branch instructions

* The BALR instruction is used to branch to the address in a register and save the current address as a return address, similar to a procedure or function call in a high-level language
* The BCR instruction is used in the same manner as the BC instruction, but is used to branch to the address in the specified second argument register.
* The BCT instruction is used is used to subtract 1 from a register then branch to the specified address if the result is not zero.
* The BCTR instruction is used is used to subtract 1 from a register then branch to the address in the register specified in the second argument if the result is not zero.
* The BRC instruction is used to branch to the address relative to the current program counter, contained in an immediate (16 bit) value.
* The BRCL instruction is used to branch to the address relative to the current program counter, contained in an immediate (32 bit) value.
| Previous Instruction BASSM | 360 Assembly Instructions | Next Instruction BCR |
| Previous Opcode 46 | 360 Assembly Instructions | Next Opcode 48 |