Search code examples
assemblymasmsse2sse

Assembly "dec" instruction for XMM


I'm currently passing a an external parameter from C to ASM using the following:

myFunction proc myVar:qword
    public myFunction

    movdqu xmm3,oword ptr myVar
myFunction endp

Ultimately, I want to something similar to the below but first need to determine how to decrease myVar by 1 before I can unpack and interleave the variable so that it is correct for use after being passed. I'm unable to think of the proper way to decrease/subtract the XMMs to make this work.

dec myVar
movd xmm3, myVar
punpcklbw xmm3, xmm3
punpcklwd xmm3, xmm3
punpckldq xmm3, xmm3
punpcklqdq xmm3, xmm3

Any help would be much appreciated!


Solution

  • You could use a simple subtract by one operation anytime in the above code, i.e.

    .data
      ddqONE dd 01010101h,01010101h,01010101h,01010101h
    .code
    ...
    movd xmm3, myVar
    punpcklbw xmm3, xmm3
    punpcklwd xmm3, xmm3
    punpckldq xmm3, xmm3
    punpcklqdq xmm3, xmm3
    psubb xmm3, oword ptr [ddqONE]   ; the DEC operation on byte values
    

    Another possible problem I noticed in your code:

    myFunction proc myVar:qword       ; passing 64 bit var on the stack
      public myFunction
    
      movdqu xmm3,oword ptr myVar     ; referencing it as 128 bit var!!!
    myFunction endp
    

    As you merely use 'myVar' for byte values, it would be cleaner to pass it like this (or pass it as dword, depending on 32 bit or 64 bit target platform):

    myFunction proc myVar:qword     ; 64 bit
      public myFunction
    
      movq xmm3, qword ptr myVar    ; 64 bit
    myFunction endp