Featured

    Featured Posts

Discuss shift and rotate instructions in detail.



Discuss shift and rotate instructions in detail.


     Shift Instructions :  Shift instructions position or move binary data to the left or right by shifting them within the register or memory location. They also perform multiplication by powers or 2-n(2 power -n) (left shift) and division by power of 2n (right shift). The shift operations can be classified as logical left shift (SHL), and a o into the leftmost bit position for a logical right shift (SHR). The arithmetic left shift (SAL) and logical left shift operations are identical. However, arithmetic and logical right shifts are different because the arithmetic right shift (SAR) copies the sign bit through the number, while the logical right shift copies a 0 through the number.
The following are main shift instructions :

1. SHL/SAL : Shift logical/arithmetic left byte or word.



Mnemonic
SAL/SHL destination, count
All flags are affected
Algorithm
Shift all bits left, the bit that goes off is set to CF. Zero bit is inserted in the right most position.

Addressing Mode
Immediate addressing Mode

Operation
CF <- MSB <- LSB <- 0


  1. SAL/SHL are two mnemonics for the same instructions. This instruction shifts each bit of the specifies destination, some number or bit positions to the left. As left bit is shifted out of the LSB position, 0 is put in the LSB position. The MSB will be shifted into CF.
  2. The count can be any immediate number.
  3. In case of multiple bit shifts, CF will contain the bit most recently shifted in from the MSB.
  4. Bits shifted into CF previously will be lost.
  5. This instruction can be used to multiply an unsigned binary number by a power of 2.
  6. The destination can be a memory location or register.
  7. The count is specified in the CX register.
  8. Negative shifts are illegal.
Example : SHL AX.01 This instruction shift AX by 1 bit to the left.
2. SHR : Shift logical right byte or word. 


Mnemonic
SHR destination, count
All flags are affected
Algorithm
Shift all bits right, the bit that goes off is set to CF. Zero bit is increased in the right most position.

Addressing Mode
Register addressing mode

Operation
0 -> MSB -> LSB -> CF



  1. This instruction shifts each bit in specified destination some number of bit position to the right  bit shifted from LSB, goes to CF.
  2. For multi bit shift CF will contain bit most recently shifted out from LSB position.
  3. This instruction is used to divide unsigned binary number by power of 2.
  4. Count is any immediate number.
  5. Negative shifts are illegal.
  6. The count is specified in the CL = 02H the CX register.
Example : SHR AX, CL
This instruction shifts each bit in the accumulator by two times to the right.

3. SAR : Shift arithmetic right byte or word

Mnemonic
SAR destination, count
All flags are affected
Algorithm
Shift all bits right, the bit that goes off is set to CF.
The sign bits that is inserted to the leftmost position has the same value as before shift.

Addressing Mode
Immediate addressing mode

Operation
MSB -> MSB -> LSB -> CF




  1. This instruction shifts each bit is specified destination, same number of bit position of the right. Number of bits to be shifted depends upon count.
  2. As bit is shifted out of the MSB position, a copy of old MSB is put in the New MSB position, i.e. the sign bit is copied into the MSB. LSB is moved into CF.
  3. Bits shifted into CF previously will be lost.
  4. The count can be an any immediate number or specified in the CX register.
  5. Negative shift are illegal.
Rotate : Rotate instruction position or move binary data by rotating the information in a register or memory location, either from one end to another or through the carry flag. This is illustrated in figure below.
Rotates Instructions : The following are main rotate instructions :-

1. ROL :  Rotate left byte or word

Mnemonic
ROL destination, source
Only CF and OF are affected.
Algorithm
Shift all bits left, the bit that goes off is set to CF and the same bit is inserted to the rightmost position.

Addressing Mode
Immediate addressing mode

Operation
CF <- MSB <- LSB



  1. This instruction rotates all the bits in a specified word or byte to the left, by some bit positions.
  2. The data bit rotated out of MSB is circled back into the LSB. The data bit rotated out of MSB is also copied to CF.
  3. CL is default register used for rotate instruction when count is greater than 1.
Example : ROL AX . 01 H
This instruction rotates the contents of AX register by 1 bit to left.
Rotate Operations

2. ROR : Rotate right byte or word.


Mnemonic
ROR destination, count

Algorithm
Shift all bits right, the bit that goes off is set to CF and the same bit is inserted to the leftmost position.

Addressing Mode
Register addressing mode

Operation
CF MSB -> LSB



  1. This instruction rotates all the bits of specified destination operand to the right. The bit moved out of the LSB is rotated around into the MSB.
  2. The data bit moved out of LSB is also copied into CF.
  3. The destination may be memory location or register.
  4. The count if greater than 1 should be specified in the CX register.
  5. Negative shifts are illegal.
3. RCL : Rotate through carry left byte or word

Mnemonic
RCL destination, count
Only CF and OF are affected.
Algorithm
Shift all bits left, the bit that goes off is set to CF and previous value of CF is inserted to the right most position.

Addressing Mode
Immediate addressing mode

Operation
CF <- MSB <- LSB


  1. This instruction rotates all the bits in specified destination by some number of bit positions to the left. The destination can be register or memory location.
  2. Negative shifts are illegal.
  3. CL is default register used for ratate instruction when count is greater than 1.
4. RCR : Rotate through carry right byte or word.

Mnemonic
RCR destination, count
It affect CF and OF
Algorithm
Shift all bits right, the bit that goes off is set to CF and previous value of CF is inserted to the left most position.

Addressing Mode
Immediate addressing mode

Operation
CF -> MSB -> LSB


  1. This instruction rotates all the bits is specified word or byte same number of bit positions to right. The destination can be register or memory location.
  2. Negative shifts are illegal.
  3. CL is default register used for rotate instruction when count is greater than 1.

Post a Comment

www.CodeNirvana.in

www.posthatke.com. Powered by Blogger.

Google+ Followers

Copyright © www.posthatke.com | Blogger Templates | Designed By posthatke.com