Course: Computer Architectures · Source lecture: ARM v7-M Architecture, Directives and Memory
1. Instructions versus directives
An instruction is encoded for the processor to execute. A directive is read by the assembler while it builds the program. The source-line form is:
label operation ; commentThe operation may be a real instruction, a pseudo-instruction or a directive. A label names the current location so branches and data references can use it. END ends the source file for the assembler; it is not a processor halt. B stop is an actual infinite branch loop.
2. Areas and attributes
At least one AREA is required in the source style shown:
AREA MyCode, CODE, READONLY
AREA MyData, DATA, READWRITECODE identifies machine instructions and is read-only by default. DATA identifies data and is read-write by default. READONLY and READWRITE describe permitted placement/updates. NOINIT reserves data that is not initialized from the image. ALIGN=expr requests section alignment.
3. Aliases and constants
RN gives a register a readable name:
counter RN r6
MOV counter, #3This is still r6; the alias does not allocate a register. EQU gives a name to an expression:
WORDS EQU 4
buffer SPACE WORDS * 4WORDS does not reserve memory. SPACE does; the example reserves 16 bytes.
4. Number and string notation
Decimal numbers can be written directly, hexadecimal with 0x, and other bases using the assembler’s n_xxx form. A character uses single quotes and a string uses double quotes:
letter DCB 'A'
text DCB "Hi", 0"Hi" occupies two bytes; the explicit 0 makes it three. Strings in this syntax are not automatically NUL-terminated.
5. Defining data
| Directive | Unit | Example |
|---|---|---|
DCB | byte | DCB 1, 2, 3 |
DCW | halfword | DCW 0x626D |
DCWU | unaligned halfword | DCWU 0x626D |
DCD | word | DCD 0x11223344 |
DCDU | unaligned word | DCDU 0x11223344 |
A word is four bytes. With little-endian memory, DCD 0x11223344 produces bytes 44 33 22 11 at increasing addresses. DCD normally aligns its start; DCDU does not.
If a section starts at 0x9000:
first DCB 0xAA
second DCD 10first is at 0x9000; padding fills 0x9001-0x9003; second starts at 0x9004. Replacing DCD with DCDU makes second start at 0x9001.
6. ALIGN, SPACE and FILL
ALIGN 4 pads until the next address divisible by four. ALIGN 4, 3 targets an address of the form 4×n+3. Padding bytes are zero.
SPACE expr reserves expr bytes:
buffer SPACE 12If it starts at 0x9000, the next location is 0x900C. FILL reserves and initializes a block:
array FILL 12, 1, 4There are 12 bytes total, each repeated value is represented in four bytes, so this creates three words containing the value 1.
7. Functions and literal pools
A function can be marked for readability:
add_two PROC
ADDS r0, r0, r1
BX LR
ENDPBX LR performs the return using the link register. PROC/ENDP do not themselves call or return.
A full constant may not fit in one immediate encoding. LDR r0, =0x12345678 is a pseudo-instruction: the assembler can place 0x12345678 in a nearby literal pool and generate a real load that copies it into r0. The constant remains in memory; the register receives a copy. LTORG asks the assembler to place the pool at that point so the load’s limited PC-relative range can reach it.
8. Lookup tables
A lookup table trades memory for computation. The byte table in the source stores squared values:
MOV r2, #8
LDR r0, =lookup
LDRB r4, [r0, r2]
lookup DCB 1,4,9,16,25,36,49,64,81,100,121Index 8 means offset 8 bytes, so the result is 81. The word factorial table needs four-byte addressing:
MOV r2, #6
LDR r0, =lookup
LDR r4, [r0, r2, LSL #2]
lookup DCD 1,1,2,6,24,120,7206 << 2 = 24 = 0x18; the result is 720. The load width and the index scaling must agree with the table element size.
Common confusions
DCDallocates data;LDRreads data at runtime.EQUnames a number;SPACEchanges the location counter.ALIGNinserts padding; it does not change the data values.LTORGstores constants in memory; it does not reserve a register permanently.DCDis a word declaration, but its bytes appear reversed by little-endian memory order.
Retrieval checks
Q: How many bytes does DCD 10,20,30 occupy?
A: 12 bytes.
Q: What does SPACE 16 do starting at 0x9000?
A: It reserves 0x10 bytes; the next address is 0x9010.
Q: Why does the factorial lookup use LSL #2?
A: Each factorial occupies one 4-byte word.
Q: What is the difference between B stop and END?
A: B stop executes a branch; END only tells the assembler the source has ended.