Assembly Arm

mohitmishra786/low-level-dev-skills/skills/low-level-programming/assembly-arm

作者 mohitmishra786bdc58472fa9f无许可证253 个星标收录于 2026年10月9日更新于 2026年10月9日仓库3个月前更新

AArch64 and ARM assembly skill for reading and writing ARM assembly code. Use when reading GCC/Clang output for AArch64 or ARM Thumb targets, writing inline asm in C/C++, understanding the ARM ABI (AAPCS64/AAPCS), or debugging register and stack state on ARM hardware or QEMU. Activates on queries about AArch64 assembly, ARM Thumb, NEON/SVE SIMD, ARM calling convention, inline asm for ARM, or reading ARM disassembly.

AI 生成的概览

指导阅读和编写 AArch64 与 ARM Thumb 汇编,涵盖寄存器、调用约定、内联汇编以及 NEON/SVE SIMD。

功能
讲解 AArch64 与 32 位 ARM Thumb 汇编:寄存器用途、AAPCS64/AAPCS 调用约定、常用指令以及典型的函数序言与收尾模式。说明如何用 GCC/Clang、objdump 和 GDB 生成并查看汇编,如何编写带 ARM 专用约束的 GCC/Clang 内联汇编,以及如何使用 NEON 内建函数。还涉及 Darwin 与 Linux 的 ABI 差异、Apple AMX 与 16KB 页大小注意事项、NEON 迁移到 SVE2 的提示,并附寄存器参考文件。
适用场景
适用于阅读 AArch64 或 ARM Thumb 目标的编译器输出或反汇编、在 C/C++ 中编写内联汇编、核对 ARM 调用约定,或在 ARM 硬件及 QEMU 上调试寄存器与栈状态。也适合 NEON/SVE SIMD 相关工作以及将 NEON 代码迁移到 SVE2。不适用于 x86-64 汇编,那由相关技能覆盖。
运行要求
不含脚本,仅为说明文档。阅读 GCC/Clang 或 objdump 输出、测试内联汇编需要 ARM 交叉工具链(如 aarch64-linux-gnu-gcc、arm-linux-gnueabihf-gcc、objdump)或 ARM 硬件/QEMU,使用 NEON 内建函数还需要带 arm_neon.h 的 C/C++ 编译器。

ARM / AArch64 Assembly

Purpose

Guide agents through AArch64 (64-bit) and ARM (32-bit Thumb) assembly: registers, calling conventions, inline asm, and NEON/SVE SIMD patterns.

Triggers

  • "How do I read ARM64 assembly output?"
  • "What are the AArch64 registers and calling convention?"
  • "How do I write inline asm for ARM?"
  • "What is the difference between AArch64 and ARM Thumb?"
  • "How do I use NEON intrinsics?"

Workflow

1. Generate ARM assembly

bash
# AArch64 (native or cross-compile)aarch64-linux-gnu-gcc -S -O2 foo.c -o foo.s
# 32-bit ARM Thumbarm-linux-gnueabihf-gcc -S -O2 -mthumb foo.c -o foo.s
# From objdumpaarch64-linux-gnu-objdump -d -S prog
# From GDB on target(gdb) disassemble /s main

2. AArch64 registers (AAPCS64)

RegisterAliasRole
x0–x7—Arguments 1–8 and return values
x8xrIndirect result location (struct return)
x9–x15—Caller-saved temporaries
x16–x17ip0, ip1Intra-procedure-call temporaries (used by linker)
x18prPlatform register (reserved on some OS)
x19–x28—Callee-saved
x29fpFrame pointer (callee-saved)
x30lrLink register (return address)
sp—Stack pointer (must be 16-byte aligned at call)
pc—Program counter (not directly accessible)
xzrwzrZero register (reads as 0, writes discarded)
v0–v7q0–q7FP/SIMD args and return
v8–v15—Callee-saved SIMD (lower 64 bits only)
v16–v31—Caller-saved temporaries

Width variants: x0 (64-bit), w0 (32-bit, zero-extends to 64), h0 (16), b0 (8).

3. AAPCS64 calling convention

Integer/pointer args: x0–x7 Float/SIMD args: v0–v7 Return: x0 (int), x0+x1 (128-bit), v0 (float/SIMD) Callee-saved: x19–x28, x29 (fp), x30 (lr), v8–v15 (lower 64 bits) Caller-saved: everything else

Stack must be 16-byte aligned at any bl or blr instruction.

4. Common AArch64 instructions

InstructionEffect
mov x0, x1Copy register
mov x0, #42Load immediate
movz x0, #0x1234, lsl #16Move zero-extended with shift
movk x0, #0xabcdMove with keep (partial update)
ldr x0, [x1]Load 64-bit from address in x1
ldr x0, [x1, #8]Load from x1+8
str x0, [x1, #8]Store x0 to x1+8
ldp x0, x1, [sp, #16]Load pair (two regs at once)
stp x29, x30, [sp, #-16]!Store pair, pre-decrement sp
add x0, x1, x2x0 = x1 + x2
add x0, x1, #8x0 = x1 + 8
sub x0, x1, x2x0 = x1 - x2
mul x0, x1, x2x0 = x1 * x2
sdiv x0, x1, x2Signed divide
udiv x0, x1, x2Unsigned divide
cmp x0, x1Set flags for x0 - x1
cbz x0, labelBranch if x0 == 0
cbnz x0, labelBranch if x0 != 0
bl funcBranch with link (call)
blr x0Branch with link to address in x0
retReturn (branch to x30)
ret x0Return to address in x0
adrp x0, symbolPC-relative page address
add x0, x0, :lo12:symbolLow 12 bits of symbol offset

5. Typical function prologue/epilogue

asm
// Non-leaf functionstp  x29, x30, [sp, #-32]!   // save fp, lr; allocate 32 bytesmov  x29, sp                  // set frame pointerstp  x19, x20, [sp, #16]     // save callee-saved registers// ... body ...ldp  x19, x20, [sp, #16]     // restoreldp  x29, x30, [sp], #32     // restore fp, lr; deallocateret
// Leaf function (no calls, no callee-saved regs needed)// Can use red zone (no rsp adjustment) — but AArch64 has no red zonesub  sp, sp, #16             // allocate locals// ... body ...add  sp, sp, #16ret

6. Inline assembly (GCC/Clang)

c
// Barrier__asm__ volatile ("dmb ish" ::: "memory");
// Load acquirestatic inline int load_acquire(volatile int *p) {    int val;    __asm__ volatile ("ldar %w0, %1" : "=r"(val) : "Q"(*p));    return val;}
// Store releasestatic inline void store_release(volatile int *p, int val) {    __asm__ volatile ("stlr %w1, %0" : "=Q"(*p) : "r"(val));}
// Read system counterstatic inline uint64_t read_cntvct(void) {    uint64_t val;    __asm__ volatile ("mrs %0, cntvct_el0" : "=r"(val));    return val;}

AArch64-specific constraints:

  • "Q" — memory operand suitable for exclusive/acquire/release instructions
  • "r" — any general-purpose register
  • "w" — any FP/SIMD register

7. NEON SIMD intrinsics

c
#include <arm_neon.h>
// Add 4 floats at oncefloat32x4_t a = vld1q_f32(arr_a);   // load 4 floatsfloat32x4_t b = vld1q_f32(arr_b);float32x4_t c = vaddq_f32(a, b);vst1q_f32(result, c);
// Horizontal sumfloat32x4_t sum = vpaddq_f32(c, c);sum = vpaddq_f32(sum, sum);float total = vgetq_lane_f32(sum, 0);

Naming convention: v<op><q>_<type>

  • q suffix: 128-bit (quad) vector
  • _f32: float32, _s32: int32, _u8: uint8, etc.

8. Darwin vs Linux AArch64 ABI differences

AspectLinux (AAPCS64)Apple Darwin (arm64)
Stack alignment16 bytes at public interfaces16 bytes
Red zone128 bytes below SPNo red zone
x18 registerPlatform reserved (TLS)Platform register (do not use)
Varargsx0–x7, then stackSame, but different objc_msgSend conventions
Name manglingItanium C++ ABISame + Apple blocks

On macOS/iOS, avoid using x18; use _DARWIN_C_LEVEL headers for platform types.

9. AMX primer (Apple Silicon)

Apple Matrix coprocessor (AMX) is not exposed via public intrinsics. Access paths:

c
// Practical: Accelerate/vecLib uses AMX internally#include <Accelerate/Accelerate.h>// cblas_sgemm, vDSP_* dispatch to AMX on M-series
// Low-level: community-documented opcodes — not portable, avoid in production

Prefer Metal Performance Shaders or Accelerate for matrix workloads on Apple Silicon (skills/platform/apple-silicon).

10. 16KB page size on Apple M-series

macOS on Apple Silicon uses 16KB pages (not 4KB):

c
#include <unistd.h>long page = sysconf(_SC_PAGESIZE);  // 16384 on macOS arm64// Align mmap and posix_memalign to page size

Code assuming PAGE_SIZE == 4096 may misalign buffers or fail mmap on macOS.

11. NEON → SVE2 migration hints

Porting checklist├── Replace 128-bit fixed loops with svcnt*() strides on SVE hardware├── Use predicates (svwhilelt) for tails instead of scalar epilogues├── Guard SVE code with #ifdef __ARM_FEATURE_SVE└── Keep NEON path for Apple M1–M3 (no SVE); use SVE2 on Graviton/M4+

See skills/platform/arm-sve for SVE intrinsics and auto-vectorization flags.

For a register reference, see references/reference.md [blocked].

Related skills

  • Use skills/low-level-programming/assembly-x86 for x86-64 assembly
  • Use skills/compilers/cross-gcc for cross-compilation toolchain
  • Use skills/debuggers/gdb for debugging ARM code with gdbserver
  • Use skills/platform/arm-sve for SVE/SVE2 scalable vectors
  • Use skills/platform/apple-silicon for M-series unified memory and AMX

来源与署名

来源:mohitmishra786/low-level-dev-skills位于skills/low-level-programming/assembly-arm提交bdc5847

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架