Keil Logo

A51: CALLING C FUNCTIONS FROM ASSEMBLY


Information in this article applies to:

  • C51 Version 5 and Later

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

ATTACHED FILES

Request the files attached to this knowledgebase article.

FORUM THREADS

The following Discussion Forum threads may provide information related to this topic.

Last Reviewed: Wednesday, September 21, 2016


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.