Search code examples
android-studioassemblyandroid-ndkarmjava-native-interface

Use assembly language of ARM64-v8a on Android Studio


I am building an Android app with Android studio on ARM64-v8a which can ask for assembly functions. After searching a lot of information online, I still cannot handle this problem.

My CMakeList.txt is as following:

cmake_minimum_required(VERSION 3.4.1)

enable_language(ASM)
set(can_use_assembler TRUE)
set(CMAKE_VERBOSE_MAKEFILE on)
#add_definitions(-DANDROID -DNDEBUG -DOC_ARM_ASM)

set_source_files_properties(multiple.s PROPERTIES COMPILE_FLAGS "-x 
assembler-with-cpp")

add_library( # Sets the name of the library.
         native-lib

         # Sets the library as a shared library.
         SHARED

         # Provides a relative path to your source file(s).
         src/main/cpp/native-lib.cpp
         src/main/cpp/multiple.s
         )

The code of multiple.s is as

.session .text
.align  2
.global armFunction
.type   armFunction, %function

armFunction:
    @ Multiply by 10. Input value and return value in r0
    stmfd   sp!, {fp,ip,lr}
    mov r3, r0, asl #3  @ r3=r0<<3=r0*8
    add r0, r3, r0, asl #1  @ r0=r3+r0<<1=r0*8+r0*2=r0*10
    ldmfd   sp!, {fp,ip,lr}
    bx  lr
    .size   armFunction, .-armFunction

The code from native-lib.cpp is as following:

extern "C"

int armFunction(int);

JNIEXPORT jstring JNICALL
Java_com_example_zyf_testarm_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++ ";
    int x = 1;
    char info[1024] = "";
    x = x + armFunction(10);

    sprintf(info,"%d",x);
    return env->NewStringUTF(info); //hello.c_str()
}

And the error message is that

error: unknown directive .session .text

error: unexpected token at start of statement @ Multiply by 10. Input value and return value in r0

Those are just part of the error message. In fact, it shows that every instruction in multiple.s is unknown.

Update:

Someone points out that the problem might be case sensitive. So I tried to use another .S file named main_asm.S. The code of main_asm.S is as following:

.text
.global asm_main
 asm_main:
    mov r0, #1
    bx lr

The problems are still there:

Unknown instruction

Update:

I raise up another question Question Link.

I think the problem is caused by the same reason. Maybe my clang is not working well? I don't know, please help : )

Thanks a lot for helping me to solve this problem : )


Solution

  • bx lr and registers r0..r15 are 32-bit ARM, not AArch64 / ARM64-v8a. ARM64 registers are x0..31 (and their low halves are w0..31), and you return with ret (an alias for br)

    Make sure a 64-bit build doesn't try to build 32-bit asm source files.
    Or if you do have 32-bit asm sources, make sure you only do a 32-bit build.