0

I am working with some assembler masks. I am trying to logical left shift one register by another. By that I mean for example: I have register %rax = 0x10 and register %rcx = 0x2. I am attempting to use this command:

salq %rcx, %rax

to shift 0x10 by 2 bits. so 00001010 -> 00101000 (10 -> 40 in decimal) however I am unable to do that between two registers. One of them has to be an immediate or a varriable (which I dont want to use)

is it possible to do this?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
TBB
  • 3
  • 2
  • Binary `00001010` is decimal 10. 0x10 would be hex, which is binary `00010000`, decimal 16. – Nate Eldredge Dec 07 '21 at 19:30
  • 1
    That assembles fine for me, though technically it is only the low byte `%cl` that is used as the shift count, so `salq %cl, %rax` would be the more normal way to write it. What's the issue for you? Note these instructions are hardcoded to use `cl` and you couldn't use `bl` for instance, you'd have to move it into `cl` first. – Nate Eldredge Dec 07 '21 at 19:32
  • 2
    If you really want to use another register, and you have the BMI2 instructions, you can use [`shlx`](https://www.felixcloutier.com/x86/sarx:shlx:shrx). It is several more bytes of code, though. – Nate Eldredge Dec 07 '21 at 19:34
  • Thanks! it works with %cl. Though I don't know why it didn't work with %rcx. Anyway, thank you all for the help! – TBB Dec 07 '21 at 19:57
  • 1
    It doesn't work with `%rcx` because the instruction-set reference manual says the shift-count register has to be CL. https://www.felixcloutier.com/x86/sal:sar:shl:shr. (GAS silently accepts `%rcx` for some reasons; clang correctly complains that anything other than `%cl` or an immediate is invalid, @Nate. Since x86 scalar shifts mask their count, %rcx would be logically equivalent so it's fine that GAS accepts this "pseudo-instruction" form.) – Peter Cordes Dec 08 '21 at 03:30
  • @PeterCordes this piece of information helped me... now I can shift within registers without an immediate.... thanks a lot!! – RajibTheKing Nov 22 '22 at 11:39

0 Answers0