Search code examples
assemblyarmpatchcmpida

Assembler patching compare istructions and branch


How Can I make "true" this istrution?

CMP             R0, #0

What I want to do is for example make

CMP R0,R0 or CMP #0, #0

00 28 is the op code. I try to do 28 28 without results! Another question what kind of istructions is BNE.W? what this the final W? How can I mod that in a BE? op code is 40 F0 65 85

NOTE: All op codes are in thumb mode!

* EDIT Does exists an armv7 compiler for Mac?


Solution

  • You can simply assemble CMP R0, R0 or CMP #0, #0 and extract the needed opcode from those instructions that way.


    Another way is to look at an ARM reference manual and manually build up the opcode. This is an ARMv5 manual I quickly found and under 7.1.22 you have CMP <Rn>, <Rm> with details of building the opcode for comparing two registers. The details seem to be the same as an ARMv7 manual I also found just before I was about to post.

    It has bits 15-6 being 0 1 0 0 0 0 1 0 1 0, then bits 5-3 are the number of the first register (number 0 represented by 3 bits for R0), and bits 2-0 being the second register (also R0). So your opcode would now be:

    0 1 0 0 0 0 1 0 1 0 (CMP) 000 (R0) 000 (R0)

    100001010000000 in hex is 4280, therefore in little-endian order the two bytes you need are 80 42.

    As you can see, it'd be a lot quicker to just let an assembler work that out for you.