What's the cause of the increase in unportable assembly code which targets the 64-bit ARM ISA in Apple’s ARM-based computers?

Writing portable ARM64 assembly
Apr 13, 2023
An unfortunate side effect of the rising popularity of Apple’s ARM-based computers is an increase in unportable assembly code which targets the 64-bit ARM ISA. This is because developers are writing these bits of assembly code to speed up their programs when run on Apple’s ARM-based computers, without considering the other 64-bit ARM devices out there, such as SBCs and servers running Linux or BSD.

The good news is that it is very easy to write assembly which targets Apple’s computers as well as the other 64-bit ARM devices running operating systems other than Darwin. It just requires being aware of a few differences between the Mach-O and ELF ABIs, as well as knowing what Apple-specific syntax extensions to avoid. By following the guidance in this blog, you will be able to write assembly code which is portable between Apple’s toolchain, the official ARM assembly toolchain, and the GNU toolchain.
The increase in unportable assembly code targeting the 64-bit ARM ISA on Apple's ARM-based computers can be attributed to a few key factors:

1. Optimizations for Apple Silicon: Developers are writing assembly code specifically optimized for Apple's ARM-based M1 and M2 chips to take advantage of their performance capabilities. This code is tailored to the Apple Silicon architecture and may not be portable to other ARM64 devices or platforms.

2. Apple-specific toolchain and syntax: Apple's Xcode toolchain and assembly syntax may include Apple-specific extensions or features that are not compatible with other toolchains or ABIs (Application Binary Interfaces) used on non-Apple ARM64 devices. This can lead to non-portable code.

3. Focus on Apple's ecosystem: Many developers working on Apple's ARM-based computers are primarily targeting the macOS and iOS ecosystems. As a result, they may not prioritize portability to other ARM64 platforms, leading to the use of Apple-specific assembly optimizations and syntax.

4. Lack of awareness or incentive: Some developers may not be aware of the need for portability or may not have the incentive to make their assembly code portable, especially if their primary target is Apple's ARM-based computers.

To address this issue and promote portability, developers should follow best practices such as:

1. Adhering to the official ARM64 instruction set and avoiding Apple-specific extensions or syntax.
2. Using portable assembly constructs and coding practices compatible with different toolchains and ABIs (e.g., Mach-O and ELF).
3. Considering cross-platform compatibility from the beginning when writing assembly code for ARM64.
4. Testing and validating the assembly code on multiple ARM64 platforms and toolchains to ensure portability.

By following these guidelines, developers can write assembly code that is optimized for Apple's ARM-based computers while maintaining portability to other ARM64 devices and platforms, promoting a more inclusive and interoperable ecosystem.