The MASM Forum Archive 2004 to 2012

General Forums => The Laboratory => Topic started by: Geryon on December 23, 2004, 10:18:09 AM

Title: ROR
Post by: Geryon on December 23, 2004, 10:18:09 AM
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
Title: Re: ROR
Post by: hutch-- on December 23, 2004, 10:50:41 AM

mov ecx, some_value
ror ecx, 2
Title: Re: ROR
Post by: Mark_Larson on December 23, 2004, 04:44:18 PM

  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?
Title: Re: ROR
Post by: Tedd on December 23, 2004, 05:37:45 PM
and ROL = Rotate On.... Laziness? :lol
Title: Re: ROR
Post by: raymond on December 23, 2004, 06:05:14 PM
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
Title: Re: ROR
Post by: 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)]

Title: Re: ROR
Post by: Ratch on December 27, 2004, 04:26:50 PM
Geryon,
     Don't forget the carry flag contains the value of the bit shifted into it.  That can be useful and important.  Ratch
Title: Re: ROR
Post by: Kestrel on January 02, 2005, 04:49:07 PM

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
Title: Re: ROR
Post by: Randall Hyde on January 20, 2005, 09:14:14 PM
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
Title: Re: ROR
Post by: Jibz on January 21, 2005, 09:11:38 AM
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.
Title: Re: ROR
Post by: BigDaddy on January 21, 2005, 10:43:46 PM
Geryon, methinks that no one else grasps the ACADEMIC nature of your query!
Title: Re: ROR
Post by: gluespill on January 26, 2005, 02:26:32 AM
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)



Title: Re: ROR
Post by: MichaelW on January 26, 2005, 04:25:38 AM
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

Title: Re: ROR
Post by: gluespill on January 27, 2005, 01:04:27 AM
MichaelW,
Cool, thanks for the validation.