AVR LED Blink (asm)

LEDs connected to PORTB on ATMega2560 (or any other chip for that matter). Sink the LED instead of source. What does that mean? Have the positive of LED connected to VCC instead of connnecting it to the pins of the microcontroller. You don't want the LEDs pulling current from the controller, you want them sinking the current into the controller. That's why this config is called sink. The other is called source.

Anyway, I used 330 ohm resistors inbetween the LEDs and uC pins and connected them all to PORTB. Here's the code in assembly.

/*
 * Atmega2560asmtest.asm
 *
 *  Created: 6/1/2015 3:46:37 PM
 *   Author: DarkSector
 */ 

 .EQU F_CPU = 16000000
 .ORG 0

 LDI R16, LOW(RAMEND)
 OUT SPL, R16
 LDI R16, HIGH(RAMEND)
 OUT SPH, R16

;MAIN LABEL
MAIN:  
LDI R16, 0xFF  
OUT DDRB, R16 ;PORTB IS OUTPUT


BACK:  
COM R16 ;COMPLEMENT THE VALUE IN R16  
OUT PORTB, R16 ;PUSH THE VALUE IN R16 TO PORTB  
CALL DELAY  
RJMP BACK ;KEEP DOING THIS INFINITELY

DELAY:  
LDI R17, 100  
LOOP3: LDI R18, 255  
LOOP2: LDI R19, 255  
LOOP1: DEC R19  
BRNE LOOP1 ;KEEP DECREASING R19  
DEC R18  
BRNE LOOP2 ;FOR EVERY DECREASE OF R18 REDO THE PREVIOUS LOOP  
DEC R17  
BRNE LOOP3 ;FOR EVERY DECREASE OF R17 REPEAT PREVIOUS LOOP  
RET        ;RETURN TO PREVIOUS PC ADDRESS  

The important thing to note here is that I set the stack pointer and declared the F_CPU. Something that I didn't have to do on 8051. But I have control and that's better. Also, since this solution was built in AVR Studio, it supports comments from C and asm. I think that shouldn't be allowed, but whatever.

Also, just for comparison's sake, here's the code in C

/*
 * atmega2560_test.c
 *
 * Created: 6/1/2015 3:23:10 PM
 *  Author: DarkSector
 */ 

#define F_CPU 16000000UL

#include <avr/io.h>
#include <util/delay.h>

int main(void)  
{
    DDRB |= 0xFF;
    while(1)
    {
        PORTB = 0xFF;
        _delay_ms(500);
        PORTB = 0x00;
        _delay_ms(500);
    }
}

Shorter? Definitely, but I find it easier to understand when I use asm. But pretty soon for very long code, I am going to have to resort to C. Till then I am using ASM.