Course: Computer Architectures
Related source: ARM v7-M Architecture, Directives and Memory
Source: R. Ferrero, Politecnico di Torino, Constants and literal pools, 28 PDF pages. Filename: 5_literalPool.pdf; no lecture number or date is stated. The PDF carries the source deck’s CC BY-SA notice and remains local.
Why this deck matters
A source line can look as if it puts any 32-bit number into a register, but the encoded instruction has limited space. This deck explains what constants MOV can encode, how MVN, MOVW and MOVT extend that range, and how LDR =..., literal pools, ADR and ADRL solve the remaining constant/address cases.
1. MOV does not accept every 32-bit constant
MOV r0, #constant means “place an immediate constant in r0.” It does not mean “read memory,” and it cannot directly place an address or memory content into a register. MOV r1, r0 copies a register value; MOV r2, label asks for an address and is not the ordinary constant form; MOV r4, [label] is invalid because memory access needs a load instruction.
The immediate encoding is restricted. The source describes values made from an 8-bit pattern shifted left by an allowed amount, plus repeated-byte patterns such as 0x00XY00XY, 0xXY00XY00 and 0xXYXYXYXY. The exact encoding determines whether a written MOV is valid; the assembler may choose a different instruction when the source syntax is a pseudo-instruction.
2. Alternatives for constants
MVN writes the bitwise one’s complement of its operand:
MVN r0, #0x14 ; r0 = 0xFFFFFFEBMOVW writes a 16-bit constant into the low halfword and clears the upper halfword:
MOVW r0, #0xA1B2 ; r0 = 0x0000A1B2MOVT writes a 16-bit constant into the high halfword while preserving the low halfword:
MOVW r0, #0x47D2
MOVT r0, #0xC901 ; r0 = 0xC90147D2The pseudo-instruction MOV32 r0, #0xC90147D2 can be assembled as that MOVW/MOVT pair. The deck also shows the assembler replacing MOV r0, #-2 with an MVN form and MOV r0, #0xA1B2 with MOVW when the plain MOV encoding is not the selected representation.
3. LDR =constant is a decision made by the assembler
LDR r0, =0xC90147D2This is a pseudo-instruction. If the constant is encodable by a suitable MOV-family instruction, the assembler can emit that instruction. Otherwise it places the constant in a literal pool and emits a PC-relative load, conceptually:
LDR r0, [PC, #offset]The literal pool is ordinary program memory containing the constant. The load copies the constant into r0; it does not leave the value “inside a register without memory,” and it does not access the eventual data address unless the stored constant itself is an address.
4. Computing the PC-relative offset
For the source’s Thumb example, the effective PC is calculated from the current instruction address:
- take the instruction address;
- add 4;
- clear bit 1 for word alignment.
If the instruction is at 0x118 and the pool word begins at 0x144:
0x118 + 4 = 0x11C
PC = 0x11C
offset = 0x144 - 0x11C = 0x28 = 40The generated instruction is therefore conceptually:
LDR r0, [PC, #40]The pool may show a 32-bit constant split across two aligned halfwords in the slide diagram, but the load is intended to reconstruct the word value according to the assembler’s literal-pool representation.
5. Range and LTORG
By default, the assembler places the literal pool at the END location. A normal Thumb LDR has a nonnegative word-aligned range shown in the deck as 0 to 1020 bytes. If the pool is farther away, LDR.W uses a Thumb-2 encoding with a range shown as -4095 to +4095.
Examples from the source:
LDRfollowed bySPACE 1020is valid when the pool remains within its range.SPACE 1021makes the ordinary Thumb form out of range.LDR.Wcan handle the larger distance in the next example, but a gap of 4095 still exceeds the stated range.LTORGplaces the pool immediately at that point, soLDR r0, =constantcan use offset zero even when later code contains a largeSPACEblock.
The problem LTORG solves is reachability, not register scarcity. The constant must be close enough for the generated PC-relative load.
6. Loading addresses: LDR, ADR and ADRL
Two address pseudo-instructions have different mechanisms:
LDR r0, =label
ADR r1, labelLDR r0, =label puts the label’s address in a literal pool and loads it through a PC-relative memory access. It can refer to a label outside the current area.
ADR r1, label computes an address by adding or subtracting a PC-relative offset. It does not need a literal-pool word, so it does not increase code size in the same way, but its offset range and alignment are limited. The deck states:
ADR: Thumb form, positive word-aligned offset 0-1020;ADR.W: Thumb-2 form, offset 0-4095 and usable when the target is not word-aligned;ADRL: two Thumb-2 instructions, reaching up to about 1 MiB.
ADR is for an address in the same area. LDR =label is the more general literal-pool mechanism.
7. Stack address example
The source declares:
Stack_Size EQU 0x00000200
AREA STACK, NOINIT, READWRITE
Stack_Mem SPACE Stack_Size
__initial_sp
AREA |.text|, CODE, READONLY
LDR r12, =Stack_MemStack_Size is the number 0x200, and SPACE reserves 512 bytes. Stack_Mem labels the bottom of that block. The symbol __initial_sp identifies the next address, the top of the stack block. LDR r12, =Stack_Mem loads the bottom address; the source explains the relationship as r12 = r13 - 0x200 when r13 is the top address.
Source page map
| Pages | Material |
|---|---|
| 2-7 | MOV, immediate encodings and shifted 8-bit constants |
| 8-13 | MVN, MOVW, MOVT and valid constant strategies |
| 14-18 | LDR =constant, literal pools, PC and range |
| 19-23 | valid/invalid Thumb, Thumb-2 and LTORG examples |
| 24-28 | address loading with LDR, ADR, ADR.W and ADRL |
Retrieval questions
- Why can
MOV r0, #0xC90147D2require another strategy?
Answer: the immediate encoding cannot represent every 32-bit pattern; use MOV-family construction or a literal-pool load. - What does
MOVWset?
Answer: a 16-bit low halfword, with the usual upper-halfword result shown as zero when constructing a fresh value. - Why does
LTORGfix an out-of-range literal load?
Answer: it places the pool closer to the instruction, reducing the PC-relative offset. - What is the difference between
LDR r0, =labelandADR r0, label?
Answer: the first loads an address stored in a literal pool; the second computes a nearby PC-relative address.