Course: Computer Architectures · Source lecture: ARM v7-M Architecture, Directives and Memory
1. Data sizes and register width
A byte is 8 bits, a halfword is 16 bits and a word is 32 bits. The registers r0-r15 are 32 bits wide. This creates two separate questions:
- How many bytes does the instruction transfer?
- How many bits can the destination register hold?
LDRB transfers one byte into a 32-bit register; LDRH transfers two; LDR transfers four. The unused high bits must be filled. Instruction length is another separate property: Thumb instructions can be 16 bits and Thumb-2 instructions can be 16 or 32 bits while still operating on 32-bit registers.
2. Important registers
| Register | Role |
|---|---|
r0-r12 | general-purpose values, addresses and temporaries |
r13 / SP | stack pointer |
r14 / LR | return address for a call |
r15 / PC | next instruction location |
The alias does not create another register. SP and r13 name the same register.
3. xPSR and flags
xPSR combines:
- APSR:
N,Z,C,V,Q. - EPSR: Thumb state and
ITconditional-execution state. - IPSR: current exception number.
Z is one when the arithmetic result is zero. N reflects the sign bit of a signed result. C means carry for addition; for subtraction, C=1 means no borrow. V means signed overflow. Q is a sticky saturation flag.
A useful contrast:
MOV r0, #5
SUBS r0, r0, #5 ; r0=0, Z=1
BEQ equal ; branches because Z=1CMP performs a comparison by setting flags without writing the subtraction result. A later flag-writing instruction could change the decision, so CMP normally sits immediately before the conditional branch.
For 0x7FFFFFFF + 1, the result becomes 0x80000000: signed overflow V=1, but unsigned carry C=0. For 0xFFFFFFFF + 1, the result wraps to zero: C=1, while V=0.
4. Signed versus unsigned branches
After CMP r0, r1, the same bits can be interpreted two ways. If r0=0xFFFFFFFF and r1=1, unsigned comparison says r0 is greater; signed comparison says -1 is less. Use BHI/BHS/BLO/BLS for unsigned relationships and BGT/BGE/BLT/BLE for signed relationships.
5. Modes, privilege and stacks
Thread mode runs normal code. Handler mode runs an exception handler and is always privileged. Thread mode can be privileged or unprivileged. CONTROL[0] selects the Thread-mode privilege level; CONTROL[1] selects MSP or PSP. An unprivileged program cannot simply change its own privilege bit.
IPSR=0 means normal Thread mode. A nonzero exception number means a handler is active. This is separate from priority: the exception number identifies the event; the priority determines urgency.
6. Vector table and interrupt controls
The vector table has four-byte entries. At base zero, entry 2 is at 0x00000008. Entry 0 contains the initial stack pointer; later entries contain handler addresses. A lower priority number is more urgent. NMI and HardFault have fixed high urgency in the source table; many other exceptions use programmable priorities.
PRIMASK, BASEPRI and FAULTMASK block different sets of exceptions. Blocking delays eligible handling; it does not necessarily erase a pending interrupt.
7. Conditional execution and source examples
The factorial example uses:
CMP r6, #0
ITTT GT
MULGT r7, r6, r7
SUBGT r6, r6, #1
BGT loopThe comparison sets flags. The next three instructions run only while r6 > 0. Starting with r6=4, r7=1, the products are 4, 12, 24, 24; the final result is 24.
LSL r1, r0, #1 shifts left one bit. For r0=0x11, r1 becomes 0x22; shifting again gives 0x44. The XOR-swap example uses three EOR operations and relies on XOR cancelling a value when applied twice.
Common confusions
- Register width is not instruction length.
CMPchanges flags but not its visible operand register.BEQdoes not compare registers itself; it checksZset by an earlier instruction.- An exception number is not a priority number.
- Handler mode is always privileged, but Thread mode can be either.
Retrieval checks
Q: What does r13 alias?
A: SP, the stack pointer.
Q: If CMP r0,#3 runs with r0=5, does BEQ branch?
A: No: 5-3 is nonzero, so Z=0.
Q: Why is V, rather than C, the relevant flag for 127+1 as signed 8-bit arithmetic?
A: The signed range is exceeded; unsigned 127+1 still fits.