Branch Instructions in AVR Microcontroller
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.
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.
|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.
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.
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.