c++ - Arduino 内联 ASM 未按预期工作

标签 c++ assembly arduino avr-gcc atmega

我正在尝试构建一个时钟,所以我正在使用 ASM 和 arduino。对于大多数部分,纯 C 就可以了,但是为了准备输出到 BCD 到十进制转换器的时间,我决定使用 ASM。我在 8086 C++/ASM 中编写了以下代码,它在我的计算机上运行良好:

#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
    for(int num = 0; num < 16; num++) {
        int bin[4];
        bin[0] = bin[1] = bin[2] = bin[3] = 0;
        asm("movl %%ebx, %%eax;"
            "andl $8, %%eax;"
            "cmp $0, %%eax;"
            "je a;"
            "movl $1, %0;"
            "jmp b;"
            "a: movl $0, %0;"
            "b: movl %%ebx, %%eax;"
            "andl $4, %%eax;"
            "cmp $0, %%eax;"
            "je c;"
            "movl $1, %1;"
            "jmp d;"
            "c: movl $0, %1;"
            "d: movl %%ebx, %%eax;"
            "andl $2, %%eax;"
            "cmp $0, %%eax;"
            "je e;"
            "movl $1, %2;"
            "jmp f;"
            "e: movl $0, %2;"
            "f: movl %%ebx, %%eax;"
            "andl $1, %%eax;"
            "cmp $0, %%eax;"
            "je g;"
            "movl $1, %3;"
            "jmp h;"
            "g: movl $0, %3;"
            "h: nop"
            : "=r" (bin[0]), "=r" (bin[1]), "=r" (bin[2]), "=b" (bin[3])
            : "b" (num)
            : "%eax"
            );
        cout << num << ": ";
        for(int i = 0; i < 4; i++) {
            cout << bin[i];
        }
        cout << endl;
    }
    return 0;
}

但是,当我修改它以在 Arduino 上运行时,一切都完全停止了:

  for(uint8_t num = 0; num < 16; num++) {
  uint8_t bin[4];
  bin[0] = bin[1] = bin[2] = bin[3] = 0;
  asm("mov __tmp_reg__, %[val];"
    "and __tmp_reg__, $8;"
    "cmp __tmp_reg__, $0;"
    "je a;"
    "mov %[bit8], $1;"
    "rjmp b;"
    "a: mov $0, %[bit8];"
    "b: mov %[val], __tmp_reg__;"
    "and __tmp_reg__, $4;"
    "cmp __tmp_reg__, $0;"
    "je c;"
    "mov %[bit4], $1;"
    "rjmp d;"
    "c: mov $0, %[bit4];"
    "d: mov %[val], __tmp_reg__;"
    "and __tmp_reg__, $2;"
    "cmp __tmp_reg__, $0;"
    "je e;"
    "mov %[bit2], $1;"
    "rjmp f;"
    "e: mov $0, %[bit2];"
    "f: mov %[val], __tmp_reg__;"
    "and __tmp_reg__, $1;"
    "cmp __tmp_reg__, $0;"
    "je g;"
    "mov %[bit1], $1;"
    "rjmp h;"
    "g: mov $0, %[bit1];"
    "h: nop"
    : [bit8] "=r" (bin[0]), [bit4] "=r" (bin[1]), [bit2] "=r" (bin[2]), [bit1] "=r" (bin[3])
    : [val] "r" (num)
    : "r0"
    );

8086 代码给出了您期望的输出:

0: 0000
1: 0001
2: 0010
3: 0011
...
13: 1101
14: 1110
15: 1111

但是在 Arduino 上运行的代码给出了不同的输出:

0: 5000
1: 0000
2: 0000
3: 0000
... (zeros continue)
13: 0000
14: 0000
15: 0000

如您所想,如果代码返回……五,代码将变得毫无用处。当源代码中没有任何接近 5 的地方时,我对它如何返回 5 毫 headless 绪。我不知道在这里做什么,所以我真的需要一些帮助。

我使用的是 Arduino Leonardo,它有一个 ATMega32U 处理器。我尝试反汇编由 Arduino 软件生成的可执行文件(使用 AVR-GCC 编译它),但我似乎无法找到我放入的代码。

感谢您的宝贵时间,Stack Overflow。

最佳答案

您的代码可以很容易地用 C++ 编写,如下所示:

 int bin[4] = {};

 bin[0] = !!(num & 8);
 bin[1] = !!(num & 4);
 bin[2] = !!(num & 2);
 bin[3] = !!(num & 1);

或:

int bin[4];
int bit = 8;
for(int i = 0; i < 4; i++)
{
    bin[i] = !!(num & bit);
    bit >>= 1;
}

如果您不喜欢 !!(这使得“获取下一个值并将其设为 0 [如果为假] 或 1 [如果为真]),您可以将其替换为:

for(int i = 0; i < 4; i++)
{
    bin[i] = (num >> 3 - i) & 1;
}

我认为您是故意想要最低 bin 索引中的最高位,而不是最高索引中最高位的通常情况。

关于c++ - Arduino 内联 ASM 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15329824/

相关文章:

c++ - 传递泛型函数以对同类类型进行操作

c++ - Arduino 在启动时自动运行代码

c - 使用 C 在 linux 中读取 arduino 串行

c++ - gettimeofday 异步信号安全吗?如果在信号处理程序中使用它会导致死锁吗?

c++ - `undefined reference` 尝试连接 C 以调用 C++

c++ - 警告匿名命名空间在结构内部使用函数指针

assembly - .fill .long 在汇编中意味着多少字节?

programming-languages - 值得学习汇编语言吗?

linux - 了解汇编代码

python - 使用 PySerial 从 Arduino 串行接收到 Raspberry Pi 一段时间后停止