CHAPTER 4 LAB

17
CHAPTER 4 LAB Logic, Shift and Rotate Instruction

description

CHAPTER 4 LAB. Logic, Shift and Rotate Instruction. Overview. In this topic, we discuss the instruction that can be used to change the bit pattern in a byte or word. The ability to manipulate bits is generally absent in high-level language (except C). 1.LOGIC instruction. - PowerPoint PPT Presentation

Transcript of CHAPTER 4 LAB

Page 1: CHAPTER  4 LAB

CHAPTER 4LAB

Logic, Shift and Rotate Instruction

Page 2: CHAPTER  4 LAB

Overview

• In this topic, we discuss the instruction that can be used to change the bit pattern in a byte or word.

• The ability to manipulate bits is generally absent in high-level language (except C).

Page 3: CHAPTER  4 LAB

1.LOGIC instruction

• We can change individual bits in the computer by using logic operations.

• The binary values, 0= false and 1 = true.• When logic operation is applied to 8- or 16-bit

operands, the result is obtained by applying the logic operation at each bit position.

Page 4: CHAPTER  4 LAB

1.1 AND, OR and XOR Inst• The AND, OR and XOR instructions perform the

named logic operations. The formats areAND dest, sourceOR dest, sourceXOR dest, source

• The result is stored in the destination, which must be a register or memory location.

• The source may be a constant, a register or memory location.

• Memory-to-memory operation is not allowed.

Page 5: CHAPTER  4 LAB

Effect on flags

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

Page 6: CHAPTER  4 LAB

Use of the logic Instruction

• To selectively modify the bits in the destination. – Construct a source bit pattern known as a mask.

The mask bit are chosen so that the corresponding dest bits are modified in the desired manner when instruction is executed.

• To choose the mask bits, use the following properties (b represents a bit – ‘0’ or ‘1’):b AND 1 = b b OR 0 = b b XOR 0 = bb AND 0= 0 b OR 1 = 1 b XOR 1 = b

Page 7: CHAPTER  4 LAB

Cont’..1. The AND instruction can be used to clear specific

dest bits while preserving the others. A 0 mask bit clear the corresponding dest bit; a 1 mask bit preserve the corresponding dest bit.

2. The OR instruction can be used to set specific dest bits while preserving the others. A 1 mask bit sets the corresponding dest bit. A 0 mask bit preserve the corresponding dest bit

3. The XOR instruction can be used to complement specific dest bits while preserving the others. A 1 mask bit complement the corresponding dest bit. A 0 mask bit preserve the corresponding dest bit

Page 8: CHAPTER  4 LAB

example

1. Clear the sign bit (MSB) of AL while leaving the other bits unchanged.

2. Set the most significant and least significant bits of AL while preserving the other bits.

3. Change the sign bit of DX

Page 9: CHAPTER  4 LAB

1.2 NOT instruction

• The NOT instruction performs the one’s complement operation on the destination. The format isNOT destination

• There is no effect on the status flags.

Page 10: CHAPTER  4 LAB

2. SHIFT instruction

• The shift and rotate instruction shift the bits in the destination operand by one or more position either to the left or right.

• For SHIFT instruction, the bits shifted out are lost; for a ROTATE instruction, bits shifted out from one end of the operand are put back into the other end.

• For a single shift or rotate, the form isOpcode dest, 1

• For a shift and rotate of N positions, the form is Opcode dest, CL

• Where CL contain N. In both cases, dest is an 8- or 16- bit register or memory location.

Page 11: CHAPTER  4 LAB

2.1 Left Shift (SHL) instruction• Shift the bits in the destinantion to the left. The format for single

shift isSHL dest, 1

• A 0 is shifted into the rightmost bit position and the msb is shifted into CF.

• Effect on flags– SF, PF, ZF reflect the result– AF is undefined– CF = last bit shifted out– OF = 1 if result changes sign on last shift.

• Eg: suppose DH contains 8Ah and CL contain 3. what are the values of DH and of CF after the instruction SHL DH, CL is executed?

• Sol: DH = 10001010. After 3 (CL) left shift, CF will contain 0. The new contents of DH = 01010000b = 50h

Page 12: CHAPTER  4 LAB

• Perform right shift on the destinantion operand. The format for a single shift is

SHL dest, 1

• A 0 is shifted into the msb position and the rightmost bit is shifted into CF.

• The effect on the flags is the same as for SHL• Eg: suppose DH contains 8Ah and CL contain 2.

what are the values of DH and CF after the instruction SHR DH, CL is excuted?

2.1 Right Shift (SHR) instruction

Page 13: CHAPTER  4 LAB

Shift arithmetic right (SAR) operation

• Operate like SHR, with one difference: the msb retains its original value. The syntax is

SAR dest, 1• The effect on the flags is the same as for SHR

Page 14: CHAPTER  4 LAB

3. ROTATE Instruction

• Rotate left (ROL)– Shift bits to the left– The msb is shifted into the rightmost bit– The CF also gets the bit shifted out of the msb– Can think of the destinantion bits forming a ciclre,

with the least significant bit folowing the msb in the circle.

ROL dest, 1 AndROL dest, CL

Page 15: CHAPTER  4 LAB

Cont’..

• Rotate Right (ROR)– the bits rotated to the right– The rightmost bit is shifted into the msb and also the CFROR dest, 1 AndROR dest, CL

• In ROL and ROR, CF reflect the bit that is rotated out.

• Eg: Use ROL to count the number of 1 bits in BX, without changing BX. Put the answer in BX.

Page 16: CHAPTER  4 LAB

Cont’..

• Rotate Carry Left (RCL)– Shift the bits of the destination to the left.– The msb is shifted into CF, and the previous value

of Cf is shifted into the righmost bit. – It works like ROL, except that CF is part of the

circle of bits being rotated.RCL dest, 1 AndRCL dest, CL

Page 17: CHAPTER  4 LAB

Cont’..• Rotate Carry Right (RCR)

– Rotated the bits to the right.RCL dest, 1 AndRCL dest, CL

• Effect of the rotate instruction on the flags:– SF, PF, ZF reflect the result– AF is undefined– CF = last bit shifted out– OF = 1 if result changes sign on last rotation

• Eg: suppose DH contain 8Ah, CF = 1, and CL contains 3. What are the values of DH and CF after the instruction RCR DH, CL is executed?