Course: Computer Architectures
Sources: R. Ferrero, Politecnico di Torino: ARM v7-M architecture (16 PDF pages), Directives (24 PDF pages), and Accessing memory (25 PDF pages). The PDFs do not state a lecture number or date; this note preserves their filenames instead of inventing either. Original files remain local.
Why these decks belong together
The three decks form one path from a program’s source text to a running ARMv7-M program:
- The architecture deck defines the processor state: registers, status flags, modes, exceptions and instruction encodings.
- The directives deck explains how source code is divided into code/data sections and how bytes, halfwords, words and constants are laid out.
- The memory deck shows how instructions move those bytes between memory and registers, then uses the same rules for arrays and lookup tables.
Use ARM Registers, Data Sizes and Status for processor state, ARM Memory Access and Addressing for memory traces, and ARM Assembly Directives and Tables for assembler-controlled layout.
1. Data sizes, instructions and registers
A byte is 8 bits, a halfword is 16 bits and a word is 32 bits. ARMv7-M registers are 32 bits wide. The size of an instruction is a separate idea: Thumb instructions can be 16 bits and Thumb-2 instructions can be 16 or 32 bits. A 16-bit instruction can still add two 32-bit register values; its length describes the encoding, not the data width.
The general-purpose registers are r0 through r12. The architectural aliases that matter most in these examples are:
| Register | Alias | Role |
|---|---|---|
r13 | SP/MSP/PSP | stack pointer |
r14 | LR | link/return register |
r15 | PC | program counter |
r0-r12 can hold ordinary values, addresses, indexes or intermediate results. A register holding an address is still only holding a number; brackets in a load/store instruction are what make the number an address to access.
2. Status, modes and privilege
xPSR can be viewed as three parts:
- APSR: arithmetic flags
N,Z,C,Vand saturation flagQ. - EPSR: execution state, including the Thumb bit and
ITconditional-execution state. - IPSR: the active exception number. Zero indicates normal Thread mode.
N reflects a negative signed result, Z is one when a result is zero, C records unsigned carry for addition and no-borrow for subtraction, and V records signed overflow. Q is sticky: saturating arithmetic can set it and a later ordinary operation does not automatically clear it.
ARMv7-M has Thread mode for ordinary program code and Handler mode while servicing an exception. Handler mode is always privileged. Thread mode may be privileged or unprivileged. CONTROL[0] selects the Thread-mode privilege level; CONTROL[1] selects the active stack pointer, MSP or PSP. Unprivileged Thread mode cannot simply grant itself privilege again.
3. Vector table, priorities and masks
The vector table stores addresses used to enter exception handlers. At the default base shown in the slides, entry 0 at 0x00000000 contains the initial stack pointer, entry 1 at 0x00000004 contains the Reset handler address, entry 2 at 0x00000008 contains the NMI handler address and entry 3 at 0x0000000C contains the HardFault handler address. Each entry is four bytes; entry address and stored handler address are different things.
The table gives Reset priority -3, NMI -2, HardFault -1, and programmable priorities for most other exceptions. A lower priority number means greater urgency. A higher-urgency interrupt can preempt a lower-urgency handler. PRIMASK, BASEPRI and FAULTMASK can block classes of exceptions; masked interrupts can remain pending and run later.
4. Instruction behaviour and flags
Most data-processing instructions operate on registers. The S suffix requests flag updates:
MOV r0, #5
SUBS r0, r0, #5 ; r0 = 0, so Z = 1CMP r0, #5 performs the subtraction for flags but does not save the result. A following BEQ label branches when Z = 1:
CMP r0, #5
BEQ equalThis is a flag-based ARM pattern. RISC-V’s beq rs1, rs2, label compares its two registers directly.
Signed and unsigned branches must be distinguished. After CMP:
- unsigned
BHImeans higher (C=1andZ=0), whileBLOmeans lower (C=0); - signed
BGTandBLTuse the signed relationship represented by the flags.
For r0 = 0xFFFFFFFF and r1 = 1, unsigned comparison sees 4,294,967,295 > 1, while signed comparison sees -1 < 1.
5. Conditional execution and source programs
Thumb-2 can make a short block conditional with IT:
CMP r6, #0
ITTT GT
MULGT r7, r6, r7
SUBGT r6, r6, #1
BGT loopITTT GT applies the GT condition to the next three instructions. SUBGT has no S suffix, so it does not replace the flags produced by CMP; BGT still uses the comparison result. The factorial trace is:
| Pass | r6 before | r7 before | product | r6 after |
|---|---|---|---|---|
| 1 | 4 | 1 | 4 | 3 |
| 2 | 3 | 4 | 12 | 2 |
| 3 | 2 | 12 | 24 | 1 |
| 4 | 1 | 24 | 24 | 0 |
The loop tests zero once more, then skips the conditional block. The final factorial is 24.
The shift example uses LSL:
MOV r0, #0x11
LSL r1, r0, #1 ; 0x22
LSL r2, r1, #1 ; 0x44For these values, shifting left one bit multiplies by two. The XOR swap uses EOR three times to exchange two registers without a temporary register; A XOR A = 0 and A XOR 0 = A explain why the original values reappear.
6. Source layout and directives
A source line has the form:
label operation ; commentThe operation may be an instruction, pseudo-instruction or assembler directive. AREA starts a section. CODE and DATA distinguish instructions from data; READONLY, READWRITE and NOINIT describe placement and initialization. RN gives a register an alias, and EQU names a constant without allocating storage.
DCB, DCW and DCD allocate bytes, halfwords and words. DCDU defines an unaligned word. ALIGN pads to a boundary, SPACE reserves bytes and FILL reserves and initializes a block. PROC/ENDP delimit a function for readability, and END terminates the source file for the assembler. None of these directives is executed by the processor.
A string is byte data and is not automatically NUL-terminated:
message DCB "Hi", 0This occupies three bytes. A literal pool stores constants that cannot conveniently be encoded in one instruction. LDR r0, =0x12345678 is a pseudo-instruction; LTORG can place the pool near the code so the generated load can reach it.
7. Memory map and memory access
The Cortex-M3 map divides the address space into code, SRAM, peripheral, external and system regions. The exact physical devices depend on the chip. Peripheral addresses are memory-mapped I/O: a load can read hardware status and a store can trigger hardware behaviour.
LDR, LDRB, LDRH, LDRSB, LDRSH, LDRD, STR, STRB, STRH, STRD, LDM and STM provide the transfers discussed in the memory deck. The memory attributes table distinguishes executable, cacheable and bufferable regions; peripheral accesses are treated differently from ordinary RAM because stale or reordered device accesses could be incorrect.
8. The memory rules to retain
Assume:
0x9004: 11 0x9005: 22 0x9006: 33 0x9007: 44Then:
LDRB r1, [r0] ; 0x00000011, one byte
LDRH r1, [r0] ; 0x00002211, two bytes
LDR r1, [r0] ; 0x44332211, four bytesThe brackets mean “access memory at the address in the base register.” Offset and writeback forms are explained in ARM Memory Access and Addressing. For an array, base + index × element_size gives the element address. A byte lookup uses the index directly; a word lookup uses LSL #2.
Source page map
| Source | Pages | Material |
|---|---|---|
2_v7Marchitecture.pdf | 2-4 | sizes, instructions, registers |
| 5-9 | xPSR, flags, modes, privilege, CONTROL | |
| 10-12 | vector table and instruction-set features | |
| 13-15 | shift, factorial, XOR swap | |
| 16 | Keil installation | |
3_directives.pdf | 2-9 | syntax, sections, aliases, constants, notation |
| 10-15 | data allocation and alignment variants | |
| 16-24 | ALIGN, SPACE, FILL, LTORG, functions, END | |
4_memory.pdf | 2-4 | map, attributes, load/store forms |
| 5-10 | load/store traces and endianness | |
| 11-20 | addressing modes and pointer updates | |
| 21-25 | lookup tables |
Retrieval questions
- Why does
LDR r1, [r0]read memory whileMOV r1, r0copies an address?
Answer: brackets request a memory access;MOVonly copies register contents. - Why does a word lookup use
LSL #2?
Answer: a word is four bytes, so the byte offset is index × 4. - What does
STR r1, [r0], #4change?
Answer: it stores four bytes at the old address, then increasesr0by four. - Why can
BNEfollowSUBSwithout a separateCMP?
Answer:SUBSalready updatedZ.