You are on page 1of 32

University of Centeral Punjab Fsd Campus

CONDITIONAL PROCESSING
Lecture # 14
1

CHAPTER OVERVIEW

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?

University of Centeral Punjab Fsd


Campus

Programs that deal with hardware devices must be able


to manipulate individual bits in numbers. Data
encryption and compression also rely on bit
manipulation.

CHAPTER OVERVIEW
Boolean and Comparison Instructions
Conditional Jumps
Conditional Loop Instructions
Conditional Structures
Conditional Control Flow Directives

University of Centeral Punjab Fsd


Campus

BOOLEAN AND COMPARISON


INSTRUCTIONS
CPU Status Flags
AND Instruction
OR Instruction
XOR Instruction
NOT Instruction
Applications
TEST Instruction
CMP Instruction

University of Centeral Punjab Fsd


Campus

STATUS FLAGS - REVIEW


The Zero flag is set when the result of an
operation equals zero
The Carry flag is set when an instruction
generates a result that is too large for the
destination operand.
The Sign flag is set if the destination operand is
negative, and it is clear if the destination
operand is positive
The Parity flag is set when an instruction
generates an even number of 1 bits in the low
byte of the destination operand.

University of Centeral Punjab Fsd


Campus

OPERATIONS WITH DESCRIPTION

University of Centeral Punjab Fsd


Campus

AND INSTRUCTION

AND destination, source

Syntax
AND

reg, reg
AND reg, mem
AND reg, imm
AND mem, reg
AND mem, imm

The operands can be 8, 16, 32, or 64 bits, and they


must be the same size.

University of Centeral Punjab Fsd


Campus

The 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

EXAMPLE
AL is initially set to 10101110 binary
ANDing it with 11110110
AL equals ??

Flags 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

University of Centeral Punjab Fsd


Campus

CONVERTING CHARACTERS TO
UPPER-CASE
The AND instruction provides an easy way to
translate a letter from lowercase to uppercase.
If we compare the ASCII codes of capital A and
lowercase a, it becomes clear that only bit 5 is
different:

The rest of the alphabetic characters have the


same relationship. If we AND any character with
11011111 binary, all bits are unchanged except
for bit 5

University of Centeral Punjab Fsd


Campus

OR INSTRUCTION

OR

destination,source

Syntax
OR

reg,reg
OR reg,mem
OR reg,imm
OR mem,reg
OR mem,imm

University of Centeral Punjab Fsd


Campus

The OR instruction performs a Boolean OR


operation between each pair of matching bits in
two operands and places the result in the
destination operand

10

EXAMPLE
AL is initially equal to 11100011
OR it with 00000100
Al is equals to ??

Flags 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.

University of Centeral Punjab Fsd


Campus

11

XOR INSTRUCTION

XOR

destination, source

The XOR instruction uses the same operand


combinations and sizes as the AND and OR
instructions.

University of Centeral Punjab Fsd


Campus

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

12

NOT INSTRUCTION
The NOT instruction toggles (inverts) all bits in an
operand. The result is called the ones complement.
The following operand types are permitted:

NOT reg
NOT mem

Flags No flags are affected by the NOT instruction.

University of Centeral Punjab Fsd


Campus

13

CMP INSTRUCTION
In x86 assembly language we use the CMP
instruction to compare integers.
Compares the destination operand to the source
operand

CMP destination, source


Example: destination == source

Flags: The CMP instruction changes the


Overflow, Sign, Zero, Carry, Auxiliary Carry, and
Parity flags

University of Centeral Punjab Fsd


Campus

Syntax:

14

CMP INSTRUCTION

CMP is a valuable tool for creating conditional


logic structures. When you follow CMP with a
conditional jump instruction, the result is the
assembly language equivalent of an IF statement.

University of Centeral Punjab Fsd


Campus

15

CMP INSTRUCTION- EXAMPLES

mov

ax,5
cmp ax,10

; ZF = 0 and CF = 1

University of Centeral Punjab Fsd


Campus

When AX equals 5 and is compared to 10, the


Carry flag is set because subtracting 10 from 5
requires a borrow:

16

SETTING AND CLEARING


INDIVIDUAL CPU FLAGS

To set the Zero flag, TEST or AND an operand with


Zero; to clear the Zero flag, OR an operand with 1:

University of Centeral Punjab Fsd


Campus

How can you easily set or clear the Zero, Sign,


Carry, and Overflow flags?

17

SETTING AND CLEARING


INDIVIDUAL CPU FLAGS

To set the Carry flag, use the STC instruction; to


clear the Carry flag, use CLC:
stc
clc

; set Carry flag


; clear Carry flag

University of Centeral Punjab Fsd


Campus

18

WHAT'S NEXT
Boolean and Comparison Instructions
Conditional Jumps
Conditional Loop Instructions
Conditional Structures
Conditional Control Flow Directives

University of Centeral Punjab Fsd


Campus

19

CONDITIONAL STRUCTURES

Two steps are involved in executing a conditional


statement:
First,

an operation such as CMP, AND, or SUB


modifies the CPU status flags.
Second, a conditional jump instruction tests the flags
and causes a branch to a new address.

University of Centeral Punjab Fsd


Campus

There are no explicit high-level logic structures in


the x86 instruction set, but you can implement
them using a combination of comparisons and
jumps.

20

EXAMPLE

University of Centeral Punjab Fsd


Campus

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:

21

JCOND INSTRUCTION

University of Centeral Punjab Fsd


Campus

A conditional jump instruction branches to a


label when specific register or flag conditions are
met

22

JUMPS BASED ON SPECIFIC FLAGS

University of Centeral Punjab Fsd


Campus

23

JUMPS BASED ON EQUALITY

University of Centeral Punjab Fsd


Campus

24

EXAMPLES

University of Centeral Punjab Fsd


Campus

25

EXAMPLE
Loop until Key Pressed In the following 32-bit
code, a loop runs continuously until the user
presses a standard alphanumeric key. The ReadKey
method from the Irvine32 library sets the Zero flag
if no key is present in the input buffer:
.data

char BYTE ?

.code

L1: mov eax,10


; create 10 ms delay
call Delay
call ReadKey ; check for key
jz L1 ; repeat if no key
mov char,AL ; save the character

University of Centeral Punjab Fsd


Campus

26

OTHER IMPORTANT EXAMPLES


Application: Sequential Search of an Array
Application: Simple String Encryption

University of Centeral Punjab Fsd


Campus

27

WHAT'S NEXT
Boolean and Comparison Instructions
Conditional Jumps
Conditional Loop Instructions
Conditional Structures
Conditional Control Flow Directives

University of Centeral Punjab Fsd


Campus

28

CONDITIONAL LOOP INSTRUCTIONS


LOOPZ and LOOPE

LOOPNZ and LOOPNE

University of Centeral Punjab Fsd


Campus

29

LOOPZ INSTRUCTIONS
LOOPZ (loop if zero) instruction works just like
the LOOP instruction except that the Zero flag
must be set in order for control to transfer to the
destination label
The syntax is

destination

University of Centeral Punjab Fsd


Campus

LOOPZ

30

LOOPE INSTRUCTIONS

ECX = ECX - 1
if ECX > 0 and ZF = 1, jump to destination

University of Centeral Punjab Fsd


Campus

The LOOPE (loop if equal) instruction is


equivalent to LOOPZ, and they share the same
opcode. They perform the following tasks:

31

LOOPNE INSTRUCTIONS

ECX = ECX - 1
if ECX > 0 and ZF = 0, jump to destination

University of Centeral Punjab Fsd


Campus

The LOOPNE (loop if not equal) instruction is


equivalent to LOOPNZ, and they share the same
opcode. They perform the following tasks:

32