SAR Instruction


The SAR (shift arithmetic right) instruction performs a right arithmetic shift on its destination operand. SAR differs from SHR in one important way: SAR uses the sign bit to fill leftmost vacated bits. In this way, positive and negative values retain their signs.

 SAR — Shift Arithmetic Right 
Usage: sar dest, count


Flag O D I S Z A P C
Result *     * * ? * *

Shifts the destination right by “count” bits with the current sign bit replicated in the leftmost bit. The Carry flag contains the last bit shifted out.

Clocks
Operands 286 386 486 Size Bytes
reg, 1 2 3 3 2
mem, 1 7 7 4 2-4
reg, CL 5+n 3 3 2
mem, CL 8+n 7 4 2-4
reg, immed8 5+n 3 2 3
mem, immed8 8+n 7 4 3-5

You can divide a signed operand by a power of 2, using the SAR instruction. Halving odd numbers such as 5 and 7 generates 2 and 3, respectively, and -5 and -7 generates -3 and -4, respectively.

 Shift Negative Right   Shift Arithmetic Right   Shift Arithmetic Right 
 .data
 value  SDWORD  -27
 .code
 shr    value, 03
 mov    eax, value
 call   WriteInt
 .data
 value  SDWORD  -27
 .code
 sar    value, 03
 mov    eax, value
 call   WriteInt
 .data
 value  SDWORD  27
 .code
 sar    value, 03
 mov    eax, value 
 call   WriteInt
 Output  Output  Output
  EAX =

 

  EAX =

 

  EAX =