The ARM Core Architecture
In questa pagina 6
In questa pagina 3
ARM is a RISC architecture born in the 1980s and today the most widespread in the world (smartphones, microcontrollers, many laptops and servers). The company designs the instruction set and the cores and sells the licence. The examples in these notes use the 32-bit version (ARMv7, A32 instruction set), with a short note on the 64-bit one.
Registers
Definition (32-bit ARM registers). 16 visible 32-bit registers,
r0-r15:
Register Name Use r0-r3arguments and return value of functions, temporaries r4-r11variables; must be preserved by called functions r12IP temporary r13SP stack pointer r14LR link register: return address after BLr15PC program counter
Example. Calling f(5, 7) the arguments arrive in r0=5 and r1=7.
The usage conventions of r0-r12 are fixed by the calling convention (AAPCS, see Stack and Function Calls in ARMCall with BL and return with BX LR; the AAPCS calling convention (arguments in r0-r3, result in r0, registers to preserve r4-r11); full-descending stack with PUSH and POP; activation record; leaf and non-leaf functions; recursive factorial example with the evolution of the stack.Stack and Function Calls in ARM →). Reading r15 gives the address of the current instruction + 8 (effect of the original 3-stage pipeline).
Definition (status register CPSR). CPSR (Current Program Status Register): flags N, Z, C, V in bits 31-28, the interrupt-disable bits and the bits of the current mode.
Example. After CMP r0, r0 the flag (bit of the CPSR) is .
Modes: User (unprivileged) and privileged modes (Supervisor, IRQ, FIQ, Abort, Undefined, System). On entering an exception mode some registers (at least SP and LR) are replaced by private copies (banked) and the CPSR is saved into the SPSR of that mode (see Interrupts and ExceptionsClasses of interrupts; interrupt-driven I/O; hardware and software handling sequence with context saving on the stack; identifying the source (multiple lines, polling, daisy chain, vector); priorities and nested interrupts; exceptions and modes in ARM.Interrupts and Exceptions →).
Memory model
Definition (ARM memory model)
- 32-bit byte addresses: bytes = 4 GB addressable.
- Words of 4 bytes, normally aligned to addresses that are multiples of 4.
- Usually little endian.
- Load/store architecture: only
LDR/STRand their variants access memory (see ARM Memory-Access InstructionsLDR and STR for words, bytes and half-words with and without sign; addressing modes with immediate or scaled-register offset, pre-indexed with write-back and post-indexed, with effective address examples; loading addresses and constants; LDM and STM.ARM Memory-Access Instructions →).
Example. With 32-bit byte addresses, bytes GB are addressable; an aligned word sits at an address that is a multiple of (for example ).
Characteristics of the instruction set
- 32-bit instructions with 3 operands:
ADD r0, r1, r2.
Definition (conditional execution). Almost every instruction can carry a condition suffix and is executed only if the flags satisfy it:
ADDEQ r0, r0, #1adds only if Z = 1. This avoids many short branches.
Example. After CMP r1, r1 the flag is and ADDEQ r0, r0, #1 takes r0 from to ; with it would do nothing.
- Flags are updated only on request: suffix S (
ADDS,SUBS) or compare instructions (CMP,TST).
Definition (flexible second operand). The second operand is an immediate constant or a register optionally scaled by a shift, in the same instruction:
ADD r0, r1, r2, LSL #2computes (see ARM Data-Processing InstructionsAdd, subtract, logical, move and multiply instructions of ARM, the immediate and shifted-register second operand, flag updates with S, CMP and TST, and translation of C expressions into ARM code.ARM Data-Processing Instructions →).
Example. With r1=100 and r2=3, ADD r0, r1, r2, LSL #2 computes .
- Load/store with base + offset addressing, pre- and post-indexed, and multiple transfers (
LDM/STM,PUSH/POP). - Thumb: an alternative 16-bit format for a subset of the instructions, code about 30% more compact; Thumb-2 mixes 16- and 32-bit instructions.
GNU assembler syntax
.data
x: .word 5 @ one word initialised to 5
.text
.global main
main: LDR r1, =x @ r1 = address of x
LDR r0, [r1] @ r0 = x
ADD r0, r0, #1 @ r0 = x + 1
STR r0, [r1] @ x = x + 1
BX lr @ return to the caller of mainLabels end with :, comments start with @ (or //), immediate constants with #, directives start with a dot (.data, .text, .word, .byte, .asciz, .space).
ARMv8 64-bit (AArch64)
31 general 64-bit registers (x0-x30, with the low halves w0-w30), zero register xzr; SP and PC are not general registers; instructions are always 32 bits; no generalised conditional execution (CSEL and conditional branches remain). The concepts of these notes remain valid.
Typical mistakes
- Using
r13-r15as ordinary registers: they have fixed roles. - Expecting
ADDto update the flags:ADDSis needed.
Versione ripasso
RISC architecture, 32-bit (ARMv7, A32).
Registers and memory
- 16 registers:
r0-r3arguments and result;r4-r11variables (preserved);r12IP;r13SP;r14LR;r15PC (reads as instruction ). AAPCS: Stack and Function Calls in ARMCall with BL and return with BX LR; the AAPCS calling convention (arguments in r0-r3, result in r0, registers to preserve r4-r11); full-descending stack with PUSH and POP; activation record; leaf and non-leaf functions; recursive factorial example with the evolution of the stack.Stack and Function Calls in ARM →. - CPSR: flags N, Z, C, V (bits 31-28), interrupt bits, mode. Modes User and privileged (Supervisor, IRQ, FIQ, Abort, Undefined, System); SP and LR banked, CPSR saved in SPSR (Interrupts and ExceptionsClasses of interrupts; interrupt-driven I/O; hardware and software handling sequence with context saving on the stack; identifying the source (multiple lines, polling, daisy chain, vector); priorities and nested interrupts; exceptions and modes in ARM.Interrupts and Exceptions →).
- 32-bit byte addresses (4 GB); aligned 4-byte words; little endian; load/store (ARM Memory-Access InstructionsLDR and STR for words, bytes and half-words with and without sign; addressing modes with immediate or scaled-register offset, pre-indexed with write-back and post-indexed, with effective address examples; loading addresses and constants; LDM and STM.ARM Memory-Access Instructions →).
Instructions
32 bits, 3 operands. Conditional execution (ADDEQ r0,r0,#1) instead of short branches; flags only with S or CMP, TST; flexible second operand (ADD r0,r1,r2,LSL #2 , ARM Data-Processing InstructionsAdd, subtract, logical, move and multiply instructions of ARM, the immediate and shifted-register second operand, flag updates with S, CMP and TST, and translation of C expressions into ARM code.ARM Data-Processing Instructions →); LDM/STM, PUSH/POP; Thumb 16-bit (about 30% more compact).
GNU assembler
.data, x: .word 5, .text, .global main; main: LDR r1,=x; LDR r0,[r1]; ADD r0,r0,#1; STR r0,[r1]; BX lr. Labels :, comments @, immediates #. AArch64: 31 64-bit registers (x0-x30), 32-bit instructions.
Typical mistakes: r13-r15 have fixed roles; ADD does not update flags, ADDS does.