1

What would be the best way (also fewer steps) to generate assembly code for a given hex code ? For e.g., if we know the architecture is x86, and given hex value 0x55, what is the best way to generate the disassembly using gcc, gdb, obdjump, or any other linux command line tools?

5
  • The fastest way I know is to use this site although it’s not command line tool. " It uses GCC and objdump behind the scenes." though.
    – bart1e
    Commented Oct 1, 2019 at 16:53
  • the fastest for me: rasm2 -d -a x86 0x55 Commented Oct 1, 2019 at 18:07
  • Would it need to do much more than echo -ne "\x55" | ndisasm - does?
    – smitelli
    Commented Oct 1, 2019 at 18:07
  • Thank you all. Was hoping to do with just gcc and objdump. One thought was can I have binary data in the c or s file, compile them, and then be do a objdump to get the disassembly. Any thoughts in that line?
    – tecMav
    Commented Oct 2, 2019 at 19:28

3 Answers 3

3

Linux binutils tools, such as objdump, gdb etc. rely on the BFD library, meaning they take well-formed ELF files, not arbitrary byte values or ASCII hex strings, as input. If you want to create your own libopcodes-based disassembler that does this, the following article will help you get started: Basic disassembly with libopcodes.

GCC is a compiler toolchain, which performs preprocessing of the source file, translates source code to assembly, translates that to machine code, and then performs relocation via the link-editor, producing an ELF binary. It sounds like you are interested in going the other way, that is, disassembling object (binary) code. Therefore GCC is not relevant here.

If you want to disassemble individual bytes, you can use the Capstone disassembly framework's Python bindings to write scripts that disassemble the byte values that you specify. Here is an example:

#!/usr/bin/python3

from capstone import *

CODE = b"\x55"

md = md = Cs(CS_ARCH_X86, CS_MODE_32)
for i in md.disasm(CODE, 0x1000):
    print("0x%x:\t%s\t%s" %(i.address, i.mnemonic, i.op_str))

The output is 0x1000: push ebp

When we consult the x86 opcode table, we see that a value of 0x50 + the register = Push Word, Doubleword or Quadword Onto the Stack. So if we change 0x55 to 0x54 the output becomes 0x1000: push esp - a different register is pushed. 0x50 = push eax. And so forth.

Here is a modified version of the above script:

#!/usr/bin/python3

from capstone import *

CODE = b"\x50\x51\x52\x53\x54\x55\x56\x57"

md = md = Cs(CS_ARCH_X86, CS_MODE_32)
for i in md.disasm(CODE, 0x1000):
    print("0x%x:\t%s\t%s" %(i.address, i.mnemonic, i.op_str))

This prints

0x1000: push    eax
0x1001: push    ecx
0x1002: push    edx
0x1003: push    ebx
0x1004: push    esp
0x1005: push    ebp
0x1006: push    esi
0x1007: push    edi
2
  • Thanks. Was hoping to do with just gcc and objdump. One thought was can I have binary data in the c or s file, compile them, and then be do a objdump to get the disassembly. Any thoughts in that line?
    – tecMav
    Commented Oct 2, 2019 at 19:27
  • @tecMav .c and .s files are source files containing ASCII characters. Binary data cannot be compiled or assembled, since the compiler and assembler parses source ASCII text. You may be able to encode the binary data using ASCII characters, store the data in an array, and then compile or assemble the file.
    – julian
    Commented Oct 3, 2019 at 11:29
2

You can do this using objdump:

echo 0000: b0 55 15 de ad f1 55 | xxd -r > x.bin
objdump -D -m i386  -b binary x.bin
0

this is a very late answer as this query popped up into the active queue
this answer is also partly due the comment BY OP after my comment to his query

if one can escape the hex one can compile a data-array
and dump the Disassembly of the objectfile using just objdump

using mingw (look for disassembly of boss is dead fiss below in .rdata section)

$ ls
gccdis.cpp

$ cat gccdis.cpp
const char *input = "\xb0\x55\x15\xde\xad\xf1\x55";
int main () {
   return 0;
}

$ gcc -g -c gccdis.cpp

$ ls
gccdis.cpp  gccdis.o

$ objdump --disassemble-all --section=.rdata -M intel gccdis.o

gccdis.o:     file format pe-i386    
Disassembly of section .rdata:

00000000 <.rdata>:
   0:   b0 55                   mov    al,0x55
   2:   15 de ad f1 55          adc    eax,0x55f1adde
        ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.