return floats to objective-c from arm assembly function

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.

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.

  • How to see assembly code in xcode 6
  • How do I use the ARM assembler in XCode?
  • How to deal with compiler optimization problems
  • How can I examine assembly in Xcode 4?
  • Optimizing RGBA8888 to RGB565 conversion with NEON
  • Compiling for iOS with CMake
  • 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
    

    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
    

    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.

    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.

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

    thanks,
    relayman357

    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
    

    Solutions Collect From Internet About “return floats to objective-c from arm assembly function”

    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