Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly - shr instruction turn on carry flag?

Tags:

x86

assembly

I saw the next code:

shr AL, 1
   jnc bit_0

I don't understand when the carry flag is turn on due to shr instrucrion?

Thanks

like image 484
Adam Sh Avatar asked Mar 22 '12 14:03

Adam Sh


1 Answers

shr AL, 1 moves all of the bits in AL right one place.

The original rightmost bit is shifted out of the AL register into the carry flag (and the new leftmost bit is set to 0). For example:

 +------------------------+               +------------------------+
 | 1  0  0  1  0  1  0  1 |               | 0  1  1  0  0  1  0  0 |
 +------------------------+               +------------------------+
    \  \  \  \  \  \  \  \       or          \  \  \  \  \  \  \  \
     \  \  \  \  \  \  \  \                   \  \  \  \  \  \  \  \
 +------------------------+ CF            +------------------------+ CF
 |(0) 1  0  0  1  0  1  0 | 1             |(0) 0  1  1  0  0  1  0 | 0
 +------------------------+               +------------------------+

In your comment on another answer:

My book gives the next example: Before shift: AL = 10101110 ; shr AL, 1 ; After shift: 01011100, CF = 1 ; Is it mistake?

If that's what it says, then yes. That's a left shift (shl AL, 1), not a right shift: the bits in AL have all been moved left by one place, and the carry flag has been set to the bit that was shifted out of the left-hand end.

like image 60
Matthew Slattery Avatar answered Sep 21 '22 16:09

Matthew Slattery