170 likes | 319 Views
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.
E N D
CHAPTER 4LAB 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 • 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.
1.1 AND, OR and XOR Inst • The AND, OR and XOR instructions perform the named logic operations. The formats are AND dest, source OR dest, source XOR 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.
Effect on flags • Effect on flags: • SF, ZF, PF reflect the result • AF is undefined • CF, OF = 0
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 = b b AND 0= 0 b OR 1 = 1 b XOR 1 = b
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 setspecific 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 complementspecific dest bits while preserving the others. A 1 mask bit complement the corresponding dest bit. A 0 mask bit preserve the corresponding dest bit
example • Clear the sign bit (MSB) of AL while leaving the other bits unchanged. • Set the most significant and least significant bits of AL while preserving the other bits. • Change the sign bit of DX
1.2 NOT instruction • The NOT instruction performs the one’s complement operation on the destination. The format is NOT destination • There is no effect on the status flags.
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 is Opcodedest, 1 • For a shift and rotate of N positions, the form is Opcodedest, CL • Where CL contain N. In both cases, dest is an 8- or 16- bit register or memory location.
2.1 Left Shift (SHL) instruction • Shift the bits in the destinantion to the left. The format for single shift is SHL 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
2.1 Right Shift (SHR) instruction • 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?
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
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 And ROL dest, CL
Cont’.. • Rotate Right (ROR) • the bits rotated to the right • The rightmost bit is shifted into the msb and also the CF ROR dest, 1 And ROR 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.
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 And RCL dest, CL
Cont’.. • Rotate Carry Right (RCR) • Rotated the bits to the right. RCL dest, 1 And RCL 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?