4

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.

ReX357
  • 1,199
  • 3
  • 19
  • 43
  • possible duplicate of [What's the purpose of the rotate instructions (ROL, RCL on x86)?](http://stackoverflow.com/questions/4976636/whats-the-purpose-of-the-rotate-instructions-rol-rcl-on-x86) – phuclv Nov 13 '14 at 16:21
  • Google came up empty when I searched for 'Uses for RCL' and 'Uses for rotate carry left'. Sorry about that. – ReX357 Nov 13 '14 at 16:29
  • they were historically more useful than they are now. for single bit shifts you can cascade to shift a number as large as you have memory to do so. only one bit at a time though. they are also useful for games you can play with rotations, perhaps saving an instruction or instruction byte, but the optimizer would have to actually care to look for that. and useful for putting any bit in the carry bit for a following comparison, which again might save a some instruction bytes, and also as Ira mentioned to reverse the bits in something one bit at a time. – old_timer Nov 16 '14 at 16:17

2 Answers2

5

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.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • I'm gonna accept your answer seeing as no one came up with a real world practical example. – ReX357 Nov 17 '14 at 04:50
  • Well, I have real world practical examples. *Explaining* them is hard in my case because that code is complex, but the instructions are useful in those examples. I showed generic examples instead. – Ira Baxter Nov 17 '14 at 05:28
1

These instructions are most useful in a graphics driver. Imagine the 16 color standard VGA resolution and having numbers of 0 to 15 split up onto 4 display planes.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • Yes, I'm imagining all that... and don't see an obvious connection. If you are hinting that bit fields in a register can be manipulated by shifting, you could be less indirect. – Ira Baxter Nov 16 '14 at 16:27
  • 3
    The OP asked "What are some **practical** uses for the rotate carry left and rotate carry right instructions?" A graphics driver I have written in far less than 30000 lines has at least 50 of those RCR's and RCL's. – Sep Roland Nov 16 '14 at 16:52