would doing arithmetic operation on a pair of signed and unsigned numbers be legal?

后端 未结 5 1525
忘了有多久
忘了有多久 2021-01-03 02:23

I\'m more than half way through learning assembly and I\'m familiar with the concept of how signed and unsigned integers are presented in bits, I know that it might seem a w

5条回答
  •  余生分开走
    2021-01-03 02:33

    At binary level, there is only one addition operation:

     0101 + (5)
     1010 = (unsigned 10 or signed -6)
    --------
     1111   (unsigned 15 or signed -1)
    

    What about the carry and overflow flags, they are both set according to a simple rules. CF can be used to detect an oveflow iff we consider that the operands were unsigned, and OF to detect the oveflow iff we consider both of them signed. Both these flags are set according to the result, and it's up to you to decide which of them to use.

    The actual formula for OF flag is

    OF = CF xor MSB_of_result.
    

    This means that if we are adding two positive numbers (that we consider signed), then if the result is negative, then it oveflowed.

提交回复
热议问题