Skip to main content
Be Bitwise

ARM Instruction Set

ARM Architecture Deep Dive · CPU Architectures track

5 min read

In this module

Fixed-Width Simplicity

x86 instructions range from 1 to 15 bytes. AArch64 instructions are always exactly 32 bits (4 bytes), no exceptions. This fixed width is a defining feature of the RISC approach. The CPU always knows where the next instruction starts: add 4 to the current address. No parsing, no variable-length decoding logic, no ambiguity.

Load/Store Architecture

ARM is a load/store architecture. Only two kinds of instructions touch memory: loads (memory → register) and stores (register → memory). Every arithmetic and logic instruction operates on register values exclusively. That's a sharp departure from x86, where a single ADD can pull a memory value straight into a register. The load/store rule simplifies the pipeline and makes instruction timing predictable.

Thumb Mode (AArch32)

The original 32-bit ARM instructions decode beautifully, but they aren't compact. For memory-pinched embedded systems, ARM introduced Thumb mode: a set of 16-bit compressed instructions that handle common operations in half the space.

Thumb-2 went further by interleaving 16-bit and 32-bit instructions, giving you something close to Thumb's code density with something close to full ARM's performance. AArch32 processors can flip between ARM and Thumb mode at runtime.

AArch64 dropped Thumb entirely. Its fixed 32-bit width is considered a fair trade between code density and decode simplicity.

Conditional Execution (AArch32)

One of ARM's most distinctive features in AArch32 is conditional execution. Every instruction includes a 4-bit condition code field. Instead of writing a branch to skip an instruction, you can make the instruction itself conditional:

text
CMP R0, #0 @ compare R0 with 0
ADDNE R1, R1, #1 @ add 1 to R1, but only if R0 was Not Equal to 0

This avoids pipeline flushes from short branches. AArch64 replaced pervasive conditional execution with conditional select and conditional compare instructions, which accomplish similar goals with a cleaner encoding.

Common AArch64 Instructions

InstructionPurpose
MOV Xd, XsCopy register to register
ADD Xd, Xn, XmAdd two registers
SUB Xd, Xn, XmSubtract
LDR Xd, [Xn]Load from memory address in Xn
STR Xs, [Xn]Store to memory address in Xn
CMP Xn, XmCompare (subtract and set flags)
B labelUnconditional branch
BL labelBranch with link (function call, saves return address in X30)
RETReturn (branch to address in X30)
AND Xd, Xn, XmBitwise AND
ORR Xd, Xn, XmBitwise OR

Notice the three-operand format: ADD X0, X1, X2 means X0 = X1 + X2. Destination separate from sources. Much cleaner than x86's two-operand ADD RAX, RBX, which means RAX = RAX + RBX and destroys one of its inputs in the process. (x86 has partial escapes: LEA can add two registers into a third, and the VEX-encoded AVX instructions are three-operand.)

A few more forms turn up constantly in real disassembly. ADDS and SUBS are the flag-setting versions of ADD and SUB (plain ADD leaves the flags alone; CMP is really SUBS into the zero register). LDP and STP load or store a pair of registers at once, which is how function prologues save X29 and X30. Many data-processing instructions accept a shifted second operand, as in add x2, x0, x1, lsl #3. And because instructions are 32 bits wide, a 64-bit constant is built 16 bits at a time with MOVZ (move and zero the rest) followed by up to three MOVK (move and keep).

Here's how those pieces combine in a real loop. The listing is a tidied version of what GCC produces at -O2 for summing an array (your compiler may lay it out a little differently). Hover over the C to find its instructions, and look at three details:

  • ldr x3, [x0], #8 is a post-index load: it reads from the address in x0, then adds 8 to x0. Loading the element and advancing the pointer is one instruction.
  • The compiler dropped i altogether. It computes an end pointer once (add x2, x0, x1, lsl #3, which is a + n*8) and stops when x0 reaches it.
  • cmp sets the flags and b.ne reads them. Nothing else in the loop touches the flags, so the branch sees exactly the comparison just before it.

Summing an array on AArch64

ARM
C Source
1long sum(const long *a, long n) {
2 long s = 0;
3 for (long i = 0; i < n; i++)
4 s += a[i];
5 return s;
6}
Assembly
1sum:
2 cmp x1, #0 // n <= 0? skip the loop
3 b.le .Lempty
4 add x2, x0, x1, lsl #3 // x2 = a + n*8, the end
5 mov x1, #0 // s = 0 (n is no longer needed)
6.Lloop:
7 ldr x3, [x0], #8 // x3 = *a, then a += 8
8 add x1, x1, x3 // s += x3
9 cmp x0, x2 // reached the end?
10 b.ne .Lloop
11 mov x0, x1 // return s
12 ret
13.Lempty:
14 mov x0, #0
15 ret

Hover over code lines or mapping badges to see correspondences

Instruction Encoding

AArch64 instructions use a regular encoding where the top bits identify the instruction group (data processing, load/store, branch, and so on). The decoder can spot the instruction type from a handful of bits, then route to the right execution unit with minimal logic. x86, by contrast, makes the decoder slog through prefix bytes, opcode bytes, ModR/M, and SIB just to figure out what the instruction even is.

Try It: Translate x86 to AArch64

The runner on this site only executes x86-64, so this one's on paper. Translate each x86-64 snippet into AArch64, using X1 for RBX, X0 for RAX (W0 for EAX) and X2 for RCX. Remember the load/store rule, and count how many instructions each one needs.

text
1. add [rbx], eax ; 32-bit add straight into memory
2. mov rax, [rbx + rcx*8] ; load element rcx of an array of 8-byte values
3. cmp rax, rcx
jl .Lsmaller ; signed less-than
Show the answer
text
1. ldr w3, [x1] // load: memory can't be an ADD operand
add w3, w3, w0 // operate in registers (any free register will do)
str w3, [x1] // store the result back
2. ldr x0, [x1, x2, lsl #3] // still one instruction: base + index shifted left by 3
3. cmp x0, x2
b.lt .Lsmaller

The first costs three instructions because x86's read-modify-write has to be split into a load, an add and a store. The second surprises people: AArch64 addressing can scale an index register too, so an array load stays a single instruction. The third maps one to one, with the condition code moving onto the branch as b.lt.