Skip to content

Latest commit

 

History

History
44 lines (23 loc) · 921 Bytes

File metadata and controls

44 lines (23 loc) · 921 Bytes

Question 15

What is the difference between sar and shr? Check Intel docs.

Answer

Performing shr will perform a logical shift. It means that the bits that slide off the end disappear. The exception is the last bit, which slides into the carry flag (CF). The spaces are filled with zeros.

For example:

mov rax, 0xFFFFFFFFFFFFFFFF      
shr rax, 4                     ; rax = 0x0FFFFFFFFFFFFFFF

Performing sar will perform an arithmetic shift. It is similar, but the spaces are filled with the sign bit (the 63-th one).

For example:

mov rax, 0xFB00000000000000      
shr rax, 4                     ; rax = 0xFFB0000000000000

The FB part was shifted to the right. However, the sign bit was equal to 1 as the part of F hexadecimal digit. Thus, the empty 4 bits were filled with its value, becoming 1111 in binary, or 0xF.

prev +++ next