Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the ARM assembler in XCode?

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, because I already read too many of them. I just don't know how to actually run the code!

What I would like to do is something like:

useless.h:

void useless();

useless.s:

useless:
      bx lr

If this also works on the simulator it would be fine... On the simulator, the .s file would not compile, so I should maybe do something like:

useless.s:

#if I_AM_ARM
useless:
      bx lr
#endif

useless.c:

#if !I_AM_ARM
void useless()
{
}
#endif

I know the syntax I use is broken, but how do I write it correctly? (Breaking an app on the simulator just because I want to try some inline assembly is no option...)

The second-best option would be to use inline assembly, but I would strongly prefer non-inline assembly.

Thanks!

Edit: I want to learn ARM assembly, so I would like to find a method to compile ARM assembly code, and to EXECUTE ARM assembly code.

like image 749
Michael Avatar asked Jan 28 '14 15:01

Michael


People also ask

What is ARM assembler?

Assembly language is a low-level programming language for a computer or other programmable device that is closest to the machine language. It is often specific to a particular computer architecture so there are multiple types of assembly languages. ARM is an increasingly popular assembly language.

What is arm64 iPhone?

arm64 is the current 64-bit ARM CPU architecture, as used since the iPhone 5S and later (6, 6S, SE and 7), the iPad Air, Air 2 and Pro, with the A7 and later chips. armv7s (a.k.a. Swift, not to be confused with the language of the same name), being used in Apple's A6 and A6X chips on iPhone 5, iPhone 5C and iPad 4.

What assembly language does iOS use?

Swift is a compiled programming language for iOS, macOS, watchOS, tvOS, and Linux applications. Here's what you need to know about Swift. Created by Apple in 2014.


1 Answers

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.)

like image 109
Michael Avatar answered Oct 11 '22 23:10

Michael