title: Can't replace MOVW / MOVT with LDR
body: <p>I want to replace this <code>movw / movt</code> combination with a single <code>ldr</code> instruction:</p>
  <pre><code>movw r0, #0x3800
  movt r0, #0x4002
  
        |
        V
  
  ldr r0, =0x40023800
  </code></pre>
  <p>However, after this change the code no longer works. Since this is the only part I changed, it must be at fault. I was under the impression that those two code segments are identical.</p>
  <p>The assembly code has the following instructions at the top</p>
  <pre><code>.syntax unified
  .cpu cortex-m4
  .thumb
  </code></pre>
  <p>and is compiled using the <a href="https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm" rel="nofollow noreferrer">GNU Arm Embedded Toolchain</a> with the following commands:</p>
  <pre><code>arm-none-eabi-as -mcpu=cortex-m4 code.s -c -o code.o
  arm-none-eabi-gcc -T link.ld -nostartfiles -o result code.o
  </code></pre>
  <p>In case it matters, the remainder of the code is identical to the one hosted at <a href="https://mcla.ug/blog/how-to-flash-an-led.html" rel="nofollow noreferrer">this page</a> (at the bottom). The code is running on an STM32F411CEU6 with an Arm Cortex-M4 core.</p>
  <p>Why does this change break my code? Is there an alternative one-line method for writing a 32bit immediate value to a register?</p>
  <hr />
  <p>Running an <code>objdump</code> on the assembler result shows this (note the beginning of <code>&lt;main&gt;</code> and the end of <code>&lt;loop&gt;</code>:</p>
  <pre><code>Disassembly of section .text:
  
  00000000 &lt;main&gt;:
     0:   4810        ldr r0, [pc, #64]   ; (44 &lt;.loop+0x2a&gt;)
     2:   f240 0101   movw    r1, #1
                   [removed]
    18:   6001        str r1, [r0, #0]
  
  0000001a &lt;.loop&gt;:
    1a:   f240 0100   movw    r1, #0
                   [removed]
    44:   40023800    andmi   r3, r2, r0, lsl #16
  </code></pre>