如何在不使用其他寄存器的情况下镜像字节? [英] How to mirror a byte without using other registers?

查看:42
本文介绍了如何在不使用其他寄存器的情况下镜像字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在AL中有此字节:01100001 应用 mirror 函数后,我希望字节为10000110.

Let's say I have this byte in AL: 01100001 After applying a mirror function I want the byte to be 10000110.

我提出的所有想法都必须使用其他寄存器,但是我很好奇是否有一种方法可以在不使用任何其他寄存器的情况下镜像字节吗?

All ideas that I came up with I have to use other registers, but I am curious if there is a way to mirror a byte without using any other register?

推荐答案

按代码立即存储"变体:

The "storage by immediate in code" variant:

mirror_bits:
    ; handle bits 0 and 7
    TEST    al,0x81
    JPE     bits07same
    XOR     al,0x81
bits07same:
    ; handle bits 1 and 6
    TEST    al,0x42
    JPE     bits16same
    XOR     al,0x42
bits16same:
    ; handle bits 2 and 5
    TEST    al,0x24
    JPE     bits25same
    XOR     al,0x24
bits25same:
    ; handle bits 3 and 4
    TEST    al,0x18
    JPE     bits34same
    XOR     al,0x18
bits34same:
    RET


关于我的疑问和一般回答是否有办法.


About my comment under question and general answer whether there's a way.

您应该始终首先问数学理论.在您的情况下,您将确定性地将8位信息转换为其他8位信息结果,而所需的最小修改步骤是交换两位",如果没有第三位进行临时存储,则不可能做到这一点,因此,您现在正在寻找一种无需额外注册即可补充临时存储的方法(我确实添加了和内存").

You should always just ask the math theory first. In your case you are deterministically changing 8 bit information into other 8 bit information result, and the minimal modification step needed is "swap of two bits", which is impossible to do without third bit for temporary storage, so you are now looking for a way to supplement temporary storage without extra register ("and memory" I did add to myself).

因此,如果您想在不更改其他寄存器的情况下镜像al(不计算ripeflags,因为这完全不可能达到99%),则需要在其他地方借用"这笔额外的费用.

Thus if you want to mirror al without changing other register (not counting rip and eflags, as that would be 99% impossible completely), you need to "borrow" this extra bit elsewhere.

由于数字计算机是类似图灵的机器,因此您可以使用代码指令的位来交换寄存器/存储中的丢失位,因此从理论上讲,有可能=> QED.

As the digital computers are turing-like machines, you can exchange missing bits in registers/storage by using bits of code instructions, so theoretically it is possible => QED.

在对问题进行了基本的验证"之后,只是要找出问题,哪种代码结构确实可以提供额外的信息位存储和交换位.

After that basic "validation" of question it was just question to find out, what kind of code structure does provide that extra information bit storage and swap bits.

最直接的残酷方式是每个位值都具有分支,即. test al,0x01 jz bit_0_clear ; else bit_0_set branch follows(然后每个分支都可以对目标位进行正确的设置/重置,以使其看起来好像交换了它们)...我也不敢编写那样的完整代码(太太冗长而乏味),但这是上述解决方案的根源.

The most direct brutal way is to have branching per bit-value, ie. test al,0x01 jz bit_0_clear ; else bit_0_set branch follows (each branch can then do the correct set/reset of target bits to make it look like it did swap them)... I wouldn't dare to write the complete code like that (too long, too tedious), but this is one root of the solution above.

解决方案的另一个目的是将这种代码思想与必须真正完成的事情"相对应,即在特定位置交换位".但是,当这些位已经具有相同的值=不需要交换时,可以将其优化.而且,通过简单地xor翻转两个位,可以在两个位不同时交换"它们.

The other root of solution was to put this code idea against the "what has to be really done", and that is "swapping bits on particular positions". But that can be optimized out, when the bits already have the same value = no swap needed. And "swapping" two bits when they are different can be achieved by simple xor flipping both of them.

在将所有这些思想训练合并为一个解决方案之后,我几乎得到了上面的内容,然后我对其进行了一些清理(例如弄清楚两个相同"的测试可以简化为单个test + jpe )并验证了它的有效性.

After I merged all those idea-trains to single solution, I got almost what is above, then I just cleaned it a bit (like figuring out the "two bits are same" test can be simplified down to single test + jpe) and verified it works.

但是只要有疑问,只要记住Turing机器的工作方式即可:)))(半开玩笑,我真的不想用类似Turing机器的语言编写任何中等大小的算法,即使很短的时间也可能在习惯了x86或C ++之类的复杂机器/语言之后就很烦人了.但是在基本级别上验证任务还是很不错的,因为这样做是否合理.)

But whenever in doubt, just remember how Turing machine works :))) (semi-joke, I wouldn't really want to write any medium-sized algorithm in Turing machine-like language, even a short one would be probably annoying after being used to complex machines/languages like x86 or C++. But it's still good to verify the task at the base level, whether it makes sense turing-wise).

这篇关于如何在不使用其他寄存器的情况下镜像字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆