home blog portfolio Ian Fisher

ARM cheatsheet

Arithmetic

// r1 := 42 (zero-extended)
mov r1, #42

// r1 := r2 + r3
add r1, r2, r3
// r1 := r2 - r3
sub r1, r2, r3

// r1 := r1 + 1
add r1, r1, #1

// r1 := r4 - (r2 * r3)
msub r1, r2, r3, r4

Bitwise

// AND, OR, XOR
and r1, r2, r3
orr r1, r2, r3
eor r1, r2, r3

// r1 := r2 shifted right/left by 8 bits
lsr r1, r2, #8
lsl r1, r2, #8

Memory

// r1 := MEM[r2]
ldr r1, [r2]

// r1 := MEM[r2 + r3]
ldr r1, [r2, r3]
// r1 := MEM[r2 + 8]
ldr r1, [r2, #8]

// r1 := MEM[r2]
// r2 := r2 + 8
ldr r1, [r2], #8

// r2 := r2 + 8
// r1 := MEM[r2]
ldr r1, [r2, #8]!

// r1 := MEM[r3 << 3]
ldr r1, [r2, r3, lsl #3]

// MEM[r2] := r1
str r1, [r2]

TODO: stp and ldp

Branching

// unconditional jump
b label

// function call (puts return address in x30)
bl label

// function return (jumps to address in x30)
ret

// jump to address in r1
br r1
blr r1

// jumps to label if register is (not) 0
cbz r1, label
cbnz r1, label

// compare and jump if condition holds
cmp r1, r2
b.lt label

Condition codes (p. 144):

Addresses

// sets r1 to the address of label relative to the PC
// +/- 1 MB
adr r1, label

// same as adr but page-aligned so low 12 bits are zero
// +/- 4 GB
adrp r1, label

Flag manipulation

// r1 := 1 if f is set, otherwise r1 := 0
cset r1, f

Note that add does not set flags by default! Use adds if you need flags.

See also