News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

ROL-ling bytes

Started by frktons, June 24, 2010, 11:51:35 AM

Previous topic - Next topic

frktons

I'm studying SHR/SHL and ROL/ROR.

I've tried with numbers, and more or less I've understood what
happens.

I'm not sure what happens if I do a ROL instruction over a string.

I've tried this code, but it feaks out. What happens? The print MACRO
stops working and the program has to be closed via OS.


include \masm32\include\masm32rt.inc


.data

str1        db "ABCD", 0
pstr1       dd 0;


.code
start:

    mov pstr1, offset str1
    print "Original string 4 bytes long = "
    print pstr1,13,10

    mov eax, pstr1
    mov eax, [eax]
    rol eax, 1
    mov [pstr1], eax

   
    print "String after rol 1 = "
   
    print pstr1,13,10
                                     
    inkey chr$(13, 10, "--- ok ---", 13)

    exit

end start


Thanks
Mind is like a parachute. You know what to do in order to use it :-)

frktons

Oh, well, I discovered what is that:


    mov ebx, pstr1
    mov [ebx], eax


this work, instead of


    mov [pstr1], eax


This means that the pstr1 is not a real pointer, maybe it should be
declared in a different way, or it simply cannot substitute registers
for indirection ?

The print MACRO accept both pstr1 and ebx to work  ::)
Mind is like a parachute. You know what to do in order to use it :-)

hutch--

Frank,

This also works in MASM fine.


.data
  text db "My written text",0
  ptxt dd text
.code
    mov eax, ptxt    ; load the address of the above text into EAX
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

frktons

Quote from: hutch-- on June 24, 2010, 12:40:21 PM
Frank,

This also works in MASM fine.


.data
  text db "My written text",0
  ptxt dd text
.code
    mov eax, ptxt    ; load the address of the above text into EAX


Thanks Hutch.

I'm wondering about a couple of things related to pointers and ROL/ROR
mnemonics.

1 - if I have a variable that contains the address of a string, can I use it
for indirection?
I mean this code will work?

mov [ptxt], eax

or the indirection has to be done by registers only?

mov ebx, ptxt
mov [ebx], eax


2 -the ROL and ROR mnemonics should do a rotate left/right without
eliminating bits from the register, but only moving them.

if I do a ROL on a register of 1 position and after a ROR on the same register
of 1 position, do I have the same original value ?

Thanks
Mind is like a parachute. You know what to do in order to use it :-)

KeepingRealBusy

1. No.

2. Yes.

Note. Look at RCL and RCR that rotate bits through the carry flag.

frktons

Quote from: KeepingRealBusy on June 24, 2010, 01:16:26 PM
1. No.

2. Yes.

Note. Look at RCL and RCR that rotate bits through the carry flag.

Thanks KRB,

after doing some experimentation I realized what you are saying.
The RCL and RCR are non destructive rotation like ROR and ROL?

Frank
Mind is like a parachute. You know what to do in order to use it :-)

FORTRANS

Hi,

QuoteThe RCL and RCR are non destructive rotation like ROR and ROL?

   ROL and ROR are rotate the contents of a register (or memory
location).  Bits shifted out one end are shifted into the other end
of the register.

   RCL and RCR are rotate the contents of a register (or memory
location) through the carry bit.  Bits shifted out one end are shifted
into the carry bit and the carry bit is shifted into other end of the
register.

HTH,

Steve

hutch--

Frank,

This line,


mov [ptxt], eax


in MASM is no different to,


mov ptxt, eax


Indirection is done in registers


mov eax, [esi]  ; where ESI holds the address of something.
mov [edi], eax ; EDI being the target.


MASM tolerates extra brackets by ignoring them, they can be used as an addition of a displacement in complex addressing mode code.


mov eax, [esi][64]  ; address in ESI + 64 bytes displacement.

Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

clive

    mov ebx, pstr1
    mov eax, [ebx]
    rol eax, 1
    mov [ebx], eax


    mov ebx, pstr1
    rol dword ptr [ebx], 1

It could be a random act of randomness. Those happen a lot as well.

frktons

Thanks all of you.  :U


Quote from: clive on June 24, 2010, 03:23:56 PM
    mov ebx, pstr1
    mov eax, [ebx]
    rol eax, 1
    mov [ebx], eax


    mov ebx, pstr1
    rol dword ptr [ebx], 1


I was thinking about something similar:

  mov ebx, pstr1
  rol [ebx], 1


But I've to give it a try, I don't know if it works, it looks a bit
different form what you propose.

Let me see...

Well my idea doesn't work  :lol

Better to stick with yours:
    mov ebx, pstr1
    rol dword ptr [ebx], 1


So dword ptr tells MASM that ebx is referring to a
4 bytes position in memory that are rolled left by 1 bit.  :P

Thanks
Mind is like a parachute. You know what to do in order to use it :-)

clive

Quote from: frktons
So dword ptr tells MASM that ebx is referring to a
4 bytes position in memory that are rolled left by 1 bit.  :P

Yes, otherwise it can't infer a size for the operation, like it can for a register operation. You could use "byte ptr", or "word ptr", as appropriate.
It could be a random act of randomness. Those happen a lot as well.

frktons

Quote from: FORTRANS on June 24, 2010, 01:56:37 PM

RCL and RCR are rotate the contents of a register (or memory
location) through the carry bit.  Bits shifted out one end are shifted
into the carry bit and the carry bit is shifted into other end of the
register.

HTH,

Steve

If I need [for example] to rotate left 4 positions a string, do something with
the new content and after rotate right 4 positions to get back the original string
can I use both the couple ROL/ROL and RCL/RCR, or in this case is
necessary to use ROL/ROR in order not to destruct any bit inside the
string?

Quote from: clive on June 24, 2010, 04:58:05 PM

Yes, otherwise it can't infer a size for the operation, like it can for a register operation. You could use "byte ptr", or "word ptr", as appropriate.
:U thanks clive
Mind is like a parachute. You know what to do in order to use it :-)

frktons

Well, apparently I can do it with RCL/RCR as well:

include \masm32\include\masm32rt.inc


.data

str1        db "ABCD", 0


.code
start:

    mov ebx, offset str1
    print "Original string 4 bytes long = "
    print ebx,13,10

    rcl dword ptr [ebx], 4

   
    print "String after rcl 4 = "
   
    print ebx,13,10

    rcr dword ptr [ebx], 4

   
    print "String after rcr 4 = "
   
    print ebx,13,10
   
                                     
    inkey chr$(13, 10, "--- ok ---", 13)

    exit

end start


and the output:

Original string 4 bytes long = ABCD
String after rcl 4 = ↕$4D
String after rcr 4 = ABCD

--- ok ---


I'm not sure if I do something between the rcl 4 and rcr 4
that changes the carry flag if it works as well  ::)
Mind is like a parachute. You know what to do in order to use it :-)

MichaelW

Rotates through carry are relatively slow.

;==============================================================================
    include \masm32\include\masm32rt.inc
    .586
    include \masm32\macros\timers.asm
;==============================================================================
    .data
    .code
;==============================================================================
start:
;==============================================================================

    invoke Sleep, 3000

    counter_begin 1000, HIGH_PRIORITY_CLASS
      REPEAT 8
        rol eax, 4
        rol ebx, 4
        rol ecx, 4
        rol edx, 4
      ENDM
    counter_end
    print str$(eax),13,10

    counter_begin 1000, HIGH_PRIORITY_CLASS
      REPEAT 8
        rcl eax, 4
        rcl ebx, 4
        rcl ecx, 4
        rcl edx, 4
      ENDM
    counter_end
    print str$(eax),13,10

    inkey "Press any key to exit..."
    exit
;==============================================================================
end start


Running on a P3:

27
248

eschew obfuscation

FORTRANS

Hi,

QuoteI'm not sure if I do something between the rcl 4 and rcr 4
that changes the carry flag if it works as well

   Well, you will have a 50 - 50 chance it will be okay.  (There are
only two possible states for the carry flag.)  You will be better
off using ROL and ROR for what you are talking about.  But you
have the general idea.

   The use I have for RCL and RCR is to multiply or divide numbers
that are larger than one register.  If you have a number in EDX:EAX
for instance, the following would divide it by four.


        MOV     EAX,[Array+0]   ; Load low DWORD of 64 bit number.
        MOV     EDX,[Array+4]   ; Load high DWORD.
                        ; Divide 64 bit number by two.
        SHR     EDX,1   ; Divide high by 2, and put low bit into carry.
        RCR     EAX,1   ; Divide Low by 2, and get high bit from carry.
                        ; Divide 64 bit number by two (four total).
        SHR     EDX,1
        RCR     EAX,1


Regards,

Steve N.