title: ARM and Android
body: <p>So, I knew nothing about ARM instructions and have only just started trying to understand it. I've looked up a bit on ARM and some of the better links were these:</p>
  
  <p><a href="https://stackoverflow.com/questions/11785973/converting-very-simple-arm-instructions-to-binary-hex">Converting very simple ARM instructions to binary/hex</a></p>
  
  <p><a href="http://simplemachines.it/doc/arm_inst.pdf" rel="nofollow noreferrer">http://simplemachines.it/doc/arm_inst.pdf</a></p>
  
  <p>According to the first link, instructions have the following format and are 32-bits:</p>
  
  <p><code>[cond][00][immediate][opcode][alerts condition codes?][Rn][Rd][Operand 2]</code></p>
  
  <p>However, when I disassembled some .so files, I saw that <s>most</s> some of the instructions were 16 bits and had a different format.</p>
  
  <p>Why the discrepancy? Is there a spec. for this?
  An example would be how it encodes <strong>mov</strong>.</p>
  
  <p>A simple <strong>mov r0 #255</strong> is <strong>20ff</strong>, only 16 bits as opposed to 32. Strange (to me).</p>
  
  <hr>
  
  <p>As I understand it, you can't normally specify an entire 32-bit value in one instruction. I don't know the syntax for assemblers to compile.</p>
  
  <p>What I've been doing is editing existing .so files using a hex editor.. And running them.</p>
  
  <p>I tried to <strong>AND</strong> two values but just couldn't do it, something like:</p>
  
  <pre><code>mov r0 #63 ;0x003f
  mov r1 #16896 ;0x4200
  
  and r0, r0, r1, lsl #8 ;should be 0x423f at this point
  mov r15,  r14 ;method returns at this point, right?
  </code></pre>
  
  <p>Except, I didn't have an assembler and had to do it like this:</p>
  
  <ul>
  <li>Find method that returns int in disassembled .so file</li>
  <li>Over-write entries</li>
  <li>Run and see output</li>
  </ul>
  
  <p>So, this is what I got:</p>
  
  <p><code>mov r0 0x003f</code> - 1110 00 1 1101 0 0000 0000 0000 00111111 - e3a0 003f</p>
  
  <p><code>mov r1 0x0042</code> - 1110 00 1 1101 0 0000 0001 0000 01000010 - e3a0 1042</p>
  
  <p><code>and r0, r0, r1, lsl #8</code> - 1110 00 0 0000 0 0000 0000 00001000 0001 - e000 0081</p>
  
  <p><code>mov r15, r14</code> - 1110 00 0 1101 0 0000 1111 0000 0000 1110 - e1a0f00e</p>
  
  <p>So.. Yeah, I thought about instructions, encoded them using a table, used a calculator to convert it into hex, used a hex editor, transferred to my phone and ran the app..</p>
  
  <p>And I just kept getting a <strong>zero</strong> when the method was called. Am I missing something here?</p>
  
  <p>So..
  Yeah.</p>
  
  <p>Why does android's ARM seem to have 16 bit instructions mixed with 32 bit instructions and why isn't my little attempt working?</p>