Assembler Blink example

Hello,

is there an assembler example (e.g. blink.s) with command line build and
upload instructions available for Duemilanove ?
I found the following code example, but do not really know how to use it.
Got errors when try to compile

avr-as ./blink.s
and i do not have "delay_n_ms.h"

; Assembly Code:

.include   "m328Pdef.inc"

start:      

         SBI      DDRB,5


blink:              LDI      r20,250
         CALL       delay_n_ms
         SBI      PORTB,5
         LDI      r20,250
         CALL       delay_n_ms
         CBI      PORTB,5
         JMP       blink

.include   "delay_n_ms.h"

thanks wally

That looks like Atmel Assembler (the "*.inc" filenames are giveaways; the gnu assembler uses the same .h files as the C compiler.)

Are you specifically wanting to use the gnu tools?

Code modified for gnu assembler, and adding (approximately) delay_n_ms:

; Assembly Code:

;;; Allow us to use names like "PORTB"
#define __SFR_OFFSET 0
	
#include   "avr/io.h"

start:
         SBI      DDRB,5

blink: LDI      r20,250
         CALL       delay_n_ms
         SBI      PORTB,5
         LDI      r20,250
         CALL       delay_n_ms
         CBI      PORTB,5
         JMP       blink

delay_n_ms:
;;; Delay about r20*1ms.  Destroys r20, r30, and r31.
;;; One millisecond is about 16000 cycles at 16MHz.
;;; The basic loop takes about 5 cycles, so we need about 3000 loops.
	ldi 31, 3000>>8		; high(3000)
	ldi 30, 3000&255	; low(3000)
delaylp:
	  sbiw r30, 1
	brne delaylp
	subi r20, 1
	brne delay_n_ms
	ret

Compile: (note that this uses the C compiler front end, cause that's the part that knows where the .include files are and etc.)
avr-gcc -mmcu=atmega328p -nostartfiles foo.S

Convert to .hex:
avr-objcopy -O ihex a.out foo.hex

Upload via Arduino bootloader:
avrdude -patmega328p -carduino -P/dev/cu.usbmodemfd3141 -b115200 -D -Uflash:w:foo.hex

Thank you very much for the explaination.

Still have problems, but i think it's a simple path problem:

> avr-gcc -mmcu=atmega328p -nostartfiles ./blink2.s
./blink2.s: Assembler messages:
./blink2.s:9: Error: constant value required
./blink2.s:13: Error: constant value required
./blink2.s:16: Error: constant value required
>

avr-gcc -mmcu=atmega328p -nostartfiles ./blink2.s

I think you need to use a capital S for the file extension if you want gcc to know to use the C preprocessor on the file (which it needs to do for the #include and #define.)

Edit: yep:

BillW-MacOSX-2<5004> avr-gcc -mmcu=atmega328p -nostartfiles foo.S
  (no errors)
BillW-MacOSX-2<5005> cp foo.S bar.s
BillW-MacOSX-2<5006> avr-gcc -mmcu=atmega328p -nostartfiles bar.s
bar.s: Assembler messages:
bar.s:10: Error: constant value required
bar.s:15: Error: constant value required
bar.s:18: Error: constant value required

yes, capital extension "S" did it :slight_smile:

avr-gcc -mmcu=atmega328p -nostartfiles ./blink2.S
ld: warning: -z relro ignored.

thanks a lot
wally

sbi 4,5; set ddrb as output (pin13)
Loop:; Label name
sbi 5,5;set bit 5 from portb
call Delay; call subroutine and save prog counter
cbi 5,5;clear bit 5 from portb
call Delay; call subroutine again to.....
rjmp LOOP; jump to label nam loop, start over
Delay:;label name
sbr r18, 0xff;put 11111111 in rigister r18
sbr r17, 0xff;put 11111111 in rigister r17
sbr r16, 0xff;put 11111111 in rigister r16
wait:;label name
dec r16 ;Decrement r16 (-1)
brne wait; jump to label name wait if <> zero
dec r17 ;Decrement r16 (-1)
brne wait;jump to label name wait if <> zero
dec r18;Decrement r16 (-1)
brne wait;jump to label name wait if <> zero
ret;go back to call --> pc +1