Search code examples
assemblymmx

x86_64 ASM: Alternative to movq?


Is there an alternate way to put a qword in an mmx register?

For example, in this code snippet:

db random 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa
run:
    movq mm2, [rel random]

I can now work with that as a qword 0xaaaaaaaaaaaaaaaa and do pxor etc with it. Is there some other way I could put that data in the register? Maybe by pushing/popping to it?


Solution

  • No idea why you want to do this, but you can avoid movq if you zero the register and then bring in the value using some operation, such as:

    pxor mm2, mm2
    pxor mm2, [rel random]
    

    Alternatively, you can load the words using pinsrw:

    pinsrw mm2, [rel random], 0
    pinsrw mm2, [rel random + 2], 1
    pinsrw mm2, [rel random + 4], 2
    pinsrw mm2, [rel random + 6], 3
    

    This approach also works in reverse using pextrw but have to go through a general purpose register:

    pextrw eax, mm2, 0
    mov [rel random], ax
    pextrw eax, mm2, 1
    mov [rel random + 2], ax
    pextrw eax, mm2, 2
    mov [rel random + 4], ax
    pextrw eax, mm2, 3
    mov [rel random + 6], ax
    

    If only movq must be avoided but maskmovq can be used then you can do:

    pcmpeqb mm1, mm1
    lea rdi, [rel random]
    maskmovq mm2, mm1