COA Tutorial
Basic CO and Design
Computer Instructions
Digital Logic Circuits
Map Simplification
Combinational Circuits
Flip - Flops
Digital Components
Register Transfer
Micro-Operations
Memory Organization
COA_Misc
- Booth's Multiplication Algorithm
- Branch Instruction in Computer Organization
- Data Representation in Computer Organization
- ALU and Data Path in Computer Organization
- External memory in Computer Organization
- Structured Computer Organization
- Types of Register in Computer Organization
- Secondary Storage Devices in Computer Organization
- Types of Operands in Computer Organization
- Serial Communication in Computer organization
- Addressing Sequencing in Computer Organization
- Simplified Instructional Computer (SIC)
- Arithmetic Instructions in AVR microcontroller
- Conventional Computing VS Quantum Computing
- Instruction set used in Simplified Instructional Computer
- Branch Instruction in AVR microcontroller
- Conditional Branch instruction in AVR Microcontroller
- Data transfer instruction in AVR microcontroller
- Difference between Memory-based and Register-based addressing modes
- Difference between 1's complement Representation and 2's complement Representation
- CALL Instructions and Stack in AVR Microcontroller
- Difference between Call and Jump Instructions
- Overflow in Arithmetic Addition in Binary number System
- Horizontal Micro-programmed Vs. Vertical Micro-programmed Control Unit
- Hardwired Vs. Micro-programmed Control Unit
- Non-Restoring Division Algorithm for Unsigned Integer
- Restoring Division Algorithm for Unsigned Integer
- Debugging a Machine-level Program
- Dependencies and Data Hazard in pipeline in Computer Organization
- Execution, Stages and Throughput in Pipeline
- Types of Pipeline Delay and Stalling
- Timing Diagram of MOV Instruction
- Advantages and Disadvantages of Flash Memory
- Importance/Need of negative feedback in amplifiers
- Anti-Aliasing - Computer Graphics
- Bus Arbitration in Computer Organization
- Convert a number from Base 2 (Binary) to Base 6
- Cache Coherence
- EHCI
- Cache Memory and Virtual Memory
- Electrical Potential and Potential Difference
- RAM and Cache
- SIM and RIM instructions in 8085 processor
- Clusters in Computer Organization
- Data Types and Addressing Modes of 80386/80386DX Microprocessor
Branch Instruction in AVR microcontroller
To 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 microcontroller
When 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:
6M
781
OOPs Concepts in Java
- LDI A, 0
- LDI B, 1
- ADD A, B
- ADD A, B
- ADD A, B
- ADD A, B
- ADD A, B
- ADD A, B
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 instructions
Conditional 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:
Instruction | Explanation | Flag Status |
---|---|---|
BREQ | BREQ refers to the "Branch if Equal". It is a type of conditional relative branch. If zero flag (Z) is set, this instruction will test the Z and branches relative to PC (Program counter). Suppose there are two registers D (destination register) and S (source register), containing signed or unsigned binary numbers. The branch will have occurred if the binary number of register D and register S is equal to each other. | Branch if Z = 1 |
BRNE | BRNE refers to the "Branch if not Equal". If zero flag (Z) is cleared, this instruction will test the Z and branches relative to PC (Program counter). Suppose there are two registers D (destination register) and S (source register), containing signed or unsigned binary numbers. The branch will have occurred if the binary number of register D and register S is not equal to each other. | Branch if Z = 0 |
BRSH | BRSH refers to the "Branch if Same or Higher". If the carry flag (C) is cleared, this instruction will test the C and branches relative to PC (Program counter). Suppose there are two registers D (destination register) and S (source register), containing unsigned binary numbers. The branch will have occurred if the unsigned number of register D is greater than or equal to the unsigned number of register S. | Branch if C = 0 |
BRLO | BRSH refers to the "Branch if Lower". If carry flag (C) is set, this instruction will test the C and branches relative to PC (Program counter). Suppose there are two registers D (destination register) and S (source register), containing unsigned binary numbers. The branch will have occurred if the unsigned number of register D is smaller than the unsigned number of register S. | Branch if C = 1 |
BRLT | BRSH refers to the "Branch if Lower". If a signed flag (S) is set, this instruction will test the S and branches relative to PC (Program counter). Suppose two registers D (destination register) and S (source register), contain the signed binary numbers. The branch will have occurred if the signed number of register D is less than the signed number of register S. | Branch if S = 1 |
BRGE | BRGE refers to the "Branch if Greater or Equal". If the signed flag (S) is cleared, this instruction will test the S and branches relative to PC (Program counter). Suppose two registers D (destination register) and S (source register), contain the signed binary numbers. The branch will have occurred if the signed number of register D is greater than or equal to the signed number of register S. | Branch if S = 0 |
BRVS | BRVS refers to the "Branch if Overflow set". If the overflow flag (V) is set, this instruction will test the V and branches relative to PC (Program counter). | Branch if V = 1 |
BRVC | BRVC refers to the "Branch if Overflow cleared". If the overflow flag (V) is cleared, this instruction will test the V and branches relative to PC (Program counter). | Branch if V = 0 |
Loop using BRNE
The 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.
- LDI A, 20; counter register
- LDI B, 0
- LDI C, 5
- ADD B, C
- DEC A; decrement the counter
- BRNE LOOP; repeat until counter = 0
- OUT PORTC, B
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 Instruction
The 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).