The SAR instruction
Arithmetic shift of a register or a memory location right.
The SAR instruction format
| Instruction | Description |
|---|---|
| SAR reg/mem8, 1 | Signed division of reg/mem8 by 2, once. |
| SAR reg/mem8, CL | Signed division of reg/mem8 by 2, CL times. |
| SAR reg/mem16, 1 | Signed division of reg/mem16 by 2, once. |
| SAR reg/mem16, CL | Signed division of reg/mem16 by 2, CL times. |
Shifts the bits in the first operand (the destination operand) right by the number of bits specified in the second operand. Bits shifted beyond the boundary of the destination operand are first shifted into the CF flag and then discarded. At the end of the shift operation the CF flag contains the last bit shifted out of the destination operand.
The arithmetic shift right (SAR) and logical shift right (SHR) instructions shift the bits of the destination operand right (towards the less significant bits). With each count of the shift, the least significant bit of the destination operand is shifted into the CF flag, while the most significant bit is either set or cleared depending on the type of instruction. The SAR instruction sets or clears the most significant bit so that it matches the sign (the most significant bit) of the original value in the destination operand. In effect, the SAR command fills the vacated bit position of the shifted value with the sign of the unchanged value.
Usage example
title .model small .stack 100h code segment public ; Start of the code segment assume cs:code main proc ; Start of the main procedure mov ax, -9 ; Copy the value -9 into the ax register mov cl, 2 ; Copy the value 2 into the cl register cwd ; Sign-extend the ax register into dx sar ax, cl ; Perform an arithmetic shift of ax by 2 sub ax, dx ; Subtract the value in the dx register from the ax register mov ax, 4C00h ; Copy the process termination function number into ah and the value into al int 21h ; Call the interrupt main endp ; End of the main procedure code ends ; End of the code segment end main ; End of file. The main procedure is the entry point
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".