Search code examples
assemblyx86bit-shift

Trying to shift left by EAX a variable


How can I use the command shl to shift 1 by a variable?

mov one, 1

shl one , eax 

Returns an error.


Solution

  • You can shift only shift by cl, unless you use BMI2 shifts. So for example:

    mov edx, 1
    shl edx, cl
    

    Since you're shifting the value 1 to the left, there is an other applicable instruction: bts. bts can take any register as index.

    xor edx, edx
    bts edx, eax
    

    Be careful when using it with a memory destination, the index won't be reduced modulo 32, so you can use it to (accidentally) set a bit quite far away from the base address.