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



5.41 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. This extension must be explicitly enabled with the -fasm-blocks.

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.) 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 subsitution of globally unique labels. Assembly syntax errors will therefore be reported by the assembler.