本文介绍了比较符号和溢出标志如何确定操作数关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于比较有符号整数的跳转使用零、符号和溢出标志来确定操作数之间的关系.在带有两个有符号操作数的 CMP 之后,有三种可能的情况:

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 operands, there are three possible scenario's:

  1. ZF = 1 - 目的地 = 源
  2. SF = OF - 目的地 > 来源
  3. SF != OF - 目的地
  1. ZF = 1 - Destination = Source
  2. SF = OF - Destination > Source
  3. SF != OF - Destination < Source

我无法理解场景 2 和 3.我已经研究了可能的组合并看到它们确实有效 - 但我仍然无法弄清楚它们为什么有效.

I'm having trouble understanding scenario 2 and 3. I've worked through the possible combinations and see that they do work - but I still can't figure out why they work.

谁能解释为什么 Sign 和 Overflow 标志的比较反映了有符号整数关系?

Can anyone explain why a comparison of the Sign and Overflow flags reflects signed integer relationships?

似乎对我的要求有所了解.基于有符号比较的跳转使用零、符号和进位标志 - 这些标志包括 JGJL 等.

There seems to be some understanding regarding what I'm asking. Jumps based on signed comparisons utilize the Zero, Sign, and Carry flag - these include JG, JL, and so on.

例如:

mov   al, 1
cmp   al, -1
jg    isGreater

isGreater:

跳转是因为Overflow flag = Sign Flag(两者都是0),说明在有符号比较方面,目的操作数大于源操作数.

The jump is taken because Overflow flag = Sign Flag (both are 0), indicating in terms of signed comparison, the destination operand is larger than the source.

如果溢出标志设置为 1 并且标志标志设置为 0,则表示目的地较小.

If the Overflow flag was set to 1 and the Sign flag set to 0, that would indicate the destination is smaller.

我的问题是 - 我似乎无法理解为什么这实际上有效.

My questions is - I just can't seem to wrap my head around WHY this actually works.

推荐答案

执行有符号减法 R = Destination - Source结果.

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

假设没有溢出 - 通常的算术法则成立:如果 R = Destination - Source > 0 那么 Destination > 来源.
没有溢出意味着 OF = 0 而 R > 0 意味着 SF = 0.

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.

现在假设发生溢出 - 让我们称 O 为最重要的非符号位,S 为符号位.
溢出条件意味着 a) 计算结果的 O 需要借位而结果的 S 不需要,或者 b) 结果的 O 不需要借位需要借用,S 做到了.

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.

如果 a) 由于结果的 S 不需要借位,操作数的两个 S 位是 (1, 0) (1, 1)或 (0, 0).
由于 result 的 O 需要借位,从而翻转第一个源 S 位,我们必须排除第二个和第三个选项.
所以操作数的符号位是 1 和 0(因此 Destination <Source),结果的符号位 SF = 0 和 OF = 1 假设.

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.

在情况 b) 由于结果的 S 确实需要借位,操作数的两个 S 位是 (0, 1).
由于 O 不需要借位,第一个操作数 S 位没有改变,我们不需要考虑任何进一步的情况.
所以操作数的符号位是 0 和 1(因此 Destination > Source),结果的符号位 SF = 1 和 OF = 1 假设.

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.

总结:

  • 如果 OF = 0 那么 Destination > Source => SF = 0.
  • 如果 OF = 1 那么 Destination > Source => SF = 1.
  • If OF = 0 then Destination > Source => SF = 0.
  • If OF = 1 then Destination > Source => SF = 1.

简而言之OF = SF.

这篇关于比较符号和溢出标志如何确定操作数关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:36