title: Iterating array in arm assembler for Android
body: <p>I want to iterate array array in arm assembly for Android but I'm getting FATAL SIGNAL error and app crash.</p>
  
  <p>I will show what is inside.</p>
  
  <p>First of all I'm using jni to access c code from java:</p>
  
  <p>I declared native function:</p>
  
  <pre><code>public native byte[] BitmapToGrayscale(byte[] bitmap);
  </code></pre>
  
  <p>After that I call my code by calling this function:</p>
  
  <pre><code> private void ConvertImageToGrayscale()
  {
      if(image == null)
      {
          GetLenaBack();
          info("Nie było obrazu, ustawiono domyślny.");
      }
  
      ByteBuffer bb = ByteBuffer.allocate(image.getByteCount());
      image.copyPixelsToBuffer(bb);
      info("Start");
      ByteBuffer result = ByteBuffer.wrap(this.BitmapToGrayscale(bb.array()));
      info("Stop");
      image.copyPixelsFromBuffer(result);
      display.setImageBitmap(image);
  }
  </code></pre>
  
  <p>Function in C looks like:</p>
  
  <pre><code>JNIEXPORT jbyteArray JNICALL Java_pl_marekbar_Main_BitmapToGrayscale
  (JNIEnv * env, jobject thisObj, jbyteArray bitmap)
  {
      jbyte arraySize = (*env)-&gt;GetArrayLength(env, bitmap);
      jbyte *arrayAddress = (*env)-&gt;GetByteArrayElements(env, bitmap, 0);
      jbyte cellSize = sizeof((*env)-&gt;GetByteArrayElements(env, bitmap, 0));
  
      Grayscale(arrayAddress, arraySize, cellSize);
      return bitmap;
  }
  </code></pre>
  
  <p>And finally this is my asm code:</p>
  
  <pre><code>@ This file is jni/Grayscale.s
      .text
      .align  2
      .global Grayscale
      .type   Grayscale, %function
  Grayscale:
  @Convert to grayscale
      stmfd   sp!, {fp,ip,lr}
  @r0 - pointer
  @r1 - array length
  @r2 - array element size
      mov r3, #0 @current element
  array_loop:
      ldr r4, [r0] @load element to memory
  
  
      str r4, [r0] @store element in memory
      add r0, r0, r4 @move pointer
      add r3, r3, #1 @increment index
  
      cmp r3, r1 @compare current index with array length
      bne array_loop @when index smaller than array length still iterate
  
  
      ldmfd   sp!, {fp,ip,lr}
      bx  lr
      .size   Grayscale, .-Grayscale
  </code></pre>
  
  <p>I need to find out how to properly work with arrays passed from c code in arm asm. I don't want to do this in C because I'm preparing app for image processing and must be quick.</p>