ARM Immediate Encoder & Decoder
Encode a 32-bit constant into the classic ARM A32 rotated-immediate Operand2 format, or decode a 12-bit immediate field into its resulting 32-bit value. Inspect imm8, rotate_imm, rotation amount, binary fields and alternative valid representations.
—
| # | Operand2 | rotate_imm | Rotation | imm8 | Decoded Constant |
|---|
—
What Is the ARM Immediate Encoder & Decoder?
The ARM Immediate Encoder & Decoder analyzes the rotated immediate constant format used by many classic ARM A32 data-processing instructions. It can test whether a 32-bit integer can be represented directly in this format and, when possible, calculate the required imm8 and rotate_imm fields.
The reverse mode accepts the 12-bit immediate Operand2 field and reconstructs the exact 32-bit constant represented by those bits.
ARM A32 Rotated Immediate Format
For a data-processing instruction with the immediate bit set, the 12-bit Operand2 field contains two components rather than a normal unsigned 12-bit number.
Operand2 bits:
11 8 7 0
+-------------------+---------------------------+
| rotate_imm | imm8 |
+-------------------+---------------------------+
4 bits 8 bitsThe eight-bit value is zero-extended to 32 bits and rotated right by an even number of positions.
ARM Immediate Decode Formula
rotation = 2 × rotate_imm
constant = ROR32(ZeroExtend(imm8), rotation)Because rotate_imm contains four bits, its possible values are 0 through 15. The corresponding rotations are therefore 0, 2, 4, 6 and so on through 30 bits.
Why ARM Uses an 8-Bit Value Plus Rotation
A 32-bit ARM instruction has limited space for operands after allocating bits to the condition code, instruction class, opcode, registers and control flags. The rotated immediate design allows many useful constants to be represented while retaining a compact 12-bit field.
This is especially useful for constants containing a compact group of set bits positioned at different locations within a 32-bit word.
Example: Encode 0x000000FF
The value 255 fits directly in eight bits, so no rotation is required.
Constant = 0x000000FF
imm8 = 0xFF
rotate_imm = 0
rotation = 0 bits
Operand2 = 0x0FF
ROR32(0x000000FF, 0)
= 0x000000FFExample: Encode 0xFF000000
This constant does not fit directly into an eight-bit value at bit positions 7 through 0, but it can be generated by rotating an eight-bit value.
imm8 = 0xFF
rotate_imm = 4
rotation = 8 bits
ROR32(0x000000FF, 8)
= 0xFF000000
Operand2:
0100 11111111
= 0x4FFExample: Decode Operand2 0x4FF
Operand2 = 0x4FF
rotate_imm = 0x4
imm8 = 0xFF
Rotation:
4 × 2 = 8 bits
ROR32(0x000000FF, 8)
Result:
0xFF000000Not Every 32-Bit Constant Is Encodable
Only constants obtainable from an eight-bit value rotated right by an even number of bit positions can use this immediate format. A value such as 0x12345678 cannot be represented as one classic A32 rotated immediate.
Why Can One Constant Have Multiple Encodings?
The ARM rotated immediate representation is not unique for every representable constant. Some values can be generated from multiple imm8 and rotation combinations.
For this reason, the encoder searches all 16 rotation fields and lists every valid representation rather than assuming the first representation is the only possible one.
What Is rotate_imm?
rotate_imm is the four-bit field stored in bits 11 through 8 of the immediate Operand2. It is not the rotation amount itself. The actual number of right rotation positions is twice the field value.
rotate_imm = 0 → ROR #0
rotate_imm = 1 → ROR #2
rotate_imm = 2 → ROR #4
rotate_imm = 3 → ROR #6
...
rotate_imm = 15 → ROR #30What Is imm8?
imm8 is the lower eight bits of the immediate Operand2 field. Before the rotation is applied, this byte is treated as an unsigned value and zero-extended to 32 bits.
imm8 = 0x80
Zero extended:
0x00000080
Then apply:
ROR32(0x00000080, 2 × rotate_imm)ARM Immediate Operand2 Bit Layout
11 8 7 0
rrrr iiiiiiii
rrrr = rotate_imm
iiiiiiii = imm8
12-bit encoded value:
Operand2 = (rotate_imm << 8) | imm8Encoder vs Decoder Mode
Constant → Immediate Encoding
Use encoder mode when you know the 32-bit constant and want to determine whether an A32 data-processing instruction can represent it directly.
Immediate Encoding → Constant
Use decoder mode when examining an instruction's Operand2 bits and you want to calculate the actual 32-bit immediate value.
ARM Immediate Encoding and MOV
The rotated immediate format is commonly encountered while decoding instructions such as MOV, ADD, SUB, CMP, AND, ORR and other A32 data-processing operations when their immediate control bit is set.
MOV r0, #constant
Data-processing immediate Operand2:
rotate_imm : imm8Whether a particular constant can appear directly in such an instruction depends on whether it has a valid rotated immediate representation.
ARM Immediate vs Normal 12-Bit Immediate
A common decoding mistake is treating the entire Operand2 field as a normal 12-bit integer. For classic ARM data-processing immediate instructions, that interpretation is incorrect.
Operand2 = 0x4FF
Incorrect:
constant = 0x4FF
Correct:
rotate_imm = 4
imm8 = 0xFF
ROR32(0xFF, 8)
= 0xFF000000ARM Immediate Encoder & Decoder FAQs
How many bits are in an ARM A32 immediate Operand2?
Is the ARM immediate field a normal 12-bit integer?
What is the ARM immediate rotation formula?
What range can rotate_imm have?
What rotations are possible?
What range can imm8 have?
Can every 32-bit number be encoded?
Can a constant have more than one valid encoding?
What does Operand2 0x4FF represent?
Is 0x12345678 directly encodable?
Does this calculator handle Thumb immediates?
Does it decode AArch64 logical immediates?
Encode or Decode ARM A32 Immediate Constants
Check whether a 32-bit constant fits the classic ARM rotated immediate format, calculate imm8 and rotate_imm, decode Operand2 fields and inspect every valid encoding for the selected value.