title: Compiling an Assembly Program using avr-gcc
body: <p>I am trying to compile a simple Assembly Program with avr-gcc to run on an Attiny85. Unfortunately, the program simply doesn't work. And I get no errors while uploading and compiling. I know that the program itself should work because it works using C. So what am I missing?</p>
  
  <p><strong>Compiling and Uploading:</strong></p>
  
  <pre><code>avr-gcc blinky.S -mmcu=attiny85 -Os -g -o blinky.out
  avr-objcopy -O ihex blinky.out blinky.hex
  sudo avrdude -p attiny85 -c usbasp -P usb -e -U flash:w:blinky.hex
  </code></pre>
  
  <p><strong>blinky.S</strong></p>
  
  <pre><code>#define F_CPU 1000000L
  #include &lt;avr/io.h&gt;
  
          .section text
          .org 0
          .global init
  
          rjmp init
  
  init:
          ldi r23,0x00
          ldi r24,0xFF
          out _SFR_IO_ADDR(DDRB), r24
          out _SFR_IO_ADDR(PORTB), r23
  
          rjmp main
  
          .org 0x020
          .global main
  main:
          out _SFR_IO_ADDR(PORTB), r24
          rjmp main
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>Philipps-MacBook-Pro:Desktop philippbraun$ sh script.sh attiny85 blinky.S
  
  avrdude: AVR device initialized and ready to accept instructions
  
  Reading | ################################################## | 100% 0.00s
  
  avrdude: Device signature = 0x1e930b
  avrdude: erasing chip
  
  avrdude: safemode: Fuses OK
  
  avrdude done.  Thank you.
  
  COMPILING AS ASSEMBLY FILE
  
  avrdude: AVR device initialized and ready to accept instructions
  
  Reading | ################################################## | 100% 0.00s
  
  avrdude: Device signature = 0x1e930b
  avrdude: erasing chip
  avrdude: reading input file "blinky.hex"
  avrdude: input file blinky.hex auto detected as Intel Hex
  avrdude: writing flash (46 bytes):
  
  Writing | ################################################## | 100% 0.03s
  
  
  
  avrdude: 46 bytes of flash written
  avrdude: verifying flash memory against blinky.hex:
  avrdude: load data flash data from input file blinky.hex:
  avrdude: input file blinky.hex auto detected as Intel Hex
  avrdude: input file blinky.hex contains 46 bytes
  avrdude: reading on-chip flash data:
  
  Reading | ################################################## | 100% 0.03s
  
  
  
  avrdude: verifying ...
  avrdude: 46 bytes of flash verified
  
  avrdude: safemode: Fuses OK
  
  avrdude done.  Thank you.
  </code></pre>
  
  <p><strong>The following C program compiles successfully!</strong></p>
  
  <pre><code>#define F_CPU 1000000L
  #include &lt;avr/io.h&gt;
  #include &lt;util/delay.h&gt;
  
  int main(void)
  {
  DDRB = 0xFF; // PORTB is output, all pins
  PORTB = 0x00; // Make pins low to start
  
  for (;;) {
  PORTB = 0xFF; // invert all the pins
  //_delay_ms(5000); // wait some time
  }
  return 0;
  }
  </code></pre>