gpt4 book ai didi

c - ARM 程序集 : Access array elements residing in C type struct

转载 作者:行者123 更新时间:2023-11-30 15:27:17 24 4
gpt4 key购买 nike

我有一个 ARM Neon 函数,它使用 C 类型结构作为参数。我在该结构中有一个固定大小的 float*float[] 数组。我可以在汇编函数中访问 float* 元素。但是当我尝试访问数组元素时,我的程序崩溃了。

这是我的 C 端应用程序:

typedef struct{
float* f1;
float* f2;
float f3[4];
}P_STRUCT;

main.c 文件:

extern void myNeonFunc(P_STRUCT*    p, float* res);
P_STRUCT p;
// memory allocation for f1,f2 and fill array f3 here.
// memory allocation for res

myNeonFunc(&p, res);

这是我的 .S 文件:

.text

.set P_STRUCT_F1, 0 @ float* f1
.set P_STRUCT_F2, 4 @ float* f2
.set P_STRUCT_F3, 8 @ float f3[4]

.globl myNeonFunc

@ void myNeonFunc (P_STRUCT* p ----> r0, r1 )

.balign 64 @ align the function to 64

myNeonFunc:
@save callee-save registers here

ldr r8, [r0,P_STRUCT_F1] @ r8 <- f1
add r8, r8, #8 @ r8 points to the f1[2] (2*4 = 8 )
ldr r9, [r0,P_STRUCT_F2] @ r9 <- f2
add r9, r9, #4 @ r9 points to the f2[1] (1*4 = 8)
ldr r10, [r0,P_STRUCT_F3] @ r10 <- f3
add r10, r10, #4 @ r10 points to the f3[1] (1*4 = 8)

vld1.f32 {d4}, [r8]! @ d4 now contains the corresponding r8 value
vld1.f32 {d6}, [r9]! @ d6 now contains the corresponding r9 value
vst1.32 {d4}, [r1]! @ store f1[2] value in result register
vst1.32 {d6}, [r1]! @ store f1[1] value in result register

// every thing is ok up to here
// this line probably causes seg fault !!!
vld1.f32 {d8}, [r10]! @ d8 now contains the corresponding r10 value

//
vst1.32 {d8}, [r1]! @ store f3[1] value in result register

// epilog part here ...

这个问题可能是由于r10没有指向f3数组的地址造成的。(可能)

现在我的问题是,为什么访问固定大小的数组会导致问题,而访问指针元素却可以。解决方案是什么。

最佳答案

指针与数组不同。 f1f2 是结构体中的 4 字节指针。 f3 是结构体中的一个 16 字节数组。整个结构体有 24 个字节长。

您要加载到 r10 中的内容是 f3 的第一个元素。如果要将 r10 设置为 &f3[0],则只需将 r10 设置为 r0 + P_STRUCT_F3

关于c - ARM 程序集 : Access array elements residing in C type struct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27083416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com