1

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Adam Lee
  • 436
  • 1
  • 14
  • 49

2 Answers2

6

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)

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • I think they tried using `ebx` as shift count (first operand in AT&T syntax). But other than that your answer is correct. – ecm Oct 30 '20 at 23:03
2

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

jcomeau_ictx
  • 37,688
  • 6
  • 92
  • 107