Shift and Rotate Instructions Sahar Mosleh California State

  • Slides: 19
Download presentation
Shift and Rotate Instructions Sahar Mosleh California State University San Marcos Page 1

Shift and Rotate Instructions Sahar Mosleh California State University San Marcos Page 1

 • Along with the bitwise instructions, from the previous lectures, shift instructions are

• Along with the bitwise instructions, from the previous lectures, shift instructions are among the most characteristic of assembly language. • Shift means to move bits right and left inside an operand. • All the following instructions affect the overflow and the carry flag. SHL SHR SAL SAR ROL ROR RCL RCR SHLD SHRD Sahar Mosleh Shift left Shift right Shift arithmetic left Shift arithmetic right Rotate left Rotate right Rotate carry left Rotate carry right double precision shift left double precision shift right California State University San Marcos Page 2

Logical Shift vs Arithmetic Shift • There are two basic ways to shift the

Logical Shift vs Arithmetic Shift • There are two basic ways to shift the bits in a number. • The first called a logical shift, fills the newly created bit position with Zero. • In the following diagram, a byte is logically shifted one position to the right. • Note that bit 7 is assigned 0 0 CF • For example, if we do a single logical right shift on the binary value 11001111, it becomes 01100111 Sahar Mosleh California State University San Marcos Page 3

 • The other type of shift is called an arithmetic shift. • The

• The other type of shift is called an arithmetic shift. • The newly created bit position is filled with a copy of the original numbers sign bit. CF • For example, the binary value 11001111, has a 1 in the sign bit. • When shifted, arithmetically 1 bit to the right, it becomes 11100111. Sahar Mosleh California State University San Marcos Page 4

SHL Instruction • The SHL instruction performs a logical left shift on the destination

SHL Instruction • The SHL instruction performs a logical left shift on the destination operand, filling the lowest bit with 0. • The highest bit is moved to the carry flag, and the bit that was in the carry flag is lost. CF • The first operand is the destination and the second is shift count. SHL destination, count • The following lists the types of operands permitted by this instruction SHL reg, imm 8 SHL mem, imm 8 Sahar Mosleh California State University San Marcos Page 5

Example • In the following instructions, BL is shifted once to the left. The

Example • In the following instructions, BL is shifted once to the left. The highest bit is copied into the carry flag and the lowest bit position is cleared. mov bl, 8 FH Shl b 1, 1 Sahar Mosleh ; BL = 10001111 b ; BL = 00011110 b, CF = 1 California State University San Marcos Page 6

Fast Multiplication • One of the best uses of SHL is for performing high-speed

Fast Multiplication • One of the best uses of SHL is for performing high-speed multiplication by powers of 2. • Shifting any operand by n bits multiplies the operand by 2^n. • For example, shifting 5 left 1 bit yields the product 5*2 mov shl Before After dl, 5 d 1, 1 00000101 = 5 00001010 = 10 • If we shift 10 by 2 bits, the result is the same as multiplying 10 by 2^2. mov dl, 10 SHL d 1, 2 ; ( 10 * 4) = 40 Sahar Mosleh California State University San Marcos Page 7

SHR Instruction • The SHR instruction performs a logical right shift on the destination

SHR Instruction • The SHR instruction performs a logical right shift on the destination operand, filling the highest bit with 0. • The lowest bit is moved to the carry flag, and the bit that was in the carry flag is lost. 0 CF • SHR uses the same instruction format as SHL. • In the following example, the 0 from the lowest bit in AL is copied into the carry flag, and the highest bit in AL is cleared. mov shr Sahar Mosleh al, 0 D 0 h al, 1 ; AL = 11010000 b ; AL = 01101000 b California State University San Marcos Page 8

Fast Division • Logically shifting any unsigned operand right by n bits divides the

Fast Division • Logically shifting any unsigned operand right by n bits divides the operand by 2^n. • For example, we divide 32 by 2^1, producing 16 mov dl, 32 shr dl, 1 Before 00100000 = 32 After 00010000 = 16 • In the following example, 64 is divided by 2^3. mov shr al, 01000000 b ; AL = 64 al, 3 ; divide by 8, al = 00001000 b • Division of signed numbers by shifting is accomplished using SAR instruction because it preserves the number’s sign bit. Sahar Mosleh California State University San Marcos Page 9

SAL and SAR instructions • SAL (Shift Arithmetic Left) is identical to SHL instruction.

SAL and SAR instructions • SAL (Shift Arithmetic Left) is identical to SHL instruction. • The SAR (Shift Arithmetic Right) instruction performs a right arithmetic shift on its destination operand. CF • The syntax and operands for SHR and SAR are identical to those for SHL and SHR. • The shift may be repeated, based on the counter in the second operand. SAR destination, count Sahar Mosleh California State University San Marcos Page 10

Example • The following example, shows how SAR duplicates the sign bit al is

Example • The following example, shows how SAR duplicates the sign bit al is negative before and after it is shifted to the right. mov sar al, 0 F 0 h al, 1 ; al = 11110000 b (-16) ; al = 11111000 b (-8) CF = 0 • Signed Division: You can divide a signed operand by a power of 2 using the SAR instruction. • In the following example, -128 is divided by 2^3. The quotient is -16 mov sar Sahar Mosleh dl, -128 dl, 3 ; dl = 10000000 b ; dl = 11110000 b California State University San Marcos Page 11

ROL Instruction • The ROL (rotate left instruction) shifts each bit to the left.

ROL Instruction • The ROL (rotate left instruction) shifts each bit to the left. • Also, the highest bit is copied both into the carry flag and into the lowest bit. • The instruction format is the same as for the SHL instruction CF • Bit rotation differs from bit shifting in that the former does not lose any bits. • A bit that is rotated of one end of a number appears again at the other end. Sahar Mosleh California State University San Marcos Page 12

 • In the following example, the high bit is copied into both the

• In the following example, the high bit is copied into both the carry flag and bit position zero mov rol rol al, 40 h al, 1 ; AL = 01000000 b ; AL = 10000000 b ; CF = 0 ; AL = 00000001 b; CF = 1 ; AL = 00000010 b; CF = 0 • You can use ROL to exchange the upper (4 -7 bit) and lower (bits 0 -3) halves of the byte. mov rol Sahar Mosleh al, 26 h al, 4 ; AL = 62 h California State University San Marcos Page 13

ROR Instruction • The ROR instruction shifts each bit to the right. Also the

ROR Instruction • The ROR instruction shifts each bit to the right. Also the lowest bit is copied into the flag and into the highest bit at the same time • The instruction format is the same as for SHL CF • In the following example, the lowest bit is copied into the carry flag and into the highest bit of the result mov ror Sahar Mosleh al, 01 h al, 1 ; AL = 00000001 b ; AL = 10000000 b, CF = 1 ; AL = 01000000 b, CF = 0 California State University San Marcos Page 14

RCL and RCR instructions • The RCL (Rotate Carry left) instruction shifts each bit

RCL and RCR instructions • The RCL (Rotate Carry left) instruction shifts each bit to the left copies the carry flag to least significant bit (LSB), and copies the most significant bit (MSB) into the carry flag. CF • If you think of the carry flag as just an extra bit added to the high end of the number, then RCL becomes a simple rotate left operation. Sahar Mosleh California State University San Marcos Page 15

 • In the following example, the CLC instruction clears the carry flag. •

• In the following example, the CLC instruction clears the carry flag. • The first RCL instruction moves the high bit of bl into the carry flag and shifts all other bits to the left • The second RCL instruction moves the carry flag into the lowest bit position CLC mov rcl Sahar Mosleh bl, 88 h bl, 1 b 1, 1 ; CF = 0 ; CF, BL = 0 1000 b ; CF, BL = 1 00010000 b ; CF, BL = 0 00100001 b California State University San Marcos Page 16

Recover a bit from the carry flag • RCL can recover a bit that

Recover a bit from the carry flag • RCL can recover a bit that has previously been shifted into the carry flag. • The following example, checks the lowest bit of testval by shifting its lowest bit into the carry flag. • Then RCL restores the number to its original value. . data testval byte 01101010 b . code shr testval, 1 jc quit rcl testval, 1 Sahar Mosleh ; shift LSB into the carry flag ; exit if carry flag set ; else restore the number California State University San Marcos Page 17

RCR Instruction • The RCR (Rotate Carry Right) instruction shifts each bit to the

RCR Instruction • The RCR (Rotate Carry Right) instruction shifts each bit to the right, copies the right flag into the most significant bit, and copies the least significant bit into the carry flag. CF • As in the case of RCL, it helps to visualize the integer in this figure as a 9 -bit value, with the carry flag to the right of the least significant bit Sahar Mosleh California State University San Marcos Page 18

 • In the following example, STC sets the carry flag before rotating the

• In the following example, STC sets the carry flag before rotating the carry flag into the MSB, and rotating the LSB into the carry flag. STC mov rcr Sahar Mosleh ah, 10 h ah, 1 ; CF = 1 ; CF, Ah = 00010000 1 ; CF, Ah = 1000 0 California State University San Marcos Page 19