问题描述
我编写了一个汇编函数,可以在 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
这篇关于从手臂组装功能返回浮点数到objective-c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!