Conditional Branch Instructions in AVR Microcontroller
Conditional branch instructions are the set of instructions that is used to branch out of a loop. We will discuss these instructions for the AVR micro-controller.
To understand these instructions, first, we need to know about the registers in the AVR micro-controller.
- Status Register (SReg) :
- It is the flag register in the AVR micro-controller.
- It is a 8 – bit register. Only 6 of these 8 bits are called conditional flags. They are C, Z, N, V, S, H.
- The value of these bits indicates some conditions that result after the execution of an instruction.
- C – Carry Flag :
- This flag is set whenever there is a carryout from the D7 bit.
- This flag bit is affected after an 8-bit addition or subtraction.
- Z – Zero Flag :
- The zero flag reflects the result of an arithmetic or logic operation.
- If the result is zero, then Z = 1. Therefore, Z = 0 if the result is not zero.
- N – Negative Flag :
- Binary representation of signed numbers uses D7 as the sign bit.
- The negative flag reflects the result of an arithmetic operation. If the D7 bit of the result is zero.
- Then N = 0 and the result is positive. If the D7 bit is one, then N = 1 and the result is negative.
- V – Overflow Flag :
- This flag is set whenever the result of a signed number operation is too large.
- Causing the high-order bit to overflow into the sign bit.
- S – Sign Flag :
- This flag is the result of Exclusive-ORing of N and V flags.
- H – Half Carry Flag :
- If there is a carry from D3 to D4 during an ADD or SUB operation, this bit is set; otherwise, it is cleared.
- This flag bit is used by instructions that perform BCD (binary coded decimal) arithmetic.
The following table shows a few branch loop instructions :
Instruction | Explanation | Flag Status |
---|---|---|
BREQ | Branch if equal | Branch if Z = 1 |
BRNE | Branch if not equal | Branch if Z = 0 |
BRSH | Branch if same or higher | Branch if C = 0 |
BRLO | Branch if lower | Branch if C = 1 |
BRLT | Branch if less than (signed) | Branch if S = 1 |
BRGE | Branch if greater than or equal (signed) | Branch if S = 0 |
BRVS | Branch if Overflow flag set | Branch if V = 1 |
BRVC | Branch if Overflow flag clear | Branch if V = 0 |