Skip to content
systemcoding.dev
Assembly

Zero a register on AArch64 with mov x0, xzr

Zero a register on AArch64 with mov x0, xzr, where register number 31 reads as zero in this encoding and as sp in others.

1 min read
Summarize in AI

Setting a register to zero is mov x0, xzr. The zero register xzr always reads as 0, and writes to it vanish, so it serves as both a constant and a bit bucket.

zero.Sasm
// GNU as 2.4x, AArch64 Linux
    .text
    .global zero
zero:
    mov   x0, xzr
    ret
Text
zero() = 0

The encoding detail is that register number 31 means different things depending on the instruction: in most data-processing forms it is xzr, while in address operands and in a few arithmetic forms it is sp. The assembler picks the right name, but reading a disassembly you may see mov x0, xzr and mov x0, sp as the same register number in different roles. mov x0, xzr is an alias of orr x0, xzr, xzr.

Writing the 32-bit name, mov w0, wzr, clears the whole 64-bit register because every write to a w register zeroes the upper half. A move from an immediate, mov x0, #0, assembles to movz and is also one instruction, so the zero register form matters mainly when you need zero as an operand, as in cmp x1, xzr or str xzr, [x0]. See the Arm A64 instruction set reference.

Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.