在 Android Studio 上使用 ARM64-v8a 的汇编语言 [英] Use assembly language of ARM64-v8a on Android Studio

查看:99
本文介绍了在 Android Studio 上使用 ARM64-v8a 的汇编语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 ARM64-v8a 上使用 Android studio 构建一个 Android 应用程序,它可以要求汇编函数.网上查了很多资料,还是解决不了这个问题.

<块引用>

我的CMakeList.txt如下:

cmake_minimum_required(VERSION 3.4.1)enable_language(ASM)设置(can_use_assembler 真)设置(CMAKE_VERBOSE_MAKEFILE 上)#add_definitions(-DANDROID -DNDEBUG -DOC_ARM_ASM)set_source_files_properties(multiple.s PROPERTIES COMPILE_FLAGS "-x汇编器与 cpp")add_library( # 设置库的名称.本地库# 将库设置为共享库.共享# 提供源文件的相对路径.src/main/cpp/native-lib.cppsrc/main/cpp/multiple.s)

<块引用>

multiple.s的代码如下

.session .text.align 2.global arm函数.type armFunction, %function臂功能:@乘以10.r0中的输入值和返回值stmfd sp!, {fp,ip,lr}mov r3, r0, asl #3 @ r3=r0<<3=r0*8添加 r0, r3, r0, asl #1 @ r0=r3+r0<<1=r0*8+r0*2=r0*10ldmfd sp!, {fp,ip,lr}bx lr.size armFunction, .-armFunction

<块引用>

native-lib.cpp 中的代码如下:

extern "C"int armFunction(int);JNIEXPORT jstring JNICALLJava_com_example_zyf_testarm_MainActivity_stringFromJNI(JNIEnv *env,作业/* 这个 */) {std::string hello = "来自 C++ 的你好";整数 x = 1;字符信息[1024] = "";x = x + armFunction(10);sprintf(信息,"%d",x);返回 env->NewStringUTF(info);//hello.c_str()}

错误信息是

<块引用>

错误:未知指令.session .text

错误:语句开始时出现意外标记@乘以10.r0中的输入值和返回值

这些只是错误消息的一部分.事实上,它表明multiple.s中的每一条指令都是unknown.

更新:

<块引用>

有人指出问题可能是区分大小写.所以我尝试使用另一个名为 main_asm.S.S 文件.main_asm.S 的代码如下:

.text.global asm_mainasm_main:移动 r0, #1bx lr

问题仍然存在:

<块引用>

未知指令

更新:

我提出了另一个问题问题链接.

我认为问题是由同样的原因引起的.也许我的 clang 工作不正常?我不知道,请帮忙:)

非常感谢帮助我解决这个问题:)

解决方案

bx lr 和寄存器 r0..r15 是 32 位 ARM,而不是 AArch64/ARM64-v8a.ARM64 寄存器是 x0..31(它们的低半部分是 w0..31),你返回 ret(br 的别名)

确保 64 位构建不会尝试构建 32 位 asm 源文件.
或者,如果您有 32 位 asm 源代码,请确保您只进行 32 位构建.

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 : )

解决方案

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.

这篇关于在 Android Studio 上使用 ARM64-v8a 的汇编语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆