What started with the need to embed a single assembly language instruction in an Arduino sketch, became a complete flashing LED program written in AVR assembler.
ATmega328P Pin Mapping:
http://arduino.cc/en/Hacking/PinMapping168 #.UyMGKPl_tV4
AVR ATmega328P Data Sheet
www.atmel.com/Images/doc8161.pdf‎
Inline Assembler Cookbook:
http://www.nongnu.org/avr-libc/user-manual/inline_asm.html
What registers are used by the C compiler?:
http://www.nongnu.org/avr-libc/user-manual/FAQ.html #faq_reg_usage
This guy did it properly:
http://ucexperiment.wordpress.com/
Arduino sketch:
void setup() {
asm("sbi 0x04, 5");
}
void loop() {
asm("start:");
asm("sbi 0x05, 5");
asm("ldi r16,40 \n\t rcall delay");
asm("cbi 0x05, 5");
asm("ldi r16,40 \n\t rcall delay");
asm("rjmp start");
asm("delay:");
asm("dec r8");
asm("brne delay");
asm("dec r9");
asm("brne delay");
asm("dec r16");
asm("brne delay");
asm("ret");
}

By Julian

Youtuber, shed dweller, solar charge controller aficionado

15 thoughts on “Arduino experiment: playing with inline assembler”
  1. Avataaar/Circle Created with python_avatars Alex Wimmer says:

    I want to make an arduino chess computer, and this may be a way to improve massively on performance

  2. Avataaar/Circle Created with python_avatars Jose Esline Jean Francois says:

    Non gracias mi marido ta djolando

  3. Avataaar/Circle Created with python_avatars RikhA Anggraeni says:

    Hhittmimjlkjjnuyopkilhjmlbk hmmjbvjtn hhyjkmih.m

  4. Avataaar/Circle Created with python_avatars S A says:

    Hi Julian! Do you know maybe why rol is not working on PORTD register? I tried
    void
    setup() {

    DDRD = 255;

    PORTD = 1;

    }

    void loop() {

    delay(1000);

    asm("rol 0x0B");

    }

    No errors but it does not work, below works… so maybve rol can not be used on PORTD register? What can be a possible solution to use rol anyway? Thx.
    asm("sbi 0x0B, 1");

    delay(1000);

    asm("sbi 0x0B, 2");

    delay(1000);

    asm("SBI 0x0B, 3");

    delay(1000);

    asm("SBI 0x0B, 4");

    delay(1000);

  5. Avataaar/Circle Created with python_avatars Pasquale Amicone says:

    You use assembler and after insert delay, for what…

  6. Avataaar/Circle Created with python_avatars Kopinjol Baishya says:

    Thank you sir. It was very educatioinal. Can you possibly upload something like a tutorial on machine code programming with arduino?

  7. Avataaar/Circle Created with python_avatars paresh mhatre says:

    Video is really good👌

  8. Avataaar/Circle Created with python_avatars Lal Chandra says:

    Making assembly language program is much harder than making flight plan over Atlantic ocean with paper map and documents.

  9. Avataaar/Circle Created with python_avatars HEX HACK BANGLA says:

    thank you very much, now i have understood the concept …

  10. Avataaar/Circle Created with python_avatars Sebastian Andreas Gruber-Kersting says:

    Assembler in fact starts to show it's very best side when it is used exclusively, without mixing it with C/C++. At least for controllers. Did you also try to use the free of charge Atmel Studio (in plain assembler instead of C/C++) along with just the bootloader from Arduino?

  11. Avataaar/Circle Created with python_avatars Buck G says:

    very helpful

  12. Avataaar/Circle Created with python_avatars Pauls Audiolabor says:

    you can solve the problem with used registers by C with PUSH and POP, put the registers value on stack and at the end of your code back from the stack.

  13. Avataaar/Circle Created with python_avatars SalmarTheFool says:

    Sir, you are a hero. You made it really easy to comprehend. Thanks for your tutorial!

  14. Avataaar/Circle Created with python_avatars Winston Smith says:

    You never illustrated how to successfully issue the sleep command in inline asm.

  15. Avataaar/Circle Created with python_avatars PirateKitty says:

    You should do a video on how to write interrupt code for Arduino.
    There are many on YT, but none of them are done as nice as your style of videos.
    I think your audience will benefit greatly from understanding this advance subject.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.