Examples of inline assembly statements

Example 1: The following example illustrates the use of the symbolic names for input and output operands.
int a ;
int b = 1, c = 2, d = 3 ;  
__asm("  addc %[result], %[first], %[second]" 
      : [result]      "=r"       (a)
      : [first]       "r"        (b), 
        [second]      "r"        (d)
      );

In this example, %[result] refers to the output operand variable a, %[first] refers to the input operand variable b, and %[second] refers to the input operand variable d.

Example 2: The following example shows a typical use of condition registers in the clobbers.
  asm ("   add. %0,%1,%2  \n"     
       :   "=r"    (c)                    
       :   "r"     (a),
           "r"     (b)
       :   "cr0"
       );
In this example, apart from the registers listed in the input and output of the assembly statement, the add. instruction also affects the condition register field 0. Therefore, you must inform the compiler about this by adding cr0 to the clobbers.
Example 3: The following example shows the usage of the memory clobber.
 asm volatile ("   dcbz 0, %0      \n" 
               :   "=r"(b)
               : 
               :   "memory"
               );
In this example, the instruction dcbz clears a cache block, and might have changed the variables in the memory location. There is no way for the compiler to know which variables have been changed. Therefore, the compiler assumes that all data might be aliased with the memory changed by that instruction.

As a result, everything that is needed must be reloaded from memory after the completion of the assembly statement. The memory clobber ensures program correctness at the expense of program performance, because the compiler might reload data that had nothing to do with the assembly statement.

Example 4: The following example shows the usage of the + modifier and the K constraint.
 asm ("   addi %0,%0,%2"
      :   "+r"    (a)
      :   "r"     (a),
          "K"     (15)
      );

This assembly statement adds operand %0 and operand %2, and writes the result to operand %0. The output operand uses the + modifier to indicate that operand %0 can be read and written by the instruction. The K constraint indicates that the value loaded to operand %2 must be an unsigned 16-bit constant value.

Example 5: The following example shows the usage of the % modifier and the f constraint.
asm("   fadd %0, %1, %2"
    :   "=f"    (c)
    :   "%f"    (a),
        "f"     (b)
    ); 
This assembly statement adds operands a and b, and writes the result to operand c. The % modifier indicates that operands a and b can be switched if the compiler can generate better code in doing so. Each operand has the f constraint, which indicates that a floating point register is required.
Example 6: The following example shows the usage of the b constraint.
char res[8]={'a','b','c','d','e','f','g','h'};
char a='y';
int  index=7;

asm ("   stbx %0,%1,%2       \n"     \
     :                               \
     :    "r"    (a),
          "b"    (index),
          "r"    (res)
     ); 
In this example, the b constraint instructs the compiler to choose a general register other than r0 for the input operand %1. The result string of this program is abcdefgy.
Example 7: The following example shows the usage of the m constraint.
 asm ("   stb %1,%0       \n"     \
      :   "=m"    (res)           \
      :   "r"     (a)
      );

In this example, the syntax of the instruction stb is stb RS,D(RA), where D is a displacement and R is a register. D+RA forms an effective address, which is calculated from D(RA). By using constraint m, you do not need to manually construct effective addresses by specifying the register and displacement separately.

You can use a single constraint m or o to refer to the two operands in the instruction, regardless of what the correct offset should be and whether it is an offset off the stack or off the TOC (Table of Contents). This allows the compiler to choose the right register (r1 for an automatic variable, for instance) and apply the right displacement automatically.