Branch Instructions in AVR Microcontroller - GeeksforGeeks

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
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.
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.
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.
Computer Organization and Architecture
microprocessor
Computer Organization & Architecture
microprocessor
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Direct Access Media (DMA) Controller in Computer Architecture
Computer Organization and Architecture | Pipelining | Set 2 (Dependencies and Data Hazard)
Architecture of 8085 microprocessor
Pin diagram of 8086 microprocessor
I2C Communication Protocol
Memory mapped I/O and Isolated I/O
Difference between Hardwired and Micro-programmed Control Unit | Set 2
Computer Architecture | Flynn's taxonomy
Computer Organization | Different Instruction Cycles
Introduction of Assembler