1.8. Immediate Values

The immediate value is a literal number baked into the bits of an instruction. For example, here is the machine code for MOV r1, #5:

313029282726252423222120191817161514131211109876543210
11100011101000000001000000000101
E3A01005

The bits for r1 (0001) and 5 (0101) can be seen in the resulting instruction. The #5 is literally part of the instruction.

However, since the immediate is part of the instruction, and the entire instruction needs to be 32 bits, there is a limited space for it. If we look at the ARM instruction reference, is shows that MOV instructions look like this:

313029282726252423222120191817161514131211109876543210
cond 0 0 op op 1 s 0 0 0 0 rd imm12

The last 12 bits are the immediate bits. Normally, 12 bits would allow us to store a value up to 4096. However, it is quite possible that we would like to have a larger value as an immediate, so the value is not stored as a plain 12-bit number. Instead, it is stored as an 8-bit number with a 4-bit shift amount:

11109876543210
rotate value

To calculate the actual value an immediate represents, we:

For example, MOV r1, #41728 looks like this:

313029282726252423222120191817161514131211109876543210
11100011101000000001110010100011
E3A01CA3

CA3 is not 41,728. Instead, those three hex chars should be interpreted as a rotation of 24 (2 * C) done to 10100011 (A3):

11109876543210
1100 1010 0011
C A 3

To decode the immediate, we take the pattern A3:

313029282726252423222120191817161514131211109876543210
00000000000000000000000010100011

And rotate it 24 bits to the right (which is the same as 8 bits to the left), which produces this pattern which equals 41,728:

313029282726252423222120191817161514131211109876543210
00000000000000001010001100000000

This scheme means not all binary patterns can be represented. We can only represent patterns that require 8-bits of significant digits (the first to last 1 must span over no more than 8 bits). And because we double the rotate bits, the final rotation amount must always be an even number of bits.

In practical terms, we can use a wide range of values as immediate, including some large values. But there will be values that can’t be represented and result in an error if we try to use them. To work with values that do not fit as an immediate, we will have to build them up in multiple steps or load them into a register from memory.

@Legal - requires a rotate
MOV   r1, #41728

@Legal - requires rotating FF
MOV   r2, #0xFF0000

@Legal - no rotate required for values < 256
MOV   r2, #0xA0

@Illegal - can't be represented - comment out to fix
MOV   r2, #10000
Try sample

Tip

The error message Error: invalid constant after fixup tells you you have an illegal immediate value.

You have attempted of activities on this page
Next Section - 2. Memory