Arduino Assembler Programming: Nothing Happens

Hi StackOverflow Community,

I am trying to program my old Arduino Duemilanove Board (Atmega 168V-10PU) in Assembler. I tried this several times but the code didn't execute every time. So I tried to program an equivalent test program in C and it worked. Here he is:

// file led.c
#include <avr/io.h>

int main(void)
{

    DDRB = 0xFF;
    PORTB = 0xFF;

    while (1) {
        asm("nop\n");
    }

    return 0;
}

      

The compiler's asm dump results in a shortcut,

ldi r24,lo8(-1)  ;  tmp44,
out 0x4,r24  ;  MEM[(volatile uint8_t *)36B], tmp44
out 0x5,r24  ;  MEM[(volatile uint8_t *)37B], tmp44

      

which works and activates the LED on pin 13 of Arduino (AVR pin PB5).

But when I use this asm file,

// file led.S
#include "avr/io.h"

.global main

main:
    ldi r24, 0xFF
    out DDRB, r24
    out PORTB, r24

 end:
    jmp end

      

compiler dump results in (shortening)

ldi r24, 0xFF
out ((0x04) + 0x20), r24
out ((0x05) + 0x20), r24

      

which might explain why nothing is happening.

Also, here are makefiles for C version and Assembler version

Thanks for the help!

EDIT: All assembly dump files C version and Assembler version are also presented here

EDIT 2: I looked at the register addresses in the iom168.h include file that links to iomx8.h where it says #define PORTB _SFR_IO8 (0x05)

. The compiler follows the include chain

io.h -> iom168.h -> iomx8.h
io.h -> common.h -> sfr_defs.h

      

Sfr_defs.h says:

#define _SFR_IO8(io_addr) ((io_addr) + __SFR_OFFSET)

      

A few more lines up to determine the offset:

#ifndef __SFR_OFFSET
/* Define as 0 before including this file for compatibility with old asm
sources that don't subtract __SFR_OFFSET from symbolic I/O addresses.  */
#  if __AVR_ARCH__ >= 100
#    define __SFR_OFFSET 0x00
#  else
#    define __SFR_OFFSET 0x20
#  endif
#endif

      

(Sorry for the formatting) Any idea where this error is coming from?

+3


source to share


1 answer


You have to use the helper macros _SFR_IO_ADDR()

and _SFR_MEM_ADDR()

access to SFR via an I / O instructions, and memory, respectively, because they have different addresses in the two namespaces. Memory appears to be mapped by default, but don't count on it.

So your code might look like this:

#include "avr/io.h"

.global main

main:
    ldi r24, 0xFF
    out _SFR_IO_ADDR(DDRB), r24
    out _SFR_IO_ADDR(PORTB), r24

 end:
    jmp end

      



Or you can switch to memory card access:

#include "avr/io.h"

.global main

main:
    ldi r24, 0xFF
    sts _SFR_MEM_ADDR(DDRB), r24
    sts _SFR_MEM_ADDR(PORTB), r24

 end:
    jmp end

      

+5


source







All Articles