News:

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

bits shift

Started by rufus16, January 13, 2012, 01:11:34 AM

Previous topic - Next topic

rufus16

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

qWord

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
FPU in a trice: SmplMath
It's that simple!

dedndave

not exactly a "bit shift"
but.....
       mov     eax,1    ;or 0
       imul    eax,7

raymond

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.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

dedndave

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

dedndave

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

raymond

I was only trying to answer his specific question:

QuoteIs there any mnemonics for bits shift that  can do that:
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

dedndave

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

donkey

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.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

dedndave

 :lol
        or     eax,7
2 bytes shorter   :bg

rufus16

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.



donkey

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.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

sinsi


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

donkey, huh?

edit: donkey, oops (must remember to read and comprehend...) :red
Light travels faster than sound, that's why some people seem bright until you hear them.

rufus16

Thanks. Looks like donkeys sulution works.