2

I know that shl instruction is like the mul operation and the shr instruction is like the div operation and it's used for optimization.

For example, shl eax, n is a more efficient way to implement eax = (eax)*(2^n) without any multiplication instructions.

Now I am reading about rol and ror assembly instructions and I got how it works, but I don't know what is the point of the rotation operation in general and when to use it?

4
  • 2
    Optimization is a secondary use of these instructions. They are mainly there to reproduce boolean algebra constructs necessary for implementing certain concepts in a more natural way. Generally, these instructions are VERY usefull when doing signal processing, cryptography, ... They are the +, *, /, - you usually use in arithmetics but used on a a binary set {0, 1} rather than an infinite set. en.wikipedia.org/wiki/Binary_operation en.wikipedia.org/wiki/Bitwise_operation – yaspr Jun 18 '17 at 5:38
  • 1
    @yaspr are you able to turn this comment into an answer? – julian Jun 18 '17 at 15:04
  • 2
    Sure, I'll set it up with better explanations. @SYS_V – yaspr Jun 18 '17 at 16:19
  • What's the purpose of the rotate instructions (ROL, RCL on x86)?. On microcontrollers rotate through carry is commonly used to do a multiword shift – phuclv Jul 18 '18 at 4:33
4

I know that shl instruction is like mul operation, and shr is like div operation, and it's used for optimization.

It is somewhat of an incorrect statement. Those operations are bitwise operations. These operations are the basis of Boolean Algebra and they're used whenever bit level manipulation is required. Applications may include bitfield/flag manipulation, working with hardware devices, network protocols, decoding/encoding, compression and many other uses.

Additionally, logical shifts have the interesting property of being efficient ways to perform multiplication or division of unsigned integers by powers of two. This is a side-effect and not the purpose of those operations. This property is also true for arithmetic shift operations and signed integers.

Similarly, rotation bitwise operations have their own applications such as in cryptographic models (encryption, hashing), for example.

4

Rotations of bitfield or 'circular shifts' are very often used in cryptography as an non-linear operator to achieve what they call 'confusion' (trying to break all the statistical links between the clear-text and the cipher-text).

The classical graphical representation of the rotation-left is quite self explanatory:

Rotate-left

But, it also has a meaning in pure arithmetic. It can be seen as a multiplication and an addition like that (n is the size of the bitfield):

rotate-left (a, 0) = a
rotate-left (a, 1) = 2^(1).a + 2^(-n).a
rotate-left (a, 2) = 2^(2).a + 2^(1-n).a
...
rotate-left (a, n-1) = 2^(n-1).a + 2^(-1).a
rotate-left (a, n) = a

In C language you might interpret this with only bitwise operators as follow (see the Wikipedia article about circular shift):

uint32_t rotl32 (uint32_t value, unsigned int count) 
{
  return ((value << count) | (value >> (32 - count)));
}

Anyway, the rotations are often neglected in most of the programming languages. You cannot find it easily in C for example. But, this is mainly because:

  1. Only cryptography is using it (and few other very low-level domains).
  2. It can be implemented as a function with the provided operators (it does not increase the expressive power of the language to add it to the language).

Yet, all the assembler languages propose rotations operators because they can be source of a lot of optimizations at low-level.

3

Data lossy shifts: shl eax,n : eax = ( eax*(2^n) ) %(2^32) and shr eax,n : eax = floor( eax/(2^n) )

Generally found in fast unchecked or logic bound integer arithmetic optimizations on a single data value e.g. address offsets for repeated size records.

Data lossless rotate: rol eax,n : eax = shl eax,n or shr eax,32-n

Generally used in cryptography, checked arithmetic optimizations or arithmetic on a string of data values (search long multiplication) or shifting graphical bit masks e.g. rol eax,8 gives: and eax,0xFFFFFF00 eax *2^8 for this data value and and eax,0x000000FF carry or overflow to next more significant data value.

1

The other proposed answers are clearly the primary applications of rotate instructions; however, I (perhaps somewhat uniquely) use them as a means to non-destructively access data in the upper half of 32-bit and 64-bit registers (primarily when dealing with packed data while reversing video game coordinate data).

Since there are no instructions that specifically do that task, it's necessary to shift (destructive) or rotate (non-destructive) that data to the lower half of registers so it can be accessed/modified accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.