¿Too old to ror & rol?

Bare metal programming in PureBasic, for experienced users
User avatar
Psychophanta
Addict
Addict
Posts: 4977
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

¿Too old to ror & rol?

Post by Psychophanta »

I have rescue 4 interesting functions from my old collection:

Code: Select all

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
:)
Last edited by Psychophanta on Thu Dec 12, 2013 12:08 pm, edited 1 time in total.
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: ¿Too old to ror & rol?

Post by davido »

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
User avatar
Psychophanta
Addict
Addict
Posts: 4977
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: ¿Too old to ror & rol?

Post by Psychophanta »

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
Will never leave this forum until the absolute bugfree PB :mrgreen:
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: ¿Too old to ror & rol?

Post by wilbert »

It's a bit faster to compute the remainder without a div instruction.
For this purpose it seems to be accurate enough.

Code: Select all

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
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply