0

In case I run the code below on Linux:

rasm2 -a arm -b 16 -d "28 46 0d f2 74 4d f0 bd"

Result is:

mov r0, r5
addw sp, sp, 0x474
pop {r4, r5, r6, r7, pc}

But now the question is, if I try to re-assemble the disassembly back into hexadecimal machine code again, it doesn't work:

rasm2 -a arm -b 16 "mov r0, r5;addw sp, sp, 0x474;pop {r4, r5, r6, r7, pc};"
Cannot assemble 'addw sp, sp, 0x474' at line 8
invalid

This is what happened.

So how can I assemble my assembly code to a hexadecimal in an accurate way, using rasm2 or else, and what is the reason of that error?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
rabbit
  • 43
  • 1
  • 4
  • Do you need an argument to tell it what encoding to ouput, eg: arm, thumb, thumb2 etc? That is a valid instruction in ARMv7M at least. – Tom V Nov 12 '22 at 08:43
  • @TomV My target machine is Cortex m4(which is ARMv7M), should be alright according to your comment... – rabbit Nov 12 '22 at 09:45
  • Don't tell me, use the `-a arm -b 16 -c cortex` arguments to tell the assembler! – Tom V Nov 12 '22 at 10:34
  • Tried, but still doesn't help :( – rabbit Nov 12 '22 at 14:31
  • It assembles just fine with GAS using `.cpu cortex-m4` and `.syntax unified`. Maybe report a bug in rasm2. – Nate Eldredge Nov 12 '22 at 23:03
  • did you try: addw sp, sp, #0x474 not sure why you would assume that the disassembly can be re-assembled, that is not a common expectation, nor the goal of a disassembler...perhaps in this case it is an advertised feature. – old_timer Nov 13 '22 at 18:41
  • @old_timer Actually, my attempt is to re-assemble modified code(just erased last branch command). I need new hex with no branch so I'm generating modified code which does not contain those. – rabbit Nov 13 '22 at 21:13
  • Typically, the syntax is `add.w sp,sp,#0x474`. Try changing to use 'add.w'. It would be a bug in the way it output 'wide' forms of the instruction (if the intent is bi-directional; but this is probably a desirable property). It is four bytes. 0d f2 74 4d. The same can be done with `printf` and `objdump`. – artless noise Nov 14 '22 at 11:59

0 Answers0