在汇编中实现 eq, lt gt 而无需跳转

发布于 2024-08-18 08:21:43 字数 211 浏览 8 评论 0原文

是否可以仅使用 AND、OR 和 NOT 运算符来编写逻辑来比较 2 个操作数并返回 true/false (-1, 0),而不使用跳转? 如果是这样,请给我一些提示,因为这对我来说似乎不可能。 我正在尝试用“计算系统的元素”一书的汇编语言实现 eq、lt 和 gt ”

Is it possible to write logic using only the AND, OR, and NOT operators to compare 2 operands and return true/false (-1, 0) without the use of jumps?
If so, can you please give me some hints as it looks impossible to me.
I am trying to implement eq, lt and gt in the assembly language of the book "The Elements of Computing Systems".

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

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

发布评论

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

评论(7

草莓味的萝莉 2024-08-25 08:21:43

如果您仅使用按位逻辑运算符,并且在丢失进位的地方进行加/减操作,则不可能从比较运算中获得 -1 或 0(或者 1 或 0)的结果:

  • For位运算符,结果的位n仅取决于两个操作数的位n

  • 对于加法,请考虑二进制加法的工作原理:结果的位 n 可能会受到位 n 以及位 n 右侧的位的影响(通过进位),每个操作数;但不能受到操作数中位 n 左侧的任何位的影响。 (您可以认为这是对两个偶数相加不能得到奇数结果的观察的概括。)

  • 作为单个加法或按位运算,无法从位 n 的左侧传播任何信息 将操作数放入结果的位 n 中,任何加法或按位运算的组合也不能;减法(此处假设 2 的补码)可以被视为这样的组合:xy = x+(NOT y)+1。

因此,对于 2==2,您无法获得 0 结果,但对于 2==4,您无法获得 -1(或 1)结果,例如:所需结果的位 0 在每种情况下都不同,但结果只能取决于位于两个操作数的位 0 上,在每种情况下都是相同的。

如果您的 true 和 false 值仅在顶部(即最左边)位不同,则可以完成。

例如,对于 8 位值:使用 0x80 表示 true,0 表示 false;那么x == y可以实现为(NOT((x - y) OR (y - x))) AND 0x80

如果可用的操作扩展为包括右移,或者如果 ADD 操作可以产生可以加回结果底部的进位,则可以解决最初所述的问题。

Getting a result of either -1 or 0 (or of either 1 or 0, for that matter) from your comparison operations is impossible if you are using only bitwise logical operators, and add/subtract where the carry is lost:

  • For the bitwise operators, bit n of the result depends only on bit n of the two operands.

  • For addition, consider how a binary addition works: bit n of the result may be influenced by bit n, and bits to the right of bit n (via carries), of each of the operands; but cannot be influenced by any bits to the left of bit n in the operands. (You could consider this to be a generalisation of the observation that adding two even numbers cannot give an odd result.)

  • As a single addition or bitwise op cannot propagate any information from the left of bit n of the operands into bit n of the result, neither can any composition of additions or bitwise ops; and a subtraction (assuming 2's complement here) can be considered as just such a composition: x-y = x+(NOT y)+1.

So you can't get a result of 0 for 2==2, but -1 (or 1) for 2==4, for example: bit 0 of the desired result is different in each case, but the result can depend only on bit 0 of the two operands, which are the same in each case.

If your true and false values differ only in the top (i.e. leftmost) bit, it can be done.

For example, with 8 bit values: use 0x80 for true and 0 for false; then x == y could be implemented as (NOT((x - y) OR (y - x))) AND 0x80.

The problem as originally stated can be solved if the available operations are extended to include a right shift, or if the ADD operation can produce a carry which may be added back in to the bottom of the result.

め七分饶幸 2024-08-25 08:21:43
XOR a, b

如果 a 和 b 相等,则结果为 0,否则结果为非零。

SUB a, b
AND a, SIGN_BIT

(其中 SIGN_BIT 是一个掩码,用于删除除...符号位之外的所有内容)

如果 a 大于 b,则结果为零,如果 a 小于或等于 b(假设 2 完整),则结果为非零。

XOR a, b

will result in 0 if a and b are equal, and something nonzero otherwise.

SUB a, b
AND a, SIGN_BIT

(where SIGN_BIT is a mask to remove everything except ... the sign bit)

will result in zero if a is greater than b, and nonzero if a is less than or equal to b (assuming 2's completent).

蘸点软妹酱 2024-08-25 08:21:43

A 等于 B 可以用异或来表示:

(A AND (NOT B)) OR ( A AND (NOT B))

如果 A==B,则输出 0;如果不是,则输出 != 0

对于 A 小于 B,您可以使用
(A - B AND SIGN_MASK)

,其中 SIGN_MASK 屏蔽除符号位之外的所有内容,将为您提供 MAX_NEGATIVE_INTEGER 的真值和 0 的假值。

大于可以简单地从小于构建

A Equal B can be expressed in terms of xor:

(A AND (NOT B)) OR ( A AND (NOT B))

This will output 0 if A==B and something != 0 if it's not

For A less than B you can use
(A - B AND SIGN_MASK)

,where SIGN_MASK masks away everything except the sign bit, would give you a true value of MAX_NEGATIVE_INTEGER and a false value of 0.

Greater than can be trivially constructed from less than

如梦亦如幻 2024-08-25 08:21:43

以防万一这是一个纯粹的理论问题:由于您正在操作一组有限的操作数,因此所有可能的函数都可以仅使用 OR、AND 和 NOT 来表达。

有关进一步说明,请参阅析取范式

出于实际目的,Anons 的答案更有用:-) ...

编辑:即使我的理论答案也可能不正确:将析取范式应用于此问题将需要移位操作,因为每个位输出字的值取决于输入位的所有位。我还没有弄清楚如何使用 AND、OR、NOT 和算术来实现移位(而且我不确定这是否可能......)

我留下这篇文章作为过早回答的反面例子......

Just in case this is a pure theoretical question: since you're operating on a finite set of operands, all possible functions can be expressed using only OR, AND and NOT.

See Disjunctive normal form for further explanation.

For practical purposes, Anons answer is more useful :-) ...

EDIT: Even my theoretical answer might not be true: Application of disjunctive normal form to this problem would require shift operations, since each single bit of the output word depends on all bits of the input bits. I have not yet figured out how to implement shifts using AND, OR, NOT and arithmetic (and I'm not sure whether that's possible at all ...)

I leave the post though as negative example of premature answering ...

别在捏我脸啦 2024-08-25 08:21:43

我们正在读同一本书,却遇到了这个问题。

我们能找到的最好的解决方案是生成唯一的标签,然后使用 JEQ 命令跳转到预期的标签,然后在完成后跳转到更下方的标签。

伪代码:

if they're equal, jump to EQUAL

// not equal section
push constant false
jump to DONE

// equal section
(EQUAL)
push constant true

// done section
(DONE)

这里是如何实现的我们专门实现了这个(在 Ruby 中)。

we're going through the same book and just hit this problem.

The best solution we could find was to generate unique labels, and then use the JEQ command to jump to the expected one, then jump to a label further down when we were done.

Pseudocode:

if they're equal, jump to EQUAL

// not equal section
push constant false
jump to DONE

// equal section
(EQUAL)
push constant true

// done section
(DONE)

Here is how we specifically implemented this (in Ruby).

失而复得 2024-08-25 08:21:43

从历史上的某个时刻开始,x86 CPU 就有了“条件移动”操作。

x86 CPUs from some point in history onward have had "conditional move" ops.

箜明 2024-08-25 08:21:43

您所讨论的汇编语言中的所有算术运算都可以根据运算结果(=0?和> 0?)进行条件跳转,这可用于获取所需的布尔结果。

all the arithmetic operations in the assembly language that you are talking about come with a possibility of conditional jumps based on the result of the operation (=0? and >0?), which can be used to get the desired boolean result.

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