Salta al contenuto
Note per Studenti The ARM Core Architecture Report

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-r3 arguments and return value of functions, temporaries
r4-r11 variables; must be preserved by called functions
r12 IP temporary
r13 SP stack pointer
r14 LR link register: return address after BL
r15 PC 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 ZZ (bit 3030 of the CPSR) is 11.

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)

Example. With 32-bit byte addresses, 2322^{32} bytes =4=4 GB are addressable; an aligned word sits at an address that is a multiple of 44 (for example 100100).

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, #1 adds only if Z = 1. This avoids many short branches.

Example. After CMP r1, r1 the flag ZZ is 11 and ADDEQ r0, r0, #1 takes r0 from 44 to 55; with Z=0Z=0 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 #2 computes r1+4⋅r2r1 + 4 \cdot r2 (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 100+4⋅3=112100+4\cdot3=112.

  • 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

armasm
        .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 main

Labels 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-r15 as ordinary registers: they have fixed roles.
  • Expecting ADD to update the flags: ADDS is needed.

Versione ripasso

RISC architecture, 32-bit (ARMv7, A32).

Registers and memory

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 =r1+4r2= r_1 + 4r_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.

Teoria collegata