A Short Blink

short blink
Here is my entry into the “smallest blink program contest” compiled in the arduino IDE. Can you determine how it works?

//program only for atmega168 based arduino with supplied bootloader
typedef void (*func_type)(uint8_t);
func_type flash_led = (func_type) 0x1d17;

int main(void) {
  for(;;)
    flash_led(0xff);
}

The arduino IDE reports 184 bytes total. The relevant part of the program compiles to the following loop of 14 bytes:


main:
a6: e0 91 00 01 lds r30, 0x0100
aa: f0 91 01 01 lds r31, 0x0101
ae: 8f ef ldi r24, 0xFF ; 255
b0: 09 95 icall
b2: f9 cf rjmp ;.-14

Obviously, the standard startup and arduino code adds about 150 bytes. Removing the global variable takes it to 142 bytes using the newest ide (1.6.0):

//program only for atmega168 based arduino with supplied bootloader
int main(void) {
  for(;;)
    asm volatile(
      "ldi r24,0xff \n\t"
      "ldi r30,0x17 \n\t"
      "ldi r31,0x1d \n\t"
      "icall \n\t"
    );
} 

I’ve compiled the same code inside ATMEL Studio using a slimmed down gcrt1.s start file, eliminating the IVT and I was able to reduced the entire hex file down to 32 total bytes.

Advertisements

About Jim Eli

µC experimenter
This entry was posted in Uncategorized and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s