News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

ROR

Started by Geryon, December 23, 2004, 10:18:09 AM

Previous topic - Next topic

Geryon

how we can define "ror" command with mathematical exp. ?

i don't get it how to works it.

dw 310Fh ;RDTSC
mov ecx,eax
;ECX=any thing
@@:
ror ecx,cl
shr ecx,cl
dec ecx   ;ECX=3-1
jmp @B

hutch--


mov ecx, some_value
ror ecx, 2
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Mark_Larson


  In an attemp to confuse everyone.  ROR = Rotate On Ready.  It only rotates when the register is ready to be rotated.  If it isn't, it stalls the pipeline.

  How did I do on confusing people?
BIOS programmers do it fastest, hehe.  ;)

My Optimization webpage
htttp://www.website.masmforum.com/mark/index.htm

Tedd

and ROL = Rotate On.... Laziness? :lol
No snowflake in an avalanche feels responsible.

raymond

Geryon

In the early days of 16-bit code, the rotates and shifts could only use "1" as an immediate count and needed to use the CL register if the required count exceeded 1. The use of CL as a possible counter has been retained and can still be usefull in some cases. However, you should not use it for rotates and shifts of ECX itself.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

Geryon

@raymond:
thanks

@rest of:
thanks for nothing...!

math. exp. of ror
ror(x,y)=[(x-(x mod (2^y))/(2^y)]+[[(x mod (2^y)]*(2^32-y)]


Ratch

Geryon,
     Don't forget the carry flag contains the value of the bit shifted into it.  That can be useful and important.  Ratch

Kestrel

#7

RCR - Rotate Through Carry Right

        Usage:  RCR     dest,count
        Modifies flags: CF OF

           +---------------+     +-+
        +->¦7 ¦---------> 0¦¦--->¦C¦¦-+
        ¦  +---------------+     +-+  ¦
        +-----------------------------+

ROR - Rotate Right

        Usage:  ROR     dest,count
        Modifies flags: CF OF

           +---------------+     +-+
        +->¦7 ¦---------> 0¦¦--->¦C¦   
        ¦  +---------------+  ¦  +-+   
        +---------------------+     

From:
http://jcomeau.freeshell.org/www/domains/risp.org/members/jcomeau/ftparea/hacking/otherstuff/intel.txt

Randall Hyde

Quote from: Geryon on December 24, 2004, 10:21:59 AM
@raymond:
thanks

@rest of:
thanks for nothing...!

math. exp. of ror
ror(x,y)=[(x-(x mod (2^y))/(2^y)]+[[(x mod (2^y)]*(2^32-y)]



I assume that you realize that this is simply a generic mathematical description of the x86 ROR instruction (sans carry)?
This, of course, is good to any number of bits. Of course, it is a *horribly slow* way to compute this value. If you know the bit size of your operands, you can do the same thing in a language like C that support shift left and right operations:

ror(x,y):
  t1 = x >> y;
  t2 = x << (bitsize - y );
  ror = t1 | t2;

Cheers,
Randy Hyde

Jibz

Quote from: Randall Hyde on January 20, 2005, 09:14:14 PM
ror(x,y):
t1 = x >> y;
t2 = x << (bitsize - y );
ror = t1 | t2;

Just make sure x is an unsigned value if you do that in C .. right-shifting a negative value is implementation-defined according to the C standard, and on most compilers it results in an arithmetic shift :U.

BigDaddy

Geryon, methinks that no one else grasps the ACADEMIC nature of your query!

gluespill

Geryon,

You're formula for 32 bit ROR is excellent.
Quotemath. exp. of ror
ror(x,y)=[(x-(x mod (2^y))/(2^y)]+[[(x mod (2^y)]*(2^32-y)]

However, it has a couple of parenthetical typos.  It has 7 begin parenthesis and only 5 ends.
Plus, I think the final (2^32-y) should actually be (2^(32-y)).
Here's what I think it should look like:
ror(x,y)=[(x-(x mod (2^y)))/(2^y)]+[(x mod (2^y))*(2^(32-y))]
or, following standard order of operations:
(x - x mod 2^y) / 2^y + (x mod 2^y) * 2^(32-y)
and, for any size register where:
x = number to be rotated
y = number of bit places to rotate
w = bit width of number or register
(x - x mod 2^y) / 2^y + (x mod 2^y) * 2^(w-y)





MichaelW

gluespill,

I checked your solution with this QBasic program, and it appears to work just as it should (I could not check Geryon's formula even after I resolved the problem with the parenthesis because with 32-signed integers it would produce an overflow).

DECLARE FUNCTION ROR& (x&, y&, w&)
DEFLNG A-Z
CLS
PRINT "w = 16"
w = 16
PRINT "press enter without input to exit"
PRINT "input in decimal, output in hex"
DO
    INPUT "x = ", x
    IF x = 0 THEN EXIT DO
    INPUT "y = ", y
    IF y = 0 THEN EXIT DO
    PRINT "x ROR y = "; HEX$(ROR(x, y, w))
LOOP

FUNCTION ROR (x, y, w)
    'x = number to be rotated
    'y = number of bit places to rotate
    'w = bit width of number or register
    ROR = (x - x MOD 2 ^ y) / 2 ^ y + (x MOD 2 ^ y) * 2 ^ (w - y)
END FUNCTION

eschew obfuscation

gluespill

MichaelW,
Cool, thanks for the validation.