Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SAL/SAR vs SHR/SAR in assembly 8086

I am learning assembly language (specific to x86). I have understood that the SAL and SHL works in similar way (clearing the lsb and carrying the msb to CF) from here Difference between SHL and SAL in 80x86.

Considering that SHR and SAR doesn't operate in similar way (the latter keeping the msb unchanged).

I would like to have a clear concept about why the functionality of Shift Arithmetic Right SAR is defined different from Shift Right SHR but at the same time the SHL and SAL are kept with similar functionality ?

like image 772
Abrar Avatar asked Mar 12 '16 11:03

Abrar


People also ask

What is the difference between SHR and SAR in a 8086?

SAR performs a signed divide with rounding toward negative infinity (not the same as IDIV); the high-order bit remains the same. SHR performs an unsigned divide; the high-order bit is set to 0.

What does SHR mean in assembly?

The SHR (shift right) instruction performs a logical right shift on the destination operand. The highest bit position is filled with a zero.

What is the difference between SAL and SHL?

Show activity on this post. According to this, they are the same: The shift arithmetic left (SAL) and shift logical left (SHL) instructions perform the same operation; they shift the bits in the destination operand to the left (toward more significant bit locations).

What is Sal instruction?

SAL (or its synonym, SHL) shifts the bits of the operand upward. The high-order bit is shifted into the carry flag, and the low-order bit is set to 0. SAR and SHR shift the bits of the operand downward. The low-order bit is shifted into the carry flag.


1 Answers

Because there's nothing to preserve in case of a left shift. In those cases that a left shift changes the sign (which you might have wanted to prevent somehow), the result without overflow would not fit in a register (this is automatically so - if it did fit, then it wouldn't have overflowed to begin with). So there's nothing you can do about it anyway.

But right shift can't overflow, it makes the number smaller (or it stays the same). So now you can choose, do I keep the top bit intact or not.

like image 183
harold Avatar answered Oct 06 '22 18:10

harold