OGeek|极客世界-中国程序员成长平台

标题: ios - 从 ARM 组装功能返回 float 到objective-c [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:42
标题: ios - 从 ARM 组装功能返回 float 到objective-c

我编写了一个汇编函数,可以在 iPhone 4(32 位代码)和 iPhone 6s(64 位代码)上正常运行。我从 Objective-c 中的调用函数传入四个 float 。

这是我用于 4 个 float 的结构,下面是函数的原型(prototype) - 可以在我的 Objective-c 代码的顶部找到。

struct myValues{    // This is a structure.  It is used to conveniently group multiple data items logically.
    float A;        // I am using it here because i want to return multiple float values from my ASM code
    float B;        // They get passed in via S0, S1, S2 etc. and they come back out that way too
    float number;
    float divisor;  //gonna be 2.0
}myValues;

struct myValues my_asm(float e, float f, float g, float h);  //  Prototype for the ASM function

在我的 Objective-c 代码中,我这样调用我的汇编函数:

myValues = my_asm(myValues.A, myValues.B, myValues.number,myValues.divisor);   // ASM function

在 iPhone 6S 上运行时,代码运行起来就像一个冠军(64 位代码)。 4 个浮点值通过 ARM 单浮点寄存器 S0-S4 从 Objective-c 代码传递到汇编代码。返回的结果也通过 S0-S4 传递。

在 iPhone 4 上运行时,代码也可以正常运行(32 位代码)。 4 个浮点值通过 ARM 单浮点寄存器 S0、S2、S4 和 S6 从 obj-c 代码传递到汇编代码(不知道为什么会跳过奇数寄存器)。代码运行良好,但返回到我的 obj-c 结构的值是垃圾。

我在哪里/如何从 ARM 32 位代码中传递浮点值,以便它们返回到 obj-c 结构中?

谢谢, 中继器357

附言下面是我的 Xcode S 文件中的汇编代码。

.ios_version_min 9, 0
.globl  _my_asm
.align  2

#ifdef __arm__
.thumb_func _my_asm
.syntax unified
.code 16

_my_asm:                   // 32 bit code
// S0 = A, S2 = B, S4 = Number, S6 = 2.0   - parameters passed in when called by the function
vadd.f32 s0, s0, s2 
vdiv.f32 s0, s0, s6   
vdiv.f32 s1, s4, s0   
vcvt.u32.f32 r0,s0
bx lr
//ret

#else
_my_asm:                  // 64 bit code
//add W0, W0, W1
; S0 = A, S1 = B, S2 = Number, S3 = 2.0  parameters passed in when called by the function
fadd s0, s0, s1  
fdiv s0, s0, s3  
fdiv s1, s2, s0  
ret

#endif



Best Answer-推荐答案


您的两个函数都没有正确返回结构。您需要了解 ARM ABI。您可以先阅读 Apple 的 iOS ABI Function Call Guide .如果在学习 ABI 后您不明白,请提出一个新问题来说明您的尝试。

HTH

关于ios - 从 ARM 组装功能返回 float 到objective-c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39811742/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4