从臂组装函数返回浮点数到目标c [英] return floats to objective-c from arm assembly function

查看:30
本文介绍了从臂组装函数返回浮点数到目标c的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个在 iPhone 4(32 位代码)和 iPhone 6s(64 位代码)上运行良好的汇编函数.我从 Objective-c 中的调用函数传入了四个浮点数.

I've written an assembly function that runs fine on an iPhone 4 (32-bit code) as well as on an iPhone 6s (64-bit code). I pass in four floating point numbers from a calling function in objective-c.

这是我用于 4 个浮点数的结构,下面是函数的原型 - 如我的 Objective-c 代码顶部所示.

Here is the structure I use for the 4 floating point numbers and below that is the prototype for the function - as found at the top of my objective-c code.

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 代码中,我这样调用我的汇编函数:

Down in my objective-c code I call my assembly function like this:

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

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

When running against the iPhone 6S the code runs like a champ (64-bit code). The 4 floating point values are passed from the objective-c code to the assembly code via the ARM single float registers S0-S4. The results returned are also passed via S0-S4.

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

When running against the iPhone 4 the code runs fine as well (32-bit code). The 4 floating point values are passed from the obj-c code to the assembly code via the ARM single float registers S0, S2, S4, and S6 (not sure why skips odd registers). The code runs fine but the values that get returned to my obj-c structure are garbage.

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

Where/how do i pass floating point values from the ARM 32-bit code so they arrive back in the obj-c structure?

谢谢,中继人357

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

p.s. Below is my assembly code from my Xcode S file.

.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

推荐答案

您的两个函数都没有正确返回结构.您需要了解 ARM ABI.您可以先阅读 Apple 的 iOS ABI 函数调用指南.如果在学习了 ABI 之后你不明白,请提出一个新问题,表明你已经尝试过什么.

Neither of your functions are correctly returning a structure. You need to understand the ARM ABI. You can start by reading Apple's iOS ABI Function Call Guide. If after studying the ABI you do not understand ask a new question showing what you've tried.

HTH

这篇关于从臂组装函数返回浮点数到目标c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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