这可能是我想到的一个愚蠢的问题,但我想我没有答案 -
如果 A < 处理器将如何找到乙?事实上还有其他的事情,比如 A>B 或 A==B。
但如果我们解决 A
我知道不同的处理器可能有不同的实现,但我想有一些执行此操作的高级想法。
我相信,一些按位运算符的技巧应该可以做到这一点。
任何帮助将不胜感激。
This is probably a dumb question came to my mind, but I think I don't have answer to this -
How processor would find if A < B? In fact there are other things as well like A>B or A==B.
But if we solve A
I understand that different processors may probably have different implementations but I wanted to have some high level idea of doing this.
Some trick with bitwise operators should do it, I believe.
Any help would be greatly appreciated.
发布评论
评论(3)
在 x86 汇编中,CMP 指令经常用于比较两个整数。
例如,如果 EAX 和 EBX 中分别有整数,并执行 CMP EAX,EBX,符号 (S) 标志和零 (Z) 标志的值将告诉您哪个数字最大:
可以获得相同的结果通过使用 SUB 指令,该指令还将数字之间的差异存储在目标寄存器中。
In x86 assembly, the CMP instruction is often used to compare two integers.
For instance, if you have integers in EAX and EBX respectively, and do CMP EAX,EBX, the values of the sign (S) flag and the zero (Z) flag will tell you which number was largest:
The same result can be obtained by using the SUB instruction, which also stores the difference between the numbers in the destination register.
从
A
中减去B
,然后查看结果的N
(负)标志或 MSb 是否已设置。Subtract
B
fromA
and see if theN
(negative) flag or the MSb of the result is set.所有 CPU 都有 ALU。我不会发布维基百科链接,因为您自己可以找到;)但我可以教您一些有关 CPU 内部的知识。
你基本上需要知道的是,所有CPU都有比较指令,即BEQ“Branch-on-Equal”,用于实现“if”指令,如果A == B则跳转(对于A < B还有另一条指令, ETC)。当CPU读取该指令和操作数时,它将它们加载到两个ALU输入总线中,并设置代表比较操作的适当ALU代码。 ALU 旨在通过硬件比较数字并在其输出端输出正确的结果,或在 CPU 上设置状态位。
数字的硬件比较很容易,几乎所有学术学生都曾经设计过比较(从4位到8位,但相同的逻辑适用于32和64),你可以在任何地方找到原理图
All CPUs have ALUs. I won't post Wikipedia links as you are good enough to find by yourself ;) but I can teach you a bit about CPU's internal.
What you basically need to know is that all CPUs have comparison instructions, ie BEQ "Branch-on-Equal" which is used to implement "if" instructions and jumps if A == B (there is another instruction for A < B, etc). When CPU reads this instruction and the operands, it loads them in the two ALU input buses and sets a proper ALU code that represents the comparison operation. The ALU is built to compare the numbers via hardware and output a proper result on its output side, or setting a status bit on the CPU.
Hardware comparison of numbers is easy, and almost all academic student have once designed a comparison (from 4 to 8 bits, but same logic applies to 32 and 64), you can find schematics anywhere