boolean and comparison_instructions

19
Boolean and Comparison Instructions Operatio n Description AND AND Destination, Source OR OR Destination, Source XOR XOR Destination, Source NOT NOT Destination TEST Implied AND Destination, Source, Only FLAGS changed BT,BTC,BTR ,BTS 06/12/22 SHOUA IQBAL 1

Upload: the-city-scholar-school

Post on 14-Apr-2017

263 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Boolean and comparison_instructions

Boolean and Comparison Instructions

Operation DescriptionAND AND Destination, Source

OR OR Destination, Source

XOR XOR Destination, Source

NOT NOT Destination

TESTImplied AND Destination, Source, Only FLAGS changed

BT,BTC,BTR,BTS  05/03/23 SHOUA IQBAL 1

Page 2: Boolean and comparison_instructions

Bitwise AND (result placed in destination)

Mov al, 00001111bAnd al, 00111011b al = ?

• al = 00001011b (0Bh)Mov al, 6DhAnd al, 4Ah al = ?

• al = 48h

• AND with a 0 to clear bits • AND with a 1 to preserve bits (bit

extraction) • Always clears the Overflow and Carry flags. • May modify the Sign, Zero, and Parity flag

AND Instruction

05/03/23 SHOUA IQBAL 2

Page 3: Boolean and comparison_instructions

Lowercase A (‘a’) is 61 0 1 1 0 0 0 0 1 Uppercase A (‘A’) is 41 0 1 0 0 0 0 0 1

Need to clear bit 5

Mov al, 61hAND al, 11011111b ;(DFh)

;al = 41h

Application:Converting Characters to Uppercase

05/03/23 SHOUA IQBAL 3

Page 4: Boolean and comparison_instructions

Bitwise OR (result placed in destination)

Mov al, 0Fh Or al, 61h al = ?

• al = 6FhMov al, 3DhOr al, 74h al = ?

• al = 7Dh OR with 1 to set selected bits OR with 0 to preserve bits Always clears the Overflow and Carry Flags May modify the Sign, Zero, and Parity Flags

OR Instruction

05/03/23 SHOUA IQBAL 4

Page 5: Boolean and comparison_instructions

Binary representation for 9 0 0 0 0 1 0 0 1 Ascii 9 is 39h 0 0 1 1 1 0 0 1

Need to set bits 4 and 5Mov al, 9OR al, 30h

;al = 00111001b (39h)

Application: Converting a Decimal Digit (byte) to ASCII

05/03/23 SHOUA IQBAL 5

Page 6: Boolean and comparison_instructions

Application:Determining the sign of a value by ORing register with

itselfOR al, al

Zero Flag Sign Flag Value in AL is …

clear clear greater than 0

set clear equal to 0

clear set less than 0

05/03/23 SHOUA IQBAL 6

Page 7: Boolean and comparison_instructions

Bitwise XOR (result stored in destination)

Mov al, 94hXOR al, 37h al = ?

al = 10100011bMov al, 72hXor al, 0DCh al = ?

al = 10101110b• XOR reverses itself when applied twice to the

same operand (data encryption)• Clears the Overflow and Carry flags. • May modify the sign, zero, and parity flags

XOR Instruction

05/03/23 SHOUA IQBAL 7

Page 8: Boolean and comparison_instructions

Indicates if the LOWEST BYTE of the result of a bitwise

or arithmetic operation has an even or odd number of 1 bits.

Mov al, 10110101bXOR al, 0

; al unchanged parity flag clear (PO)Mov al, 11001100bXOR al, 0

; al unchanged parity flag set (PE)

Parity Flag

05/03/23 SHOUA IQBAL 8

Page 9: Boolean and comparison_instructions

Perform an XOR between upper-and lower bytes

Mov ax, 64C1h ;0110 0100 1100 0001XOR ah, al

;ah = 10100101 Parity bit set (PE)

Application:How to check the parity of 16-bit registers

05/03/23 SHOUA IQBAL 9

Page 10: Boolean and comparison_instructions

Perform an XOR between bytes

Mov eax, 56D764C1h;0101 0110 1101 0111 0110 0100 1100 0001

XOR ah, al ;ah = 1010 0101Shr eax, 8XOR ah, al ;ah = 0111 0010Shr eax, 8XOR ah, al ;ah = 0010 0100

Parity bit set (PE)

Application: How to check the parity of 32-bit registers

05/03/23 SHOUA IQBAL 10

Page 11: Boolean and comparison_instructions

Toggles all bits in an operand No flags are affected

NOT Instruction

05/03/23 SHOUA IQBAL 11

Page 12: Boolean and comparison_instructions

Implied AND (no registers are changed)

(flags may be modified)

• Valuable for determining if individual bits are set.

• Always clears the Overflow and Carry flags• May modify the sign, zero, and parity flags

TEST Instruction

05/03/23 SHOUA IQBAL 12

Page 13: Boolean and comparison_instructions

Implied SUB (no registers are changed)

(flags may be modified)

• May modify Overflow, Sign, Zero, Aux. Carry, and Parity flags

CMP Instruction

05/03/23 SHOUA IQBAL 13

Page 14: Boolean and comparison_instructions

CMP Instruction Results

Zero Flag Carry Flag CMP Results

0 1 destination < sourceCMP 5, 10

0 0 destination > sourceCMP 105, 10

1 0 destination = sourceCMP 10, 10

05/03/23 SHOUA IQBAL 14

Page 15: Boolean and comparison_instructions

CMP of Signed IntegersFlags CMP Results

SF ≠ OF destination < source

SF = OF destination > source

ZF = 1 destination = source

05/03/23 SHOUA IQBAL 15

Page 16: Boolean and comparison_instructions

1. Write a single instruction that clears the high

8 bits of AX and does not change the low 8 bits.

AND AX, 00FFh

In Class Problems

05/03/23 SHOUA IQBAL 16

Page 17: Boolean and comparison_instructions

2. Write a single instruction that sets the high 8

bits of AX and does not change the low 8 bits.

OR AX, FF00h

In Class Problems

05/03/23 SHOUA IQBAL 17

Page 18: Boolean and comparison_instructions

3. Write a single instruction that complements

all the bits in EAX (do not use the NOT instruction)

XOR EAX, FFFFFFFFh

In Class Problems

05/03/23 SHOUA IQBAL 18

Page 19: Boolean and comparison_instructions

4. Write instructions that set the Zero flag if the

32-bit value in EAX is even, and clear the Zero flag if EAX is odd.

TEST EAX, 0000 0000 0000 0000 0000 0000 0000 0001b

In Class Problems

05/03/23 SHOUA IQBAL 19