Actually, I wish to compute two to a power. My restraints on the problem should make it simple, I'll need 2^X, where 1 <= X <= 8, so the result will never exceed 256 (a word quantity), but I usually work in dwords anyway.

Anyone know some simple algorythum for this? Or is it a brute force kind of thing?
Posted on 2002-03-16 10:40:25 by Ernie
How about:

eax = 2^cl

xor eax, eax
inc eax
shl eax, cl

Posted on 2002-03-16 10:46:30 by Thomas
why not just
mov eax,1
shl eax,cl
Posted on 2002-03-16 11:33:26 by The Svin
xor eax, eax/inc eax saves one byte.. Would mov eax, 1 be faster?
Posted on 2002-03-16 12:10:49 by Thomas
Thanks guys

<backs out quietly while the optomization gurus battle it out>
Posted on 2002-03-16 13:23:57 by Ernie
xor eax, eax ; and eax, 0 ; mov eax, 0
bts eax, ecx

; regmem = register or memory address

; regimm = register or immediate byte
SetPowerOfTwo MACRO regmem:REQ, regimm:REQ
;; sets regmem to equal to 2^(regimm)
and regmem, 0
bts regmem, regimm
SetPowerOfTwo BYTE PTR , eax
SetPowerOfTwo DWORD PTR , 8
SetPowerOfTwo ecx, eax
Posted on 2002-03-16 13:24:54 by bitRAKE
bitRAKE: Nice one, never used bts :). For immediate values you can just do (1 SHL X) as well, produces no code.

Posted on 2002-03-16 14:28:53 by Thomas
Would mov eax, 1 be faster?

Yes IMHO it's faster.
But if we need one instruction to put power in cl
we can brake dependences and your logic would be
as fast as with mov eax,1 and also, of course, smaller.

mov eax,1
shl eax,cl

faster than

xor eax,eax
inc eax
shl eax,cl


mov cl,power
mov eax,1
shl eax,cl

is not faster then

xor eax,eax
mov cl,power
inc eax
shl eax,cl
Posted on 2002-03-17 00:05:39 by The Svin
I see, thanks for the explanation.

Posted on 2002-03-17 03:19:03 by Thomas