Professional Documents
Culture Documents
Computer Organization and Assembly Language: Shift and Rotate Instructions
Computer Organization and Assembly Language: Shift and Rotate Instructions
Assembly Language
Lecture 7 - Integer Arithmetic
CF
• An arithmetic shift is filled with a copy of the original
number’s sign bit.If we do a single arithmetic right shift on
11001111, it becomes 11100111.
SHL Instruction
• The Shift Left instruction performs a left shift on the
destinations operand, filling the lowest bit with 0. The
highest bit is moved into the Carry Flag.
• The instruction format is:
SHL destination, bits_shifted
• Instruction formats include:
SHL reg, imm8
SHL mem, imm8
SHL reg, CL
SHL mem, C L
SHL Instruction - Examples
SHR Instruction
• The Shift Right instruction performs a right shift on the
destinations operand, filling the lowest bit with 0. The
lowest bit is moved into the Carry Flag.
• The instruction format is:
SHR destination, bits_shifted
• Instruction formats include:
SHR reg, imm8
SHR mem, imm8
SHR reg, CL
SHR mem, C L
SHR Instruction - Examples
ROL Instruction
• The ROL instruction shifts each bit to the left, with the
highest bit copied in the Carry flag and into the lowest bit.
• Instruction formats
include:
ROL reg, imm8
ROL mem, imm8
ROL reg, CL CF
ROL mem, C L
ROL Instruction - Examples
ROR Instruction
• The ROR instruction shifts each bit to the right, with the
lowest bit copied in the Carry flag and into the highest bit.
• Instruction formats
include:
ROR reg, imm8
ROR mem, imm8
ROR reg, CL CF
ROR mem, C L
ROR Instruction - Examples
RCL Instruction
• The RCL (Rotate and Carry Left) instruction shifts each bit
to the left, copies the Carry flag to the least significant bit
and copies the most significant bit into the Carry flag.
• In this examples, the lowest bit is copied into the Carry
flag and into the highest bit of the result:
clc ; CF = 0
mov bl, 88h ; CF = 0 BL = 10001000b
rcl bl, 1 ; CF = 1 AL = 00010000b
rcl bl, 1 ; CF = 0 AL = 00100001b
CF S
Example – Recovery a Carry Flag Bit
RCR Instruction
• The RCR (Rotate and Carry Right) instruction shifts each
bit to the right, copies the Carry flag to the most significant
bit and copies the least significant bit into the Carry flag.
• In this examples, the lowest bit is copied into the Carry
flag and into the highest bit of the result:
stc ; CF = 1
mov ah, 10h ; CF = 1 AH = 00010000b
rcr ah, 1 ; CF = 0 AL = 00001000b
rcr ah, 1 ; CF = 0 AL = 00000100b
CF S
SHLD/SHRD Instructions
• The SHLD and SHLR instructions (Shift
Left/Right Doubleword) require at least a 386
processor.
• When the SHLD (SHRD) is called, the bit
positions opened by the shift in the first operand
are filled by the the most (least) significant bits of
the second operand.
• The second operand is unaffected but the Sign,
Zero, Auxiliary Parity and Carry Flags are
affected.
wval AX
9BA6 AC36
BA6A AC36
SHRD – An Example
mov ax, 234Bh
mov dx, 7654h
shrd ax, dx, 4 ; wval = 4234h
DX AX
7654 234B
7654 4234
Shift and Rotate Applications
• Shift and Rotate instructions are included because
they are helpful in certain applications.
• These applications includes:
– Shifting Multiple Doublewords (for bit-mapped
graphics images)
– Binary multiplication
– Display Binary Bits
– Isolating a Bit String
INCLUDE Irvine32.inc
.data
binValue DWORD 1234ABCDh ; sample bin. value
buffer BYTE 32 dup(0), 0
.code
main PROC
mov eax, binValue ; number to display
mov ecx, 32 ; number of bits in EAX
mov esi, offset buffer
L1: shl eax, 1 ; shift high bit into CF
mov BYTE ptr [esi], '0‘
; choose 0 as default
; digit
jnc L2
mov BYTE ptr[esi], '1‘
; else move to buffer
DH DL
0 0 1 0 0 1 1 0 0 1 1 0 1 0 1 0
IMUL Instruction
AX r/m8 AL AH
DX:AX r/m16 AX DX
Dividing By 0
ADC Instruction