旋转指令(x86 上的 ROL、RCL)的用途是什么?

发布于 2024-10-17 00:10:30 字数 349 浏览 9 评论 0原文

我一直想知道某些 CPU 的旋转指令(例如 x86 上的 ROL、RCL)的用途是什么。什么样的软件使用这些指令?

我首先认为它们可能用于加密/计算哈希码,但这些库通常是用 C 编写的,它没有映射到这些指令的运算符。 (编者注:请参阅循环移位(旋转)操作的最佳实践在 C++ 中了解如何编写将编译为旋转指令的 C 或 C++。此外,优化的加密库通常具有针对特定平台的 asm。)

有人发现它们的用途吗?为什么他们添加到指令集中?

I always wondered what's the purpose of the rotate instructions some CPUs have (ROL, RCL on x86, for example). What kind of software makes use of these instructions?

I first thought they may be used for encryption/computing hash codes, but these libraries are written usually in C, which doesn't have operators that map to these instructions. (Editor's note: see Best practices for circular shift (rotate) operations in C++ for how to write C or C++ that will compile to a rotate instruction. Also, optimized crypto libraries often do have asm for specific platforms.)

Has anybody found an use for them? Why where they added to the instructions set?

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

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

发布评论

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

评论(6

櫻之舞 2024-10-24 00:10:30

跨多个字的位移需要循环。当您对低位字进行 SHL 处理时,高位会溢出到进位中。要完成该操作,您需要移动高位字,同时将进位移至低位。 RCL 是完成此操作的指令。

                      High word             Low word         CF
Initial          0110 1001 1011 1001   1100 0010 0000 1101    ?
SHL low word     0110 1001 1011 1001   1000 0100 0001 1010    1
RCL high word    1101 0011 0111 0011   1000 0100 0001 1010    0

ROL 和 ROR 对于以(最终)非破坏性的方式逐位检查值非常有用。它们还可以用于分流位掩码而不引入垃圾位。

Rotates are required for bit shifts across multiple words. When you SHL the lower word, the high-order bit spills out into the carry. To complete the operation, you need to shift the higher word(s) while bringing in the carry to the low-order bit. RCL is the instruction that accomplishes this.

                      High word             Low word         CF
Initial          0110 1001 1011 1001   1100 0010 0000 1101    ?
SHL low word     0110 1001 1011 1001   1000 0100 0001 1010    1
RCL high word    1101 0011 0111 0011   1000 0100 0001 1010    0

ROL and ROR are useful for examining a value bit-by-bit in a way that is (ultimately) non-destructive. They can also be used to shunt a bitmask around without bringing in garbage bits.

ζ澈沫 2024-10-24 00:10:30

旋转移位操作码 ROL、RCL、ROR、RCR)几乎专门用于散列和 CRC 计算。它们非常神秘并且很少使用。

移位操作码(SHL、SHR)用于快速乘以 2 的幂,或将低字节移至大型寄存器的高字节中。

ROL 和 SHL 之间的区别在于 ROL 获取高位并将其滚动到低位位置。 SHL 丢弃高位并用零填充低位位置。

The rotate shift opcodes ROL, RCL, ROR, RCR) are used almost exclusively for hashing and CRC computations. They are pretty arcane and very rarely used.

The shift opcodes (SHL, SHR) are used for fast multiplication by powers of 2, or to move a low byte into a high byte of a large register.

The difference between ROL and SHL is ROL takes the high bit and rolls it around into the low bit position. SHL throws the high bit away and fills the low bit position with zero.

音盲 2024-10-24 00:10:30

ROR ROL 是“历史性的”,但在很多方面仍然有用。

在 80386(和操作码 BT)之前,ROL 经常被用来测试一位(SHL 不会传播到进位标志) - 实际上在 8088 中,ROR/ROL 一次只会移动 1 位! !

另外,如果您想先以一种方式移位,然后再以另一种方式移位,而又不丢失已移出范围的位,则可以使用 ROR/ROL 而不是 SHR/SHL

ROR ROL are "historic" but still useful in a number of ways.

Before the 80386 (and opcode BT), ROL would be used a lot to test a bit (SHL doesn't propagate to the carry flag) - actually in 8088, ROR/ROL would only shift by 1 bit at a time !!!!

Also if you want to shift one way and then the other way without loosing the bits that have been shifted out of scope, you'd use ROR/ROL instead of SHR/SHL

倦话 2024-10-24 00:10:30

如果我理解正确的话,你的问题是这样的:

“鉴于旋转指令似乎非常特殊用途并且不是由编译器发出的,那么它们何时实际使用以及为什么它们包含在 CPU 中?”。< /em>

答案是双重的:

  1. CPU 并不是专门为执行 C 程序而设计的。相反,它们被设计为通用机器,旨在使用各种不同的工具和语言解决各种问题。

  2. 语言的设计者没有义务使用 CPU 中的每个操作码。事实上,大多数时候,它们并不这样做,因为有些 CPU 指令是高度专业化的,语言设计者没有迫切需要使用它们。

有关按位运算符(以及它们与 C 编程的关系)的更多信息,请访问:http://en。 wikipedia.org/wiki/Bitwise_operation

If I understand you correctly, your question is this:

"Given the fact that rotation instructions seem to be very special-purpose and not emitted by compilers, when are they actually used and why are they included in CPUs?".

The answer is twofold:

  1. CPU's are not designed specifically to execute C programs. Rather, they are designed as general purpose machines, intended to solve a wide array of problems using a wide variety of different tools and languages.

  2. The designers of a language are under no obligation to use every opcode in the CPU. In fact, most of the time, they do not, because some CPU instructions are highly specialized, and the language designer has no pressing need to use them.

More information about bitwise operators (and how they relate to C programming) can be found here: http://en.wikipedia.org/wiki/Bitwise_operation

橪书 2024-10-24 00:10:30

当微处理器最初被创建时,大多数程序都是用汇编编写的,而不是编译的。大多数 CPU 指令可能不是由编译器发出的(这是创建 RISC 的动力),但通常相对容易在硬件中实现。

图形和密码学中的许多算法都使用旋转,并且将它们包含在 CPU 中使得可以在汇编中编写非常快速的算法。

Back when microprocessors were first created, most programs were written in assembly, not compiled. The majority of CPU instructions are probably not emitted by compilers (which is the impetus for creating RISC), but are often relatively easy to implement in hardware.

Many algorithms in graphics and cryptography use rotation, and their inclusion in CPUs makes it possible to write very fast algorithms in assembly.

谜兔 2024-10-24 00:10:30

我认为这里的许多答案都有些倒退,包括目前接受的答案。最大的应用是跨字节/字边界移动数据,广泛用于

  • 提取和插入位模式
    • 协议(从第 6 位开始插入 5 位)
    • 压缩方案(LZW77 及更多)
    • 数据传输(300 波特调制解调器?7 位数据 + 奇偶校验)
  • 任意精度算术
    • 乘/除 2 利用循环进位
    • 乘以/除以 2 的其他幂需要 ROL(或 ROR)
    • 水平滚动 1 位图形

利基应用:

  • crc16/32
  • 密码
  • 非破坏性地将位移动到符号位或从历史角度来看

,移位的成本很高:当需要在 8 位块中左移 16 位(或在 64 位块中左移 128 位)时,ROL 会在以下位置执行两次昂贵的移位: 1 的成本:

rotate all bits left by 3
      hi       lo
src = fedcba98|76543210
dst = cba98765|43210---

请注意,位“765”需要右移 5 位,而位“43210”需要左移 3 位。这一切都是通过单次循环完成的,它将所有正确的位放入正确的位置,即使它们伴随着错误的位,这些位通过掩码重新组合,这是一种廉价的操作:

dst_lo = ((src_lo ROL 3) & 0b11111000)
dst_hi = ((src_lo ROL 3) & 0b00000111) | (src_hi << 3)

这延伸到 bignum 移位,或水平滚动单色图形平面任意数量的像素。

该算法非常重要,以至于 80386 为其添加了双循环指令。

I think many answers here got it somewhat backwards, including the currently accepted one. The biggest application is in shifting data across byte/word boundaries, which is extensively used in

  • extracting and inserting bit patterns
    • protocols (insert 5 bits starting from bit 6)
    • compression schemes (LZW77 and more)
    • data transfer (300 baud modems anyone? 7-bit data + parity)
  • arbitrary precision arithmetic
    • multiplying/dividing by 2 utilises rotations-through-carry
    • multiplying/dividing by other powers of two need the ROL (or ROR)
    • scrolling 1-bit graphics horizontally

And the niche applications:

  • crc16/32
  • ciphers
  • non-destructive moving bits to sign bit or to carry for testing

The historical perspective is that shifting was expensive: when one needs to shift say 16 bits left by 3, in chunks of 8 bits (or 128 bits left in chunks of 64 bits), a ROL performs two expensive shifts at the cost of one:

rotate all bits left by 3
      hi       lo
src = fedcba98|76543210
dst = cba98765|43210---

Notice, that the bits "765" need to be shifted right by 5, while bits "43210" need to be shifted left by 3. This is all accomplished by a single rotation, which put all the right bits to the correct position, even if they are accompanied by the wrong bits, which are recombined by masking, which is an inexpensive operation:

dst_lo = ((src_lo ROL 3) & 0b11111000)
dst_hi = ((src_lo ROL 3) & 0b00000111) | (src_hi << 3)

This extends to bignum shifting, or scrolling a monochrome graphics plane horizontally by arbitrary number of pixels.

This algorithm is so essential, that 80386 included a double-rotate instruction for it.

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