Go Down

Topic: Progrsmming arduino with assembly (Read 10945 times) previous topic - next topic



Recently I learned AVR assembly language and now I would like to implement in my arduino board.But,I was using Arduino software to write and upload my sketch which supports arduino C only. I also saw a glance on Inline assembler for arduino,but that seems to have lot of rules and it doesn't seems to be flexible.I have Atmel studio,to make and verify file which basically generates a .hex file after build.Is there any way to upload that .hex file to my arduino uno board?


I think your title should be changed to "How to upload a hex file to an Arduino". You can change the title if you edit your Original Post.

It is possible. The Arduino IDE uses a program called AVRDUDE to do that.

Nick Gammon has a page about it here

Two or three hours spent thinking and reading documentation solves most programming problems.


Jul 17, 2016, 03:55 pm Last Edit: Feb 16, 2018, 04:23 am by pert
You don't need to use inline, you can use standard assembly files right in the Arduino IDE. I seem to remember there was some work done on this in the last year or two so I'd recommend using a recent version of the Arduino IDE.

Here's something I just cobbled together. It's the only time I've messed with assembly so I'm sure there's a better way to do this but it does work:

Code: [Select]
extern "C" {
  // function prototypes
  void start();
  void blink();

void setup() {

void loop() {

Code: [Select]
; Blink LED on PB5(Arduino Uno pin 13)
; http://forum.arduino.cc/index.php?topic=159572#msg1194604

#define __SFR_OFFSET 0
#include "avr/io.h"

.global start
.global blink

  sbi   DDRB,5  ; Set PB5 as output

  ldi   r20,250 ; Set the delay duration in ms. Maximum value is 255.
  call  delay_n_ms
  sbi   PORTB,5 ; Set PB5 HIGH
  ldi   r20,250
  call  delay_n_ms
  cbi   PORTB,5 ; Set PB5 LOW

  ; Delay about r20*1ms. Destroys r20, r30, and r31.
  ; One millisecond is about 16000 cycles at 16MHz.
  ; The basic loop takes about 5 cycles, so we need about 3000 loops.
  ldi   31, 3000>>8 ; high(3000)
  ldi   30, 3000&255  ; low(3000)
  sbiw    r30, 1
  brne    delaylp
  subi    r20, 1
  brne    delay_n_ms


Thanks @pert. I have bookmarked that.

Two or three hours spent thinking and reading documentation solves most programming problems.


Thank you @ pete. Can you please tell me the step by step procedure to write in assembly that you mentioned in that post


  • Open Arduino IDE. I tested the code with the hourly build, v1.6.9, and 1.6.5-r5 but I'd guess any recent version will work.
  • File > New
  • Paste the code titled blink.ino into the IDE editor window
  • Click the upside down triangle button at the top right of the editor window.
  • New Tab > Name for new file: > blink.S - actually you can give it any name you like as long as it has the .S extension. It has to be an upper case S.
  • Paste the code titled blink.S into the IDE editor window in the blink.S tab.
  • Upload the sketch to your board as usual and you should see the LED connected to pin 13 start blinking.

You can also use assembly files in libraries with the Arduino IDE in the same way, you would put the function prototypes in a header file and then include that header file in your sketch.


Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.


I'm glad if it can be of use to some people. I've been wanting to try an assembly blink just for fun ever since I learned that the IDE supported it. I'm not very interested in learning assembly but I'm glad the IDE has this capability. It's really cool that the Arduino IDE is so beginner friendly but also has support for doing advanced things.


Hi pete

Sorry for the long delay.I still get some error in the program from the blink.S while compiling. 

Error message:

blink.S.ino:5:1: error: expected unqualified-id before '.' token
blink.S.ino:20:27: error: expected constructor, destructor, or type conversion before '(' token
blink.S.ino:21:26: error: expected constructor, destructor, or type conversion before '(' token
expected unqualified-id before '.' token

Is there any solution for these error pete. I am using the latest Arduino IDE.


That's your problem. The assembly file needs to be named blink.S, not blink.S.ino. You'll need to rename that file.


Hi pete,

I changed the file to .S extension and it automatically changed it icon as asm ,where I already have atmel studio. That's fine. But now ,when I try to reopen the sketch (blink.S) ,arduino IDE shows an error message that ,it can't open this extension


The project folder should look like this:

When you open blink.ino with the Arduino IDE it will automatically load blink.S as another tab.


Hi pete,

Yes,as you told it works.But sorry to interrupt you with this last doubt.I get a small error.You gave me the code for ATmega328p,in which CALL instruction works.In my case I use Atmega8a,so I changed as RCALL. That's okay.Still I get an error.This shows "error compilation for Arduino NG or Older"

"C:\Users\acer\AppData\Local\Temp\build9c7c7e558bc8fbc32ec51c7294aac46d.tmp/core\core.a(main.cpp.o): In function `main':

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/main.cpp:46: undefined reference to `loop'

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino NG or older."

How to get rid of this error?


I got "blink.S:20: Error: illegal opcode jmp for mcu avr4" after I change the CALL instructions to RCALL. I guessed that I should change JMP to RJMP and after that it compiles fine for Arduino NG or older, ATmega8 using Arduino IDE 1.6.9 and 1.6.10.


pert ... Thank you VERY much !!!

Your post #2 above is what I have been trying to accomplish for a long time ... almost!  It seems to me that once "start" is called, it never returns to the ".ino" file code.  Doesn't it just drop down into the "blink" code in the ".S" file?  How would I get the code to return to the ".ino" file so that I am just using the ".S" code as a library type function?  I want to do some things in the IDE "C" code and some time-critical things in assember.

Thank you for your gracious assistance with this question.

Blessings in abundance, all the best, & ENJOY!
Art in Carlisle, PA USA
Blessings in abundance, all the best, & ENJOY!
Art in Carlisle, PA USA

Go Up