Instruction set:
- Data Transfer
Instructions.
- Arithmetic Instructions.
- Logical instructions.
Shift instructons
Not Allowed instructions
• MOV X,Y
• MOV X,[BX]
• MOV Y, ARRAY[SI]
• MOV Y,[200]
• MOV AX,@DATA
• MOV DS,AX
• MOV BX,DS
• MOV CS,BX
MOV BX, OFFSET VALW
• MOV AL,3
• MOV BL,5
• MUL bl
• A= 41H 0100 0001
• 00100000 or
• a= 61h 0110 0001
• 1101 1111 AND
• Xyz db ‘+===* Alquds-5-University-2025’
Shift Instructions
• Used to position or move numbers to the left or to the right within a
register or a memory location
• They also perform simple arithmetic (multiply or divide by 2n).
• Shift up to 8 bits in a byte, 16 bits in a word, 32 bits in a double word
(386 and later)
• Shift Logically (unsigned) or arithmetically(signed data).
27
Shift Instructions
[label:] shift register/memory,CL/immediate
• 1st operand is data to be shifted.
• 2nd operand is the number of shifts
• register: can be any register except segment
register
• for 8086, immediate value must be 1.
• Use CL if need to shift by more than one bit.
• for later processors, immediate value can be any
positive integer up to 31.
28
Shift Instructions
• Shift right
• SHR: Logical shift right
• SAR: Arithmetic shift right
• Shift left
• SHL: Logical shift left
• SAL: Arithmetic shift left
29
Shift Right
SHR: 0 C
SAR: S C
30
SHR
Instruction Binary Decimal CF
MOV AL,10110011B 1011 0011 179 -
SHR AL,01 0101 1001 89 1
MOV CL,02
SHR AL,CL 0001 0110 22 0
(80286+)
SHR AL,02
31
SAR
Instruction Binary Decimal CF
MOV AL,10110011B 1011 0011 -77 -
SAR AL,01 1101 1001 -39 1
MOV CL,02
SAR AL,CL 1111 0110 -10 0
(80286+)
SAR AL,02
32
SHR and SAR
• Right shifts are especially useful for halving values: i.e. integer
division by 2
• Right shift by 2 bits => divide by 4
• Right shift by 3 bits => divide by 8 etc.
• SHR: for unsigned numbers
• SAR: for Signed numbers
• Much faster than the divide instruction.
• Shifting by 1 bit, the remainder is in CF.
33
Shift Left
C 0
SHL & SLA:
34
• X = 00001000
• Shl x,1 ; 00010000 16
• Mov y,-1
• Sal y,1
• y= 11111111
• 11111110 * 2
• 11111100
SHL
Instruction Binary Decimal CF
MOV AL,00001101B 0000 1101 13 -
SHL AL,01 0001 1010 26 0
MOV CL,02
SHL AL,CL 0110 1000 104 0
(80286+)
SHL AL,02 1010 0000 160 1
36
SAL
Instruction Binary Decimal CF
MOV AL,11110110B 1111 0110 -10 -
SAL AL,01 1110 1100 -20 1
MOV CL,02
SAL AL,CL 1011 0000 -80 1
(80286+)
SAL AL,02 1100 0000 -64 0
37
• F = X * 20
F= X*16 + X*4
MOV AL,X
SHL AL,4
SHL X,2
ADD X,AL
SHL and SAL
• SHL and SAL are identical
• SHL for unsigned and SAL for signed
• can be used to double numbers.
• Each bit shift to the left , double the value
• shifting left by 2 bits = multiply by 4 etc.
• Note: if after a left shift CF=1
• size of the register/memory location is not large enough for the result.
39
Example
A code segment that will multiply AX by 10
Assume the number N is the content of AX
SHL AX, 1 ; AX = 2*N
MOV BX, AX ; save in BX
SHL AX, 2 ; AX = 8*N
ADD AX, BX ; AX = 2*N+8*N
40
Rotate Instructions
• Rotate binary data in a memory location or a register either from one
end to the other, or through the CF.
• Used mostly to
• inspect specific bits
• shift numbers that are wider that register size (I.e. wider that 16 bits in
8086/286).
41
Rotate Instructions
[label:] rotate register/memory,CL/immediate
• Rotate Right
• ROR
• RCR
• Rotate Left
• ROL
• RCL
42
Rotate Right
ROR: C
RCR: C
43
Rotate Left
C
ROL:
C
RCL:
44
Example
Instruction Binary CF
MOV BL,10110100B 1011 0100 -
ROR BL,01 0101 1010 0
MOV CL,02
ROR BL,CL 1001 0110 1
MOV BL,10110100B 1011 0100 1
RCR BL,01 1101 1010 0
MOV CL,02
RCR BL,CL 0011 0110 1
45
Example
• Rotate instructions are often used to shift wide numbers to the left or
right.
Example: Assume a 48-bit number is stored in registers DX, BX, AX
write a code segment to shift number to the left by one position:
SHL AX,1
RCL BX,1
RCL DX,1
46