Branch Instruction in AVR microcontrollerTo understand the branch instruction, we should know about looping in an AVR microcontroller. After that, we will learn about the branch instructions with two types: Conditional branch instruction and unconditional branch instruction. Looping in AVR microcontrollerWhen we write the code, the most fundamental technique which comes to our mind is a loop. The loop can be described as a set of instructions or a repeated operation in terms of programming. There is only one way to execute the loop if we repeatedly write a set of instructions in AVR (Alf and Vegard's RISC processor). For example: With the help of above example, we can see that the above code is taking a lot of space and is also very inefficient. Therefore, we can make the above code of loop simpler and space-efficient with the help of branch instructions. Branch instruction:In the program loops and conditionals, the control flow is implemented with the help of branch instruction. If the given condition is satisfied, only then a particular sequence of instruction is executed. When we execute a branch instruction, the execution is switched to a different instruction. The branch instructions are of two types: Conditional branch instruction and unconditional branch instruction. Conditional Branch instructionsConditional branch instruction can be described as a set of instructions. It is used to control the program flow with the help of providing a branch out of a loop. The following table describes the various types of conditional branch instructions and their explanation:
Loop using BRNEThe BRNE refers to the branch if not equal. If Z is cleared, it will test the zero flag (Z) and branches relatively to PC. It basically uses the Z flag in a status register. For example: In this example, we will write a code to add 5 to C, and this process will be repeated 20 times. Lastly, with the help of BRNE instruction, we will send the sum to PORTC. All of the conditional branches basically use short jumps. This means that the target address must be within 64 bytes of the program counter. Unconditional Branch InstructionThe unconditional branching can be described as the jump where the control is transferred to the target address unconditionally. The AVR basically has three types of unconditional branch instructions, i.e., JMP, IJMP, and RJMP. The instructions of an unconditional branch are described as follows: JMP: JMP instructions are also known as long jump. It is a type of unconditional jump that can go to any memory location, but that location must be within the 4M(word) address space of AVR. The JMP instruction is the 4-byte instruction. With the help of 10 bits, the opcode is represented, and with the help of rest of the bits means 22 bits, the 22-bit address of the target location is represented. RJMP: RJMP instructions are also known as the relative jump. RJMP instructions are the 2-byte instruction. With the help of first four bits, the opcode is represented, and with the help of rest of bits, the relative address of a target location is represented. The range of relative address is 000-$FFF, which is divided into two jumps: backward jumps and forward jumps. This jump must be within -2048 and +2047 of memory, which is relative to the current program counter address. IJMP: IJMP instructions are also known as the indirect jump. The IJMP instructions are the 2-byte instruction. When we execute the IJMP instruction, the program counter will be loaded with the help of Z register's content. That means the Z register provides an address, and IJMP jumps to this address. This instruction is able to jump within the lowest 64k words (128KB) of the PC (program counter). |