Examples of inline assembly statements (IBM extension)
int main(void) {
int val=40, dest;
__asm__(" ST %1,%0\n"
:"=m"(dest)
:"r"(val)
);
return 40 == dest ? 55 :66;
}
In this example, the syntax of the instruction ST is 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.
int main(void){
int sum = 0, one=1, two = 2;
__asm (" AR %[result],%[first]\n"
" AR %[result],%[second]\n"
:[result] "+r"(sum)
:[first] "r"(one),
[second] "r"(two)
);
return sum == 3 ? 0 : 1;
}
In this example, %[result] refers to the output operand variable sum, %[first] refers to the input operand variable one, and %[second] refers to the input operand variable two.
int main(void) {
int res = 25;
int newRes = 55;
__asm(" LR %0,%1\n" :"=r"(res) :"r"(newRes));
return res;
}
The LR instruction places the content of the second general purpose register to the first register. The %0 and %1 operands are substituted by the C expressions in the output/input operand fields.
The output operand uses the = modifier to indicate that a modifiable operand is required; it uses the r constraint to indicate that a general purpose register is required. Likewise, the r constraint in the input operand indicates that a general purpose register is required. Within these restrictions, the compiler is free to choose any registers to substitute for %0 and %1.
int main(void) {
int res = 25;
__asm(" AHI %0,%1\n"
:"+r"(res)
: "K"(30)
);
return res;
}
This assembly statement adds operand %0 and operand %1, 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 %1 must be an integer constant value.