[关闭]
@oro-oro 2015-08-18T03:17:27.000000Z 字数 5307 阅读 3623

六、在Android中调用汇编代码

AndroidARM


C可以调用汇编代码,而Android则通过JNI调用C代码。
所以,可以通过写汇编代码和C,用ndk-build编译成so,让Android调用。

1、建立hello-jni项目

直接使用 \android-ndk-r10d\samples\hello-jni 项目。
将jni目录删除,用Android Studio初始化hello-jni。
代码修改为:

  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.example.hellojni;
  17. import android.app.Activity;
  18. import android.os.Bundle;
  19. import android.widget.TextView;
  20. public class HelloJni extends Activity {
  21. /* this is used to load the 'hello-jni' library on application
  22. * startup. The library has already been unpacked into
  23. * /data/data/com.example.hellojni/lib/libhello-jni.so at
  24. * installation time by the package manager.
  25. */
  26. static {
  27. System.loadLibrary("hello-jni");
  28. }
  29. /**
  30. * Called when the activity is first created.
  31. */
  32. @Override
  33. public void onCreate(Bundle savedInstanceState) {
  34. super.onCreate(savedInstanceState);
  35. /* Create a TextView and set its content.
  36. * the text is retrieved by calling a native
  37. * function.
  38. */
  39. TextView tv = new TextView(this);
  40. tv.setText(stringFromJNI() + factorialJNI(13));
  41. setContentView(tv);
  42. }
  43. /* A native method that is implemented by the
  44. * 'hello-jni' native library, which is packaged
  45. * with this application.
  46. */
  47. public native String stringFromJNI();
  48. /* This is another native method declaration that is *not*
  49. * implemented by 'hello-jni'. This is simply to show that
  50. * you can declare as many native methods in your Java code
  51. * as you want, their implementation is searched in the
  52. * currently loaded native libraries only the first time
  53. * you call them.
  54. *
  55. * Trying to call this function will result in a
  56. * java.lang.UnsatisfiedLinkError exception !
  57. */
  58. public native String unimplementedStringFromJNI();
  59. /**
  60. * Multiply the number by 10.
  61. *
  62. * @param input, the number to be multiplied
  63. * @return the multiple of the number
  64. */
  65. public native int factorialJNI(int input);
  66. }

2、编译 so

  1. @ This file is jni/multiple.s
  2. .text
  3. .align 2
  4. .global armFunction
  5. .type armFunction, %function
  6. armFunction:
  7. @ Multiply by 10. Input value and return value in r0
  8. stmfd sp!, {fp,ip,lr}
  9. mov r3, r0, asl #3 @ r3=r0<<3=r0*8
  10. add r0, r3, r0, asl #1 @ r0=r3+r0<<1=r0*8+r0*2=r0*10
  11. ldmfd sp!, {fp,ip,lr}
  12. bx lr
  13. .size armFunction, .-armFunction
  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. #include <string.h>
  18. #include <jni.h>
  19. /* This is a trivial JNI example where we use a native method
  20. * to return a new VM String. See the corresponding Java source
  21. * file located at:
  22. *
  23. * apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java
  24. */
  25. jstring
  26. Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
  27. jobject thiz )
  28. {
  29. #if defined(__arm__)
  30. #if defined(__ARM_ARCH_7A__)
  31. #if defined(__ARM_NEON__)
  32. #if defined(__ARM_PCS_VFP)
  33. #define ABI "armeabi-v7a/NEON (hard-float)"
  34. #else
  35. #define ABI "armeabi-v7a/NEON"
  36. #endif
  37. #else
  38. #if defined(__ARM_PCS_VFP)
  39. #define ABI "armeabi-v7a (hard-float)"
  40. #else
  41. #define ABI "armeabi-v7a"
  42. #endif
  43. #endif
  44. #else
  45. #define ABI "armeabi"
  46. #endif
  47. #elif defined(__i386__)
  48. #define ABI "x86"
  49. #elif defined(__x86_64__)
  50. #define ABI "x86_64"
  51. #elif defined(__mips64) /* mips64el-* toolchain defines __mips__ too */
  52. #define ABI "mips64"
  53. #elif defined(__mips__)
  54. #define ABI "mips"
  55. #elif defined(__aarch64__)
  56. #define ABI "arm64-v8a"
  57. #else
  58. #define ABI "unknown"
  59. #endif
  60. return (*env)->NewStringUTF(env, "Hello from JNI ! Compiled with ABI " ABI ".");
  61. }
  62. /* This stub calls the function. It helps to have a stub like this to
  63. * save yourself the hassle of defining the function call in
  64. * Assembly. */
  65. jint Java_com_example_hellojni_HelloJni_factorialJNI(
  66. JNIEnv* env, jobject object, jint input) {
  67. /* Try calling some local code */
  68. return armFunction(input);
  69. }
  1. # Copyright (C) 2009 The Android Open Source Project
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. LOCAL_PATH := $(call my-dir)
  16. include $(CLEAR_VARS)
  17. # I want ARM, not thumb.
  18. LOCAL_ARM_MODE := arm
  19. # Name of the local module
  20. LOCAL_MODULE := hello-jni
  21. # The files that make up the source code
  22. LOCAL_SRC_FILES := hello-jni.c multiple.s
  23. include $(BUILD_SHARED_LIBRARY)

Android.mk
hello-jni.c
multiple.s
准备好这3个文件后,就可以使用ndk-build来编译so了。
其中libs就是包含so的目录,将这个目录,拷贝给Android项目使用即可。

  1. $ ndk-build
  2. [armeabi] Compile arm : hello-jni <= multiple.s
  3. [armeabi] SharedLibrary : libhello-jni.so
  4. [armeabi] Install : libhello-jni.so => libs/armeabi/libhello-jni.so

代码打包地址:http://yunpan.cn/ccaxuNLVH5c9R 访问密码 cff9

参考:
http://www.eggwall.com/2011/09/android-arm-assembly-calling-assembly.html

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注