Including .asm function in C

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello all.
How can I include some function I wrote in assembly in my C code? They are much smaller in size (LCD init, 1wire etc.) and all are void function(void)!

Thanks.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What compiler? Each compiler differs on the specifics - read the relevant doc.

Usually it is good practice to have your assembler code in a separate file and have it linked in. In your 'c' code you can simply declare the functions as 'extern' but beware of 'name mangling' - for a 'c' function named 'myfunction' my be internally named as '_myfunction'. Again, read the relevant doc.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is the syntax for AVR CodeVision:

#pragma warn- // this will prevent warnings
void function(void) // or what you prefer
{
#asm
...
...
...
...
#endasm
}
#pragma warn+ //Enable warnings

Hopefully you can use it :-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It will be compiler specific, so we will need to know what compiler you are using. Furthermore, be careful on the belief that it will be much smaller. The compiler will normally do a very good job. Much of what you may think of as waste in the compiler generated code, is actually necessary to preserve the operating environment that C expects. If you do not do this in your hand written subroutines, you run the risk of corrupting the state of your program, resulting in unpredictable behaviour. Now I'm not saying that you can't write more efficient code, I'm jsut saying to be careful when you do.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.