Function-level

The function-level inline assembler lets you refer to local variables and function arguments yourself, handles such references for you.

For your own references, you must explicitly save and restore processor registers and local variables when entering and leaving your inline assembly function. You cannot refer to the variables by name, but you can refer to function arguments off the stack pointer. For example, this function moves its argument into d0:

    asm void alpha(short n)

    {

      move.w      4(sp),d0 //  n

      // . . .

    }  

To let the inline assembler handle references, use the directives fralloc and frfree, according to these steps:

  1. Declare your variables as you would in a normal C function.
  2. Use the fralloc directive. It makes space on the stack for the local stack variables. Additionally, with the statement link #x,a6, this directive reserves registers for the local register variables.
  3. In your assembly, you can refer to the local variables and variable arguments by name.
  4. Finally, use the frfree directive to free the stack storage and restore the reserved registers. (It is somewhat easier to use a C wrapper and statement level assembly.)

The code listed below is an example of using local variables and function arguments in function-level inline assembly.

Listing: Function-level Local Variables, Function Arguments
__declspec(register_abi) asm int f(int n)
    {

    register int a; // Declaring a as a register variable

    volatile int b; // and b as a stack variable

    // Note that you need semicolons after these statements.

    fralloc + // Allocate space on stack, reserve registers.

    move.l n,a // Using an argument and local var.

    add.l a,b

    move.l a,D0

    frfree // Free space that fralloc allocated

    rts

}