Next: , Previous: Explicit Reg Vars, Up: C Extensions


5.39 Blocks and Functions of Assembly Language

(This feature is APPLE ONLY.)

In addition to writing single statements in assembly, you can also define blocks and entire functions to use a mixed assembly and C syntax. The syntax follows that used in Metrowerks' CodeWarrior on PowerPC and Microsoft Visual Studio on x86. This extension must be explicitly enabled with the -fasm-blocks option.

The block syntax consists of asm followed by braces, with the assembly instructions on separate lines. (However, ';' may be used to put several instructions on one line in CW-style, and asm in either style.) You write labels with either a preceding '@' or a trailing ':' (or both, if you prefer); labels are always local to the asm block, and there is no way for a label in one block to refer to a label in another block. Comments and lexical rules are as for standard C/C++.

int foo (int arg) {
  register int bar;
  asm {
    li bar, 42
    add bar, arg, bar ; nop ; ; nop
  }
  return bar;
}

The function syntax uses asm as a keyword in the function definition. In this form, C declarations may appear at the beginning of the function body, in order to declare variables that you want to use in the body, but may not be used after the first assembly opcode or label (even in C99 or C++).

asm int baz (int arg1) {
    register int loc1, loc2;
    @123
    li loc1,4 * 89
    nand. r5,arg1,loc1
    ble- cr0, @123
    otherlab: nop
    mr r3,r5
}

Note that the compiler just passes the instructions through to the assembler with only necessary changes, such as a substitution of globally unique labels. Assembly syntax errors will therefore be reported by the assembler.

Also note that the use of literal registers (such as r3) in functions may not work properly with functions that are being inlined.

The following PowerPC instructions are assumed to affect memory: l... except la, li and lis (all memory loads), st... (all memory stores), sc, td..., trap, tw.... All other instructions are assumed to not affect memory.

The following PowerPC instructions take a memory operand (address operand) as their second operand, all other instructions are assumed to not:

la, lbzu, ld, ldu, lfd, lfdu, lfs, lfsu, lha, lhau, lhz, lhzu, lmw, lwa, lwz, lwzu, stb, stbu, std, stdu, stfd, stfdu, stfs, stfsu, sth, sthu, stmw, stw, stwu.

Arguments that require substitution beyond vector registers, floating point registers, general registers are not supported; an example would be trying to use the compiler to allocate condition code registers instead of just writting a specific condition code register.

On x86, the following instructions are not yet implemented by the assembler:

bound r m, cmovpe r rm, cmovpo r rm, cmovz r rm, ins m d, lods m, movs m m scas m, stos m, and xlat m.

Note, the letters after the instructions are the usual x86 contraint letters for the operands.