Programming  Arduino in assembly language

It's certainly possible to program the AVR in assembly language, but you'll have to do a little extra legwork. The compiler used by the development kit is AVR-GCC, which supports assembly language as an input, but this isn't directly an option from the GUI.

If you want to use an Arduino with its entire GUI infrastructure, libraries, etc. then it's best to use C/C++. If you just want to use it as a microcontroller board, you can achieve the same results using AVR-GCC and AVRDUDE for sending your results to your board, and bypass the GUI altogether. It's easier to program in assembly that way.

You can also mix C and assembly using the "asm" pseudo-function. By using the "naked" attribute for functions, you can write your entire program in assembly code, but from within a C file so that the compiler takes care of all the labels, section directives, etc. For example:

void somefunction(void) __attribute__((naked))
{
  asm volatile ("

 ; your assembly code here

  ");
}

Final word of advice: it's really not worth it. I understand the desire to be as close to the machine as possible, but the AVR architecture makes hand-assembly not that much less efficient than C-language-generated code. Unlike other processors (mostly from the past) where a 10-to-1 improvement is fully expected when comparing higher-level languages to assembly, in my experiences this just no longer holds. The underlying architecture is so C-friendly (lots of registers, orthogonal instructions) that the compiler-generated code is really good.

1 Like