如何使用NDK在ARM内联汇编中进行简单的循环?

时间:2014-01-13 18:58:01

标签: c loops assembly android-ndk arm

我正在尝试在ARM程序集中执行一个简单的循环,但每次运行它都会崩溃 这是日志:

  

01-13 15:34:21.277:A / libc(27296):致命信号11(SIGSEGV)位于0x00000000(代码= 1),线程27312(线程-2932)

这是我的代码我做错了什么?

 void foo(int *pIn, int *pOut) {
    //pIn contains the number of iterations the loop will have
    asm volatile(
        "ldr r3, %[in];"
        "ldr r4, %[out];"
        "ldr r5, [r3];"

        "loop:; "
        //here would go the code inside the loop perhaps put something in output, in this case just do nothing
        "subs r5, r5, #1;"
        "bne loop"

        :[out] "=m" (pOut)
        :[in] "m" (pIn)
        :"r3","r4","r5","memory" 
    );
}

在Android.mk文件中我放了32bit指令

  

LOCAL_ARM_MODE:= arm

任何想法为什么会崩溃? 崩溃只发生在我放置循环时,在此之前我尝试移动东西并且它工作得非常好,给出了我预期的输出值。

1 个答案:

答案 0 :(得分:1)

问题解决了,在我的clobber列表中添加“r5”和“cc”使其正常工作。 这是工作代码:

void foo(int *pIn, int *pOut) {
     //pIn contains the number of iterations the loop will have
     asm volatile(
         "ldr r3, %[in];"
         "ldr r4, %[out];"
         "ldr r5, [r3];"

         "loop:; "
         //here would go the code inside the loop perhaps put something in output, in this case just do nothing
         "subs r5, r5, #1;"
         "bne loop"

         :[out] "=m" (pOut)
         :[in] "m" (pIn)
         :"r3","r4","r5","cc","memory" 
     );
 }