This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

give C struct to assembler...

Note: This was originally posted on 24th November 2011 at http://forums.arm.com

how can I send the offset of a C struct to en assembly code ? For example

In my C code I have

typedef struct
{
  unsigned int a;
  unsigned int b;
} CMyStruct;
I send to an ASM function a pointer of a CMyStruct structure Let suppose that my pointer is into R0

To access to a and b attribute I need to do that.

ldr      r1, [r0, #0] // read a
ldr      r2, [r0, #4] // read b
Is there anyway to not specify #0 and #4 as contant value ? Something like

ldr      r1, [r0, CMyStruct.a] // read a
ldr      r2, [r0, CMyStruct.b] // read b
Thank's Etienne
  • Note: This was originally posted on 25th November 2011 at http://forums.arm.com


    For RVCT, the answer is (Example) 42 in "Using the Compiler ":
    http://infocenter.ar...f/Chdcceie.html


    Thank you, but in fact there is 2 problems:

    1 - I'm using gcc
    2 - I only make standalone assembly code.

    I suppose there is no solution to my problem !

  • Note: This was originally posted on 26th November 2011 at http://forums.arm.com


    I'm not sure you can in GCC assembler - I've never seen it attempted directly.

    The obvious workaround is calculate the necessary offsets in C and pass then in to the assembler function as parameters.


    Hum. that's not very efficient because you'll use register for offset instead of constant value. You could then have assembly code slower than C code. glups ;(

    My problem is to be sure that If I change my struct my asm function will be updated too.
    Since I do not find a correct solution, I've add a asm function for every C struct that check that struct size have not changed.
    That's not very good, but that's better that nothing ;)

    I will update this process by making a king of checksum instead of passing struct size.

    Etienne
  • Note: This was originally posted on 26th November 2011 at http://forums.arm.com

    Inline assemble will work for this purpose.
    typedef struct{
            unsigned int a;
    unsigned int b;
    }MyStruct_t, *MyStruct_p;
    void pass_para_to_asm(MyStruct_t para)
    {
    __asm__ volatile("mov  %0, 0x0a\n"
         "mov %1, 0x0b\n"
         :
         :=r(para.a), =r(para.B)
         ;
         )
    }
  • Note: This was originally posted on 29th November 2011 at http://forums.arm.com


    I haven't ever come across a way to communicate this information from GCC to GAS. I use #defines in my assembly files to specify the offsets. I usually create them manually. But you could write a program which converts a header and all the structs in it into a C program which creates a new ASM safe offset header files that you can #include from the ASM. The tricky part here is that the program has to run on the same device that's executing the C code, which may not be an option for some embedded platforms. So it's not something you can easily put in a makefile if you're cross-compiling.

    Doing it manually does at least help you pay more attention to organizing your data efficiently.



    Basically I did the same thing. You know once you forget to sync the offsets, the result will become undebuggable.. So additionally I will include the header file in the c code, and do a self test against the real offset in debug build to make sure things are not messed up.
  • Note: This was originally posted on 26th November 2011 at http://forums.arm.com

    I haven't ever come across a way to communicate this information from GCC to GAS. I use #defines in my assembly files to specify the offsets. I usually create them manually. But you could write a program which converts a header and all the structs in it into a C program which creates a new ASM safe offset header files that you can #include from the ASM. The tricky part here is that the program has to run on the same device that's executing the C code, which may not be an option for some embedded platforms. So it's not something you can easily put in a makefile if you're cross-compiling.

    Doing it manually does at least help you pay more attention to organizing your data efficiently.
  • Note: This was originally posted on 25th November 2011 at http://forums.arm.com

    I'm not sure you can in GCC assembler - I've never seen it attempted directly.

    The obvious workaround is calculate the necessary offsets in C and pass then in to the assembler function as parameters.
  • Note: This was originally posted on 25th November 2011 at http://forums.arm.com

    For RVCT, the answer is (Example) 42 in "Using the Compiler ":
    http://infocenter.arm.com/help/topic/com.arm.doc.dui0472f/Chdcceie.html

    Regards
    Marcus