本文介绍了通过arm c内联汇编程序在内存中操作数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

您应该将%[ptrToSmpl] >方括号内 - 你想要加载/存储/注入寄存器中的地址。



这个 ldr r2 ,%[ptrToSmpl] 被翻译成 ldr r2,rX - 废话



ldr r2,[%[ptrToSmpl]] ldr r2,[rX] - 正确

此外 - 当你覆盖编译器放在那里的时候,你应该把r2放到破坏寄存器列表中。在最后一个(第三个)冒号后面加上r2(带引号)。


int smplSize = 48;
int Smpl[48];

for(int i = 0; i < smplSize; i++) Smpl[i] = 0x0;
Smpl[smplSize-1] = 0x1;

int *ptrToSmpl = &Smpl[0];

printf("Sample @%p of Size %i :\n",(void*)ptrToSmpl,smplSize);

asm volatile(

             "@ ------------------------------------------------- \n"
             "@ Invert the sample \n"
             "@ ------------------------------------------------- \n"

            //"0:                \n"
            "ldr r2,[r3] \n"
            //"cmp r2,#0x1          \n"
            //"bne 1f             \n"
            "add r2,#0x1       \n"

            //"add r2,#0x1          \n"
            "str r2,[r3]        \n"

            //"ldr r1, .0             \n"
            //"bx  r1             \n"
            //"1:                \n"


            :
            : "r"   (ptrToSmpl)
            : "r3", "memory"

             );

printf("Sample[0] = %i" , Smpl[0]);

Edit:

As you can see I want to manipulate a variable of an array via the inline assembler on arm, but I always get a segfault. How can I access the memory without segfault?

printf("Hello inline asmTest start!\n\n");

int smplSize = 48;
int Smpl[48];

for(int i = 0; i < smplSize; i++) Smpl[i] = 0x0;
Smpl[smplSize-1] = 0x1;

int *ptrToSmpl = &Smpl[0];

printf("Sample @%p of Size %i :\n",(void*)ptrToSmpl,smplSize);

asm volatile(

             "@ ------------------------------------------------- \n"
             "@ Invert the sample \n"
             "@ ------------------------------------------------- \n"

            //"0:                \n"

            "ldr r2,%[ptrToSmpl] \n"
            //"cmp r2,#0x1          \n"
            //"bne 1f             \n"
            "add r2,#0x1       \n"

            //"add r2,#0x1          \n"
            "str r2,%[ptrToSmpl]        \n"

            //"ldr r1, .0             \n"
            //"bx  r1             \n"
            //"1:                \n"


            :
            : [ptrToSmpl]"r"   (ptrToSmpl)
            :

             );

printf("Sample[0] = %i" , Smpl[0]);

Ok, I did so, but now I get a "/tmp/cczQDyiw.s|72|Error: internal_relocation (type: OFFSET_IMM) not fixed up|" when compiling it.

BTW: Can I "org [Adress of the programm]" the inline asm code ?

printf("Hello inline asmTest start!\n\n");

int smplSize = 48;
int Smpl[48];

for(int i = 0; i < smplSize; i++) Smpl[i] = 0x0;
Smpl[smplSize-1] = 0x1;

int *ptrToSmpl = &Smpl[0];

printf("Sample @%p of Size %i :\n",(void*)ptrToSmpl,smplSize);

asm volatile(

             "@ ------------------------------------------------- \n"
             "@ Invert the sample \n"
             "@ ------------------------------------------------- \n"

            "init:                \n"
            "ldr   r0,%[ptrToSmpl] \n"

            "loop :                 \n"
            "ldr r4,[r0]        \n"
            "cmp r4,#0x0          \n"
            "bne  end             \n"

            "add r4,#0x1          \n"
            "str r4,[r0]        \n"

            "add r0,#0x1       \n"


            "b  loop             \n"
            "end:                \n"


            :
            : [ptrToSmpl]"r"   (ptrToSmpl)
            :  "r0" , "r4", "memory"

             );

printf("Sample[0] = %i" , Smpl[0]);

Edit 2:

As you can see above theres still a bug in the adressing I think.The error message now is:"/tmp/ccE69oZd.s|75|Error: undefined symbol r6 used as an immediate value|"but there's no r6 anywhere.

解决方案

You should put %[ptrToSmpl] inside square brackets - you want to load/store from/to the address that is in the register.

This ldr r2,%[ptrToSmpl] gets translated into ldr r2,rX - nonsense

This ldr r2,[%[ptrToSmpl]] ldr r2,[rX] - correct

Moreover - you should put r2 into the list of clobbered registers, as you overwrite what compiler puts there. Add "r2" (with quotes) after the last (third) colon.

这篇关于通过arm c内联汇编程序在内存中操作数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 15:14