Section 4.43

Modified immediate encoding

Definition

A modified immediate encoding is a way to store a 32-bit constant in fewer than 32 bits of an instruction: the instruction holds a short field, which the processor expands into the constant by a fixed rule. A constant stored inside an instruction is called an immediate. In 32-bit Arm Thumb instructions the field has 12 bits, and the rule uses an 8-bit value as it is, repeats it in two or four bytes, or moves an 8-bit value whose top bit is 1 left by 1 to 24 places.

The idea

A 32-bit instruction cannot also carry an arbitrary 32-bit constant, because it needs bits for the operation and the registers. Most constants that programs use, however, are small numbers from 0 to 255 or bit masks §4.50 such as 0x80000000, 0x0000FF00 or 0x00FF00FF. Twelve bits cannot hold such masks as plain numbers, since 12 bits reach only 4095, but they can hold a short recipe such as “the 8-bit value 0x80, moved to the top of the word”. The modified immediate encoding spends its 12 bits on such recipes, so one instruction covers the common masks as well as the small numbers. Exactly 4093 of the 2322^{32} possible constants can be encoded.

The cost is that the field is not the constant. A reader of machine code who takes the 12 bits as a number gets the wrong constant. A reader who ignores which recipe was used also gets the carry flag §4.42 wrong, because the rotation that forms some constants produces a carry bit that certain instructions copy into the C flag, the status bit that records an unsigned carry.

How it works

Where the 12 bits are. In a 32-bit Thumb instruction, which consists of two 16-bit halfwords, the field is split into three parts: bit i is bit 10 of the first halfword, the 3-bit part imm3 is bits 14 to 12 of the second halfword, and the 8-bit part imm8 is bits 7 to 0 of the second halfword. Joined in that order, with i as the most significant bit, they form the 12-bit field, written imm12. Number its bits from 11 (highest) down to 0, and call bits 7 to 0 the byte xx.

Expansion. The Armv7-M manual defines the rule as the pseudocode function ThumbExpandImm_C, which returns the constant and a carry output.

  1. If bits 11 and 10 are both 0, bits 9 and 8 choose a byte pattern. Below, XY stands for the two hexadecimal digits of xx.

    • 00: the constant is 0x000000XY, the byte xx itself;

    • 01: 0x00XY00XY, the byte in bytes 0 and 2;

    • 10: 0xXY00XY00, the byte in bytes 1 and 3;

    • 11: 0xXYXYXYXY, the byte in all four bytes.

    The patterns 01, 10 and 11 with x=0x = 0 are unpredictable, Arm’s term for an encoding whose behavior the architecture does not define and that software must not rely on. In this class the carry output equals the incoming C flag, unchanged.

  2. Otherwise, form the 8-bit value 1 followed by bits 6 to 0, a value from 128 to 255, and rotate it right within 32 bits by the number in bits 11 to 7, which lies between 8 and 31. The carry output is bit 31 of the result.

Rotation right by rr moves every bit rr places toward bit 0, and bits that fall off the bottom re-enter at the top. The value to be rotated sits in bits 7 to 0 and rr is at least 8, so every one of its bits wraps around: rotating right by rr gives the same result as shifting left by 32−r32 - r, a shift of between 1 and 24 places.

Which instructions use the carry output.

Other immediates in Thumb code are not modified immediates. ADDW and SUBW take a plain 12-bit value from 0 to 4095, and MOVW and MOVT take a plain 16-bit value for the bottom or the top half of a register. A decoder must apply the rule that belongs to the specific encoding.

The original 32-bit Arm instruction set, called A32, uses a different rule: an 8-bit value rotated right by an even number of places, twice a 4-bit field. Statements about one rule do not carry over to the other.

Worked example

A rotated constant. An assembler encodes and.w r0, r1, #0x80000000 (set r0 to r1 AND 0x80000000, without setting flags) as the bytes 01 f0 00 40.

  1. Read as little-endian §4.21 halfwords, the first halfword is 0xF001 and the second is 0x4000.

  2. 0xF001 is binary 1111 0000 0000 0001, so i, its bit 10, is 0. 0x4000 is binary 0100 0000 0000 0000, so imm3, its bits 14 to 12, is 100, and imm8, its bits 7 to 0, is 0x00.

  3. The field is 0 100 00000000, which is 0x400, decimal 1024.

  4. Bits 11 and 10 are 0 and 1, not both 0, so this is the rotation class.

  5. The value to rotate is 1 followed by bits 6 to 0, which are 0000000: binary 10000000, or 128. The rotation amount is bits 11 to 7, 01000, or 8.

  6. Rotating 128 right by 8 equals shifting it left by 32−8=2432 - 8 = 24: 128×224=128×16,777,216=2,147,483,648128 \times 2^{24} = 128 \times 16{,}777{,}216 = 2{,}147{,}483{,}648, which is 0x80000000. Its bit 31 is 1, so the carry output is 1.

The constant is 0x80000000, not 0x400. This AND sets no flags, so the carry output goes unused. The flag-setting form ands.w r0, r1, #0x80000000, stored as 11 f0 00 40 and differing only in the S bit, bit 4 of the first halfword, sets C to 1 whatever C was before.

A repeated byte. ands.w r0, r1, #0x00010001 is stored as 11 f0 01 10. The second halfword 0x1001 gives imm3 = 001 and imm8 = 0x01, and i is 0, so the field is 0 001 00000001, or 0x101. Bits 11 and 10 are both 0 and bits 9 and 8 are 01, so the constant is the byte 01 in bytes 0 and 2: 0x00010001. The carry output is the incoming C. If C was 1 before, it stays 1, although bit 31 of the constant is 0. N and Z come from r1 AND 0x00010001.

Three tests of one bit. Each instruction below tests a single bit of r0 and sets N and Z alike, yet each treats C differently.

Stated precisely

Let mm be the 12-bit field, x=mmod256x = m \bmod 256 its bits 7 to 0, t=⌊m/256⌋t = \lfloor m / 256 \rfloor its bits 11 to 8, and CinC_{\mathrm{in}} the incoming carry flag, where ⌊y⌋\lfloor y \rfloor means yy rounded down to a whole number. For a 32-bit value vv and 0<r<320 < r < 32, rotation right is ROR(v,r)=⌊v/2r⌋+(v⋅232−rmod232);\mathrm{ROR}(v, r) = \lfloor v / 2^{r} \rfloor + \left(v \cdot 2^{32 - r} \bmod 2^{32}\right); the two parts occupy different bits, so adding them is the same as combining them with a bitwise OR. The constant KK and the carry output CoutC_{\mathrm{out}} are (K,Cout)={(x,Cin)if t=0,(x⋅(216+1),Cin)if t=1 and x≠0,(x⋅(224+28),Cin)if t=2 and x≠0,(x⋅(224+216+28+1),Cin)if t=3 and x≠0,(ROR(128+(mmod128),⌊m/128⌋),bit 31 of K)if t≥4.(K, C_{\mathrm{out}}) = \begin{cases} (x,\ C_{\mathrm{in}}) & \text{if } t = 0, \\ (x \cdot (2^{16} + 1),\ C_{\mathrm{in}}) & \text{if } t = 1 \text{ and } x \ne 0, \\ (x \cdot (2^{24} + 2^{8}),\ C_{\mathrm{in}}) & \text{if } t = 2 \text{ and } x \ne 0, \\ (x \cdot (2^{24} + 2^{16} + 2^{8} + 1),\ C_{\mathrm{in}}) & \text{if } t = 3 \text{ and } x \ne 0, \\ (\mathrm{ROR}(128 + (m \bmod 128),\ \lfloor m / 128 \rfloor),\ \text{bit 31 of } K) & \text{if } t \ge 4. \end{cases} The three fields with t∈{1,2,3}t \in \{1, 2, 3\} and x=0x = 0, namely 0x100, 0x200 and 0x300, are unpredictable.

Counting the cases: t=0t = 0 gives 256 constants, 0 to 255; each of t=1,2,3t = 1, 2, 3 gives 255, one for each nonzero xx; and the rotation class has 24 rotation amounts (8 to 31) times 128 values of bits 6 to 0, which is 24×128=307224 \times 128 = 3072 encodings. All 256+3×255+3072=4093256 + 3 \times 255 + 3072 = 4093 valid fields give different constants, and 4093+3=4096=2124093 + 3 = 4096 = 2^{12} accounts for every field. The encodable constants are exactly the values whose 1 bits all lie within 8 adjacent bit positions, which is an 8-bit value shifted left by 0 to 24 places, together with the three repeated-byte patterns.

This rule is the one for 32-bit Thumb data-processing instructions as the Armv7-M architecture defines it; the same expansion serves the 32-bit Thumb instructions of later M-profile and A-profile architectures. How the carry output reaches the C flag is a property of each instruction, as listed above, not of the field.

Limits and common mistakes

The 12-bit field is not the constant. Reading the field 0x400 as 1024, when the instruction means 0x80000000, is the classic decoding mistake.

Not every field is valid. Three fields are unpredictable, and Arm permits a processor to treat an unpredictable encoding as an undefined instruction. A decoder or verifier should reject these fields rather than invent a constant for them.

Logical instructions do not all update carry in the same way. Forms without an S suffix change no flags. Flag-setting forms with a constant take C from the rotation in the rotation class and keep it in the byte-pattern class. Flag-setting forms with a register operand take C from the shift applied to that register, the last bit shifted out, and keep it when the shift amount is 0. Arithmetic instructions never take C from the constant.

Not every constant fits. 0x101, for example, has 1 bits at positions 0 and 8, which span nine adjacent bit positions, one more than an 8-bit value covers, and it matches no repeated-byte pattern. Such a constant needs a plain 16-bit MOVW, a MOVW and MOVT pair for a full 32-bit value, or a load from a literal pool, a block of constants stored next to the code. Assemblers also substitute an equivalent instruction when its constant fits: and r0, r1, #0xFFFFFF00 is encoded as bic r0, r1, #0xFF, which clears the same bits.

The carry effect is easy to overlook in hand-written assembly. A test of bit 31 changes C, while a test of bit 7 does not, so code that relies on keeping a carry across a test can break when someone changes the tested constant.

Questions answered

Are all encoded patterns valid, and do all logical operations update carry identically?

No, on both counts. Of the 4096 values of the 12-bit modified immediate field in 32-bit Thumb instructions, three, 0x100, 0x200 and 0x300, are unpredictable, Arm’s term for encodings whose behavior the architecture does not define: they ask for a repeated pattern of the byte 0. A decoder should reject them. Each of the other 4093 fields gives a different 32-bit constant.

Logical operations with a constant operand do not all treat the carry flag C the same way. Forms that do not set flags (no S suffix) change no flags at all. Forms that set flags, such as ANDS, ORRS, EORS, BICS, ORNS, MOVS, MVNS, TST and TEQ, set N and Z from the result and leave V unchanged. They set C to bit 31 of the constant when the constant was formed by rotation (field bits 11 and 10 not both 0), and they leave C unchanged when the constant was formed by placing or repeating a byte (field bits 11 and 10 both 0). So tst r0, #0x80000000 sets C to 1, tst r0, #0x100 sets C to 0, and tst r0, #0x80 leaves C as it was. With a register operand instead of a constant, C comes from the shift applied to that register. Arithmetic instructions use the same constants but take C and V from the addition or subtraction.

Related entries: Condition flags (APSR) (§4.42), Arm Thumb and Thumb-2 (§4.40), Undefined versus unsupported semantics (§3.35), Bit operations: truth tables, masks, shifts, and rotations (§4.50). Further reading: [6, 81].