How does comparing the Sign and Overflow Flag determine operand relationships?

前端 未结 2 459
北荒
北荒 2021-01-07 02:30

Jump\'s based on comparing signed integers use the Zero, Sign, and Overflow flag to determine the relationship between operands. After CMP with two signed opera

2条回答
  •  自闭症患者
    2021-01-07 03:05

    Performing the signed subtraction R = Destination - Source yields a signed result.

    Suppose there is no overflow - the usual arithmetic laws holds: if R = Destination - Source > 0 then Destination > Source.
    Having no overflow means OF = 0 and R > 0 means SF = 0.

    Now suppose there is an overflow - let's call O the most significant, non-sign, bit and S the sign bit.
    An overflow condition means that either a) Computing the result's O needed a borrow and result's S didn't or b) result's O didn't need a borrow and S did.

    In case a) since result's S didn't need a borrow, the two S bits of the operands were either (1, 0) (1, 1) or (0, 0).
    Since result's O needed a borrow, and thus flipping the first source S bit, we must exclude the second and third option.
    So the operands sign bits were 1 and 0 (thus Destination < Source), the result's sign bit SF = 0 and OF = 1 by hypothesis.

    In case b) since result's S did need a borrow, the two S bits of the operands were (0, 1).
    Since O didn't need a borrow, the first operand S bit has been not changed and we don't need to consider any further case.
    So the operands sign bits were 0 and 1 (thus Destination > Source), the result's sign bit SF = 1 and OF = 1 by hypothesis.

    To recap:

    • If OF = 0 then Destination > Source => SF = 0.
    • If OF = 1 then Destination > Source => SF = 1.

    In short OF = SF.

提交回复
热议问题