处理器如何计算是否 A <乙

发布于 2024-09-27 00:26:15 字数 204 浏览 3 评论 0 原文

这可能是我想到的一个愚蠢的问题,但我想我没有答案 -

如果 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黑色毁心梦 2024-10-04 00:26:15

在 x86 汇编中,CMP 指令经常用于比较两个整数。

例如,如果 EAX 和 EBX 中分别有整数,并执行 CMP EAX,EBX,符号 (S) 标志和零 (Z) 标志的值将告诉您哪个数字最大:

EAX > EBX:  Z=0, S=0
EAX == EBX: Z=1, S=0
EAX < EBX:  Z=0, S=1

可以获得相同的结果通过使用 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:

EAX > EBX:  Z=0, S=0
EAX == EBX: Z=1, S=0
EAX < EBX:  Z=0, S=1

The same result can be obtained by using the SUB instruction, which also stores the difference between the numbers in the destination register.

长安忆 2024-10-04 00:26:15

A 中减去 B,然后查看结果的 N(负)标志或 MSb 是否已设置。

Subtract B from A and see if the N (negative) flag or the MSb of the result is set.

淡淡的优雅 2024-10-04 00:26:15

所有 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文