how to call a extern function within embedded ASM ?

Note: This was originally posted on 4th June 2009 at http://forums.arm.com

is it possibe to get the embedded assembly to call my function1()?
How do I write this ?

Thanks!
  • Note: This was originally posted on 6th June 2009 at http://forums.arm.com

    > Compiles and links quite happily.


    Cool, thanks Sim =)
  • Note: This was originally posted on 4th June 2009 at http://forums.arm.com

    You need to import the symbol for the C function in to your assembler file (equivalent of "extern" in C). Assuming you are using the ARM tools the syntax for this is:

    IMPORT function_name

    Then you assembler codecan use that like any other label:

    bl function_name

    You need to make user you obey the calling convention ABI which you compiler uses.
  • Note: This was originally posted on 5th June 2009 at http://forums.arm.com

    Hmm - does that work from the embedded assembler too (never tried - so just curuious really)?

    That's certianly the right way for the inline assembler - but I thought the "embedded" asm functions just got passed to armasm "as is".
  • Note: This was originally posted on 4th June 2009 at http://forums.arm.com

    From memory, I thought the "proper" way was actually:
    __asm void foo(void)
    {
       PUSH{r4,lr};
       BL __cpp(some_func);
       POP {r4,pc};
    }

    or
    __asm void foo(void)
    {
    LDR r0,=__cpp(some_func);
    BX r0;
    }

    i.e. using the __cpp() operator.

    hth
    s.
  • Note: This was originally posted on 5th June 2009 at http://forums.arm.com

    #include <string.h>

    __asm int myasm(char *r0)
    {
      B __cpp(strlen);
    }

    int main(void)
    {
      return myasm("abcd");
    }


    Compiles and links quite happily.

    s.
  • Note: This was originally posted on 5th June 2009 at http://forums.arm.com

    #include <string.h>

    __asm int myasm(char *r0)
    {
      B __cpp(strlen);
    }

    int main(void)
    {
      return myasm("abcd");
    }


    Compiles and links quite happily.


    For C code, generally, __cpp isn't needed (but you'll need to do an IMPORT for the name) because the symbol name is same as the name in the source code.  This isn't the case for C++ code - in that case __cpp will mangle the name to the correct symbol name so that it links correctly.

    I think (well, above example proves it) __cpp also adds in the necessary IMPORT directives too, so generally it's good practice to use __cpp(x) when comunicating between the C/C++ and embedded asm code.
More questions in this forum