0% found this document useful (0 votes)
67 views17 pages

Lecture No.14

Chapter 6 of 'Assembly Language for x86 Processors' covers conditional processing, including Boolean and comparison instructions, conditional jumps, and control flow directives. It explains the use of various instructions such as AND, OR, XOR, NOT, TEST, and CMP, along with their applications in programming tasks. The chapter also discusses CPU status flags and their significance in determining the outcomes of operations.

Uploaded by

ahada8878
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)
67 views17 pages

Lecture No.14

Chapter 6 of 'Assembly Language for x86 Processors' covers conditional processing, including Boolean and comparison instructions, conditional jumps, and control flow directives. It explains the use of various instructions such as AND, OR, XOR, NOT, TEST, and CMP, along with their applications in programming tasks. The chapter also discusses CPU status flags and their significance in determining the outcomes of operations.

Uploaded by

ahada8878
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/ 17

Assembly Language for x86 Processors

7th Edition
Kip R. Irvine

Chapter 6: Conditional Processing

Slides prepared by the author


Revision date: 1/15/2014

(c) Pearson Education, 2014. All rights reserved. You may modify and copy this slide show for your personal use, or for
use in the classroom, as long as this copyright statement, the author's name, and the title are not changed.
Chapter Overview

• Boolean and Comparison Instructions


• Conditional Jumps
• Conditional Loop Instructions
• Conditional Structures
• Application: Finite-State Machines
• Conditional Control Flow Directives

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 2


Boolean and Comparison Instructions

• CPU Status Flags


• AND Instruction
• OR Instruction
• XOR Instruction
• NOT Instruction
• Applications
• TEST Instruction
• CMP Instruction

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 3


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 (or too small) 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 Overflow flag is set when an instruction generates an invalid
signed result (bit 7 carry is XORed with bit 6 Carry).
• The Parity flag is set when an instruction generates an even
number of 1 bits in the low byte of the destination operand.
• The Auxiliary Carry flag is set when an operation produces a carry
out from bit 3 to bit 4

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 4


AND Instruction
• Performs a Boolean AND operation between each
pair of matching bits in two operands
• Syntax:
AND destination, source
AND
(same operand types as MOV)

00111011
AND 0 0 0 0 1 1 1 1

cleared 00001011 unchanged

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 5


OR Instruction
• Performs a Boolean OR operation between each pair
of matching bits in two operands
• Syntax:
OR destination, source
OR

00111011
OR 0 0 0 0 1 1 1 1

unchanged 00111111 set

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 6


XOR Instruction
• Performs a Boolean exclusive-OR operation between
each pair of matching bits in two operands
• Syntax:
XOR
XOR destination, source

00111011
XOR 0 0 0 0 1 1 1 1

unchanged 00110100 inverted

XOR is a useful way to toggle (invert) the bits in an operand.

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 7


NOT Instruction
• Performs a Boolean NOT operation on a single
destination operand
• Syntax:
NOT
NOT destination

NOT 00111011
11000100 inverted

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 8


Applications (1 of 4)

• Task: Convert the character in AL to upper case.


• Solution: Use the AND instruction to clear bit 5.

mov al,'a' ; AL = 01100001b


and al,11011111b ; AL = 01000001b

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 9


Applications (2 of 4)

• Task: Convert a binary decimal byte into its equivalent


ASCII decimal digit.
• Solution: Use the OR instruction to set bits 4 and 5.

mov al,6 ; AL = 00000110b


or al,00110000b ; AL = 00110110b

The ASCII digit '6' = 00110110b

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 10


Applications (3 of 4)

• Task: Jump to a label if an integer is even.


• Solution: AND the lowest bit with a 1. If the result is Zero,
the number was even.

mov ax,wordVal
and ax,1 ; low bit set?
jz EvenValue ; jump if Zero flag set

JZ (jump if Zero) is covered in Section 6.3.

Your turn: Write code that jumps to a label if an integer is


negative.

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 11


Applications (4 of 4)

• Task: Jump to a label if the value in AL is not zero.


• Solution: OR the byte with itself, then use the JNZ (jump
if not zero) instruction.

or al,al
jnz IsNotZero ; jump if not zero

ORing any number with itself does not change its value.

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 12


TEST Instruction
• Performs a nondestructive AND operation between each pair of
matching bits in two operands
• No operands are modified, but the Zero flag is affected.
• Example: jump to a label if either bit 0 or bit 1 in AL is set.

test al,00000011b
jnz ValueFound

• Example: jump to a label if neither bit 0 nor bit 1 in AL is set.


test al,00000011b
jz ValueNotFound

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 13


CMP Instruction (1 of 3)

• Compares the destination operand to the source operand


• Nondestructive subtraction of source from destination (destination
operand is not changed)
• Syntax: CMP destination, source
• Example: destination == source

mov al,5
cmp al,5 ; Zero flag set

• Example: destination < source

mov al,4
cmp al,5 ; Carry flag set

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 14


CMP Instruction (2 of 3)

• Example: destination > source

mov al,6
cmp al,5 ; ZF = 0, CF = 0

(both the Zero and Carry flags are clear)

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 15


CMP Instruction (3 of 3)

The comparisons shown here are performed with signed


integers.

• Example: destination > source


mov al,5
cmp al,-2 ; Sign flag == Overflow flag

• Example: destination < source


mov al,-1
cmp al,5 ; Sign flag != Overflow flag

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 16


4C 6F 70 70 75 75 6E

Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2014. 17

You might also like