You are on page 1of 52

Control Flow

Rodelyn Aguinaldo
Intended Learning Outcomes
- To discuss the use of CMP and conditional
JMP instructions
- To create an assembly program equivalent
of the WHILE, FOR DO and DO-WHILE
- To apply iterative statements using
assembly language in programming
Control Flow
- Unconditional
- Conditional
- Compare
Control Flow
UNCONDITIONAL
- A jump that is always executed
- Does not depend on condition being
TRUE or FALSE
- Uses JMP (jump)
- Syntax:
- JMP <label>
Control Flow
• CONDITIONAL
– Two steps involved:
– First, a conditional jump instruction tests the
flags and causes a branch to a new address .
– Second, an operation such as CMP, AND, or
SUB modifies the CPU flags.
CONDITIONAL
• A conditional jump instruction branches to
a destination label when a flag condition is
true . If the flag condition is false, the
instruction immediately following the
conditional jump is executed.
• The syntax is:
– Jcond desti na tion
– cond refers to a flag condition, identifying the
state of one or more flags.
Conditional
• For example
Types of Conditional Jump
Instructions
Jumps Based on Specific Flag
Values
Control Flow
COMPARE
-used to compare character data, as well as,
numeric data fields.
-used in conjunction with conditional jump
instructions
Syntax:
CMP <register>, <register>
CMP<register>,<hex/character/ASCII/decimal>
Control Flow
• The CMP instruction compares AL to Zero.
The JZ (jump if Zero) instruction jumps to
label L1 if the Zero flag was set by the
CMP instruction:
Control Flow
• The AND instruction perf orms a bitwise
AND on the DL regis ter, affecting the Zero
flag. The JNZ (jump if not Zero) instruction
jumps if the Zero flag is clear:
Control Flow
COMPARE
-performs an implied subtraction of a source
operand from a destination operand
- changes the Overflow, Sign, Zero, Carry,
Auxiliary Carry and Parity Flags
- Provides the basis for most conditional logic
structures
- Uses the same operand combinations as the
AND instruction
Control Flow
Compare
Compare
Compare and Jump
The Compare and Jump
Instructions
Equality Comparisons
• CMP leftOp,rightOp
• Jump based on equality
Jumps based on Unsigned
Comparison
Jumps based on Signed
Comparison
Jumps based on Signed
Comparison
Examples
Examples
If statement
Logical AND operator
JA ( jump if above)
JBE
Logical OR
JBE
While LOOP
If statement: Nested loop
Assembly code
Loop containing if statement
Example1: Loop1.ams
Example2: Loop2.asm
.model small
.code
org 100h
start:
main proc near
mov ah, 2
call dia
call dia
call dia
Conditional Loop Instructions
• LOOPZ and LOOPE Instructions
• The LOOPZ (loop if zero) instruction
permits a loop to continue while the Zero
flag is set and the unsigned value of ECX
is greater than zero . The destination label
must be between -128 and +127 bytes
from the location of the following
instruction.
Conditional Loop Instructions
• LOOPZ and LOOPE Instructions
• The syntax is:
LOOPZ destination
• The LOOPE (loop if equal) instruction is
equivalent to LOOPZ because they share
the same circuitry.
• This is the execution logic of LOOPZ and
LOOPE:

• Otherwise, no jump occurs and control


passes to the next instruction.
LOOPNZ and LOOPNE
Instructions
• The LOOPNZ (loop if not zero) instruction
is the counterpart of LOOPZ. The loop
continues while the unsigned value of ECX
is greater than zero and the Zero flag is
clear.
• The syntax is:
• LOOPNZ destination
LOOPNZ and LOOPNE
Instructions
• The LOOPNE (loop if not equal)
instruction is equivalent to LOOPNZ
because they share the same circuitry.
Otherwise, no jump occurs and control
• This is the execution
passes logicOtherwise,
to the next instruction. of LOOPNZ and
no jump occurs and control passes to the
LOOPNE: next instruction.

• Otherwise, no jump occurs and control


passes to the next instruction.
Assignment
• In all compound expressions, use short-
circuit evaluation. Assume that X, val1,
val2 and val3 are I6-bit variables .
Assignment
Assignment
Assignment
• Rewrite the following code so that it is
functionally equivalent, but uses fewer
instructions:

You might also like