Page 1 of 1

ROL with 2 variables

Posted: Thu Apr 21, 2016 3:45 pm
by Lord
Hi!

Question to the experts:

How can I use "ROL" with two variables?

Background: I saw this little piece of code (part) here:
http://www.purebasic.fr/english/viewtopic.php?p=258937

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : LightWeightEncryptString
; File : LightWeightEncryptString.pb
; File Version : 1.0.0
; Programmation : OK
; Programmed by : Hroudtwolf
; Modified by : Guimauve
; Date : 09-10-2008
; Last Update : 31-03-2009
; Coded for PureBasic V4.30
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Enumeration
 
  #LIGHTWEIGHT_ENCRYPT
  #LIGHTWEIGHT_DECRYPT
 
EndEnumeration

Procedure.c Private_RotateChar(Value.c, Direction.b)
 
  Select Direction
     
    Case #LIGHTWEIGHT_ENCRYPT
      CompilerIf #PB_Compiler_Unicode
        ! rol word [p.v_Value], 2
      CompilerElse
        ! rol byte [p.v_Value], 2
      CompilerEndIf
     
    Case #LIGHTWEIGHT_DECRYPT
      CompilerIf #PB_Compiler_Unicode
        ! ror word [p.v_Value], 2
      CompilerElse
        ! ror byte [p.v_Value], 2
      CompilerEndIf
     
  EndSelect
 
  ProcedureReturn Value
EndProcedure
;---- snip ----
This question
is it possible, to change the fixed amount of roled bits (=1) to a dynamic value (e.g. in dependency of the character position)?
was answered
2) Yes, of course. Just change the 2nd operand of ROL/ROR to a variable.
But how is this done?
I tried
"! rol byte [p.v_Value], byte [p.v_Bits]",
"! rol byte [p.v_Value], [p.v_Bits]" and
"! rol byte [p.v_Value], p.v_Bits"
with "Bits.b" as extra argument for calling the Procedure:
Procedure.c Private_RotateChar(Value.c, Direction.b, Bits.b)
What is the correct way?

Re: ROL with 2 variables

Posted: Thu Apr 21, 2016 3:56 pm
by wilbert
You can either rotate by an immediate value or by cl (the low byte of the rcx/ecx register).

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : LightWeightEncryptString
; File : LightWeightEncryptString.pb
; File Version : 1.0.0
; Programmation : OK
; Programmed by : Hroudtwolf
; Modified by : Guimauve
; Date : 09-10-2008
; Last Update : 31-03-2009
; Coded for PureBasic V4.30
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Enumeration
 
  #LIGHTWEIGHT_ENCRYPT
  #LIGHTWEIGHT_DECRYPT
 
EndEnumeration

Procedure.c Private_RotateChar(Value.c, Direction.b, Bits.b)
 
  Select Direction
     
    Case #LIGHTWEIGHT_ENCRYPT
      !movzx ecx, byte [p.v_Bits]
      CompilerIf #PB_Compiler_Unicode
        ! rol word [p.v_Value], cl
      CompilerElse
        ! rol byte [p.v_Value], cl
      CompilerEndIf
     
    Case #LIGHTWEIGHT_DECRYPT
      !movzx ecx, byte [p.v_Bits]
      CompilerIf #PB_Compiler_Unicode
        ! ror word [p.v_Value], cl
      CompilerElse
        ! ror byte [p.v_Value], cl
      CompilerEndIf
     
  EndSelect
 
  ProcedureReturn Value
EndProcedure
;---- snip ----

Re: ROL with 2 variables

Posted: Thu Apr 21, 2016 5:16 pm
by Keya
wilbert wrote:You can either rotate by an immediate value or by cl (the low byte of the rcx/ecx register).
lol i hate ones like that!!! let you use ecx for example but not eax, so sometimes its led me to believe im trying to do the impossible when its perfectly possible if i just read the damn manual to use the correct register, lol. now though if eax fails to compile i just quickly try ecx/edx/esi/edi, whew... getting there! :)

Re: ROL with 2 variables

Posted: Thu Apr 21, 2016 7:14 pm
by Lord
Hello wilbert!

Thank you for showing me the right way (in the dark [of assembler])! :D
It works like a charme.