Adam Lee
Adam Lee

Reputation: 586

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.

Upvotes: 1

Views: 3659

Answers (2)

jcomeau_ictx
jcomeau_ictx

Reputation: 38492

Since this question is one of the top search engine hits for that message: the syntax required for an immediate shift count is: shr $13, %ebx

found it here: https://stackoverflow.com/a/46185989/493161

Upvotes: 2

Chris Dodd
Chris Dodd

Reputation: 126536

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)

Upvotes: 6

Related Questions