0% found this document useful (0 votes)
15 views13 pages

Chapter 8 Conditions and Loops

Chapter 8 discusses conditional execution and looping in assembly language, focusing on instructions like JMP for unconditional jumps and CMP for comparisons. It highlights the use of conditional jumps based on specific conditions and presents examples of implementing loops using the LOOP instruction. Additionally, it includes program examples demonstrating how to find the largest of three variables and print numbers 1 to 9.

Uploaded by

obotemiltone4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views13 pages

Chapter 8 Conditions and Loops

Chapter 8 discusses conditional execution and looping in assembly language, focusing on instructions like JMP for unconditional jumps and CMP for comparisons. It highlights the use of conditional jumps based on specific conditions and presents examples of implementing loops using the LOOP instruction. Additionally, it includes program examples demonstrating how to find the largest of three variables and print numbers 1 to 9.

Uploaded by

obotemiltone4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Chapter 8

Conditions and Loops


Introduction
• Conditional execution in assembly language is accomplished by
several looping and branching instructions.
• These instructions can change the flow of control in a program.
• Conditional execution is observed in two scenarios:
• Unconditional jump
– This is performed by the JMP instruction.
– Conditional execution often involves a transfer of control to the address of
an instruction that does not follow the currently executing instruction.
– Transfer of control may be forward, to execute a new set of instructions or
backward, to re-execute the same steps.
• Conditional jump
– This is performed by a set of jump instructions j<condition> depending
upon the condition.
– The conditional instructions transfer the control by breaking the sequential
flow and they do it by changing the offset value in IP. 2
The CMP Instruction
• The CMP instruction compares two operands.
• It is generally used in conditional execution.
• This instruction basically subtracts one operand from the other for
comparing whether the operands are equal or not.
• It does not disturb the destination or source operands.
• It is used along with the conditional jump instruction for decision
making.
• Syntax
CMP destination, source
• CMP compares two numeric data fields.
• The destination operand could be either in register or in memory.
• The source operand could be a constant (immediate) data, register
3
or memory.
The CMP Instruction
• Example
CMP DX, 00 ; Compare the DX value with zero
JE L7 ; If yes, then jump to label L7
.
.
L7: ...
• CMP is often used for comparing whether a counter
value has reached the number of times a loop needs
to be run.
• Consider the following typical condition:-
INC EDX
CMP EDX, 10 ; Compares whether the counter
has reached 10
JLE LP1 ; If it is less than or equal to 10, then jump to LP1 4
Unconditional Jump
• This is performed by the JMP instruction.
• Transfer of control may be forward, to execute a new set of
instructions or backward, to re-execute the same steps.
• The JMP instruction provides a label name where the flow of
control is transferred immediately.
• The syntax of the JMP instruction is −
JMP label
• The following code snippet illustrates the JMP instruction:-

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.

• When the program is executed, it produces the following


13
result:− 123456789:

You might also like