assemblybit-manipulationprogramming-languagesbitwise-operators

Practical uses for rotate carry left/right


What are some practical uses for the rotate carry left and rotate carry right instructions?

In my assembly class and we cannot come up with a good example where this would be useful.


Solution

  • If you want to shift bits out of one operand, and into another:

           SHL  EAX, 1 ; move sign bit of EAX ...
           RCL  EDX    ; into LSB of EDX
    

    If you wanted to reverse the bits in EAX:

              MOV  ECX, 32
       loop:  SHR EAX, 1
              RCL EDX
              DEC  ECX
              JNE  LOOP
       ; EDX == EAX with bits reversed here
    

    The real point is that these rotate instructions capture "bits" of data from other operands, and allow you to combine with existing data. You want your machine to provide with a rich set of data manipulation primitives so that you can do arbitrary data shuffling.

    Having said that, in searching through an application of mine of some 30,000 assembly source lines, I only see 3 or 4 uses. But then, I have no uses of certain other instructions in the Intel instruction set. Rarely used doesn't mean useless.

    Can you live without these instructions? Sure, your CPU is Turing capable.