Branch Instructions in AVR Microcontroller
Last Updated :
23 Jul, 2025
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
ADD R20, R21
ADD R20, R21
ADD R20, R21
ADD R20, R21
ADD R20, R21
ADD R20, R21
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.
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 |
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
LOOP: ADD R20, R21
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.
- 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.
- 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.
- 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.
Similar Reads
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
3 min read
Arithmetic instructions in AVR microcontroller Arithmetic Instructions are the instructions which perform basic arithmetic operations such as addition, subtraction, multiplication, etc. AVR micro-controller has 2 operands which are actually registers that hold the data. The left register is the source register while the right one is the source r
2 min read
Logical Instructions in AVR Microcontroller Logical Instructions are the instructions which perform basic arithmetic operations such as AND, OR, XOR, etc. In AVR micro-controller, the destination operand is always a register. The following table shows the logical instructions : Instruction Operand Explanation Example AND D, S D = D AND S Perf
2 min read
Data Transfer instructions in AVR microcontroller Data transfer instructions are the instructions that are used to transfer data into micro-controller. These instructions can be used to transfer data from: Register to Register : In register-to-register transfer, data is transfer from one register to another register. Consider an example where you h
3 min read
CALL Instructions and Stack in AVR Microcontroller Introduction : The AVR microcontroller is a type of microcontroller developed by Atmel Corporation. It is widely used in various embedded systems due to its low power consumption and high performance. In this context, I will discuss the introduction to CALL instructions and the stack in AVR microcon
7 min read
Branching instructions in 8085 microprocessor Branching instructions refer to the act of switching execution to a different instruction sequence as a result of executing a branch instruction. The three types of branching instructions are: Jump (unconditional and conditional) Call (unconditional and conditional) Return (unconditional and conditi
5 min read