Search code examples
assemblyx86-64gnu-assembler

Why I get segmentation fault for psubd xmm, memory?


I have a label named LABELX in bss and I maintain a 64 bit address in it. It is introduced as follows in my code:

.section    .bss
.lcomm LABELX, 16  ### I reserved 128 bit to have the same length as XMMs

At some point in my code I have a subtraction as follows:

psubd   LABELX, %xmm11

The values are as follows:

Value in XMM11 : F7A2D830 ### the higher bits are all zeros.
Value at LABELX: 7FFFF7A2D740

I expect that the lowest 32 bits of value stored at LABELX to be subtracted from XMM11 and the result to be stored at lowest 32 bits of XMM11. Why I get segmentation fault?


Solution

  • The segmentation fault is because LABELX is not aligned to a 16 byte address.

    Define LABELX using the following:

    .local LABELX
    .comm LABELX, 16, 16
    

    The second 16 is the alignment requirement. It can be omitted, because 16 is the default ("the largest power of two less than or equal to the size of the symbol, up to a maximum of 16"). In this case, I think it is good practice to make the alignment requirement explicit, since you are relying on it.