Control Structures
Branching
(Conditional branching and jumps)
Definition Conditional branching is a fundamental concept in assembly language programming that allows the flow of execution to be changed, based on the evaluation of a condition.
In the context of the 8086 microprocessor, this involves making decisions to either continue executing the next instruction in sequence or to jump to a different part of the code based on the status of specific flags.
Purpose Enables implementation of control structures such as if-then-else conditions, loops, and switches which are essential for creating logical pathways in code.
The Intel 8086 microprocessor evaluates conditions using the flags in the flag register, particularly the zero flag (ZF), carry flag (CF), sign flag (SF), overflow flag (OF), and parity flag (PF). These flags are set or cleared by arithmetic, logical, and comparison operations.
JMP (Jump)
The JMP instruction is used to transfer control to another location in the program unconditionally. It simply transfers execution to the specified target address.
For example:
JE (Jump If Equal)
The JE instruction is used to jump to a target address if the Zero Flag (ZF) is set (indicating that the result of a previous operation was zero or equal). It is often used in conditional branching for equality checks.
For example:
JNE (Jump If Not Equal)
The JNE instruction is used to jump to a target address if the Zero Flag (ZF) is clear (indicating that the result of a previous operation was not zero or not equal). It is used for inequality checks.
For example:
JG (Jump If Greater)
The JG instruction is used to jump to a target address if the Zero Flag (ZF) is clear, and the Sign Flag (SF) and Overflow Flag (OF) are the same (indicating a signed greater-than comparison).
For example:
JGE (Jump If Greater or Equal)
The JGE instruction is used to jump to a target address if the Zero Flag (ZF) is set, or the Sign Flag (SF) and Overflow Flag (OF) are the same (indicating a signed greaterthan-or-equal comparison).
For example:
JL (Jump If Less)
The JL instruction is used to jump to a target address if the Zero Flag (ZF) is clear, and the Sign Flag (SF) and Overflow Flag (OF) are different (indicating a signed less-than comparison).
For example:
JLE (Jump If Less or Equal)
The JLE instruction is used to jump to a target address if the Zero Flag (ZF) is set, or the Sign Flag (SF) and Overflow Flag (OF) are different (indicating a signed less-thanor-equal comparison).
For example:
Other Branching Instructions
▪ JE/JZ (Jump if Equal/Zero): Jump if the Zero Flag (ZF) is set (indicating that the operands are equal, or the result of the last operation was zero).
▪ JNE/JNZ (Jump if Not Equal/Not Zero): Jump if the Zero Flag (ZF) is clear (indicating that the operands are not equal, or the result of the last operation was not zero).
▪ JG (Jump if Greater): Jump if the Zero Flag (ZF) is clear, and the Sign Flag (SF) and Overflow Flag (OF) are the same (indicating a signed greater-than comparison).
▪ JGE/JNL (Jump if Greater or Equal/Not Less): Jump if the Zero Flag (ZF) is set, or the Sign Flag (SF) and Overflow Flag (OF) are the same (indicating a signed greater-than-or-equal comparison).
▪ JL/JNGE (Jump if Less/Not Greater or Equal): Jump if the Zero Flag (ZF) is clear, and the Sign Flag (SF) and Overflow Flag (OF) are different (indicating a signed less-than comparison).
▪ JLE/JNG (Jump if Less or Equal/Not Greater): Jump if the Zero Flag (ZF) is set, or the Sign Flag (SF) and Overflow Flag (OF) are different (indicating a signed less-than-or-equal comparison).
▪ JC (Jump if Carry): Jump if the Carry Flag (CF) is set (indicating a carry or borrow occurred in an arithmetic operation).
▪ JNC/JNB (Jump if No Carry/Not Below): Jump if the Carry Flag (CF) is clear (indicating no carry or borrow occurred).
▪ JO (Jump if Overflow): Jump if the Overflow Flag (OF) is set (indicating signed overflow occurred in an arithmetic operation).
▪ JNO (Jump if No Overflow): Jump if the Overflow Flag (OF) is clear (indicating no signed overflow occurred).
▪ JS (Jump if Sign): Jump if the Sign Flag (SF) is set (indicating the result is negative).
▪ JNS (Jump if No Sign): Jump if the Sign Flag (SF) is clear (indicating the result is non-negative or positive).
▪ JP/JPE (Jump if Parity/Parity Even): Jump if the Parity Flag (PF) is set (indicating an even number of set bits in the result).
▪ JNP/JPO (Jump if No Parity/Parity Odd): Jump if the Parity Flag (PF) is clear (indicating an odd number of set bits in the result).
▪ JB/JNAE (Jump if Below/Not Above or Equal): Jump if the Carry Flag (CF) is set (indicating an unsigned less-than comparison).
▪ JAE/JNB (Jump if Above or Equal/Not Below): Jump if the Carry Flag (CF) is clear (indicating an unsigned greater-than-or-equal comparison).
▪ JBE/JNA (Jump if Below or Equal/Not Above): Jump if the Carry Flag (CF) is set or the Zero Flag (ZF) is set (indicating an unsigned less-than-or-equal comparison).
▪ JA/JNBE (Jump if Above/Not Below or Equal): Jump if the Carry Flag (CF) is clear and the Zero Flag (ZF) is clear (indicating an unsigned greater-than comparison).
Looping
For Loop
A for loop is typically implemented using a combination of instructions like MOV, CMP, JNZ, and loop control variables. The loop control variable is typically decremented or incremented within the loop body until a certain condition is met.
For loop repeats a block of code a fixed number of times.
In Intel 8086 assembly language, a for loop can be implemented using the CX register as a loop counter.
Here's an example of a simple for loop that counts from 1 to 5:
The For Loop Block Diagram
While Loop
A while loop can be implemented using a combination of instructions such as CMP, JZ, and loop control variables. The loop continues as long as a specified condition is true.
A while loop is used to repeat a specific block of code an unknown number of times, until a condition is met.
The While-Loop block diagram
Do-While Loop
A do-while loop can be implemented using a combination of instructions like JMP, CMP, and JZ. The loop body is executed at least once, and then the condition is checked to determine if the loop should continue.
Do-While Loop block diagram
Infinite Loop
An infinite loop is created using an unconditional jump instruction (e.g., JMP), causing the program to continuously execute a block of code until manually interrupted or terminated.
Nested Loops
Nested loops can be implemented by using multiple loop control variables and appropriately placed labels and jump instructions. This allows creation of more complex looping structures.
Looping with Counters
Looping can also be controlled using loop counters. The CX register is commonly used as a loop counter in Intel 8086 assembly language. You can decrement or increment the counter in each iteration and use conditional jumps (e.g., JNZ or JZ) to control the loop.
Break Statement
A break statement can be implemented using conditional jumps and labels to exit a loop prematurely based on a certain condition.
Continue Statement
A continue statement can be implemented using conditional jumps and labels to skip the current iteration of a loop and proceed to the next iteration based on a certain condition.
Looping with Arrays
Use loops to iterate over arrays or data structures, performing operations on each element or item in the array.
Looping with Strings
Loops can be used for string manipulation, iterating over characters in a string, and performing string-related operations.
Last updated