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 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 .
Expansion. The Armv7-M manual defines the rule as the pseudocode function ThumbExpandImm_C, which returns the constant and a carry output.
If bits 11 and 10 are both 0, bits 9 and 8 choose a byte pattern. Below,
XYstands for the two hexadecimal digits of .00: the constant is0x000000XY, the byte 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,10and11with 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.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 moves every bit 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 is at least 8, so every one of its bits wraps around: rotating right by gives the same result as shifting left by , a shift of between 1 and 24 places.
Which instructions use the carry output.
Logical instructions with a constant operand (
AND,BIC,ORR,ORN,EOR,MOV,MVN,TST,TEQ) use the expanded constant. When they set flags, which they do when written with an S suffix andTSTandTEQalways do, they set N and Z from the result, leave V unchanged, and set C to the carry output. In the rotation class C therefore becomes bit 31 of the constant; in the byte-pattern class C keeps its old value. Without an S suffix they change no flags.Arithmetic instructions with a constant operand (
ADD,ADC,SUB,SBC,RSB,CMP,CMN) use the same expansion for the value only. Their C and V come from the addition or subtraction.
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.
Read as little-endian §4.21 halfwords, the first halfword is
0xF001and the second is0x4000.0xF001is binary1111 0000 0000 0001, soi, its bit 10, is 0.0x4000is binary0100 0000 0000 0000, soimm3, its bits 14 to 12, is100, andimm8, its bits 7 to 0, is0x00.The field is
0 100 00000000, which is0x400, decimal 1024.Bits 11 and 10 are 0 and 1, not both 0, so this is the rotation class.
The value to rotate is 1 followed by bits 6 to 0, which are
0000000: binary10000000, or 128. The rotation amount is bits 11 to 7,01000, or 8.Rotating 128 right by 8 equals shifting it left by : , 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.
tst r0, #0x80: the field is0x080, byte-pattern class, so C is unchanged.tst r0, #0x100:0x100is 128 shifted left by 1, which is 128 rotated right by 31, so the field is binary11111 0000000, or0xF80. This is the rotation class, and C becomes bit 31 of0x100, which is 0.tst r0, #0x80000000: the rotation class again, and C becomes 1.
Stated precisely
Let be the 12-bit field, its bits 7 to 0, its bits 11 to 8, and the incoming carry flag, where means rounded down to a whole number. For a 32-bit value and , rotation right is the two parts occupy different bits, so adding them is the same as combining them with a bitwise OR. The constant and the carry output are The three fields with and , namely 0x100, 0x200 and 0x300, are unpredictable.
Counting the cases: gives 256 constants, 0 to 255; each of gives 255, one for each nonzero ; and the rotation class has 24 rotation amounts (8 to 31) times 128 values of bits 6 to 0, which is encodings. All valid fields give different constants, and 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].