ARM 组装难题
首先,我不确定是否存在解决方案。我花了几个多小时试图想出一个,所以要小心。
问题:
r1 包含任意整数,标志未根据其值设置。如果 r1 为 0x80000000,则将 r0 设置为 1,否则设置为 0,仅使用两条指令。
用 3 条指令很容易做到这一点(有很多方法),但是用 2 条指令做到这一点似乎非常困难,而且很可能是不可能的。
First of all, I'm not sure if solution even exists. I spent more than a couple of hours trying to come up with one, so beware.
The problem:
r1 contains an arbitrary integer, flags are not set according to its value. Set r0 to 1 if r1 is 0x80000000, to 0 otherwise, using only two instructions.
It's easy to do that in 3 instructions (there are many ways), however doing it in 2 seems very hard, and may very well be impossible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个部分解决方案,它在
r0
的最高位给出了正确的答案,因此它可用作移位操作数 (r0 lsr #31
)。这是有效的,因为
0
和0x80000000
是唯一在求反时保留其符号位的数字。我相当确定精确的解决方案是不可能的。编辑:不,这并非不可能。请参阅马丁的回答。
Here's a partial solution that gives the correct answer in the top bit of
r0
, so it is available as a shifter operand (r0 lsr #31
).This works because
0
and0x80000000
are the only numbers that retain their sign bits when negated. I'm fairly sure an exact solution is impossible.EDIT: no, it's not impossible. See Martin's answer.
这相当于:
This is equivalent to:
类似于:
mov r0,r1,lsr #31
Something like:
mov r0,r1,lsr #31
如果想使用“快速”指令,这是一个难题。我不太能想出一个解决方案,但可以提供更多“想法”:
这些都不是完美的解决方案,但它们很有趣。
Tough puzzle if one wants to use "fast" instructions. I can't quite come up with a solution, but can offer a couple more 'notions':
None of those is a perfect solution, but they're interesting.
类似的东西
something like