Search code examples
x86att

AT&T x86 | What does `xadd %eax, (%ecx)` do?


Does it exchange %eax and value stored at address%ecxx and store the sum in address%ecx?


Solution

  • The instruction XADD ...

    ...Exchanges the first operand (destination operand) with the second operand (source operand), then loads the sum of the two values into the destination operand.

    So, according to its Operation, it executes the following microcode:

    TEMP ← SRC + DEST;
    SRC  ← DEST;
    DEST ← TEMP;
    

    In your case this means that xadd %eax, (%ecx)

    • Creates a TEMP variable containing the addition of the value of EAX plus the value at the address pointed to by ECX
    • Moves the value at the address pointed to by ECX to EAX
    • Moves the TEMP variable to the address pointed to by ECX

    This instruction can be combined with a LOCK prefix and hence be executed atomically.