Here is blink implemented in assembler (inline assembler within the C compiler, withing C++, within Arduino.
There are two copies here; one pretty-printed, and one that should be easily cut&pasteable.
/* blick-asm.pde
*
- Blink using inline assembler, from Arduino IDE.
- Turns on an LED on for one second, then off for one second, repeatedly.
- One of a series of "Blink" examples using various techniques.
- This praticular example uses inline assembler, and ignores the
- arduino functions and "background" tasks.
- The circuit:
- LED connected from digital pin 13 to ground, with suitable resistor.
- Note: On most Arduino boards, there is already an LED on the board
- connected to pin 13, so you don't need any extra components for this example.
- Created Nov 2009, by Bill Westfield.
- Based on the blink.pde example distributer with the Arduino IDE,
- Created 1 June 2005 By David Cuartielles
- based on an orginal by H. Barragan for the Wiring i/o board
*/#define LEDPORT PORTB // Arduino pin 13 is bit 5 of port B
#define LEDPORT_DIR DDRB
#define LEDBIT 5// The setup() method runs once, when the sketch starts
// For this example, we're going to put ALL of our code in setup, and
// never actually return...void setup() {
asm volatile (" sbi %[portdir], %[lbit] \n" // Set bit direction
"3: " // main loop label
" sbi %[port], %[lbit] \n" // Turn on.
/*
- About the delay loop:
- The inner loop (dec, brne) is 3 cycles.
- For one second, we want 16million cycles, or 16000000/(3256256) loops.
- This is "about" 81.
*/
" clr r16 \n"
" clr r17 \n"
" ldi r18, 81 \n" // 100 * 256
"1:" // 1st delay loop label
" dec r16 \n"
" brne 1b \n"
" dec r17 \n"
" brne 1b \n"
" dec r18 \n"
" brne 1b \n"" cbi %[port], %[lbit] \n" // Turn off.
" clr r16 \n"
" clr r17 \n"
" ldi r18, 81 \n"
"2:" // 2nd delay loop label
" dec r16 \n"
" brne 2b \n"
" dec r17 \n"
" brne 2b \n"
" dec r18 \n"
" brne 2b \n"" rjmp 3b \n"
:
: [portdir] "I" (_SFR_IO_ADDR(LEDPORT_DIR)),
[port] "I" (_SFR_IO_ADDR(LEDPORT)),
[lbit] "I" (LEDBIT)
);
}// Note that we never get to loop, since setup above loops forever.
void loop()
{
}
/* blick-asm.pde
*
* Blink using inline assembler, from Arduino IDE.
*
* Turns on an LED on for one second, then off for one second, repeatedly.
*
* One of a series of "Blink" examples using various techniques.
* This praticular example uses inline assembler, and ignores the
* arduino functions and "background" tasks.
*
* The circuit:
* LED connected from digital pin 13 to ground, with suitable resistor.
*
* Note: On most Arduino boards, there is already an LED on the board
* connected to pin 13, so you don't need any extra components for this example.
*
* Created Nov 2009, by Bill Westfield.
* Based on the blink.pde example distributer with the Arduino IDE,
* Created 1 June 2005 By David Cuartielles
* based on an orginal by H. Barragan for the Wiring i/o board
*/
#define LEDPORT PORTB // Arduino pin 13 is bit 5 of port B
#define LEDPORT_DIR DDRB
#define LEDBIT 5
// The setup() method runs once, when the sketch starts
// For this example, we're going to put ALL of our code in setup, and
// never actually return...
void setup() {
asm volatile (" sbi %[portdir], %[lbit] \n" // Set bit direction
"3: " // main loop label
" sbi %[port], %[lbit] \n" // Turn on.
/*
* About the delay loop:
* The inner loop (dec, brne) is 3 cycles.
* For one second, we want 16million cycles, or 16000000/(3*256*256) loops.
* This is "about" 81.
*/
" clr r16 \n"
" clr r17 \n"
" ldi r18, 81 \n" // 100 * 256
"1:" // 1st delay loop label
" dec r16 \n"
" brne 1b \n"
" dec r17 \n"
" brne 1b \n"
" dec r18 \n"
" brne 1b \n"
" cbi %[port], %[lbit] \n" // Turn off.
" clr r16 \n"
" clr r17 \n"
" ldi r18, 81 \n"
"2:" // 2nd delay loop label
" dec r16 \n"
" brne 2b \n"
" dec r17 \n"
" brne 2b \n"
" dec r18 \n"
" brne 2b \n"
" rjmp 3b \n"
:
: [portdir] "I" (_SFR_IO_ADDR(LEDPORT_DIR)),
[port] "I" (_SFR_IO_ADDR(LEDPORT)),
[lbit] "I" (LEDBIT)
);
}
// Note that we never get to loop, since setup above loops forever.
void loop()
{
}