简体   繁体   English

Arduino Assembler编程:没有任何事情发生

[英]Arduino Assembler programming: Nothing happens

Hi StackOverflow community, 您好StackOverflow社区,

I am trying to program my old Arduino Duemilanove Board (Atmega 168V-10PU) in Assembler. 我正在尝试在Assembler中编写我的旧Arduino Duemilanove Board(Atmega 168V-10PU)。 I tried it a few times before but everytime the code was not executed. 我之前尝试了几次,但每次都没有执行代码。 So i tried to program an equivalent test program in C, and it worked. 所以我试着在C中编写一个等效的测试程序,并且它有效。 Here it is: 这里是:

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

int main(void)
{

    DDRB = 0xFF;
    PORTB = 0xFF;

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

    return 0;
}

The asm dump of the compiler results in (shortened), 编译器的asm转储导致(缩短),

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 at Arduino Pin 13 (AVR pin PB5). 它可以在Arduino Pin 13(AVR引脚PB5)上工作并激活LED。

But when I use this asm file, 但是当我使用这个asm文件时,

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

.global main

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

 end:
    jmp end

the compiler dump results in (shortened), 编译器转储导致(缩短),

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

what might explain why nothing happens. 什么可以解释为什么没有发生。

In addition here are the makefiles for the C version and the Assembler version 另外,这里是C版本汇编程序版本的makefile

Thanks for helping! 谢谢你的帮助!

EDIT: Here are also the full assembler dump files of the C version and the Assembler version 编辑:这里也是C版本汇编程序版本的完整汇编程序转储文件

EDIT 2: I looked up the register addresses in the include file iom168.h, which references to iomx8.h, where it says #define PORTB _SFR_IO8 (0x05) . 编辑2:我在包含文件iom168.h中查找了寄存器地址,该文件引用了iomx8.h,其中显示了#define PORTB _SFR_IO8 (0x05) The compiler follows the include chain 编译器遵循包含链

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

In sfr_defs.h is written: 在sfr_defs.h中写道:

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

A few more lines upwards the offset is defined: 向上定义了更多的偏移量:

#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 comes from? (抱歉格式化)这个错误来自哪里?

You should use the helper macros _SFR_IO_ADDR() and _SFR_MEM_ADDR() to access SFRs using i/o and memory instructions, respectively, because they have different addresses in the two namespaces. 您应该使用辅助宏_SFR_IO_ADDR()_SFR_MEM_ADDR()分别使用i / o和内存指令访问SFR,因为它们在两个名称空间中具有不同的地址。 The default is apparently memory mapped, but don't count on it. 默认显然是内存映射,但不要指望它。

As such your code could look like: 因此,您的代码可能如下所示:

#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 mapped 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM