Inline assembly statements (IBM extension)

When the ASM compiler option is in effect, the compiler provides support for embedded assembly code fragments among C and C++ source statements in LE enabled code.

When the METAL or GENASM option is in effect, the compiler provides support for embedded assembly code fragments among C source statements only.

The syntax is as follows:

asm statement syntax — statement in local scope


1 asm
1 __asm
1 __asm__
2?  volatile
2  (  code_format_string?  :?  output?  :?  input?  :?  clobbers )
input

1 + ,? modifier constraint ( C_expression )
output

1 + , modifier constraint ( C_expression )
volatile
The qualifier volatile instructs the compiler to perform only minimal optimizations on the assembly block. The compiler cannot move any instructions across the implicit fences surrounding the assembly block.
code_format_string
The code_format_string is the source text of the asm instructions and is a string literal similar to a printf format specifier.
output
The output consists of zero, one or more output operands, separated by commas. Each operand consists of a constraint(C_expression) pair. The output operand must be constrained by the = or + modifier (described below).
input
The input consists of zero, one or more input operands, separated by commas. Each operand consists of a constraint(C_expression) pair.
clobbers
clobbers is a comma-separated list of register names enclosed in double quotes. If an asm instruction updates registers that are not listed in the input or output of the asm statement, the registers must be listed as clobbered registers. The following register names are valid :
r0 or R0 to r15 or R15
General purpose registers
modifier
The modifier can be one of the following operators:
=
Indicates that the operand is write-only for this instruction. The previous value is discarded and replaced by output data.
+
Indicates that the operand is both read and written by the instruction.
&
Indicates that the operand may be modified before the instruction is finished using the input operands; a register that is used as input should not be reused here.
Note: The & modifier is ignored in z/OS® V1R9.
constraint
The constraint is a string literal that describes the kind of operand that is permitted, one character per constraint. The following constraints are supported:
a
Use an address register (general purpose register except r0).
d
Use a data register that is an arbitrary general purpose register. This constraint is the same as the r constraint.
g
Use a general register, memory, or immediate operand.
i
Use an immediate integer or string literal operand.
m
Use a memory operand supported by the machine.
n
Use an immediate integer.
o
Use a memory operand that is offsetable.
r
Use a general register.
s
Use a string literal operand.
0, 1, …8, 9
A matching constraint. Allocate the same register in output as in the corresponding input.
I, J, K
Constant values. Fold the expression in the operand and substitute the value into the % specifier.
  • I — signed 16-bit
  • J — unsigned 16-bit shifted left 16 bits
  • K — unsigned 16-bit constant
XL
Use only the parameter constraints listed in this constraint. XL is an optional prefix, followed by a colon (:), to introduce any of the following parameter constraints:
DS
Only valid for Metal C programs. Do not generate a definition for the operand defined in the assembly statement; instead, substitute an assembly instruction to define the operand. Optionally, to specify the data size of the operand defined in the assembly statement, use a colon (:) followed by a positive integer. If you do not specify a data size, the size specified in the ASMDATASIZE option is used.
RP
The operand requires a register pair. Optionally, to specify the constraint for the register pair, specify a :, followed by the register_type, optionally followed by another : and an optional register_pair_flag. The register_pair_flag can be one of the following:
o
The operand needs an odd/even register pair.
e
The operand needs an even/odd register pair.
If you do not specify a register type, r (general purpose register) is used as the default. If you do not specify a register pair flag, e (even/odd pair) is used as the default.
NR
Use the named general purpose register. Use a colon (:) followed by the general purpose register name (see below for acceptable register names).
Note: The XL constraints can be used for both input and output operands, with the exception of DS, which can only be used for output operands.
C_expression

The C_expression is a C or C++ expression whose value is used as the operand for the asm instruction. Output operands must be modifiable lvalues. The C_expression must be consistent with the constraint specified on it. For example, if i is specified, the operand must be an integer constant number.

Note: If pointer expressions are used in input or output, the assembly instructions honor the ANSI aliasing rule (see Type-based aliasing for more information). This means that indirect addressing using values in pointer expression operands should be consistent with the pointer types; otherwise, you must disable the ANSIALIAS option during compilation.

For more information about ASM and ANSIALIAS options, see ASM and ANSIALIAS options in the z/OS XL C/C++ User's Guide.