I am reverse engineering a piece of assembly. I am trying to write the equivalent code in C, however, when I compile my code, gcc prefers to use simple instructions instead of the complex ones that are used in my assembly. I was wondering if I can suggest or force gcc to use more complex instructions.

EDIT: I have already tried using the -O options but they will change lots of other variables and therefore, don't look like a good solution.

You can find examples here:

// original assembly
lea     rdx, [rax+0x20]

// generated by gcc
add     rax, 0x20
mov     rdx, rax

// C code
0x20+((uint8_t *)lParm1)  // long lParm1
// original assembly
test    eax, eax

// generated by gcc
mov     [rbp-0x10], eax
cmp     [rbp-0x10], 0x0

// C code
if (!iVar)  // int iVar
// original assembly
mov     eax, [rax+0x40]

// generated by gcc
add     rax, 0x40
mov     eax, [rax]

// C code
(float)*(int *)(lParm1 + 0x40)
  • can you please share the c code that generates that set of instructions? – sbluff May 11 at 13:48
  • 3
    Often the original code smaller (is only one command vs. two). Therefore you may try to enable optimization for code size via -Os parameter. – Robert May 11 at 13:59
  • I have already tried that. The thing is that it more or less achieves what I want but then changes lots of other variables. Therefore, doesn't seem like a good option – Bat May 11 at 14:02
  • Please edit your question and include in detail what you already tried and why it is not a suitable solution. – Robert May 11 at 14:04
  • 1
    Actually in order to achieve what you want, you need to have a toolchain (including not just compiler frontend and backend, but also assembler and linker - linker if you use LTO or similar) which can produce the desired code. And then, depending on those (and the specs file - try gcc -dumpspecs) you can use -mtune, -mcpu and -march as well as some more architecture-specific options to optimize for a CPU model and so on. However, your question should be clarified further ... also, you may want to look at RetDec and LLVM IR, perhaps?! – 0xC0000022L May 11 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.