Compiling a library on a 64 bit architecture: Incorrect register `%rax' used with `l' suffix

assembly, gcc, linux, x86-64

Solution

Use the `k` operand modifier to select a 32-bit sub-register: `xaddl %k0,%1`.

The syntax: `xaddl %k0,%k1` is also harmless, since `%1` is mem addr anyway. The operand modifiers for 8, 16, 32, and 64 bits are `b`, `w`, `k`, `q` respectively.

The second `"m"` in the input list seems suspect to me. I might be wrong, but I think it should be:

`"1" (indexTable[entityId])`

With `xadd` I don't suppose it matters, but this would technically be argument `%3` otherwise. Personally, I'd go with:

: "=r" (indexToWrite), "+m" (indexTable[entityId]) : "0" (1)

And yes, `"+m"` is perfectly legal. It has been for a long time, and was only recently corrected as a bug in the documentation of gcc!

Problem

I have to compile a library on a 64 bit architecture, anyway I get that error. The lines of code affected by the error are in assembler, here's an example (they are all very similar): ``` //=== get the index to write ===/// __asm__ __volatile__ ("lock; xaddl %0,%1" : "=r" (indexToWrite), "=m" ( indexTable[entityId] ) : "0" (1), "m" ( indexTable[entityId] )); ``` can you help me out? I am under linux 64bit (ubuntu) and I am using gcc.

Original source