2

I would like to use (link) a function written in assembler in an Arduino project. What I want to do is:

  • write the function in assembler using avr-gcc calling conventions
  • declare the function as extern in the .ino file
  • provide the instructions and other information to link the assembler function into the project

I do not want(or I already know how to):

  • use inline assembly (prologue and epilogue prevents effective parameter handling)
  • use alternative ways/toolchains/programmers
Cybergibbons
  • 1,934
  • 1
  • 16
  • 21
  • Is this different to this question? http://electronics.stackexchange.com/questions/12750/arduino-with-assembly – Cybergibbons Mar 01 '13 at 09:07
  • Have you tried the Arduino forum? – Leon Heller Mar 01 '13 at 09:35
  • I personally never had to use assembler in an Arduino project so I'm curious: what is your use case, why can't C do the job? – jippie Mar 01 '13 at 13:28
  • I'm writing code for a 3phase frequency converter which is quite time critical. This contains repeated divisions by 3*2^n (because of the 3 phases). This makes the use of shift operations for division impossible so i'm planning to use a shift multiply routine. – Stefan Nilsson Mar 01 '13 at 13:37
  • You could write a short routine in C, then check the disassembly and see what the compiler made out of it. The compiler is surprisingly good in optimizing code. And of course you are free to reuse that code in your own assembly code. – jippie Mar 01 '13 at 14:00
  • Yes, and i am doing that. BUT: this does NOT answer my original questtion, how to use assembler in arduino without using external toolchains or inline code. – Stefan Nilsson Mar 01 '13 at 14:16
  • How isn't this just looking in the compiler manual to find the calling conventions, then implementing your assembler routine accordingly? I don't understand what the question is exactly. – Olin Lathrop Mar 01 '13 at 14:17
  • I know the calling conventions. The problem is to get the arduino IDE to acccept the assembler code or the assembled object code. Putting the object- or assembler file in the code- or build directory does not work. As i stated i do not want to use an external toolchain or inline code which are the only options i found so far – Stefan Nilsson Mar 01 '13 at 14:29
  • @jippie: perhaps you're right about compiler optimization, but there is no way of knowing that before actually testing and in order to do that i need to able to insert the assembler code – Stefan Nilsson Mar 01 '13 at 14:31
  • I don't think you can get the Arduino IDE to accept this as is. You can change it to, but why not just used an avr-gcc toolchain at this point? http://www.cs.nmsu.edu/~jcook/arduino/index.php?n=Notes.AssemblyMods – Cybergibbons Mar 01 '13 at 16:41
  • My point was to have a look at the compiled code and reuse it if it turns out smarter/more efficient than your own. – jippie Mar 02 '13 at 00:23

2 Answers2

2

I found the answer myself here. So either you write to the libraries or you recompile the IDE. But never mind, it works! A bit messy (as expected). Please note that the folder, the .h file and the .S file, but not the function itself, must all have the same name.

m.Alin
  • 10,638
  • 19
  • 62
  • 89
1

You could create static ".a" library rather than a Arduino library. This is were you compile ".o" objects files derived from your source (be it .s .c or .cpp) into "libname.a" files. This is similar to "libc.a". The following article explains there operation and creation http://www.nongnu.org/avr-libc/user-manual/library.html

The difficulty would be how to get the Arduino IDE's built in make to use it. I see from the IDE's verbose output that it creates core.a, but not libc.a, which is already existing. As would be desired case here. Further I now my projects use the pre-build libc.a and libc is not present in the verbose build log. So it may be as simple as putting the and new libname.a files into the appropriate directories. Which looks like it may be at least ..\Arduino\hardware\tools\avr\avr\lib.. but also may need to be built for each family of the avr's.

mpflaga
  • 1,345
  • 6
  • 10