Code your Arduino using Assembly language - Dwell deeper and learn the nut and bolts of standard blink LED example

Introduction

Assembly coding lets you talk to the Microcontroller's bare metal. You will be in charge of everything (from declaring heap, stack and RAM etc). Assembly instructions also execute faster. The compilation time will be less. In this project, we will see how to blink an LED using assembly programming alone.  

Assembly code for Blink LED

        ; 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 main

main:
  sbi   DDRB, 5     ; Set PB5 as output

blink:
  sbi   PINB, 5     ; Toggle PINB
  ldi   r25, hi8(1000)
  ldi   r24, lo8(1000)
  call  delay_ms
  jmp   blink

delay_ms:
  ; Delay about (r25:r24)*ms. Clobbers r30, and r31.
  ; One millisecond is about 16000 cycles at 16MHz.
  ; The inner loop takes 4 cycles, so we repeat it 3000 times
  ldi   r31, hi8(4000)
  ldi   r30, lo8(4000)
1:
  sbiw    r30, 1
  brne    1b
  sbiw    r24, 1
  brne    delay_ms
  ret
    
wokwi Arduino simulator 39.gif

What's next

To learn about programming, the following challenges can be taken by you. Once you complete the project please post the link in the comments. I will be glad to add them as well in the article if they are working. 

  • Change the code to blink the LED every two seconds once
  • Change the code so that LED will be ON for 200 ms and OFF for 1000 ms

Link to the project

https://wokwi.com/arduino/projects/290348681199092237

Other examples on free Arduino simulator

Tiny4kOLED on  ATTiny85 enables you to drive an OLED display using ATTiny85 and an OLED SSD1306 display. 

Servo motor driving is a very helpful skill. Play with the Arduino simulator, tweak your logic, tinker with the code all for free on the wokwi Arduino simulator 

A comparison between Tinkercad simulator versus wokwi Arduino simulator 

a collection of the project on the wokwi Arduino simulator

A bit about Wokwi Arduino Simulator

Arduino simulators help cut the learning time. They help in presenting your project to others very easily. You can easily share the code with others. Hardware hurdles such as hardware not available, or faulty USB cables etc won't slowdown you. Also, it will save time and money especially when you experiment with your code and projects. The simulator from wokwi comes with all the benefits.

you can visit https://wokwi.com to learn Arduino faster and free!

Puneeth pksn
Hardware enthusiast with ample interest in Arduino projects