Sei sulla pagina 1di 7

Shift, Rotate and XLAT Instructions

Submitted To
Sir Ikram Ul Haq

Submitted By
Khush Bakht (BCS-F16-06)
Zoya Islam (BCS-F16-20)
Ayesha Maqsood (BCS-F16-40)
Hira Hanif (BCS-F16-12)

Submitted Date
13th September, 2018

PUNJAB UNIVERSITY JHELUM CAMPUS

i
Contents
1. Shift and rotate instructions ........................................................................................ 1

Shift instructions ............................................................................................................. 1

Shl (shift logical left) ...................................................................................................... 1

SHR(Shift Logical Right) ............................................................................................... 2

SAR(Shift Arithmetic Right) .......................................................................................... 2

SHLD(Double Precision Shift Left) ............................................................................... 2

SHRD .............................................................................................................................. 3

Table 1: Shift Instructions .......................................................................................... 3

Rotate Left (rol) .............................................................................................................. 3

Rotate With Carry Left (rcl) ........................................................................................... 4

Rotate Right (ror) ............................................................................................................ 4

Rotate through carry right(RCR) .................................................................................... 4

2. The XLAT (transLATe) instruction ........................................................................... 4

3. References....................................................................................................................... 5

ii
1. Shift and rotate instructions
The Shift and Rotate Instructions can be a useful for creating/incrementing sequences and
the like. This instruction allows you to shift a bit pattern within a word. Shift and rotate
instructions move bit strings (or operand treated as a bit string). Shift instructions move a
bit string (or operand treated as a bit string) to the right or left, with excess bits discarded
(although one or more bits might be preserved in flags). In arithmetic shift left or logical
shift left zeros are shifted into the low-order bit. In arithmetic shift right the sign bit (most
significant bit) is shifted into the high-order bit. In logical shift right zeros are shifted into
the high-order bit. Rotate instructions are similar to shift instructions, except that rotate
instructions are circular, with the bits shifted out one end returning on the other end.
Rotates can be to the left or right. Rotates can also employ an extend bit for multi-precision
rotates. A swap instruction swaps the high and low order portions of a register or contents
of a series of memory locations. The carry bit typically receives the last bit shifted out of
the operand. Sometimes an extend bit will receive the last bit shifted out also. Sometimes
an overflow bit is used to indicate a sign change has occurred..
Some of them are as follows
 SHL
 ROL
 SHR
 ROR
 SHLD
 RCL
 SAR
 RCR
Shift instructions
Shift instruction move a bit string from Right to Left with access bit discarded (although
one or more bit might preserved in flag). In Arithmetic shift left or logical shift left zeros
are shifted into low-order bit. In Arithmetic shift right, the sign bit is shifted into high-
order bit.
Shl (shift logical left)
Intel 80x86 shifts contents of data register or memory location to left(towards least
significant bit) by a specified amount specified by immediate operands or by 0-31 bits

1
contents specified by Cl register with high-order bit being shift into carry flag and zeros
shifted into low-order bit or set or clear flag
SHR(Shift Logical Right)
Intel 80x86 shifts contents of data register or memory location to right(towards least
significant bit) by a specified amount specified by contents of Cl register with low-order
bit being shift into carry flag and zeros shifted into high-order bit or set or clear flag
SAR(Shift Arithmetic Right)
The SAR and SHR instructions can be used to perform signed or unsigned division,
respectively, of the destination operand by powers of 2. For example, using the SAR
instruction to shift a signed integer 1 bit to the right divides the value by 2. Using the SAR
instruction to perform a division operation does not produce the same result as the IDIV
instruction. The quotient from the IDIV instruction is rounded toward zero, whereas the
"quotient" of the SAR instruction is rounded toward negative infinity. This difference is
apparent only for negative numbers. For example, when the IDIV instruction is used to
divide -9 by 4, the result is -2 with a remainder of -1. If the SAR instruction is used to shift
-9 right by two bits, the result is -3 and the "remainder" is +3; however, the SAR instruction
stores only the most significant bit of the remainder (in the CF flag). The OF flag is
affected only on 1-bit shifts. For left shifts, the OF flag is set to 0 if the most significant
bit of the result is the same as the CF flag (that is, the top two bits of the original operand
were the same); otherwise, it is set to 1. For the SAR instruction, the OF flag is cleared for
all 1-bit shifts. For the SHR instruction, the OF flag is set to the most-significant bit of the
original operand.
SHLD(Double Precision Shift Left)
Intel 80x86 shift the contents of general-purpose key to left towards most significant bit
by a specified amount 0-31 bits specified by immediate operands or by 0-31 bits specified
by contents of CL register with high order bit being shifted into general-purpose key or
memory location use to implement multi-precision shifts or insert set or clear flag.
Flags Affected ¶ If the count is 1 or greater, the CF flag is filled with the last bit shifted
out of the destination operand and the SF, ZF, and PF flags are set according to the value
of the result. For a 1-bit shift, the OF flag is set if a sign change occurred; otherwise, it is
cleared. For shifts greater than 1 bit, the OF flag is undefined. If a shift occurs, the AF flag
is undefined. If the count operand is 0, the flags are not affected. If the count is greater
than the operand size, the flags are undefined.

2
SHRD
The SHRD instruction is useful for multi-precision shifts of 64 bits or more. The
instruction shifts the first operand (destination operand) to the right the number of bits
specified by the third operand (count operand). The second operand (source operand)
provides bits to shift in from the left (starting with the most significant bit of the destination
operand). The destination operand can be a register or a memory location; the source
operand is a register. The count operand is an unsigned integer that can be stored in an
immediate byte or the CL register. If the count operand is CL, the shift count is the logical
AND of CL and a count mask. In non-64-bit modes and default 64-bit mode, the width of
the count mask is 5 bits. Only bits 0 through 4 of the count register are used (masking the
count to a value between 0 and 31). If the count is greater than the operand size, the result
is undefined.

Table 1: Shift Instructions


Shifts (rotates) the bits of the first operand (destination operand) the number of bit
positions specified in the second operand (count operand) and stores the result in the
destination operand.
Rotate Left (rol)
The left rotate instruction shifts all bits in the register or memory operand specified. The
most significant bit is rotated to the carry flag, the carry flag is rotated to the least
significant bit position, and all other bits are shifted to the left. The result does not include
the original value of the carry flag.

3
The first operand value indicates how many times the rotate takes place. The value is either
the contents of the CL register or an immediate number. For a single rotate, where the first
operand is one, the overflow flag (OF) is defined. For all other cases, OF is undefined.
After the shift, the carry flag bit is XORed with the most significant result bit.
Rotate With Carry Left (rcl)
The left rotate instruction shifts all bits in the register or memory operand specified. The
carry flag (CF) is included in the rotation. The most significant bit is rotated to the carry
flag, the carry flag is rotated to the least significant bit position, and all other bits are shifted
to the left. The result includes the original value of the carry flag.
The first operand value indicates how many times the rotate takes place. The value is either
the contents of the CL register or an immediate number. For a single rotate, where the first
operand is one, the overflow flag (OF) is defined. For all other cases, OF is undefined.
After the shift, the carry flag bit is XORed with the most significant result bit.
Rotate Right (ror)
The rotate right (ROR) and rotate through carry right (RCR) instructions shift all the bits
toward less significant bit positions, except for the least-significant bit, which is rotated to
the most-significant bit location
Rotate through carry right(RCR)
The right rotate instruction shifts all bits in the register or memory operand specified. The
least significant bit is rotated to the carry flag, the carry flag is rotated to the most
significant bit position, and all other bits are shifted to the right. The result does not include
the original value of the carry flag. The first operand value indicates how many times the
rotate takes place. The value is either the contents of the CL register or an immediate
number. For a single rotate, where the first operand is one, the overflow flag (OF) is
defined. For all other cases, OF is undefined. After the shift, the carry flag bit is XORed
with the two most significant result bits.
2. The XLAT (transLATe) instruction
The syntax of the XLAT instruction is XLAT or XLAT TranslationTableName where
TranslationTable is an unsigned byte array of maximum size 256. The instruction requires
BX to be initialized to have the offset of the translation table:
MOV BX, OFFSET TranslationTableName

4
The contents of the byte that is AL bytes from the start of the translation table pointed to
by DS: BX is copied into AL, i.e., the effect of XLAT is equivalent to the invalid statement:
MOV AL, [BX + AL]
Example: Suppose a byte variable VAR1 has a value in the range 0 to 15. Display this
value as a hexadecimal digit.
.DATA
HEXDIGIT DB ‘0123456789ABCDEF’
VAR1 DB ?
. . .
MOV AX , @DATA
MOV DS , AX
. . .
MOV AL , VAR1
MOV BX , OFFSET HEXDIGIT
XLAT
MOV DL , AL
MOV AH , 02H
INT 21H
. . .
3. References
[1] Source[online], http://whatis.techtarget.com/definition/xlate_instructions
[2] http://www.google.com.pk/webhp?sourceid=chromeinstant&ion=1&espv=2&ie
=UTF-8#q=shift+instruction
[3] Source[online], http://www.shsu.edu/~csc_tjm/fall2002/cs272/shift.html
[4] Source[online], http://www.osdata.com/topic/language/asm/shiftrot.htm
[5] Source[online], http://cs.uwindsor.ca/~angom/teaching/cs266/Lec26605.pdf
[6] https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Overall/steps.html

Potrebbero piacerti anche