ARM Technical Support Knowledge Articles

CALLING C FUNCTIONS FROM ASSEMBLY

Applies to: A51 Macro Assembler

Answer


Information in this article applies to:


QUESTION

I wish to call a function written in C from my assembly code. How do I do it?

ANSWER

The easiest way to achieve this is to let the C compiler generate the correct assembly code for you.

Suppose you have a C function called 'foo' which takes a single unsigned char argument and returns an unsigned char value. In a new C file write a dummy function that calls 'foo'. For example:

#pragma src

extern unsigned char foo(unsigned char);

void dummy(void)
{
  unsigned char x,y;
  x = 1;
  y = foo(x);
}

#pragma SRC directs the C compiler to generate assembly code when the file is compiled. The extension of the assembly file will be 'src'.

If you view the src file you will see how to call the function 'foo' from assembly. It shows the registers or memory locations used to pass the function arguments and which registers or memory locations are used to return a value. In addition it also gives you the correct function naming convention to use, which is essential for interfacing assembly to C.

You can then use the src file as a template to write the function call in your own assembly code. Note that you must also include the EXTRN directive for the function, eg:

EXTRN   CODE (_foo)

MORE INFORMATION

SEE ALSO

Article last edited on: 2010-08-25 19:31:56

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential