It is currently Wed Jan 20, 2021 4:15 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: ¿Too old to ror & rol?
PostPosted: Wed Dec 11, 2013 9:53 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Jun 11, 2003 9:33 pm
Posts: 4655
Location: Spa, relaxing and thinking, and learning...
I have rescue 4 interesting functions from my old collection:
Code:
Debug "************* rol12 ************************"
Macro rol12(n,b)
  !mov ecx,12
  !xor edx,edx
  !mov eax,dword[v_#b]
  !div ecx
  !mov ecx,edx
  !mov ax,word[v_#n]
  !mov bx,ax
  !shl bx,4
  !shld ax,bx,cl
  !and ax,$0fff
  !mov word[v_#n],ax
EndMacro
;Test it:
For r=0 To 25
  c.w=$d05
  rol12(c,r)
  Debug RSet(Bin(c),12,"0")
Next
Debug "************* rol24 **************************"
Macro rol24(n,b)
  !mov ecx,24
  !xor edx,edx
  !mov eax,dword[v_#b]
  !div ecx
  !mov ecx,edx
  !mov eax,dword[v_#n]
  !mov ebx,eax
  !shl ebx,8
  !shld eax,ebx,cl
  !and eax,$00ffffff
  !mov dword[v_#n],eax
EndMacro
;Test it:
For r=0 To 25
  cl.l=$123d05
  rol24(cl,r)
  Debug RSet(Bin(cl),24,"0")
Next
Debug "************* ror12 **************************"
Macro ror12(n,b)
  !mov ecx,12
  !xor edx,edx
  !mov eax,dword[v_#b]
  !div ecx
  !mov ecx,edx
  !add ecx,4
  !mov ax,word[v_#n]
  !mov bx,ax
  !shl ax,4
  !shrd ax,bx,cl
  !and ax,$0fff
  !mov word[v_#n],ax
EndMacro
;Test it:
For r=0 To 25
  c.w=$d06
  ror12(c,r)
  Debug RSet(Bin(c),12,"0")
Next
Debug "************* ror24 **************************"
Macro ror24(n,b)
  !mov ecx,24
  !xor edx,edx
  !mov eax,dword[v_#b]
  !div ecx
  !mov ecx,edx
  !add ecx,8
  !mov eax,dword[v_#n]
  !mov ebx,eax
  !shl eax,8
  !shrd eax,ebx,cl
  !and eax,$00ffffff
  !mov dword[v_#n],eax
EndMacro
;Test it:
For r=0 To 25
  cl.l=$321d05
  ror24(cl,r)
  Debug RSet(Bin(cl),24,"0")
Next

:)

_________________
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend


Last edited by Psychophanta on Thu Dec 12, 2013 12:08 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: ¿Too old to ror & rol?
PostPosted: Wed Dec 11, 2013 11:03 pm 
Offline
Addict
Addict

Joined: Fri Nov 09, 2012 11:04 pm
Posts: 1807
Location: Uttoxeter, UK
Nice and fast! :D

Thank you for sharing.

Just a thought; doesn't this deserve to be in Tricks 'n' Tips section?

_________________
DE AA EB


Top
 Profile  
Reply with quote  
 Post subject: Re: ¿Too old to ror & rol?
PostPosted: Thu Dec 12, 2013 11:52 am 
Offline
Addict
Addict
User avatar

Joined: Wed Jun 11, 2003 9:33 pm
Posts: 4655
Location: Spa, relaxing and thinking, and learning...
davido wrote:
Just a thought; doesn't this deserve to be in Tricks 'n' Tips section?


Thanks!
Well, since all the functions are only assembler, i decided to put it here. :)

PD to any reader: Please never forget i always challenge to anybody to improve my ASM codes. :wink:

_________________
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend


Top
 Profile  
Reply with quote  
 Post subject: Re: ¿Too old to ror & rol?
PostPosted: Thu Dec 19, 2013 10:42 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3710
Location: Netherlands
It's a bit faster to compute the remainder without a div instruction.
For this purpose it seems to be accurate enough.
Code:
Procedure ROL12(n, b)
  !mov eax, [p.v_b]
  !mov ecx, eax
  !mov edx, 0x15555556
  !imul edx
  !imul edx, 12
  !sub ecx, edx
  !mov eax, [p.v_n]
  !mov edx, eax
  !ror edx, 12
  !shld eax, edx, cl
  !and eax, 0xfff
  ProcedureReturn
EndProcedure

Procedure ROL24(n, b)
  !mov eax, [p.v_b]
  !mov ecx, eax
  !mov edx, 0xAAAAAAB
  !imul edx
  !imul edx, 24
  !sub ecx, edx
  !mov eax, [p.v_n]
  !mov edx, eax
  !ror edx, 24
  !shld eax, edx, cl
  !and eax, 0xffffff
  ProcedureReturn
EndProcedure

Procedure ROR12(n, b)
  !mov eax, [p.v_b]
  !neg eax
  !mov ecx, eax
  !mov edx, 0x15555556
  !imul edx
  !imul edx, 12
  !sub ecx, edx
  !mov eax, [p.v_n]
  !mov edx, eax
  !ror edx, 12
  !shld eax, edx, cl
  !and eax, 0xfff
  ProcedureReturn
EndProcedure

Procedure ROR24(n, b)
  !mov eax, [p.v_b]
  !neg eax
  !mov ecx, eax
  !mov edx, 0xAAAAAAB
  !imul edx
  !imul edx, 24
  !sub ecx, edx
  !mov eax, [p.v_n]
  !mov edx, eax
  !ror edx, 24
  !shld eax, edx, cl
  !and eax, 0xffffff
  ProcedureReturn
EndProcedure

_________________
macOS 10.15 Catalina, Windows 10


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye