# Golf this Thumb-2 Constant!

One thing that is constantly frustrating when golfing ARM Thumb-2 code is generating constants.

Since Thumb only has 16-bit and 32-bit instructions, it is impossible to encode every immediate 32-bit value into a single instruction.

Additionally, since it is a variable length instruction set, some methods of generating a constant are smaller than others.

I hate that. Write a program or function that does this work for me so I don't have to think about it. (*shudder*)

There are four different variants of the mov instruction as well as a pseudo instruction which loads from a constant pool.

They are either 2 bytes, 4 bytes, or 6 bytes long.

These are the following:

1. movs: 2 bytes. Generates any 8-bit unsigned value from 0x00 to 0xFF in the low 8 bits, setting the other bits to 0.
2. mov: 4 bytes. Generates any 8-bit unsigned value bitwise rotated any number of bits, filling the rest with 0 bits.
3. mvn: 4 bytes. The same as mov, but generates the one's complement of that rotated 8-bit value, filling the rest with 1 bits.
4. movw: 4 bytes. Can set the low 16 bits to any 16-bit unsigned value from 0x0000-0xFFFF, and the high 16 bits to 0.
5. ldr: 6 bytes. The last resort. Can generate any 32-bit value, but is slow and larger than the other options.

†: Note that this is dramatically oversimplified, the real encoding would make this challenge quite frustrating.

The input will be a 32-bit integer, either by string (base 10 signed/unsigned or hex), stdin, or value.

Every value from 0x00000000 to 0xFFFFFFFF must be handled. It is a critical part of the challenge. Watch out for signed shift issues.

However, you can safely assume all inputs will fit into 32 bits, since ARM is a 32-bit architecture.

The output will be the shortest instruction to encode this constant.

The output format is whatever is easiest, as long as the values are distinct and you indicate which instruction corresponds to each output.

There will be a few cases where multiple answers are correct. You may either output one of the correct answers, or all of them. However, you must only display the shortest answers. So, for example, you must only output ldr when none of the other instructions can generate it.

Test cases (values are in hex to show the bit patterns)

0x00000000 -> movs
0x00000013 -> movs
0x000000ff -> movs
0x12345678 -> ldr
0x02300000 -> mov
0xffffffff -> mvn
0x00ffffff -> mvn
0x00001200 -> mov or movw
0x11800000 -> mov
0x80000000 -> mov
0x00000100 -> mov or movw
0x0000f00d -> movw
0xfff3dfff -> mvn
0x03dc0000 -> mov
0xf000000f -> mov
0x00003dc0 -> mov or movw
0x0021f300 -> ldr
0x00100010 -> ldr
0xffff0000 -> ldr


Standard loopholes apply.

Note that as and objdump will not help; they will use the real encoding which is wrong. 😏

Being , the smallest answer in bytes per language wins.

• (remark: banning built-ins is a type of "do X without Y" which is not encouraged. Boring answers are usually down voted anyway. – user202729 Feb 8 at 14:48
• @user202729 In most languages I know, as and objdump are not built-ins. – the default. Feb 8 at 14:58
• Just as a potentially interesting fact, the "correct" values representable by mov is either x * y for x a 8-bit value and y in (0x00000001, 0x00010001, 0x01000100, 0x01010101); or x << y for x in [0x80..=0xFF] and y in [1..=24] (which covers most useful integer literals, as numbers like 0xF000000F are rarely used) – user202729 Feb 9 at 2:26
• You are correct. I am using a modified version of the original ARM encoding, only I enable rotating by any number of bits instead of only multiples of 2. If there is interest, I might make a part 2 which does the actual encoding. – EasyasPi Feb 9 at 2:36

# JavaScript (ES6), 57 bytes

Returns false for "mov", true for "movs", 2 for "ldr", 3 for "mvn" or 4 for "movw".

f=(n,r)=>n>>8?~n>>8?r>31?n>>16?2:4:f(n>>>31|n*2,-~r):3:!r


Try it online!

### Commented

f = (                       // f is a recursive function taking:
n,                        //   n = input
r                         //   r = rotation counter, initially undefined
) =>                        //
n >> 8 ?                  // if some of the upper 24 bits of n are not 0's:
~n >> 8 ?               //   if some of the upper 24 bits of n are not 1's:
r > 31 ?              //     if the input was rotated 32 times (which means
//     that we've tried all possible rotations and we
//     are back to the original value):
n >> 16 ?           //       if some of the upper 16 bits of n are not 0's:
2                 //         return 2 for "ldr"
:                   //       else:
4                 //         return 4 for "movw"
:                     //     else:
f(                  //       do a recursive call:
n >>> 31 | n * 2, //         rotate n by 1 position to the left
-~r               //         increment r
)                   //       end of recursive call
:                       //   else:
3                     //     return 3 for "mvn"
:                         // else:
!r                      //   return true for "movs" if r is zero'ish
//   or false for "mov" if r is greater than 0


# Jelly, 29 bytes

+Ø%BḊ¹¬ƭṙJ$Ḅ<⁹Ẹ ⁹²>;Ç;Ç;<⁹$TṀ


Try it online!

Returns 0, 1, 2, 3, 4 respectively for ldr, movw, mov, mvn and movs. Probably could be improved.

# C (gcc), 105 $$\\cdots\$$73 72 bytes

Saved 15 bytes thanks to EasyasPi!!!
Saved 3 4 bytes thanks to ceilingcat!!!

i;z;f(n){for(i=0;z=n>>8,i++<32&&z&&~z;)n=n*2|n<0;i=z?~z?z>>8?4:3:2:i>1;}


Try it online!

Returns $$\0,1,2,3,4\$$ for movs, mov, mvn, movw, ldr.

### Explanation (before some golfs)

i;f(n){                         // function taking a 32 bit parameter n
for(i=0;i++<32           // loop for maximum of 32 times
&&n>>8           // while there's set bits in the 24 msb
&&~n>>8;)        // and in the 24 msb of n's 1s compliment
n=n*2|n<0;           // rotate n 1 bit to the left
i=                       // return:
n>>8?~n>>8?            // if these are still truth values then we
// must have rotated n 32 times
n>>16?      // does the original n have set bits in the
// 16 msb?
4:    // ldr if it does
3:    // movw if it doesn't.
// the ones below are because we stopped
// looping when we found no/all 1 bits in
// the 24 msb
2:    // mvn if all 24 msb are set in the
// current of a rotation of n
i>1;  // mov if there's no set bits in the 24 msb
// of the current rotation of n
// movs if there's no set bit in the 24 msb
}                               // of n at the start.

• 90 bytes: Ironically, signed arithmetic is shorter – EasyasPi Feb 8 at 16:25
• @EasyasPi Nice one - thanks! :D – Noodle9 Feb 8 at 16:42
• In the commented code, 8&&~n>>8; is supposed to be &&~n>>8;, right? – EasyasPi Feb 8 at 22:59
• @EastasPi Of course, copy-and-paste oops - thanks! :) – Noodle9 Feb 8 at 23:53
• @ceilingcat Nice one - thanks! :D – Noodle9 Feb 10 at 22:39

# Charcoal, 56 bytes

≔⍘×⊕Ｘ²¦³²Ｎ²θ¿‹Ｌθ⁴¹movs¿‹Ｌθ⁴⁹movw¿№θ×0²⁴mov¿№θ×1²⁴mvn¦ldr


Try it online! Link is to verbose version of code. Explanation:

≔⍘×⊕Ｘ²¦³²Ｎ²θ


Input the integer, multiply it by 2³²+1, and convert to binary.

¿‹Ｌθ⁴¹movs


If the length is less than 41, then the original number had 8 bits or fewer, so a movs instruction will work.

¿‹Ｌθ⁴⁹movw


Otherwise if the length is less than 49, then the original number had 16 bits or fewer, so a movw instruction will work.

¿№θ×0²⁴mov


Otherwise if the binary contains a string of 24 0s, then they can be rotated to the beginning of the original number, so a mov instruction will work.

¿№θ×1²⁴mvn


Otherwise if the binary contains a string of 24 1s, then they can be rotated to the beginning of the original number, so a mvn instruction will work.

¦ldr


Otherwise give up and use ldr.

This challenge reminds me of the difficulty of loading immediate values using original ARM instructions, so I wrote a 32-byte program that identifies values that can be loaded into a register via mov or mvn:

≔⍘×⊕Ｘ²¦³²Ｎ⁴θ¿№θ×0¹²mov¿№θ×3¹²mvn


Try it online! Link is to verbose version of code. Outputs mov if the value can be used directly as an immediate operand, or mvn if you can at least load it into a register.

• Note: You don't have to output the mnemonics. Returning a number or w/e is perfectly fine. Life's too short to waste chars on printing a string 😏 – EasyasPi Feb 8 at 23:58