How do I use the ARM assembler in XCode?

后端 未结 5 738
傲寒
傲寒 2021-01-01 04:59

Just for educational purposes, I would like to add a function to an existing iPhone app, written in ARM assembly. I don\'t need a tutorial on ARM assembly in general, becaus

5条回答
  •  我在风中等你
    2021-01-01 05:46

    I finally found the answer myself. It's actually not that hard. I only solved it for the 32-bit ARM version though.

    useless.h:

    void useless();
    

    useless.s:

    #ifdef __arm__
    
    
        .syntax        unified
        .globl         _useless
        .align         2
        .code          16
        .thumb_func    _useless
    
    _useless:
        //.cfi_startproc
        bx    lr
        //.cfi_endproc
    
    // CFI means Call Frame Information
    // Optionally. Use for better debug-ability.
    
    
    #endif
    

    useless.c:

    #ifndef __arm__
    
    void useless()
    {
    }
    
    #endif
    

    Notes:

    The CLANG ARM Assembler syntax is a bit different from what you see in example all over the web. Comments start with // and /* multiline comments */ are also supported. It also understands the standard C preprocessor. The function has to be defined as a Thumb function, if you specify an arm function (.code 32) the program will just crash. The line .thumb_func _useless can be ommited and it works still. I have no Idea what it means. If you omit the .code 16 line, the program crashes.

    about the #ifdef. For ARMv7, __arm__ is defined. For ARMv8, i.e. the 64bit-variant on the iPhone 5S, __arm__ is not defined, but __arm64__ is defined instead. The above code does not work for the 64bit-ARM-version. Instead, the implementation from useless.c will be used. (I didn't forget ARMv7s, I just don't have a device with that arch in my hands currently, so I cannot test.)

提交回复
热议问题