ROL with 2 variables

Bare metal programming in PureBasic, for experienced users
User avatar
Lord
Addict
Addict
Posts: 847
Joined: Tue May 26, 2009 2:11 pm

ROL with 2 variables

Post 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?
Image
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: ROL with 2 variables

Post 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 ----
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: ROL with 2 variables

Post 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! :)
User avatar
Lord
Addict
Addict
Posts: 847
Joined: Tue May 26, 2009 2:11 pm

Re: ROL with 2 variables

Post by Lord »

Hello wilbert!

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