Logic, shift and rotate instruction

29
Presentation Title Your company information 1 [email protected]

Transcript of Logic, shift and rotate instruction

Page 1: Logic, shift and rotate instruction

Presentation TitleYour company information

[email protected]

Page 2: Logic, shift and rotate instruction

•Logic, Shift, and Rotate Instructions

[email protected]

Page 3: Logic, shift and rotate instruction

presented

by kashif shafqat(6905)

and meer jalal khan

[email protected]

Page 4: Logic, shift and rotate instruction

Logic instruction• To manipulate individual bits

• Binary Value 0 treated as false

• Binary Value 1 treated as true

• AND• OR • XOR• NOT• TEST

[email protected]

Page 5: Logic, shift and rotate instruction

TRUTH TABLE

a b a AND b a OR b a XOR b

0 0 0 0 0

0 1 0 1 1

1 0 0 1 1

1 1 1 1 0

a NOT a

0 1

1 0

[email protected]

Page 6: Logic, shift and rotate instruction

examples

• AND

1010 1010

1111 0000

1010 0000

• OR

1010 1010

1111 0000

1111 10106

[email protected]

Page 7: Logic, shift and rotate instruction

• XOR

1010 1010

1111 0000

0101 1010

• NOT

1010 1010

0101 0101

[email protected]

Page 8: Logic, shift and rotate instruction

SYNTAXAND destination,source

OR destination,source

XOR destination,source

• Destination:• Store result• Can be a Register or Memory Location

• Source:• May be a Constant,Register or Memory Location

• Memory to memory operation not allowed

[email protected]

Page 9: Logic, shift and rotate instruction

Effects on flags

• SF,ZF,PF reflects the result• AF is undefined• CF,OF = 0

[email protected]

Page 10: Logic, shift and rotate instruction

MASK• To modify only selective bits in destination, we construct

a source bit pattern known as MASK• T o choose mask , use following properties:

• b AND 1 = b• b AND 0 = 0• b OR 1 = 1• b OR 0 = b• b XOR 0 = b• b AND 1 = ~b ( complement of b)

• Where b represents a bit (0 or 1)

[email protected]

Page 11: Logic, shift and rotate instruction

Contd…1. The AND Instruction:

– May be used to clear specific destination bits while preventing the others.

– A 0 mask bit clears the corresponding destination bit.

– A 1 mask bit preserves the corresponding destination bit.

[email protected]

Page 12: Logic, shift and rotate instruction

Example:

• Clear the sign bit of AL while leaving the other bits unchanged.

Solution:

AND AL,7Fh

Where 7Fh (0111 1111) is the mask.

[email protected]

Page 13: Logic, shift and rotate instruction

Contd… The OR Instruction:

May be used to SET specific destination bits while preventing the others.

– A 1 mask bit sets the corresponding destination bit.

– A 0 mask bit preserves the corresponding destination bit.

[email protected]

Page 14: Logic, shift and rotate instruction

Example:• Set the MSB and LSB of AL while

preserving the other bits.

• Solution:

OR AL,81h

Where 81h (1000 0001) is the mask.

[email protected]

Page 15: Logic, shift and rotate instruction

Contd… The XOR Instruction:

– May be used to Complement specific destination bits while preventing the others.

– A 1 mask bit complements the corresponding destination bit.

– A 0 mask bit preserves the corresponding destination bit.

[email protected]

Page 16: Logic, shift and rotate instruction

example• Change the sign bit of DX.

• Solution:

XOR DX,8000h

Where 80h ( 1000 0000 ) is the mask.

[email protected]

Page 17: Logic, shift and rotate instruction

Clearing a register: MOV AX,0 ;machine code 3 bytes

OR

SUB AX,AX ;machine code 2 bytes

OR

XOR AX,AX ;machine code 2 bytes

[email protected]

Page 18: Logic, shift and rotate instruction

TESTING A REGISTER FOR ZERO:•

CMP CX,0

Is same like :

OR CX,CX

;Sets ZF=1 if CX is 0

[email protected]

Page 19: Logic, shift and rotate instruction

Not instruction• Performs the one’s complement operation

on the destination.

• Syntax:

• NOT destination

• No effects on flags

• Example: Complement the bit in AX:

NOT AX

[email protected]

Page 20: Logic, shift and rotate instruction

SHIFT AND ROTATE INSTRUCTION:

• Shift the bits in destination operand by one or more positions either to the left or right.

• Shift: Bit shifted out is lost

• ROTATE: Bit shifted out from one end of the destination operand is put back on the other end.

• Syntax:• OPCODE destination,1

• OPCODE destination,[email protected]

Page 21: Logic, shift and rotate instruction

SHIFT INSTRUCTION:

SHL Instruction (left shift) SHR Instruction (right shift) SAL Instruction (shift Arithmetic left) SAR Instruction (shift Arithmetic right) ROL Instruction (shift left) ROR Instruction (shift right) RCL Instruction (shift Carry left) RCR Instruction (shift Carry Right)

[email protected]

Page 22: Logic, shift and rotate instruction

SHL Instruction (left shift)• The SHL (shift left) instruction performs a

logical left shift on the destination operand, filling the lowest bit with 0.

• Operand types:

SHL reg,imm8SHL mem,imm8SHL reg,CLSHL mem,CL

[email protected]

Page 23: Logic, shift and rotate instruction

SHR Instruction (right shift)

• The SHR (shift right) instruction performs a logical right shift on the destination operand. The highest bit position is filled with a zero.

mov dl,80shr dl,1 ; DL = 40shr dl,2 ; DL = 10

[email protected]

Page 24: Logic, shift and rotate instruction

SAL and SAR Instructions• SAL (shift arithmetic left) is identical to SHL.

• SAR (shift arithmetic right) performs a right arithmetic shift on the destination operand.

mov dl,-80sar dl,1 ; DL = -40sar dl,2 ; DL = -10

[email protected]

Page 25: Logic, shift and rotate instruction

ROL Instruction (shift left)• ROL (rotate) shifts each bit to the left• The highest bit is copied into both the

Carry flag and into the lowest bit• No bits are lost

mov al,11110000brol al,1 ; AL = 11100001b

mov dl,3Fhrol dl,4 ; DL = F3h

[email protected]

Page 26: Logic, shift and rotate instruction

ROR Instruction (shift right)• ROR (rotate right) shifts each bit to the right

• The lowest bit is copied into both the Carry flag and into the highest bit

• No bits are lost

mov al,11110000bror al,1 ; AL = 01111000b

mov dl,3Fhror dl,4 ; DL = F3h

[email protected]

Page 27: Logic, shift and rotate instruction

RCL Instruction (shift Carry left)• RCL (rotate carry left) shifts each bit to the left• Copies the Carry flag to the least significant bit• Copies the most significant bit to the Carry flag

clc ; CF = 0mov bl,88h ; CF,BL = 0 10001000brcl bl,1 ; CF,BL = 1 00010000brcl bl,1 ; CF,BL = 0 00100001b

[email protected]

Page 28: Logic, shift and rotate instruction

RCR Instruction (shift Carry Right)• RCR (rotate carry right) shifts each bit to the right• Copies the Carry flag to the most significant bit• Copies the least significant bit to the Carry flag

stc ; CF = 1mov ah,10h ; CF,AH = 00010000 1rcr ah,1 ; CF,AH = 10001000 0

[email protected]