0

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:

Converting very simple ARM instructions to binary/hex

http://simplemachines.it/doc/arm_inst.pdf

According to the first link, instructions have the following format and are 32-bits:

[cond][00][immediate][opcode][alerts condition codes?][Rn][Rd][Operand 2]

However, when I disassembled some .so files, I saw that most some of the instructions were 16 bits and had a different format.

Why the discrepancy? Is there a spec. for this? An example would be how it encodes mov.

A simple mov r0 #255 is 20ff, only 16 bits as opposed to 32. Strange (to me).


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.

What I've been doing is editing existing .so files using a hex editor.. And running them.

I tried to AND two values but just couldn't do it, something like:

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?

Except, I didn't have an assembler and had to do it like this:

  • Find method that returns int in disassembled .so file
  • Over-write entries
  • Run and see output

So, this is what I got:

mov r0 0x003f - 1110 00 1 1101 0 0000 0000 0000 00111111 - e3a0 003f

mov r1 0x0042 - 1110 00 1 1101 0 0000 0001 0000 01000010 - e3a0 1042

and r0, r0, r1, lsl #8 - 1110 00 0 0000 0 0000 0000 00001000 0001 - e000 0081

mov r15, r14 - 1110 00 0 1101 0 0000 1111 0000 0000 1110 - e1a0f00e

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..

And I just kept getting a zero when the method was called. Am I missing something here?

So.. Yeah.

Why does android's ARM seem to have 16 bit instructions mixed with 32 bit instructions and why isn't my little attempt working?

Community
  • 1
  • 1
Justin AnyhowStep
  • 1,130
  • 3
  • 12
  • 19

1 Answers1

0

Do realize that when you say 'ARM instructions' that isn't just one instruction set but a multitude depending on the architecture and what extensions are supported. The document you linked to starts off by mentioning architecture v4 which if you check your favorite wiki for 'ARM architecture' - it would point out that v4 is a 'legacy' architecture.

Android itself started support on ARMv6 with most modern devices running ARMv7 or ARMv7a. While I don't know for sure I would think that the 16 bit instructions are Thumb2 and not the original Thumb extension which was intended to improve code density as ARM was designing for the embedded market of 10 to 20 years ago where a megabyte would be a lot of memory.

If you are learning, I would reference ARM's documentation available at their website: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0406b/index.html

and possibly look at the ARM instructions coming out of gcc:

http://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html

and get a cross-compiler for ARM setup so that you can take a higher level language like C and generate ARM binaries.

Morrison Chang
  • 11,691
  • 3
  • 41
  • 77
  • Thanks for the help =) I got it working by using Thumb2. I'm guessing you can't mix ARM and Thumb in the same 'branch' or whatever it is called. – Justin AnyhowStep Apr 13 '14 at 03:18