0% found this document useful (0 votes)
11 views49 pages

Assembly Language Conditional Processing

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)
11 views49 pages

Assembly Language Conditional Processing

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

Chapter 6

Conditional Processing

Lecture by: Atiya Jokhio


General Concepts

• This chapter introduces a major item to your assembly language toolchest,


giving your programs the ability to make decisions.
• First, we start by introducing you to the Boolean operations that are the core of
all decision statements because they affect the CPU status flags.
• Then we show how to use conditional jump and loop instructions that interpret
CPU status flags.

2
Conditional Branching

• A programming language that permits decision making lets you alter the flow of
control, using a technique known as conditional branching.
• Assembly language, provides all the tools you need for decision-making logic.
• Programs that deal with hardware devices must be able to manipulate individual bits in
numbers. Individual bits must be tested, cleared, and set. Data encryption and
compression also rely on bit manipulation.

3
Conditional Branching

This chapter should answer some basic questions:


• How can I use the Boolean operations introduced in Chapter 1 (AND, OR, NOT)?
• How do I write an IF statement in assembly language?
• How are nested-IF statements translated by compilers into machine language?
• How can I set and clear individual bits in a binary number?
• How can I perform simple binary data encryption?
• How are signed numbers differentiated from unsigned numbers in boolean
expressions?

4
Boolean and Comparison Instructions
• AND, OR, XOR, and NOT. These operations can be carried out at the binary
bit level, using assembly language instructions.
• These operations are also important at the boolean expression level, in IF
statements.
• Table shows selected Boolean Instructions

5
Boolean and Comparison Instructions
CPU Status Flags: (Revision)
Boolean instructions affect the Zero, Carry, Sign, Overflow, and Parity flags.
• The Zero flag is set when the result of an operation equals zero.
• The Carry flag is set when an operation generates a carry out of the highest bit of the
destination operand.
• The Sign flag is a copy of the high bit of the destination operand, indicating that it is
negative if set and positive if clear. (Zero is assumed to be positive.)
• The Overflow flag is set when an instruction generates a result that is outside the
signed range of the destination operand.
• The Parity flag is set when an instruction generates an even number of 1 bits in the low
byte of the destination operand.

6
Boolean and Comparison Instructions
AND Instruction
• Performs a boolean (bitwise) AND operation between each pair of matching bits in
two operands and places the result in the destination operand:
AND destination , source
• The following operand combinations are permitted, although immediate operands
can be no larger than 32 bits:
AND reg, reg
AND reg, mem
AND reg, imm
AND mem, reg
AND mem, imm
• The operands can be 8, 16, or 32 bits, and they must be the same size.
7
Boolean and Comparison Instructions

AND Instruction [Cont.]


• Example:

• The AND instruction lets you clear 1 or more bits in an operand without affecting
other bits.
• The technique is called bit masking.
• The AND instruction always clears the Overflow and Carry flags. It modifies the
Sign, Zero, and Parity flags in a way that is consistent with the value assigned to the
destination operand.

8
Boolean and Comparison Instructions

AND Instruction [Cont.]


:
• Converting Characters to Upper case
• We convert lower case to upper case by clearing bit 5.

• The rest of the alphabetic characters have the same relationship.

9
Boolean and Comparison Instructions

OR Instruction
• Performs a boolean OR operation between each pair of matching bits in two operands
: and places the result in the destination operand:

OR destination,source

• The OR instruction uses the same operand combinations as the AND instruction:
OR reg,reg
OR reg,mem
OR reg,imm
OR mem,reg
OR mem,imm
• The operands can be 8, 16, 32, or 64 bits, and they must be the same size.
10
Boolean and Comparison Instructions

OR Instruction [Cont.]
• The OR instruction is particularly useful when you need to set 1 or more bits in an
:
operand without affecting any other bits.
• Example:

• The OR instruction always clears the Carry and Overflow flags. It modifies the Sign,
Zero, and Parity flags in a way that is consistent with the value assigned to the
destination operand.

11
Boolean and Comparison Instructions
XOR Instruction
• performs a boolean exclusive-OR operation between each pair of matching bits in two
: operands and stores the result in the destination operand:

XOR destination , source

• The XOR instruction uses the same operand combinations and sizes as the AND and OR
instructions.
• The XOR instruction always clears the Overflow and Carry flags. XOR modifies the Sign,
Zero, and Parity flags in a way that is consistent with the value assigned to the
destination operand.

12
Boolean and Comparison Instructions
XOR Example: Checking the Parity Flag
• Parity checking is a function performed on a binary number that counts the
: number of 1 bits contained in the number.

• if the resulting count is even, we say that the data has even parity; if the count is
odd, the data has odd parity.
• An effective way to find the parity of a number without changing its value:

13
Boolean and Comparison Instructions
NOT instruction
• The NOT instruction toggles (inverts) all bits in an operand.
:
• The result is called the one’s complement.
NOT reg
NOT mem
• Example:

• No flags are affected by the NOT instruction.

14
Boolean and Comparison Instructions
TEST instruction
• performs an implied AND operation between corresponding bits in the two
: operands and sets the flags without modifying either operand.

• It sets the Sign, Zero, and Parity flags based on the value assigned to the
destination operand.
• The TEST instruction permits the same operand combinations as the AND
instruction.
• TEST is particularly valuable for finding out whether individual bits in an operand
are set.

15
Boolean and Comparison Instructions
TEST instruction [Cont.]
• Testing Multiple Bits
: • The TEST instruction can check several bits at once.

• Suppose we want to know whether bit 0 or bit 3 is set in the AL register.

• From the following example data sets, we can infer that the Zero flag is set only when
all tested bits are clear:

16
Boolean and Comparison Instructions
CMP instruction
• In x86 assembly language we use the CMP instruction to compare integers.
:
• The CMP (compare) instruction performs an implied subtraction of a source
operand from a destination operand. Neither operand is modified:
CMP destination , source
• CMP uses the same operand combinations as the AND instruction.
CMP reg, reg
CMP reg, mem
CMP reg, imm
CMP mem, reg
CMP mem, imm

17
Boolean and Comparison Instructions
CMP instruction [Cont.]
• When two unsigned operands are compared, the Zero and Carry flags indicate the
: following relations between operands.

18
Boolean and Comparison Instructions
CMP instruction [Cont.]
• When two signed operands are compared, the Sign, Zero, and Overflow flags
: indicate the following relations between operands:

19
Boolean and Comparison Instructions
CMP instruction [Cont.]
• Example
:

20
Boolean and Comparison Instructions
• Setting and Clearing Individual CPU Flags
How can you easily set or clear the Zero, Sign, Carry, and Overflow flags? There are
: several ways, some of which require modifying the destination operand. To set the
Zero flag, TEST or AND an operand with Zero; to clear the Zero flag, OR an operand
with 1.

21
Conditional Jumps

• Conditional Structures
• we can implement them using a combination of comparisons and jumps. Two steps are
: involved in executing a conditional statement:

1. an operation such as CMP, AND, or SUB modifies the CPU status flags.
2. a conditional jump instruction tests the flags and causes a branch to a new address
(instruction).

22
Conditional Jumps
• Conditional Structures [Cont.]
Example 1
: The CMP instruction in the following example compares EAX to Zero. The JZ( Jump if
zero) instruction jumps to label L1 if the Zero flag was set by the CMP instruction:

Example 2
The AND instruction in the following example performs a bitwise AND on the DL register,
affecting the Zero flag. The JNZ ( jump if not Zero) instruction jumps if the Zero flag is
clear:

23
Conditional Jumps

• Jcond Instruction
• A conditional jump instruction branches to a destination label when a status flag
: condition is true. Otherwise, if the flag condition is false, the instruction immediately
following the conditional jump is executed. The syntax is as follows:
Jcond destination
• cond refers to a flag condition identifying the state of one or more flags.
• The following examples are based on the Carry and Zero flags:

24
Conditional Jumps

Types of Conditional Jump Instructions


• The conditional jump instructions can be divided into four groups:
:
1. Jumps based on specific flag values
2. Jumps based on equality between operands or the value of (E)CX
3. Jumps based on comparisons of unsigned operands
4. Jumps based on comparisons of signed operands.

25
Conditional Jumps

Types of Conditional Jump Instructions [Cont.]


1. Jumps based on specific flag values.
:
Following is a list of jumps based on the Zero, Carry, Overflow, Parity, and Sign flags.

26
Conditional Jumps

Types of Conditional Jump Instructions [Cont.]


2. Jumps based on equality between operands or the value of (E)CX
:
Following is lists jump instructions based on evaluating equality.

27
Conditional Jumps
Types of Conditional Jump Instructions [Cont.]
2. Jumps based on equality between operands or the value of (E)CX
:

28
Conditional Jumps
Types of Conditional Jump Instructions [Cont.]
3. Jumps based on comparisons of unsigned operands
:

29
Conditional Jumps
Types of Conditional Jump Instructions [Cont.]
4. Jumps based on comparisons of signed operands.
:

30
Conditional Jumps
Types of Conditional Jump Instructions [Cont.]
Example:
:
The following instruction sequence demonstrates the comparison of two signed
values:

• The JA instruction, which is designed for unsigned comparisons, does not jump
because unsigned 7Fh is smaller than unsigned 80h. The JG instruction, on the
other hand, is designed for signed comparisons—it jumps because +127 is greater
than 128.
31
Conditional Jumps
Types of Conditional Jump Instructions [Cont.]

32
Conditional Jumps
Applications
• Testing Status Bits: One of the things assembly language does best is bit testing. Often,
we do not want to change the values of the bits we’re testing, but we do want to modify
:
the values of CPU status flags.
• Suppose, for example, that an 8-bit memory operand named status contains status
information about an external device attached to the computer.

33
Conditional Jumps
Applications
Example: Larger of Two Integers
: The following code compares the unsigned integers.

34
Conditional Jumps
Applications
Example: Smallest of Three Integers
: • The following instructions compare the unsigned 16-bit values in the
variables V1, V2, and V3 and move the smallest of the three to AX:

35
Conditional Jumps
Applications

36
Conditional Jumps
Applications
Find the first even number in an array of unsigned integers

37
Conditional Loop Instructions
LOOPZ and LOOPE Instructions
The LOOPZ (loop if zero) instruction works just like the LOOP instruction except that it has
one additional condition: the Zero flag must be set in order for control to transfer to the
:destination label.
The syntax is
LOOPZ destination
The LOOPE (loop if equal) instruction is equivalent to LOOPZ, and they share the same
opcode. They perform the following tasks

38
Conditional Loop Instructions
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 (after being decremented) and the Zero
:flag is clear.
The syntax is
LOOPNZ destination
The LOOPNE (loop if equal) instruction is equivalent to LOOPNZ, and they share the same
opcode. They perform the following tasks

39
Conditional Loop Instructions
LOOPNZ and LOOPNE Instructions
Example:

40
Conditional Structures
• We define a conditional structure to be one or more conditional expressions that
trigger a choice between different logical branches.
• Each branch causes a different sequence of instructions to execute.
:
• Assembly language programmers can easily translate logical statements written in
C++/Java into assembly language.
• For example: If then

41
Conditional Structures
• For example: If then Else

42
Conditional Structures
• For example: If then Else

43
Conditional Structures
• Compound Expressions
• Logical AND Operator

44
Conditional Structures
• Compound Expressions
• Logical OR Operator

45
Conditional Structures
• While Loops
A WHILE loop is really an IF statement followed by the body of the loop, followed
by an unconditional jump to the top of the loop.

46
Conditional Structures
• While Loops

47
Conditional Structures
• TASK: Implement following C code in Assembly

48
Conditional Structures
• TASK: Implement following C code in Assembly

49

You might also like