The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: rufus16 on January 13, 2012, 01:11:34 AM

Title: bits shift
Post by: rufus16 on January 13, 2012, 01:11:34 AM
This is not related with masm but couldnt find any mnemonic for this so im asking here.

Is there any mnemonics for bits shift that  can do that:

before  0000000000000000000000000001 -32 bits

after    00000000000000000000000000111

thanks in advanced
Title: Re: bits shift
Post by: qWord on January 13, 2012, 01:36:17 AM
maybe this does it:
mov eax,1
shl eax,4
sub eax,1

more general:
mov eax,1
mov ecx,nBits
lea ecx,[ecx+1]
shl eax,cl
sbb eax,1
Title: Re: bits shift
Post by: dedndave on January 13, 2012, 01:37:27 AM
not exactly a "bit shift"
but.....
       mov     eax,1    ;or 0
       imul    eax,7
Title: Re: bits shift
Post by: raymond on January 13, 2012, 01:45:06 AM
How about:

mov  eax,1
mov  edx,-1
shld eax,edx,2  ;you can use any count you need
;with a count of 2, EAX becomes 00000000000000000000000000000111
;and EDX remains -1


You could actually start with EAX = 0 followed by
mov  edx,-1
shld eax,edx,3

to get the same result in EAX.
Title: Re: bits shift
Post by: dedndave on January 13, 2012, 03:03:54 AM
my assumption was that if the input value was 0, the output value would also be 0
otherwise, we'd just use
        mov     eax,7
:P

or, perhaps...
        or      eax,7
Title: Re: bits shift
Post by: dedndave on January 13, 2012, 03:07:12 AM
that being the case, this may be the fastest way   :bg
        mov     eax,1   ;or 0
        neg     eax
        and     eax,7


but IMUL EAX,7 is probably smaller
Title: Re: bits shift
Post by: raymond on January 13, 2012, 03:30:00 AM
I was only trying to answer his specific question:

QuoteIs there any mnemonics for bits shift that  can do that:
Title: Re: bits shift
Post by: dedndave on January 13, 2012, 09:23:53 AM
you could also use CDQ
        ror     eax,1
        cdq
        and     edx,7

but, it uses an additional register - and an additional instruction

another way...
combine CDQ with Ray's SHLD idea

i am guessing that this is fastest, though
        neg     eax
        and     eax,7

and it uses only the one register
Title: Re: bits shift
Post by: donkey on January 13, 2012, 09:27:13 AM
Quote from: dedndave on January 13, 2012, 09:23:53 AM
you could also use CDQ
        ror     eax,1
        cdq
        and     edx,7

but, it uses an additional register - and an additional instruction

another way...
combine CDQ with Ray's SHLD idea

i am guessing that this is fastest, though
        neg     eax
        and     eax,7

and it uses only the one register

mov eax,7  :bg

I'm guessing that although the example was fixed at 7 he was looking to shift because he needed to vary the size of the shift otherwise there really is no point in using anythng but MOV. In which case Ray's works best.
Title: Re: bits shift
Post by: dedndave on January 13, 2012, 09:29:26 AM
 :lol
        or     eax,7
2 bytes shorter   :bg
Title: Re: bits shift
Post by: rufus16 on January 13, 2012, 11:25:40 AM
Hello once again. I should have given a little more information about my problem but QWords ans Raymonds answers might work.

The numbers of bits starting  from the lest significiant  that i want to set to 1  is generated dynamicially . number 7 was only an example.


Title: Re: bits shift
Post by: donkey on January 13, 2012, 11:51:13 AM
Hi rufus16,

If you only want to set a specific number of low order bits to 1 then you can use this:

mov eax,1
shl eax, nBits
dec eax


Where nBits is the number of bits to set (0-31) - 32 is a special case.
Title: Re: bits shift
Post by: sinsi on January 13, 2012, 12:00:34 PM

sub eax,eax
or edx,-1
shld eax,edx,number_of_bits

donkey, huh?

edit: donkey, oops (must remember to read and comprehend...) :red
Title: Re: bits shift
Post by: rufus16 on January 13, 2012, 12:09:28 PM
Thanks. Looks like donkeys sulution works.