title: Using BX in Thumb code to call a Thumb function, or to jump to a Thumb instruction in another function
body: <p>I'm trying to learn skills useful in firmware modding (for which i don't have source code)
  These questions concern use of BX from thumb code to jump or call other existing thumb code.</p>
  
  <ol>
  <li>How do i use BX to JUMP to existing firmware THUMB code, from my THUMB code.</li>
  <li>How do i use BX to CALL an existing THUMB function (must set LR first), from my THUMB code.</li>
  </ol>
  
  <p>My understanding is that cpu looks at lsb bit (bit 0) and i have to make sure this is set to <code>1</code> in order to keep cpu state to "thumb state". 
  So I guess i have to ADD 1, to set lsb bit to 1.</p>
  
  <p>So ...say i want to just JUMP to 0x24000  ( in the middle of some existing THUMB code)</p>
  
  <pre><code>LDR R6, =0x24000
  ADD R6, #1       @ (set lsb to 1)
  BX R6
  </code></pre>
  
  <p>I think this is correct ?</p>
  
  <p>Now say i want to CALL an existing thumb function, using BX, and i want it to return to me, so i need to set LR to where i want it to return.</p>
  
  <p>Lets say the function i want to call is at 0x24000
  It was <a href="https://stackoverflow.com/questions/9348709/arm-thumb-code-for-firmware-patches-how-to-tell-gcc-assembler-linker-to-bl-t">suggested to me</a> to use:</p>
  
  <pre><code>ldr r2, =0x24000
  mov lr, pc
  bx r2
  </code></pre>
  
  <p>Here comes what i don't understand:</p>
  
  <ol>
  <li><p>the address in R2 doesn't have lsb bit set... so won't <code>bx r2</code> switch mode to ARM mode??</p></li>
  <li><p>The LR..
  The PC has the address of (begining of current instruction, + 4), i was told.
  In both Thumb and Arm, any instruction address has to be aligned (16 bit or 32 bit), so it won't have the LSB bit set to 1.  Only odd numbers have lsb bit set to 1.</p></li>
  </ol>
  
  <p>So in the code above, i'm setting LR to (PC), an address that DOESN'T have lsb bit 1 set either. So when the function i called comes to it's epilogue, and does <code>BX LR</code>, ... uhmmm.. how can that work to return to my THUMB code ?  I must be missing something...</p>
  
  <p>Normally BL is used to call functions. The manual says BL instruction sets the LR to the next line of code... 
  So does this mean that a (normally used) <code>BL</code> THUMB instruction,  sets the LR to <code>return addr + 1</code> automatically? </p>