Branching and Condition Codes

Home Articles Branching and Condition Codes

The article is the sequel to memory operations and instructions. In branching, when we have to add a list of n numbers, then the addresses of the memory locations are to be given names in the sequence. These contain the n numbers which are to add. The instruction which is used for adding them is Add instruction, and then it takes the result of the contents of the register R0. After all the numbers have been added, then the memory location SUM is used for placing the result.

Rather than using a large and separate add instruction for every two operands, placing a single add instruction in a program loop is effective. Yes, this is what the functions and the for loop teaches us in the C language. The straight-line which contains the sequence of instructions which can be executed as many times as the need of the program is the loop. The location LOOP is the step at which the program gets started. Branch>0 is the end instruction. Through every process of the loop, there is a determination of the address of the next entry list. After this, the entry is fetched and then added to R0. There are various ways in which the address of an operand can be specified.

A new register R1 is used for the determining the number of times for which the loop gets executed. It acts as a counter. Location N is the space where the number of entries in the list of n numbers. Register R1 is used for locating the contents of the location N which are at the beginning of the programs. The instruction reduces the contents of R1 every time the loop uses. The example of all these things within the loop is the instruction


There is a repetition of the loop execution until the result of the decrement operations which comes is greater than zero.


The instructions which are used for loading the new value into the program counter are called the branch instructions. There is another term which is associated with the branch instructions; it is the branch target. It comes into picture when the processor fetches the instructions and then executes them at the new address. This is known as the branch target. It happens rather than the instructions to be followed in the sequential address order. Every time the specified condition is specified, then the conditional branch instruction cause a branch. In the otherwise condition, there is an increment in the normal ways of the PC. In addition to this, there is fetching and the execution of the next instruction in the sequential address order.

Branch>0 LOOP is a conditional branch. It is used for causing a branch location which is LOOP. It happens only when the result of the immediate preceding of the instruction is greater than zero when the value in the register R 1 is decremented. As long as there are a number of entries in which are still to be added to the list, we can repeat the loop. The instruction, Decrement produces a zero value when there is an end of the nth pass in the loop. The result is that no branching occurs in that situation. In this place, there is fetching and execution of the Move instruction. Hence, we get the resultant into the memory location SUM from the register R0.

The applications which are related to the loop control are less than the capability of the set of alternative ways of continuing the computation. These are the capabilities which are found in the instruction set of all the computers. Most of the nontrivial tasks consider this as the fundamental approach to the programming task.


The element of the computer system which keeps track of all the information and checks the results of the various operations is the processor. It also keeps an eye on the subsequent conditional branch instructions in addition to the other tasks such as recording of the required information in the individual bits. This is called as condition code flags. These are the flags which are used for grouping together the special purpose registers, and such registers are called the status register or condition code register.

The value which is set to the individual condition code flags is 1 and 0 is assigned when outcome operations are performed. There are for commonly used flags. These are-

1. N (negative)- It sets the value to 1 if the result of the process is negative. If it is positive, then it is set to 0.
2. Z (zero)- IF the result of the process is 0; then the flag is set to 1. In the otherwise situation, it is cleared to 0.
3. V (overflow)- If there is overflow, the flag is set to 1. In the otherwise situation, it is cleared to 0.
4. C (carry)- if there is any carry out in the process, then it is set to 1. If the result does not operate any carryout, then the results are set to 0.

The flags which indicate the sign of the result of the arithmetic and the logic operation, these are N and Z flags. The instructions that are used for transferring the data using the instructions such as Move, Load, or Store, also affect these two flags. Causing the branch by the sign and the value of the operand has been made possible by the flags N and Z which are to be used by the conditional branch instruction. Examining the values in the register or the memory is done with the special test instructions. Some computers provide this and also help in setting or clearing the N and Z flags.

V flag depicts that even the system has overflow is there or not. It happens when the result and the outcomes of the arithmetic operation are out of the range of the representable values. The number of bits which are available for the operands is used for the representation of the flags. Allowing the programmer to test the overflow is done by the V flag. In addition to this, the branches which are used for correcting the problems are provided in the appropriate routine. The instruction used here is the BranchIfOverflow.

C flag is used for setting its value to 1 when there is a carry on the position of the most significant bit. It is used in terms of the arithmetic operation. The task of making the performance of the arithmetic operations is with C flag. The operands which are used here are those who are not longer than the processor’s word length. These are the special and multiple-precision arithmetic operations.

If you are looking for an example in the field of the branch instruction, then read the instruction which we have discussed previously. It is Branch>0. These are the instructions which test the conditional flags. There is a creation of the branch if the value which has been tested is neither equal to zero nor negative. Yes, if neither of the values of Z nor N is 1, then it is applicable. Enabling the variety of the conditions which are to be tested are provided by the many of the conditional branch instructions. As the logic expressions involve the condition code flags, similarly, these conditions are given the logic expressions.

There is an automatic effect on the condition code flags by the instructions which perform logic operations and arithmetic operations. It happens on many computers, but this is not the case in all the systems. There are two versions of an Add instruction.

1. Version 1- It does not affect the flags. For example, Add.
2. Version 2- AddSetCC is the example which affects the flags.

When you are preparing the programs for the execution via the pipeline, the programmer feels relaxed and flexible. It is the same with the compilers as well.