ch. 7 logic, shift and rotate instr. logic instructions – and, or, xor and not test instruction...

20
Ch. 7 Logic, Shift and Rotate instr. • Logic Instructions – AND, OR, XOR and NOT • TEST Instruction • Shift Instructions - Left shift – doubles a number - Right shift – halves a number - … multiply or divide by powers of 2 - MUL, DIV – much slower than shift instr… • Rotate • Stack Operations • Introduction to Procedures

Upload: rebecca-dixon

Post on 11-Jan-2016

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Ch. 7 Logic, Shift and Rotate instr. Logic Instructions – AND, OR, XOR and NOT TEST Instruction Shift Instructions -Left shift – doubles a number -Right

Ch. 7 Logic, Shift and Rotate instr.

• Logic Instructions– AND, OR, XOR and NOT

• TEST Instruction• Shift Instructions

- Left shift – doubles a number - Right shift – halves a number- … multiply or divide by powers of 2- MUL, DIV – much slower than shift instr…

• Rotate • Stack Operations• Introduction to Procedures

Page 2: Ch. 7 Logic, Shift and Rotate instr. Logic Instructions – AND, OR, XOR and NOT TEST Instruction Shift Instructions -Left shift – doubles a number -Right

7.1 AND, OR and XOR Instructions

AND destination, sourceOR destination, sourceXOR destination, source

• Dest. – must be a Register or mem.• Source – const, reg., mem• Memory-to-memory operations are not allowed• Effect on flags– SF, ZF, PF reflect the result– AF is undefined – CF, OF = 0

SF – sign ZF – zeroPF – parityAF – auxiliary carryOF – overflowCF – carry

Page 3: Ch. 7 Logic, Shift and Rotate instr. Logic Instructions – AND, OR, XOR and NOT TEST Instruction Shift Instructions -Left shift – doubles a number -Right

Use of Logic Instructions

• Selectively modify the bits of destination– b AND 1 = b (b represents a bit, 0/1)– b AND 0 = 0– b OR 0 = b– b OR 1 = 1– b XOR 0 = b– b XOR 1 = ~b (complement of b)

• So, AND can be used to clear specific destination bit• OR can be used to set specific destination bit• XOR can be used to complement specific destination bit

Page 4: Ch. 7 Logic, Shift and Rotate instr. Logic Instructions – AND, OR, XOR and NOT TEST Instruction Shift Instructions -Left shift – doubles a number -Right

Examples

Example 7.2: Clear the sign bit of AL while leaving the other bits unchanged.AND AL, 7Fh

Example 7.3: Set the msb and lsb of AL while preserving the other bits.OR AL, 81h

Example 7.4: Change the sign bit of DXXOR DX, 8000h

0111 1111 = 7Fh

1000 0001 = 81h

mask

See example from book

Page 5: Ch. 7 Logic, Shift and Rotate instr. Logic Instructions – AND, OR, XOR and NOT TEST Instruction Shift Instructions -Left shift – doubles a number -Right

How to clear a register?

1. MOV AX, 0 ; machine code 3 bytes

2. SUB AX, AX ; .. 2 bytes

3. XOR AX, AX ; .. 2 bytes

-- BUT mem2mem operations only for MOV is allowed here – so to clear a memory location, use MOV.

Page 6: Ch. 7 Logic, Shift and Rotate instr. Logic Instructions – AND, OR, XOR and NOT TEST Instruction Shift Instructions -Left shift – doubles a number -Right

7.1.2 NOT Instruction

1’s Complement operation

NOT destination

- No effect on status flags

Example 7.5: Complement the bits in AXNOT AX

Page 7: Ch. 7 Logic, Shift and Rotate instr. Logic Instructions – AND, OR, XOR and NOT TEST Instruction Shift Instructions -Left shift – doubles a number -Right

7

• Logic Instructions– AND, OR, XOR and NOT

• TEST Instruction• Shift and Rotate Instructions• Stack Operations• Introduction to Procedures

Page 8: Ch. 7 Logic, Shift and Rotate instr. Logic Instructions – AND, OR, XOR and NOT TEST Instruction Shift Instructions -Left shift – doubles a number -Right

7.2 TEST Instruction

• TEST performs AND of the destination with source – but no change of the dest. contents

TEST destination, source

• Effects on flags– SF, ZF, PF reflect the result– AF is undefined– CF, OF = 0

• TEST vs. CMP– CMP is subtraction operation

Page 9: Ch. 7 Logic, Shift and Rotate instr. Logic Instructions – AND, OR, XOR and NOT TEST Instruction Shift Instructions -Left shift – doubles a number -Right

TEST Example

• Jump to label BELOW if AL contains an even number

TESET AL, 1 ; is AL even?JZ BELOW ; yes, go to BELOW

- use to examine individual bits in an operand. - mask contains 1’s in the bits positions to be tested & 0’s elsewhere-Even numbers have a 0 in bit#0.-So, mask = 0000 0001b = 1

JZ – jump if equal to zero

Page 10: Ch. 7 Logic, Shift and Rotate instr. Logic Instructions – AND, OR, XOR and NOT TEST Instruction Shift Instructions -Left shift – doubles a number -Right

Agenda

• Logic Instructions– AND, OR, XOR and NOT

• TEST Instruction• Shift and Rotate Instructions• Stack Operations• Introduction to Procedures

Page 11: Ch. 7 Logic, Shift and Rotate instr. Logic Instructions – AND, OR, XOR and NOT TEST Instruction Shift Instructions -Left shift – doubles a number -Right

7.2 Shift and Rotate Instructions

• Two types of shift and rotate instructions– Logical Shift / Rotate– Arithmetic Shift/Rotate

• Both logical and arithmetic left shift are identical• But right shifts are different

Page 12: Ch. 7 Logic, Shift and Rotate instr. Logic Instructions – AND, OR, XOR and NOT TEST Instruction Shift Instructions -Left shift – doubles a number -Right

opcode dest, 1 ; 1 bit change

opcode dest, CL ; N bits change; CL contians N

- Dest reg., mem- SHL Multiplication by left shift by multiplies of 2- SAL for numeric multiplication

Page 13: Ch. 7 Logic, Shift and Rotate instr. Logic Instructions – AND, OR, XOR and NOT TEST Instruction Shift Instructions -Left shift – doubles a number -Right

Shift and Rotate Instructions

SHL DH, 3 ; DH = 1110 1111

DH = 0111 1000 C = 1

SAL DH, 2 ; DH = 1110 1111

DH = 1011 1100 C = 1

SHR DH, 3 ; DH = 1110 1111

DH = 0001 1101 C = 1

SAR DH, 2 ; DH = 1110 1111

DH = 1111 1011 C = 1

Page 14: Ch. 7 Logic, Shift and Rotate instr. Logic Instructions – AND, OR, XOR and NOT TEST Instruction Shift Instructions -Left shift – doubles a number -Right

Rotate Instructions

Let DH = 8Ah = 1000 1010CF = 1

After first RCRDH = 1100 0101 CF = 0

After second RCRDH = 0110 0010 CF = 1

ROL – rotate leftROR – r rightRCL – r carry leftRCR – r c right

Page 15: Ch. 7 Logic, Shift and Rotate instr. Logic Instructions – AND, OR, XOR and NOT TEST Instruction Shift Instructions -Left shift – doubles a number -Right

Agenda

• Logic Instructions– AND, OR, XOR and NOT

• TEST Instruction• Shift and Rotate Instructions• Stack Operations

Page 16: Ch. 7 Logic, Shift and Rotate instr. Logic Instructions – AND, OR, XOR and NOT TEST Instruction Shift Instructions -Left shift – doubles a number -Right

Stack vs. Queue

• Stack– LIFO : Last In First Out

• Queue– FIFO : First In First Out

StackQueue

Page 17: Ch. 7 Logic, Shift and Rotate instr. Logic Instructions – AND, OR, XOR and NOT TEST Instruction Shift Instructions -Left shift – doubles a number -Right

PUSH vs. POP in Stack

Page 18: Ch. 7 Logic, Shift and Rotate instr. Logic Instructions – AND, OR, XOR and NOT TEST Instruction Shift Instructions -Left shift – doubles a number -Right

PUSH Instructions

Page 19: Ch. 7 Logic, Shift and Rotate instr. Logic Instructions – AND, OR, XOR and NOT TEST Instruction Shift Instructions -Left shift – doubles a number -Right

POP Instructions

Page 20: Ch. 7 Logic, Shift and Rotate instr. Logic Instructions – AND, OR, XOR and NOT TEST Instruction Shift Instructions -Left shift – doubles a number -Right

References

• Some materials are from Dr. Sazzad, NSU• Ch 7, Assembly Language Programming – by

Charls Marut• Ch 4, Intel Microprocessors – by Brey