This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How to access 'C' header file defined structures in in-line assembly?

If I try to to add an in line asm("xxx") statement as below, it is refusing to accept, though the appropriate C header files are included in .c files. The generated error E0009 listed at the end.

MOV *+XAR5[MYCONTROL.sampleCount], #0

A construct like following is also disallowed either within function or globally:
 asm(" .cdecls C,LIST, \"system.h\" ");
Then how can we pass on the structure related information to inline assembly? 

 

 at line 1761:

 [E0009]

         Missing struct/union member or tag

 

Can someone suggest a way out?

Thanks in anticipation.

Sayee

  • The cdecls statement is not allowed in a C/C++ asm statement as described here

    This comes from the ARM assembly language and tools users guide (http://focus.ti.com/lit/ug/spnu118i/spnu118i.pdf).  Other processor families supporting the cdecls directive have similar statements.

    The key point here is that when you compile your C code, it doesn't generate machine instructions but rather an assembly file which is then passed to the assembler for conversion to machine instructions which are stored in an object file suitable for the linker to use.  Thus all of your C/C++ structures, unions, enum, etc.  have already been converted into appropriate assmbly style constructs which are available for you to use within an asm( ) statement.  Generally you only need to preceed types or global variables with a leading underscore to gain access to them using asm( ) statements.  If this doesn't work directly for you, set the compiler to save the generated assembly files and take a look at the assembly definition of the structure you are looking for.

    Note that local variables are stored on the stack or optimized into registers.  In either case, most of your local variables will not be available to your asm statements as they are merely an offset onto the stack or a register reference known only to the compiler during translation.

    If you are wanting to include your C/C++ files directly in an assembly source file, you don't need the asm statement as that is purely a C/C++ source file construct.

    Jim Noxon

     

  • hello,

    I'm trying to write some inline assembler code that accesses the attributes of a structure that is passed to it. The previous email said you could just add an '_' to the front of the type, but I've had no luck in getting this working. I've also looked at the generated .asm file and still couldn't see anything that helped.

    In my simple example below I want to use +XAR4[_MyStruct.b] instead of +XAR4[1]. I don’t want to hard code the attribute's offset in the assembler, I want to be able to get the attributes offset from the structure. Can somebody please show me how to achieve this.


    typedef struct MyStruct
    {
        int a;
        int b;
        int r;
    }MyStruct;

    void Add (MyStruct * Ptr)
    {
    #if 0
        Ptr->r = Ptr->a + Ptr->b;
    #else
        asm(
        "  MOV  AL, *+XAR4[0]\n" //*+XAR4[_MyStruct.a]
        "  ADD  AL, *+XAR4[1]\n" //*+XAR4[_MyStruct.b]
        "  MOV  *+XAR4[2], AL\n" //*+XAR4[_MyStruct.r]
        );
    #endif
    }

    void main(void)
    {
        MyStruct x;
        x.a = 4;
        x.b = 2;
        Add(&x);
    }

    My final goal is to write a inline 2p2z controller that is called from "C" code. It needs to be assembler because there aren't the required intrinsic commands to do this. Also to avoid the calling overhead it has to be inline. And to enable me to debug the rest of my code I don’t want to turn on full program level optimization.

    cheers

    Chris

  • Maybe the .struct/.endstruct directive (see the spnu118j) helps to construct something like you want - but I'm unsure if that is a safe way, as in my opinion the alignment of C struct and the "shadow declaration" by .struct/.endstruct not necessarily will be the same.

    Me too was interested in a running example or contra hints concerning this. If nobody else answers here, it might be a good idea to start a new thread, as this thread already has been marked as answered and so the new question might be overlooked by others.

    Besides, the link provided by Jim to spnu118i did not work for me - in those cases it's always helpful to use TI's document mechanism www.ti.com/lit/pdf/<docname> - it delivers the alphabetically latest document with the given <docname> in its name root.

    So currently

    www.ti.com/lit/pdf/spnu118

    has lead to

    www.ti.com/lit/ug/spnu118j/spnu118j.pdf

    This way makes sense of course only for those documents which have a sometimes increased release character in their name, like above the 'j'.

    Kind regards,
    Joern.

  • Hi Joern,

    I've moved my question to a new post http://e2e.ti.com/support/development_tools/compiler/f/343/t/219557.aspx

    I did read in the assembler tools you could use .cstruct (Acts like .struct, but adds padding and alignment like that which is done to C structures), but that would mean creating a copy of my structure within the inline assembler function.

    Thank you for your help.

    cheers

    Chris