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. Apple's Transition to ARM-based Architecture:
   When Apple transitioned from Intel's x86 architecture to their own ARM-based processors (Apple Silicon), developers started optimizing their code for the new hardware. This often involved writing assembly code specifically tailored to Apple's ARM64 implementation, without considering portability to other ARM64 devices or platforms.

2. Performance Optimization:
   Assembly code is often used for performance-critical sections of applications, as it allows developers to directly control the hardware and optimize instructions for specific architectures. With the introduction of ARM-based Macs, developers may have focused on optimizing their code for Apple's ARM64 processors, potentially sacrificing portability in favor of performance gains on Apple's hardware.

3. Vendor-Specific Extensions and ABI Differences:
   Apple's implementation of the ARM64 architecture, while adhering to the overall instruction set architecture (ISA), may include vendor-specific extensions, syntax variations, and Application Binary Interface (ABI) differences compared to other ARM64 platforms. Developers might be using these Apple-specific features, making their assembly code less portable to other ARM64 environments.

4. Developer Focus on Apple's Ecosystem:
   Many developers primarily target Apple's ecosystem, which includes macOS, iOS, iPadOS, and other Apple platforms. As a result, they may prioritize optimizations and code tailored for Apple's ARM64 implementation, without considering the need for portability to other ARM64 devices or operating systems.

To address this issue and promote portability, developers should be aware of the differences between the Mach-O (Apple's object file format) and ELF (Executable and Linkable Format used on other platforms) ABIs, as well as avoid using Apple-specific syntax extensions or features. By following best practices and adhering to the ARM64 Architecture Reference Manual, developers can write assembly code that remains portable across different ARM64 platforms and toolchains, including Apple's toolchain, the official ARM assembly toolchain, and the GNU toolchain.