Related Articles

# Branch Instructions in AVR Microcontroller

• Last Updated : 14 Dec, 2020

In this article, we will be discussing looping in AVR and branch instructions, both Conditional and Unconditional.

Looping in AVR :
A repeated operation or a set of instructions is known as a loop in programming. It is one of the most fundamental techniques which comes in very handy in writing code. One way to execute a loop in AVR is to write a set of instructions repeatedly.

Example –
For example:

LDI R20, 0
LDI R21, 1

As we can see, this way of executing a code is very inefficient and it takes up a lot of code space. Therefore, we use branch instructions (Conditional and Unconditional) to make the loop more simple and space-efficient.

Conditional Branch Instructions :
We have discussed the Conditional Branch Instructions in detail in this article. We will discuss it in brief here.

The following table shows different conditional branch instructions along with their explanation.

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 = 0
BRVSBranch if Overflow flag set Branch if V = 1
BRVCBranch if Overflow flag clear Branch if V = 0

Loop using BRNE :
The BRNE (branch if not equal) instruction uses the Z flag in the status register.

Example –
Write a program to add 5 to R20 20 times and send the sum to PORTC using the BRNE instruction.

LDI R16, 20; counter register
LDI R20, 0
LDI R21, 5
DEC R16;          decrement the counter
BRNE LOOP;        repeat until counter = 0
OUT PORTC, R20

All conditional branches are short jumps: This means that the address of the target must be within 64 bytes of the program counter.

Unconditional Branch Instructions :
The unconditional branch is a jump in which control is transferred unconditionally to the target address. In AVR, there are 3 unconditional branch instructions: JMP, RJMP, and IJMP. Using which instruction depends upon the target address.

1. JMP (long jump) –
JMP is an unconditional jump that can go to any memory location in the 4M (word) address space of the AVR. It is a 4-byte instruction in which 10 bits are used for the opcode, and the other 22 bits represent the 22-bit address of the target location.
2. RJMP (relative jump) –
In this 2-byte instruction, the first 4 bits are used for the opcode and the rest of the bits are used for the relative address of the target location. The relative address range of 000-\$FFF is divided into forward and backward jumps, that is within -2048 to +2047 of memory relative to the address of the current program counter.
3. IJMP (indirect jump) –
It is a 2-byte instruction. When it executes, the program counter is loaded with the contents of the Z register, so it jumps to the address provided by the Z register. IJMP can jump within the lowest 64K words of the program memory.

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