Related Articles

# Conditional Branch Instructions in AVR Microcontroller

• Last Updated : 24 Sep, 2020

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.

1. 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.
2. 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.
3. 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.
4. 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.
5. 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.
6. S – Sign Flag :
• This flag is the result of Exclusive-ORing of N and V flags.
7. 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 :

InstructionExplanationFlag Status
BREQBranch if equalBranch if Z = 1
BRNEBranch if not equalBranch if Z = 0
BRSHBranch if same or higherBranch if C = 0
BRLOBranch if lowerBranch if C = 1
BRLTBranch if less than (signed)Branch if S = 1
BRGEBranch if greater than or equal (signed)Branch if S = 0BRVSBranch if Overflow flag setBranch if V = 1
BRVCBranch if Overflow flag clearBranch if V = 0

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up