Basic format:

// Note that __asm__ is also valid.
asm( <assembly> )
// When assembly code modifies registers
asm( <assembly>
    : <outputs>              // optional
    : <inputs>               // optional
    : <clobbered-registers>  // optional
);
// Clobbered registers include used registers that are not listed as an input or output.

Extended inline assembly

int a=10, b;
// b = a
asm("movl %1, eax, %0;"
    :"=r"(b)        /* output */
    :"r"(a)         /* input */
    :"%eax"         /* clobbered register */
);
  • output: "constraints-and-modifiers" (expr), ...
  • input: "constraints-and-modifiers" (expr), ...
    • same thing as output, but expr can be any expression (not necessarily a lvalue)
  • constraints
    • r: use any register for this operand (refer to it using %n, where n is the operand’s zero-based index in the entire output & input list)
    • g: TODO
    • <n>: use a zero-based index to refer to a previous operand
    • <r>: specify a register to use, a = eax, b = ebx, …, S = esi, D = edi
  • constraint modifier
    • =<...>: output registers should use a ”=” constraint modifier
    • +<...>: both an input and output register
  • clobber list: list any registers that are changed and are not part of the output list
    • cc: if condition codes are modified

Registers can also be named, so instead of %%ecx or %0, we can use [var_name]. The corresponding name must appear in the operand list, e.g., asm( ... : [sum] "+r" (sum_var) : ... : ...).