Carry flag in substraction

纵然是瞬间 提交于 2020-01-01 11:33:51

问题


I am using MASM32.

With this code:

mov eax,5
sub eax,10

CF status flag will be set. But using my pencil and paper, I actually see that there is no any carry from MSB. Yes, I know that from subtraction from less number great number set CF. But I want to know why?

Because using this code:

mov eax,5
mov ebx,10
not ebx
add ebx,1
add eax,ebx

CF flag won't be ever set.


回答1:


5 - 10 = 5 + (-10) = 0x05 + (0xF5 + 1) = 0x05 + 0xF6 = 0xFB

  00000101 -- 0x05
  11110101 -- 0xF5
+ 00000001 -- 0x01
==========
  11111011 -- 0xFB

And this continues for 16 or 32 or 64 bits 0+1+0 = 1, carry 0

You are right in the sense that it doesnt carry out. A subtract is an add with the second operand inverted and the carry in inverted. Some processor families invert the carry out some dont. Sounds like you are looking at something that inverts the carry out on a subtract.

So if 5 - 10 gives carry out (borrow) then try 10 - 5 on the same processor, you should not see the carry bit set (no borrow).

Your second example is an add operation, the carry out is not inverted on any processor that I know of, further supporting the carry bit being positive logic indicating a borrow.




回答2:


In first case carry flag is set because you subtracted larger number from smaller one.In second case Lets suppose eax is 8 bit register then

    eax=00000101=05
    ebx=00010000=10
not ebx=11101111
         +     1
---------------------
        11110000
         +  0101
 ---------------------
        11110101

Not overflow is occurs i.e no carry flag is set.



来源:https://stackoverflow.com/questions/12237725/carry-flag-in-substraction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!