Top Banner
CONDITIONAL PROCESSING Lecture # 14 U n i v e r s i t y o f C e n t e r a l P u n j a b F s d C a m p u s 1
32

Lecture no 15

Apr 14, 2017

Download

zaidshaidzaid
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture no 15

University of Centeral Punjab Fsd Campus

1

CONDITIONAL PROCESSINGLecture # 14

Page 2: Lecture no 15

2

University of Centeral Punjab Fsd Campus

CHAPTER OVERVIEWPrograms that deal with hardware devices must be able to manipulate individual bits in numbers. Data encryption and compression also rely on bit manipulation.

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?

Page 3: Lecture no 15

3

University of Centeral Punjab Fsd Campus

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

Page 4: Lecture no 15

4

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

Page 5: Lecture no 15

5

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.

Page 6: Lecture no 15

6

University of Centeral Punjab Fsd Campus

OPERATIONS WITH DESCRIPTION

Page 7: Lecture no 15

7

University of Centeral Punjab Fsd Campus

AND INSTRUCTION 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 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.

Page 8: Lecture no 15

8

University of Centeral Punjab Fsd Campus

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

Page 9: Lecture no 15

9

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

Page 10: Lecture no 15

10

University of Centeral Punjab Fsd Campus

OR INSTRUCTION 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 OR destination,source

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

Page 11: Lecture no 15

11

University of Centeral Punjab Fsd Campus

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.

Page 12: Lecture no 15

12

University of Centeral Punjab Fsd Campus

XOR INSTRUCTION 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.

Page 13: Lecture no 15

13

University of Centeral Punjab Fsd Campus

NOT INSTRUCTION The NOT instruction toggles (inverts) all bits in an

operand. The result is called the one’s complement.

The following operand types are permitted: NOT reg NOT mem

Flags No flags are affected by the NOT instruction.

Page 14: Lecture no 15

14

University of Centeral Punjab Fsd Campus

CMP INSTRUCTION In x86 assembly language we use the CMP

instruction to compare integers. Compares the destination operand to the

source operand Syntax: CMP destination, source Example: destination == source

Flags: The CMP instruction changes the Overflow, Sign, Zero, Carry, Auxiliary Carry, and Parity flags

Page 15: Lecture no 15

15

University of Centeral Punjab Fsd Campus

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.

Page 16: Lecture no 15

16

University of Centeral Punjab Fsd Campus

CMP INSTRUCTION- EXAMPLES When AX equals 5 and is compared to 10, the

Carry flag is set because subtracting 10 from 5 requires a borrow: mov ax,5 cmp ax,10 ; ZF = 0 and CF = 1

Page 17: Lecture no 15

17

University of Centeral Punjab Fsd Campus

SETTING AND CLEARING INDIVIDUAL CPU FLAGS How can you easily set or clear the Zero, Sign,

Carry, and Overflow flags?

To set the Zero flag, TEST or AND an operand with Zero; to clear the Zero flag, OR an operand with 1:

Page 18: Lecture no 15

18

University of Centeral Punjab Fsd Campus

SETTING AND CLEARING INDIVIDUAL CPU FLAGS

To set the Carry flag, use the STC instruction; to clear the Carry flag, use CLC: stc ; set Carry flag clc ; clear Carry flag

Page 19: Lecture no 15

19

University of Centeral Punjab Fsd Campus

WHAT'S NEXT

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

Page 20: Lecture no 15

20

University of Centeral Punjab Fsd Campus

CONDITIONAL STRUCTURES 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.

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.

Page 21: Lecture no 15

21

University of Centeral Punjab Fsd Campus

EXAMPLE 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:

Page 22: Lecture no 15

22

University of Centeral Punjab Fsd Campus

JCOND INSTRUCTION A conditional jump instruction branches to a

label when specific register or flag conditions are met

Page 23: Lecture no 15

23

University of Centeral Punjab Fsd Campus

JUMPS BASED ON SPECIFIC FLAGS

Page 24: Lecture no 15

24

University of Centeral Punjab Fsd Campus

JUMPS BASED ON EQUALITY

Page 25: Lecture no 15

25

University of Centeral Punjab Fsd Campus

EXAMPLES

Page 26: Lecture no 15

26

University of Centeral Punjab Fsd Campus

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

Page 27: Lecture no 15

27

University of Centeral Punjab Fsd Campus

OTHER IMPORTANT EXAMPLES Application: Sequential Search of an

Array Application: Simple String Encryption

Page 28: Lecture no 15

28

University of Centeral Punjab Fsd Campus

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

Page 29: Lecture no 15

29

University of Centeral Punjab Fsd Campus

CONDITIONAL LOOP INSTRUCTIONS

LOOPZ and LOOPE

LOOPNZ and LOOPNE

Page 30: Lecture no 15

30

University of Centeral Punjab Fsd Campus

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 LOOPZ destination

Page 31: Lecture no 15

31

University of Centeral Punjab Fsd Campus

LOOPE INSTRUCTIONS The LOOPE (loop if equal) instruction is

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

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

Page 32: Lecture no 15

32

University of Centeral Punjab Fsd Campus

LOOPNE INSTRUCTIONS The LOOPNE (loop if not equal) instruction is

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

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