gcc 中的 arm 内联汇编 [英] arm Inline assembly in gcc

查看:39
本文介绍了gcc 中的 arm 内联汇编的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理一些内联汇编代码时遇到了一些麻烦.我知道应该做什么,但我想念如何做"!

I have some trouble with some inline assembly code. I know what should be done but I miss the "how" !

我有这个几乎"工作的校验和函数:

I have this checksum function that is "almost" working :

static unsigned long cksum_unroll( unsigned short **w, int *mlen)
{
  int len;
  unsigned short *w0;
  unsigned long sum=0;

  len = *mlen;
  w0 = *w;

  while( len >= 8) {
    asm volatile (
          "ldmia %[w0]!, {v1, v2}\n\t"
          "adds %[sum], %[sum], v1\n\t"
          "adcs %[sum], %[sum], v2\n\t"
          "adcs %[sum], %[sum], #0"
          : [sum] "+r" (sum) : [w0] "r" (w0)
          );
    len -= 8;
  }
  *mlen = len;
  *w = w0;
  return (sum);
}

我相信,我的问题是: [sum] "+r" (sum) : [w0] "r" (w0)"在第一条流水线上,w0ldmia正确处理了(这条流水线执行时,数据在r4,r5,w0递增).但是 w0 的增量值没有保存在某处,当代码循环时,w0 的原始值会再次加载(参见下面的汇编代码).我的猜测是我应该将 w0 的值存储在 ": [sum] "+r" (sum) : [w0] "r" (w0)" 行上,但我不知道如何...

My problem, I believe, is on the line ": [sum] "+r" (sum) : [w0] "r" (w0)" On the first assembly line, w0 is processed correctly by ldmia (when the line is executed, data are in r4,r5 and w0 is incremented). But the incremented value of w0 is not saved somewhere and when the code loop, the original value of w0 is loaded again (see assembly code below). My guess is that I should store the value of w0 on the line ": [sum] "+r" (sum) : [w0] "r" (w0)" but I don't know how...

这里是函数内联汇编部分的反汇编代码:

Here's the disassembly code of the inline assembly part of the function :

注意:

len is stored at r11, #-16
w0 is stored at r11, #-20
sum is stored at r11, #-24

编译后,代码如下:

asm volatile (
          "ldmia %[w0]!, {v1, v2}\n\t"
          "adds %[sum], %[sum], v1\n\t"
          "adcs %[sum], %[sum], v2\n\t"
          "adcs %[sum], %[sum], #0"
          : [sum] "+r" (sum) : [w0] "r" (w0)
);
len -= 8;

生成:

00031910:   ldr r3, [r11, #-20]
00031914:   ldr r2, [r11, #-24]
00031918:   mov r4, r2
0003191c:   ldm r3!, {r4, r5}
00031920:   adds r4, r4, r4
00031924:   adcs r4, r4, r5
00031928:   adcs r4, r4, #0
0003192c:   str r4, [r11, #-24]
00031930:   ldr r3, [r11, #-16]
00031934:   sub r3, r3, #8
00031938:   str r3, [r11, #-16]

如您所见,我想在 31928 和 3192c 行之间添加类似str r3, [r11, #-20]"这样的内容,因为当程序循环到 31910 行时,r3 加载了初始值r3...

As you can see, I would like to add something like "str r3, [r11, #-20]" between lines 31928 and 3192c because when the program loop to the line 31910, r3 is loaded with the initial value of r3...

我认为这对于堆栈溢出社区的内联汇编专家来说很容易!

I think this is an easy one for the inline assembly expert of the stack overflow community!

顺便说一下,我正在研究 ARM7TDMI 处理器(但这可能与这个问题无关...)

By the way, I'm working on a ARM7TDMI processor (but this may not be relevant for this question...)

提前致谢!

为了验证我的想法,我测试了以下内容:

To verify my idea, I tested the following :

asm volatile ( 
"ldmia %[w0]!, {v1, v2}\n\t" 
"adds %[sum], %[sum], v1\n\t" 
"adcs %[sum], %[sum], v2\n\t" 
"adcs %[sum], %[sum], #0\n\t" 
"str %[w0], [r11, #-20]" 
: [sum] "+r" (sum) : [w0] "r" (w0) 
); 

这是有效的.也许这是解决方案,但我用什么来替换r11,#20",如果我修改函数,它可能会改变?

And this works. Maybe this is the solution, but what do I use to replace "r11, #20" that will probably change if I modify the function?

推荐答案

问题似乎是您将 w0 指定为 INPUT 操作数,而实际上它应该是读写 OUTPUT 操作数,比如 sum.此外,您需要指定它在使用这些寄存器时破坏 v1 和 v2(否则,gcc 可能会将一些其他 var 放入这些 reg 中并期望它们被保留.)

The problem would seem to be that you specify w0 as an INPUT operand, when it actually should be a read-write OUTPUT operand, like sum. In addition, you need to specify that it clobbers v1 and v2 as you use those registers (otherwise, gcc might put some other var into these regs and expect them to be preserved.)

所以你应该:

asm volatile (
      "ldmia %[w0]!, {v1, v2}\n\t"
      "adds %[sum], %[sum], v1\n\t"
      "adcs %[sum], %[sum], v2\n\t"
      "adcs %[sum], %[sum], #0"
      : [sum] "+r" (sum) , [w0] "+r" (w0) : : "v1", "v2"
      );

即两个读写输入/输出操作数,没有独占输入操作数,以及两个寄存器破坏

that is, two read-write input/output operands, no exclusively input operands, and two register clobbers

这篇关于gcc 中的 arm 内联汇编的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆