简体   繁体   English

内部工作大于/小于

[英]Internal working of greater than/ less than

I was just wondering how is the result of greater than / less than computed and returned to the high level languages. 我只是想知道大于/小于计算结果如何,并返回到高级语言。

I'm looking for the hardware gate model here. 我在这里寻找硬件门模型。

Lets use a uniform example to explain, say 5 > 3. 让我们用一个统一的例子来解释,比如5> 3。

It is usually implemented via subtraction with carry-detection. 它通常通过带进位检测的减法来实现。

From a gating perspective, subtracting binary numbers is performed by passing matched pairs of bits from each operand through a subtractor: 从门控的角度来看,减去二进制数是通过从每个操作数通过减法器传递匹配的位对来执行的:

            +-----+
carry_in -->|     |
            |     |--> a_minus_b
       a -->| SUB |
            |     |--> carry_out
       b -->|     |
            +-----+

a_minus_b = carry_in ⊕ a ⊕ b
carry_out = (carry_in ∧ b) ∨ (¬a ∧ (carry_in ∨ b))

Bit 0 from arguments a and b is passed through the first subtractor, with a carry_in of 0. Bit 1 from each argument is passed through the second subtractor, with carry_in set to the carry_out of the bit-0 stage. 参数ab位0通过第一个减法器传递,其中carry_in为0.每个参数的位1通过第二个减法器,其中carry_in设置为bit-0阶段的carry_out。 This continues down the chain until the final carry_out at the end sets the CPU's carry flag, which holds a 1 if a < b, otherwise 0. 这继续沿着链继续,直到最后的最后一个carry_out设置CPU的进位标志,如果a <b则保持1,否则为0。

Additionally, every a_minus_b is ORed together and negated, with the result going into the CPU's zero flag, denoting that a = b. 此外,每个a_minus_b都被“或”在一起并被否定,结果进入CPU的零标志,表示a = b。

These flags can be tested by machine instructions, which are generated by compilers when you write if (a < b) { ... } . 这些标志可以通过机器指令进行测试,机器指令在编写if (a < b) { ... }时由编译器生成。

I'll leave 5 > 3 as an exercise for the reader. 我将留下5> 3作为读者的练习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM