X86assembly/Instructions/shr
Jump to navigation
Jump to search
You are here:  shr/sar

Contents
Description
 The shr or sar instruction is used to shift the bits of the operand destination to the right, by the number of bits specified in the count operand.
 Bits shifted beyond the destination are first shifted into the CF flag.
 Equivalent to dividing by 2^{3}
 This could be written as follows in python:
>>> def shr(dest, count): ... return hex(dest >> count) ... >>> shr(0xA, 2) '0x2'
 shr fills with zeroes, and sar fills with the sign bit.
# Emulate the sar machine instruction
def sar_8(a, width):
sign = a & 0x80
a &= 0x7F
a >>= width
a = sign
return a
Syntax
shr destination, count
Example
mov eax, 0xA ; set EAX to 0xA (1010 in binary) shr eax, 2 ; shifts 2 bits to the right in EAX, now equal to 0x2 (0010 in binary)
+++++┬───┬───┬───┬───┐ mov eax, 0xA  0  0  0  0  0 │ 1 │ 0 │ 1 │ 0 │ +++++┴───┴───┴───┴───┘ │ │ └───────────┐ │ └───────┐ │ └───────┐ │ │ +++++++┬───┬───┐ ┌───┐ shr eax, 2  0  0  0  0  0  0  0 │ 1 │ 0 │ │ 1 │ +++++++┴───┴───┘ └───┘ CF
shr eax, 1 ;Signed division by 2 shr eax, 2 ;Signed division by 4 shr eax, 3 ;Signed division by 8 shr eax, 4 ;Signed division by 16 shr eax, 5 ;Signed division by 32 shr eax, 6 ;Signed division by 64 shr eax, 7 ;Signed division by 128 shr eax, 8 ;Signed division by 256