Current location - Education and Training Encyclopedia - Education and training - How to use assembly language in Android
How to use assembly language in Android
Because the Android environment is very complex, the framework is Java, using C/C++ requires a lot of configuration, and using assembly requires more work.

I use the latest development tool of Android4.0, and NDK is the latest support for 4.0. There are some obvious differences between this NDK and the old version.

Because I use Mac OS X, it is much easier to configure than on a personal computer. You don't need to install any miscellaneous third-party tools, so you can use the NDK you downloaded directly.

First, set the target path-enter the root directory of NDK in your terminal, and then type NDK _ Project _ Path = "". Enter and enter the export NDK project path.

Get into the car.

Note here that the path after NDK _ Project _ Path = needs quotation marks, otherwise it will be invalid.

Since the default compilation options supported by NDK only support ARMv5 to ARMv5TE architectures by default, there are two ways to use more advanced functions:

1, you can change the value of TARGET_ARCH_ABI to armeabi-v7a. I tried it myself, but it didn't work. So you can use the second method, which is simpler and more convenient:

2. in your NDK directory, find toolchains, and then find the ARM-Linux-Android ABI-x.y.z directory, where you can find the setup.mk file. Find -march=armv7-a, remove all #ifdef above and #endif below. This ensures that the compiler compiles with ARMv7A.

After completing the above operations, we can write the assembly in the simplest way, that is, inline assembly-

Static int my_thumb(int dummy)

{

__asm__("movw r0,# 100 1 \t\n "

movw r 12,#2020 \t\n

Add r0, r0, r 12 \t\n

" bx lr ");

Returns a virtual object;

}

jstring

Java _ com _ example _ hello JNI _ hello JNI _ stringFromJNI(JNIEnv * env,

jobject thiz)

{

my _ thumb(0);

return(* env)-& gt; NewStringUTF(env, "Hello, from JNI!" );

}

The above code is actually modified based on the hello-jni project attached to NDK. Finally, it can be compiled successfully with ndk-build.

The above code is compiled by the compiler using Thumb/Thumb-2 by default, so all the instructions I wrote for inline assembly are Thumb codes.

We will discuss how to use ARM code and NEON instruction set.

First, modify LOCAL_SRC_FILES in your Android.mk, and add a. neon suffix after the source file name. For example, change LOCAL_SRC_FILES := hello-jni.c to local _ src _ files: = hello-jni.c.neon.

It should be noted here that you don't need to modify your own real source file name, just modify the value of the symbol LOCAL_SRC_FILES.

Then we add a new variable to instruct ARM GCC to compile with the ARM instruction set-local _ arm _ mode: = arm.

It doesn't matter. Let's modify the code:

Static int my_arm(int dummy)

{

__asm__("movw r0,# 100 1 \t\n "

movw r 12,#2020 \t\n

Add r0, r0, r 12 \t\n

vdup.32 q0,r0 \t\n

" bx lr ");

Returns a virtual object;

}

jstring

Java _ com _ example _ hello JNI _ hello JNI _ stringFromJNI(JNIEnv * env,

jobject thiz)

{

my _ arm(0);

return(* env)-& gt; NewStringUTF(env, "Hello, from JNI!" );

}

After using ndk-build, you can compile normally.

Finally go to the top. Write the assembly file directly. NDK has gas tools, so it is reasonable to compile assembly files. Usually, the suffix of an assembly file is. So we can create a xxx.s file.

Then I created a name here called hey.s Add this file to Android.mk: local _ src _ files+= hey.s.neon

We see here that in order to use the NEON instruction set in the assembly file, we also added. Neon suffix is here. The Makefile of the assembler can also recognize this sign.

We edit the hey.s file:

. text

. Alignment 4

. arm

. Globe, my real arm

My real arm:

Add r0, r0, #256

vmov q0,q 1

vdup.32 q0,r0

bx lr

It should be noted here that in Apple's assembler, function names should have prefixes and underscores, while the assembler provided by NDK does not.

Let's modify hello-jni.c and put this function in:

extern void my _ real _ arm(int I);

Static int my_arm(int dummy)

{

__asm__("movw r0,# 100 1 \t\n "

movw r 12,#2020 \t\n

Add r0, r0, r 12 \t\n

vdup.32 q0,r0 \t\n

" bx lr ");

Returns a virtual object;

}

jstring

Java _ com _ example _ hello JNI _ hello JNI _ stringFromJNI(JNIEnv * env,

jobject thiz)

{

my _ real _ arm(0);

my _ arm(0);

return(* env)-& gt; NewStringUTF(env, "Hello, from JNI!" );

}

Of course, in order to ensure that the compiler can correctly mix the ARM and Thumb instruction sets, we can add -mthumb-interwork to the TARGET_CFLAGS flag in setup.mk just now.

After testing in Windows operating system, it is finally found that neon can be used smoothly as long as the flag in APP_ABI in Application.mk is removed and only armeabi-V7A is left. In this way, you don't need to modify setup.mk, and you don't need to remove the flag judgment in Sample, which is very convenient.

The following is a list of available Android.mk compilation profiles:

LOCAL_PATH := $ (calling my-dir)

Including USD (VARS settlement)

LOCAL_MODULE := HelloNeon

LOCAL_SRC_FILES := helloneon.c

Local _ ARM _ mode: = arm

TARGET _ CFLAGS+=-m thumb-interwork

TARGET _ CFLAGS+=-STD = GNU 1 1

TARGET_CFLAGS += -O3

Ifeq ($ (target _ arch _ABI), armeabi-v7a)

LOCAL _ CFLAGS:=-d have _ NEON = 1

LOCAL _ SRC _ FILES+= neontest . s . neon

LOCAL_ARM_NEON := true

endif

LOCAL_LDLIBS := -llog

Include $(BUILD_SHARED_LIBRARY)

$ (calling import module, CPU function)

When using jni, you only need to add the JNI folder to your current project directory, and then follow the file layout provided in the example. When compiling with ndk-build (cygwin console uses ndk-build.cmd under Windows), if the build is successful, a libXXX.so will be generated in the libs folder. Then reopen your project with Eclipse ADT, and you will find that the jni file directory and the generated so file will be displayed in your project file directory. Of course, you can also edit it. The assembly file of is placed directly under Eclipse IDE, which makes it easier to read.

Finally, if you want to annotate a statement in the Android assembler, you must use the annotation character ... */

The semicolon and//form introduced after C++98 are useless.

In the latest NDK version android-ndk-r8d, ARM-Linux GCC4.7 and the popular LLVM Clang3. 1 are added. However, many compilation options of LLVM Clang3. 1 are different from GCC, so you need to configure the corresponding compilation options when using Clang3. 1. The default compiler tool chain of this version of NDK is GCC version 4.6. If you want to use GCC4.7, you can add NDK _ Toolchain _ Version = 4.7 in the Application.mk file; If you want to use Clang3. 1, you can add ndk _ toolkit _ version = clang3.1to the application.mk. The following is a legal application.mk.:

# Building clang3 with LLVM.1

# NDK _ Toolchain _ Version = 3. 1

# built with ARM-Linux GCC4.7

NDK Toolchain Version =4.7

# only build ARMv7-A machine code.

APP_ABI := armeabi-v7a

Refer to Douding.com above.

Nanjing Baoyun Android training class is in hot registration ~