Usually the instructions in a program are executed in order, one after another. Often, though, we want to make decisions, as in:
If (A is equal to B) Then do something Else do dome other thing End-If
How do we do this in assembly language? We make use of the Condition Code or CC, a 2-bit "special register" in the hardware. As it is 2 bits long, it can contain only 4 possible values: 0, 1, 2 and 3. Many different instructions affect the value of the CC. After one of these instructions, we use a "branch" instruction to cause the flow of execution to move to some location in the code as needed.
Which instructions set the CC?
The yellow card contains a list of which instructions set the CC and how. In general:
Branch Instructions
A branch instruction can be understood as:
If the CC has one of the following values then go to the indicated address else continue with the next instruction after the branch.
The two simplest branch instructions are:
BCR Branch on Condition Register
and
BC Branch on Condition
In either case, we use a mask. A mask is a binary 4-bit value expressed often in the form B'bbbb' with 4 bits b. For instance:
B'1101' B'0110' B'1111'
The idea of a mask is that we are providing a list of values of the CC. The 4 bits in the mask correspond, left to right, with the 4 values 0 to 3 of the CC. The bit is 1 if we want to include that value of the CC. Thus, in B'1101', we are indicating possible CC values of 0, 1 and 3.
By the way, some instructions other than branches also use masks, and in some cases these are 8-bit masks instead of 4-bit masks.
The formats of these instructions are:
BCR mask,R
and
BC mask,D(X,B)
When the branch instruction is executed, we do the following:
Example
CR 7,8 BCR B'1011',5
Here the CR will set the CC:
If the values in registers 7 and 8 are equal, we will have CC = 0. As the leftmost bit in the mask is 1, we will jump to the address in register 5.
If the value in register 7 is less than the value in register 8, we will have CC = 1. As the corresponding bit in the mask is 0, we will do nothing and continue with the instruction next after the BCR.
If the value in register 7 is greater than the value in register 8, we will have CC = 2. As the corresponding bit in the mask is 1, we will jump to the address in register 5. Notice that in this example we never have CC = 3.
How are these encoded?
If you check the yellow card, you will find that BCR is a type RR instruction and BC is a type RX instruction. In each case, we insert the value of the mask into the machine instruction in place of the first register.
For instance:
BCR B'1010',7
is encoded as
07 A7
Special Cases
Suppose the mask is B'1111', as in:
BCR B'1111',14
As the CC can only be 0 to 3 and all four bits in the mask are 1, we will certainly jump to the address in register 14. This is an unconditional branch. (Modern high-level languages usually do not allow these any more.)
Suppose the mask is B'0000' as in:
BCR B'0000',0
In this case, all the bits in the mask are 0 and we will certainly not jump anywhere. This is a "do nothing" or "no operation" instruction. It is actually irrelevant which register we provide in it.
It is not actually essential to write the mask in binary. We could write it in decimal or in hexadecimal:
BC 8,ENDLOOP
or
BC X'A',THERE
Most of us probably find it easier to understand the meaning of the
mask in binary, but it is more characters to type. To make life
easier, we have a set of abbreviations called
We actually use one of these to end our programs:
BR 14
means
BCR B'1111',14
Other Branch Instructions
There are several other branch instructions which are much more specialized. We will sooner or later run into some of these: