PRODIP GHOSH
Flow Control
Instructions
Flow Control Instructions
 Program using the different instruction :-
 Conditional and Unconditional Jump Instructions
Compare Instruction
Loop Instruction
Conditional Jump
Syntax:
Jxxx destination_label
Where xxx represents the condition
If condition is true, the next instruction to be executed is the one at
destination_label.
If condition is false, the instruction immediately following the jump is
done next.
Conditional Jump
The destination_label must precede the jump instruction by no more
than 126 bytes.
To implement a conditional jump, the CPU looks at the FLAG
register (set by last instruction executed by the processor).
JMP instructions themselves do not affect flags.
Unconditional Jump
Syntax:
JMP destination_label
Purpose: To Transfer control to another part of the program.
Example:
 MOV AX, 5
 MOV BX, 2
 JMP LABEL_SUB
 ADD AX, BX ;this instruction will never execute
 LABEL_SUB:
 SUB AX, BX
Categories of Conditional Jump
Signed Jumps: This is used when a signed interpretation is being
given to result.
Symbol Description Condition for Jumps
JG/JNLE Jump if greater than
Jump if not less than or equal to
ZF = 0 and SF = OF
JGE/JNL Jump if greater than or equal to
Jump if not less than
SF = OF
JL/JNGE Jump if less than
Jump if not greater than or equal to
SF <> OF
JLE/JNG Jump if less than or equal to
Jump if not greater than
ZF = 1 and SF <> OF
Categories of Conditional Jump
Unsigned Jumps: This is used for an unsigned interpretation of result
Symbol Description Condition for Jumps
JA/JNBE Jump if above than
Jump if not below than or equal to
ZF = 0 and CF = 0
JAE/JNB Jump if above than or equal to
Jump if not below than
CF = 0
JB/JNAE Jump if below than
Jump if not above than or equal to
CF = 1
JBE/JNA Jump if below than or equal to
Jump if not above than
ZF = 1 and CF = 1
Categories of Conditional Jump
Single-Flag Jumps: Which operate on settings of individual flags.
Compare Instruction
CMP (compare) instruction performs an implied subtraction of a
source operand from destination operand. Neither operand is
modified.
 CMP destination, source
Compare Instruction maintain three condition:
 Destination < Source
 Destination = Source
 Destination > Source
Examples
OUTPUT
High Level Language Structures
Branching Structure
 IF-THEN
 IF-THEN-ELSE
 CASE
IF-THEN
IF-THEN-ELSE
CASE
A case is a multiway branch structure that tests a register,
variable, or expression for particular values or range of
values. The general form is as follows:
Case Expression
values_1: statements_1
values_2: statements_2
…………..
values_n: statements_n
END_CASE
CASE Examples
High Level Language Structures
Branching with Compound Conditions
 AND CONDITIONS
 OR CONDITIONS
AND CONDITION
 An AND condition is true if and only if condition_1 and
condition_2 are both true.
 If any condition is false, then the whole thing is false.
AND CONDITION EXAMPLES
OR CONDITION
 If condition_1 OR condition_2 is true then an OR conditions is true.
 If both conditions are false, then the whole thing is false.
OR CONDITION EXAMPLES
High Level Language Structures
Looping Structures
FOR LOOP
WHILE LOOP
REPEAT LOOP
LOOPING STRUCTURES
FOR LOOP
Executed at least once.
 If CX contains 0, the loop instruction decrements CX (CX =
FFFFh)
 To prevent this, use instruction JCXZ before the loop.
;initialize CX
JCXZ SKIP
TOP:
;body of the loop
LOOP TOP
SKIP:
Examples For Looping Structures
LOOPING STRUCTURES
WHILE LOOP
 WHILE condition DO
 ;statements
 END_WHILE
 WHILE LOOP checks the terminating condition at the top of the
loop, so don’t forget to initialize variables.
EXAMPLES WHILE STRUCTURES
LOOPING STRUCTURES
REPEAT LOOP
 REPEAT
 ;statements
 UNTIL condition
First statements are executed, then the condition is checked.
If true, the loop terminates; if false, control branches to the top of the
loop
Examples Repeat Structures
Flow control instructions
Flow control instructions

Flow control instructions

  • 2.
  • 3.
  • 4.
    Flow Control Instructions Program using the different instruction :-  Conditional and Unconditional Jump Instructions Compare Instruction Loop Instruction
  • 5.
    Conditional Jump Syntax: Jxxx destination_label Wherexxx represents the condition If condition is true, the next instruction to be executed is the one at destination_label. If condition is false, the instruction immediately following the jump is done next.
  • 6.
    Conditional Jump The destination_labelmust precede the jump instruction by no more than 126 bytes. To implement a conditional jump, the CPU looks at the FLAG register (set by last instruction executed by the processor). JMP instructions themselves do not affect flags.
  • 7.
    Unconditional Jump Syntax: JMP destination_label Purpose:To Transfer control to another part of the program. Example:  MOV AX, 5  MOV BX, 2  JMP LABEL_SUB  ADD AX, BX ;this instruction will never execute  LABEL_SUB:  SUB AX, BX
  • 8.
    Categories of ConditionalJump Signed Jumps: This is used when a signed interpretation is being given to result. Symbol Description Condition for Jumps JG/JNLE Jump if greater than Jump if not less than or equal to ZF = 0 and SF = OF JGE/JNL Jump if greater than or equal to Jump if not less than SF = OF JL/JNGE Jump if less than Jump if not greater than or equal to SF <> OF JLE/JNG Jump if less than or equal to Jump if not greater than ZF = 1 and SF <> OF
  • 9.
    Categories of ConditionalJump Unsigned Jumps: This is used for an unsigned interpretation of result Symbol Description Condition for Jumps JA/JNBE Jump if above than Jump if not below than or equal to ZF = 0 and CF = 0 JAE/JNB Jump if above than or equal to Jump if not below than CF = 0 JB/JNAE Jump if below than Jump if not above than or equal to CF = 1 JBE/JNA Jump if below than or equal to Jump if not above than ZF = 1 and CF = 1
  • 10.
    Categories of ConditionalJump Single-Flag Jumps: Which operate on settings of individual flags.
  • 11.
    Compare Instruction CMP (compare)instruction performs an implied subtraction of a source operand from destination operand. Neither operand is modified.  CMP destination, source Compare Instruction maintain three condition:  Destination < Source  Destination = Source  Destination > Source
  • 12.
  • 13.
  • 14.
    High Level LanguageStructures Branching Structure  IF-THEN  IF-THEN-ELSE  CASE
  • 15.
  • 16.
  • 17.
    CASE A case isa multiway branch structure that tests a register, variable, or expression for particular values or range of values. The general form is as follows: Case Expression values_1: statements_1 values_2: statements_2 ………….. values_n: statements_n END_CASE
  • 18.
  • 19.
    High Level LanguageStructures Branching with Compound Conditions  AND CONDITIONS  OR CONDITIONS
  • 20.
    AND CONDITION  AnAND condition is true if and only if condition_1 and condition_2 are both true.  If any condition is false, then the whole thing is false.
  • 21.
  • 22.
    OR CONDITION  Ifcondition_1 OR condition_2 is true then an OR conditions is true.  If both conditions are false, then the whole thing is false.
  • 23.
  • 24.
    High Level LanguageStructures Looping Structures FOR LOOP WHILE LOOP REPEAT LOOP
  • 25.
    LOOPING STRUCTURES FOR LOOP Executedat least once.  If CX contains 0, the loop instruction decrements CX (CX = FFFFh)  To prevent this, use instruction JCXZ before the loop. ;initialize CX JCXZ SKIP TOP: ;body of the loop LOOP TOP SKIP:
  • 26.
  • 27.
    LOOPING STRUCTURES WHILE LOOP WHILE condition DO  ;statements  END_WHILE  WHILE LOOP checks the terminating condition at the top of the loop, so don’t forget to initialize variables.
  • 28.
  • 29.
    LOOPING STRUCTURES REPEAT LOOP REPEAT  ;statements  UNTIL condition First statements are executed, then the condition is checked. If true, the loop terminates; if false, control branches to the top of the loop
  • 30.