Assembly - instructions for reducing the arrow on the carry flag?

I saw the following code:

shr AL, 1 jnc bit_0 

I do not understand when the carry flag is turned on due to a ban on use?

thanks

+6
source share
3 answers

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

The original right bit is shifted from the AL register to the carry flag (and the new leftmost bit is 0). For instance:

  +------------------------+ +------------------------+ | 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 my comment on another answer:

The following example is given in my book: before the shift: AL = 10101110; shr AL, 1; After the shift: 01011100, CF = 1; This is mistake?

If this is what he says, then yes. This is a left shift ( shl AL, 1 ), and not a right shift: the bits in AL were moved one place from the left, and the carry flag was set to a bit that was shifted from the left end.

+9
source

This is not true; scr (shift with carry) does. However, the test may mean (and does) verify how the carry bit was set before the scrap was performed.

0
source

To add some clarification:

shr AL, 1; it will be (SH) ift (R) ight (1) x the value contained in the AL register, which is the 8 lower bytes of the AX register. Therefore, it will divide by 2 the value contained in AL.

If AL was previously even like 0b100 (4), it will become 0b10 (2) and put 0 in the carry flag. Transfer flag - bit 0 in the flag register https://en.wikipedia.org/wiki/FLAGS_register

If previously AL was an odd value, for example, 0b101 (5), it will become 0b10 (2) and put 1 in the flag register. Consequently, the carry flag will act as a remainder if you divide it by 2.

jnc bit_0; This will be (J) ump for the label "bit_0" if the (N) o (C) arry flag was set, i.e. If the value was equal (for example, 0b100 in the above example) before the shift.

0
source

Source: https://habr.com/ru/post/911410/


All Articles