Search code examples
assemblyx86bit-manipulationbit-shiftatt

x86 Assembly Programming (GAS Syntax): Operand type mismatch for `shl'


I am attempting to shift a number stored in the EAX register by the quantity stored in the EBX register. However, when I attempt to execute my program with the following shift statement:

shll %ebx, %eax

I retrieve the following error upon compilation:

Error: operand type mismatch for `shl'

I am confused as to what this error means as from my understand, passing in register references as parameters should be the correct usage of a shift function.


Solution

  • The shift count in an x86 instruction must either be a constant or in the %cl register. You can't use any other register for the shift count. %ebx is neither a constant nor %cl, so you get an error.

    Intel's manual shows the available forms of shl.

    If you can assume support for BMI2 extensions, shlx %ebx, %eax, %eax allows the shift count to be an arbitrary register, and is faster on Intel CPUs. (https://www.felixcloutier.com/x86/sarx:shlx:shrx)