Question

Some AMD64/x86 commands expect immediate values as operands (e.g. imm8). Is it possible to use a value stored in some register (e.g. ah) instead of this hard coded value?

As example:

If I want to shift a xmm register, there is the the pslldq-command:

PSLLDQ xmm1, imm8

I have to shift it n times (whereby n is stored in a register). Is it possible to do it without conditional jump?

Was it helpful?

Solution

You could emulate it with a pshufb and a lookup table.

shl eax, 4
pshufb xmm0, [lut + eax]

The lookup table would start with (I think)

00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
80 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E
80 80 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D

You could also use plain old unaligned reads, and use nothing "weird": (not tested)

movdqa [temp + 16], xmm0
pxor xmm0, xmm0
movdqa [temp], xmm0
neg eax
movdqu xmm0, [eax + temp + 16]

But that may suffer from a store forwarding failure, which may cost a dozen cycles.

OTHER TIPS

You could use a jump table or an indexed branch into a repeated code section if the sections are of equal length.

There is no such as instruction (for now) under x86. But you can do on hardcore way:

    mov     cl, n                        ;n as shift count number from 0 to 31
    mov     byte ptr[@_PSLLDQ + 4], cl   ;overwrite last byte of instruction
    mfence                               ;ensure that intruction is globally visible 
@_PSLLDQ:
    PSLLDQ  xmm1, 0

And of coures your code momory must have write access enabled.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top