Chapter 8 Conditions and Loops
Chapter 8 Conditions and Loops
5
Conditional Jump
• Some specified condition is satisfied in conditional jump,
before the control flow is transferred to a target instruction.
• There are numerous conditional jump instructions depending
upon the condition and data.
• Following are the conditional jump instructions used on
signed data used for arithmetic operations:−
6
Conditional Jump
• Following are the conditional jump
instructions used on unsigned data used for
logical operations:-
7
Conditional Jump
• The following conditional jump instructions
have special uses and check the value of
flags:−
8
Program Example
• This program
displays the largest
of three variables.
• The three double-
digit variables num1,
num2 and num3
have values 47, 22
and 31, respectively
• When the program is
executed, it
produces the
following result:−
The largest digit is: 47
9
Loops
• The JMP instruction can be used for
implementing loops.
• For example, the following code snippet can
be used for executing the loop-body 10 times.
MOV CL, 10
L1:
<LOOP-BODY>
DEC CL
JNZ L1
10
Loops
• The processor instruction set, however, includes a group
of loop instructions for implementing iteration.
• The basic LOOP instruction has the following syntax:-
LOOP label
• Where, label is the target label that identifies the target
instruction as in the jump instructions.
• The LOOP instruction assumes that the ECX register
contains the loop count.
• When the loop instruction is executed, the ECX register
is decremented and the control jumps to the target
label, until the ECX register value, i.e., the counter
reaches the value zero. 11
Loops
• Example - The previous code snippet could be
written as:-
mov ECX,10
l1:
<loop body>
loop l1
12
Program Example
• This program prints the number 1 to 9 on the screen.