Conditional branch instructions are used mainly in two situations. Most frequently they are employed to check the result of instruction for a specified condition, such as whether the result equals 0, if it is negative, and so on.
If the specified condition is met, control is transferred to a given location in the program. The other usual situation is to compare two operands, asking whether they are equal, for instance, and then to approach a given location if the specified condition is met.
There are two basic approaches to how instruction set architectures (ISA) check the results of operations such as the result state concept and the direct check concept.
The result state concept supposes that a result state is declared in the ISA concerned. It is represented in the form of a condition code or flags. The result state holds relevant information about the results of the operations, such as whether the result is = 0, < 0, and so on. In this concept, the result of operations is automatically checked during instructions execution for a specific condition like = 0, < 0, and so on.
For instance, if the result of an addition operation is subsequently used in a division as a divisor, the result of the arithmetic operation can be checked as follows −
add r1, r2, r3; // r1←(r2)+(r3)
beq zero; // test for result equals zero and if yes branch to the location ‘zero’
div r5, r4, r1; // r5←(r4)/(r1) …….
zero: // processing the case if divisor equals zero
In addition, the architecture also has to provide some specific instructions to check for the relevant condition of any operand value which has been generated and to set the result state accordingly.
The direct check concept is the other basic alternative for checking the results of operations. In this case, no result state is declared. Instead, if required, the results of the operations are checked directly for specified conditions by using dedicated instructions. If the specified conditions are met, a conditional branch is initiated.
When direct checking is implemented by two separate instructions, first the result value is checked by an appropriate compare instruction. This instruction writes the outcome of the check into a chosen register. A conditional branch instruction is used to test the deposited test result and branch to a given area if the specified particular is met, as the following example shows −
add r1, r2, r3; cmpeq r7, r1; // r7←true,if (r1)=0,else NOP bt r7, zero; // branch to ‘zero’: if (r7) = true, else NOP div r5, r4, r1; …….. zero:
In this example, the cmpeq (compare for equal) instruction tests the content of register r1 for zero, and if (r1) = 0, sets the Boolean value ‘true’ into register r7. The subsequent bt (branch if true) instruction interrogates the deposited Boolean value in register r7 and initiates a branch to the label ‘zero’ if the stored value is ‘true’.