Crossware

Table of Contents        Previous topic       Next topic       

C COMPILER->In-Line Assembler Code->Accessing Automatic Variables and Function Parameters

Automatic variables and function parameters are allocated space on the microprocessor stack.  The compiler sets up the frame pointer A6 so that they can be readily accessed as an offset from A6.  Since the offset from A6 may vary depending upon the rest of your code, it is preferable to use the _asm syntax because the compiler will insert the appropriate offset for you.

To access an automatic variable or function parameter, you need to load its address into an address pointer:

int put_bytes(int ch1, int ch2)
{
    _asm(    move.l ({ch1},a6),a0);
    _asm(    move.l (a0),d1);
    _asm(    trap #15);
    _asm(    move.l ({ch2},a6),a0);
    _asm(    move.l (a0),d1);
    _asm(    trap #15);
    _asm(    move.l (a0),d0);
}

compiles to:

_put_bytes
    LINK.W    A6,#0
    move.l (8,a6),a0
    move.l (a0),d1
    trap #15
    move.l (12,a6),a0
    move.l (a0),d1
    trap #15
    move.l (a0),d0
    UNLK    A6
    RTS


*   Function 'put_bytes' Internal Objects
*
*   long ch1                       (8,A6)
*   long ch2                       (12,A6)

As you can see, {ch1} has been replaced by 8 which is its offset from A6 and {ch2} has been replaced by 12 which is its offset from A6.