Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 40 ยท ARM Assembly

ARM64 (AArch64) Basics

Meet 64-bit ARM: the 31 registers x0-x30, w registers, xzr and sp, fixed 32-bit instructions, and mov, add, sub, ldr and str, with runnable toy examples.

So far this tutorial has focused on x86-64. But a large share of the world's processors, including every iPhone, most Android phones, Apple's Mac chips, AWS Graviton servers and the Raspberry Pi, run ARM64, officially called AArch64, the 64-bit execution state of the Arm architecture. Its assembly language looks familiar (destination first, registers and immediates) but its design philosophy is different, and the differences are worth learning properly.

This lesson introduces the register file, the instruction format and the first handful of instructions. The runnable examples use an ARM-flavored toy interpreter written in JavaScript, since real ARM code cannot execute in your browser; all real instruction encodings shown are for the genuine architecture. The next lesson, ARM Load/Store, Conditions and Branches, covers memory access modes and control flow.

Registers

AArch64 has 31 general-purpose registers, named x0 to x30, each 64 bits wide. There is no pattern of historical names as in x86: they are just numbered, and the conventions give them roles.

Register Role (standard AAPCS64 calling convention)
x0-x7 arguments and results (x0 holds the return value)
x8 indirect result location (e.g. large struct returns); also the syscall number on Linux
x9-x15 temporaries (caller-saved)
x16, x17 intra-procedure-call scratch registers (used by linkers and veneers)
x18 platform register: reserved by some operating systems (Apple, Windows), so avoid it
x19-x28 callee-saved
x29 frame pointer (FP)
x30 link register (LR): holds the return address after bl

Besides these there are a few special names:

  • sp: the stack pointer, a separate register that must be 16-byte aligned whenever it is used to access memory.
  • xzr and wzr: the zero register. Reading it always gives 0, and writing to it throws the result away. It looks like a register in the encoding (number 31) but is no storage cell.
  • pc: the program counter. Unlike x86's rip, it is not an ordinary operand: you cannot write mov x0, pc. Instructions such as adr and bl read it implicitly.
  • nzcv: the four condition flags Negative, Zero, Carry and oVerflow, set only by instructions that ask for it (such as cmp or adds).
  • v0-v31: 128-bit registers for floating point and NEON vector work, covered in Floating Point and SIMD.

The same register has two views. x0 is the full 64 bits; w0 is the low 32 bits. This mirrors the rax/eax pair of x86, and the zero-extension rule matches too: writing a w register clears the upper 32 bits of the full register. Writing w6 leaves x6 equal to the 32-bit value, zero-extended.

flowchart LR
  X["x0, 64 bits"] --> W["w0, low 32 bits"]
  W -->|"a write to w0 clears bits 63 to 32"| X

One register, two names; the w view is always zero-extended on writes.

One instruction, 32 bits

Every AArch64 instruction is exactly 4 bytes long. x86 instructions range from 1 to 15 bytes, so a decoder has to work out where each one ends. ARM64 hardware always knows the next instruction starts 4 bytes later, which simplifies fetching and decoding, and makes it easy to decode several instructions in parallel. The price is that a 32-bit word has to hold the opcode, the registers and any constant, so constants are small and some operations need two instructions.

Here is the layout of the register form of add: a single word holds everything.

flowchart LR
  A["sf, 1 bit, 1 means 64-bit"] --> B["opcode bits, 7 bits"] --> C["shift type, 2 bits"] --> Z["fixed zero, 1 bit"] --> D["Rm, 5 bits"] --> E["imm6, 6 bits, shift amount"] --> F["Rn, 5 bits"] --> G["Rd, 5 bits"]

The add (shifted register) format, 32 bits in total: three 5-bit register fields allow all 31 registers plus the zero register.

Five bits per register name is why there are 32 register numbers; number 31 means xzr in most places and sp in a few (address bases and some arithmetic forms).

The first instructions

The assembly syntax puts the destination first, like Intel syntax on x86. Arithmetic takes three operands, so the sources are not destroyed:

ASM
add  x0, x1, x2        // x0 = x1 + x2
sub  x0, x1, x2        // x0 = x1 - x2
mul  x0, x1, x2        // x0 = x1 * x2
add  x0, x1, #16       // second source can be a small immediate (12 bits)
mov  x3, x4            // copy a register
mov  x5, #1000         // load a constant

(The GNU assembler uses // for comments. The # before a constant is conventional in ARM syntax and optional in some assemblers.)

Many things that look like separate instructions are aliases. mov x3, x4 is really orr x3, xzr, x4 (OR the zero register with x4), and mul x0, x1, x2 is madd x0, x1, x2, xzr (multiply and add zero). You do not need to remember this, but it explains why a disassembler may print a different mnemonic than you wrote.

The last operand of arithmetic can be shifted for free:

ASM
add  x0, x1, x2, lsl #3     // x0 = x1 + (x2 << 3)   (address math for 8-byte elements)
sub  x4, x3, x1, lsl #2     // x4 = x3 - (x1 << 2)

This is the ARM counterpart of x86's scaled-index addressing, and compilers use it everywhere, for example a * 3 + b for long values becomes add x0, x0, x0, lsl #1 followed by add x0, x0, x1.

Loading constants

An instruction word has no room for a 64-bit constant, so large values are built from 16-bit pieces: movz sets a 16-bit field and clears the rest, movk ("keep") overwrites one field without touching the others, and movn loads the inverted value, which is how small negative numbers are made:

ASM
movz x0, #0x1234, lsl #16     // x0 = 0x12340000
movk x0, #0x5678              // x0 = 0x12345678
mov  x1, #-1                  // assembled as movn x1, #0 : x1 = 0xFFFFFFFFFFFFFFFF

When you write mov x0, #0x12345678 the assembler either picks one instruction (if the value fits one of the patterns it knows) or complains. Compilers emit movz/movk sequences, or load constants from a literal pool near the code.

Memory: ldr and str

ARM64 is a load-store architecture: arithmetic works on registers only, and only loads and stores touch memory. The basic forms:

ASM
ldr  x0, [x1]          // x0 = the 8 bytes at address x1
ldr  x0, [x1, #8]      // ...at address x1 + 8
str  x0, [x1, #16]     // store x0 at address x1 + 16
ldr  w0, [x1]          // load 4 bytes, zero-extended into x0

The offset is a constant. Where x86 would add to memory directly (add DWORD PTR [rdi], 1), ARM needs three instructions: load, add, store. The next lesson covers the other addressing modes, including pre- and post-indexing.

Real compiler output stays short. For these two C functions, GCC and Clang at -O2 emit essentially the following:

C
int  add32(int a, int b)   { return a + b; }
long mul3add(long a, long b) { return a * 3 + b; }
ASM
add32:
    add  w0, w0, w1            // 32-bit add, result in w0
    ret                        // return to the address in x30

mul3add:
    add  x0, x0, x0, lsl #1    // a * 3
    add  x0, x0, x1
    ret

A toy ARM machine

The first demo is a small ARM-flavored interpreter. It keeps 31 registers as 64-bit BigInts, supports w views with the zero-extension rule, the xzr register, shifted operands, mov, add, sub, mul, and ldr/str with a base register and an optional constant offset. Memory holds 8-byte values. A pseudo-instruction print shows a register in decimal (signed) and hex; it does not exist on real hardware. Syntax and semantics follow the real architecture for the instructions it implements, but it is a model, not an emulator: there are no flags, no 4-byte loads, and no instruction encoding.

JS Registers, w views and xzr in a toy ARM CPU

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check the interesting lines. x5 is the 64-bit all-ones pattern, which prints as -1 signed. After mov w6, w5 the register x6 is 0xffffffff, not all ones, exactly as in the x86 rule you learned in The x86-64 Register Set. print w6 shows the same bits through the 32-bit view, where they mean -1 again. The store/load pair at the end round-trips x4 through memory using the constant offset 8.

Seeing the 32-bit encodings

The second demo hand-assembles a few real AArch64 instructions into their 32-bit words, to prove the claim that every one is four bytes and to show where the register numbers sit. The encodings below are for 64-bit register forms: add and sub (shifted register, shift 0), mul, mov from a register (orr with xzr), mov of a 16-bit immediate (movz), ldr/str with an unsigned scaled offset, ret and nop. The tool then prints each word in hex together with its little-endian byte order, which is the order you would see in a memory dump.

JS Encode real AArch64 instructions into 32-bit words

Press Run (or Ctrl + Enter) to see the console output here.

No output.

You will see that add x0, x1, x2 becomes 8b020020, and that every row advances the address by exactly 4. The register numbers 0, 1 and 2 appear directly in the three 5-bit fields. The hex word for ret is d65f03c0: it is the same for every ret, because ret with no operand returns through register x30 (number 30 is in the Rn field of that word).

On Apple Silicon and most Linux ARM64 systems code runs little-endian, as on x86, so the bytes of each instruction word appear reversed in a memory dump. Instruction words are always 4-byte aligned. Arm also defines other instruction sets (32-bit A32 and T32); this tutorial covers only AArch64.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How many bytes does every AArch64 instruction occupy?

    AArch64 uses a fixed-width encoding: each instruction is one 32-bit word. That is why constants are limited and large ones need movz/movk sequences.
  2. 2. What happens to x6 after mov x6, #-1 followed by mov w6, w6?

    Writing a w register zero-extends into the full x register, so the upper half is cleared. This matches the x86-64 rule for 32-bit writes.
  3. 3. Which statement about xzr is correct?

    xzr (and wzr) is the zero register: reading gives 0 and writing does nothing. It lets aliases like mov be built from ordinary instructions (orr x0, xzr, x1).
  4. 4. Which register holds the return address after a bl instruction?

    x30 is the link register (LR). bl stores the return address there, and ret branches to it. x29 is the frame pointer, and x0 holds the return value.
  5. 5. What is the correct way to add x2 shifted left by 3 to x1 in one instruction?

    The last operand of arithmetic instructions can carry a free shift. lea and bracketed scaled operands are x86 ideas that do not exist in ARM64 arithmetic.

Summary

  • AArch64 has 31 general registers x0 to x30 (64-bit) with w0 to w30 as 32-bit views; writing a w register zero-extends.
  • xzr/wzr read as zero and discard writes, sp is separate, x30 is the link register, x29 the frame pointer, and pc is not a normal operand.
  • Every instruction is a fixed 32-bit word, so constants are small; wide immediates use movz/movk.
  • Arithmetic is three-operand (add x0, x1, x2) and the last operand can be shifted for free.
  • Memory is touched only by loads and stores such as ldr x0, [x1, #8].
  • Many familiar instructions (mov, mul, cmp) are aliases of more general ones using xzr.

Next: ARM Load/Store, Conditions and Branches, where memory addressing modes, flags and function calls complete the picture.