News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

what are the uses of rol and ror instructions?

Started by jack, September 05, 2022, 01:00:11 PM

Previous topic - Next topic

jack

I know what the instructions do, just can't see a practical use, any examples where these instruction are useful?
note, just rol and ror and not the carry variants

zedd151

One of the uses is to get the mouse coordinates from lParam in response to a WM_LBUTTONUP message.

mov eax, lParam ; The x parameter is in ax, the low word of eax.
                         ; You need to either ror 16 or shr 16 to get the high word (y) into ax

mov mousex, ax
ror eax, 16         ; shr has the same affect for this purpose, and is more commonly used
mov mousey, ax


I'm on iPad right now, but that's the gist of one usage.
From MSDM re WM_LBUTTON and lParam

WM_LBUTTON
lParam


The low-order word specifies the x-coordinate of the cursor. The coordinate is relative to the upper-left corner of the client area.


The high-order word specifies the y-coordinate of the cursor. The coordinate is relative to the upper-left corner of the client area.

It is also (ror and rol as well as shr and shl) used in encryption/decryption algorithms, and compression algos as well.

hutch--

Jack,

ROL and ROR allow you to move bit patterns around in either direction by an arbitrary amount. Now if you wanted to write 2 x byte values to al and ah, thats easy but to write to the last two bytes in a DWORD, you need to rotate that DWORD by 16 bits so you can write again to al - ah and in this manner you can set all 4 bytes in a DWORD.

NoCforMe

Quote from: swordfish on September 05, 2022, 02:04:58 PM
One of the uses is to get the mouse coordinates from lParam in response to a WM_LBUTTONUP message.

You can do that, and I've done that, but you can also get both coordinates (packed into lparam) in fewer instructions:


MOVZX EAX, WORD PTR lParam
MOV MouseX, EAX
MOVZX EAX, WORD PTR lParam + 2
MOV MouseY, EAX

Assembly language programming should be fun. That's why I do it.

zedd151

NoCforMe, he had asked about practical uses of ror and rol.
I gave him an example of one usage of ror (and shr as well)
So, I believe the answer that I gave was well within the bounds of his query.
I didn't post that to make a debate about a better method.

NoCforMe

You're right. Very easy to veer off-topic here.
Assembly language programming should be fun. That's why I do it.

NoCforMe

I've never, ever used RCR or RCL. Anyone here ever used them?
Assembly language programming should be fun. That's why I do it.

hutch--

Hi guys, the usual trick with Intel mnemonics is pick what you need out of an Intel manual and use it if it does the job. Keep an eye on how and where you use shifts and rotates as they tend to be slow instructions on many CPUs.

jj2007

My statistics: 4 x rol, 5 x ror in 44,000 lines of code. One lonely rol in another 22k source. So yes, it's a rarely used instruction, but handy for checking a DWORD for bits set, using the Carry? flag.

Siekmanski

Or swap between little and big endian.
Used those instructions a lot when porting some of my old amiga code to pc code.
Creative coders use backward thinking techniques as a strategy.

NoCforMe

Ah yes, the good old 68000. Always wanted to program for that, never did. (I had an Amiga, too; great computer for its time.)
Assembly language programming should be fun. That's why I do it.

daydreamer

#11
Old few bits/ pixel bitmap data convert to new 24 bit bitmap
Most older computers had  fewer colors displays far from true color screens
Old few bits / vector format to draw vector data
Apple II had such format 3 vectors /byte
I have posted some vector draw code somewhere  in 16bit dos forum
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding


FORTRANS

Hi,

Quote from: NoCforMe on September 05, 2022, 04:02:00 PM
I've never, ever used RCR or RCL. Anyone here ever used them?

   Well yes.  Rather old code though.  16-bit code multiplying and dividing larger
data types.  Monochrome (mode 06) graphics, line drawing and state machines.
Data reformatting, such as base64 encoding and decoding.  Flag testing for the
FPU (and probably other cases).  Random number seed generator.  Moving pixels
around in some graphics programs.  So mostly moving bit patterns across byte
boundaries.

Cheers,

Steve N.

TimoVJL

May the source be with you