Search code examples
assemblyhexobjdump

gcc assembly to hex


For a lab, I need to know the hex instructions for assembly that I wrote. For example I have bang.s:

movl 0xaaaaaaaa 0xbbbbbbbb
push 0xcccccccc
ret

Now I want to get the hex code for these instructions. How can I use cc and/or objdump to accomplish this?

I’ve tried:

objdump -S bang.s

but I get "File format not recognized"


Solution

  • Here's what I do.

    1. Compile the assembly file.
    2. Disassemble the object file (objdump -d objFile.o > objFile.asm)
    

    In the above example, it does not have to be an object file. It could be an executable file.

    Sometimes, I just need to know the byte sequence for or two instructions. In that case, I use the following.

    1. In my .bashrc file (Linux), add the lines ...
    
    opcode32() {
      echo $* > tmp.S && gcc -c tmp.S -o tmp.o && objdump -d tmp.o
      rm -f tmp.o tmp.S
    }
    
    2. From the command line, use opcode32 'instruction'.  For example ...
           opcode32 'pushl %ecx'
       This yields the following output ...
    
    tmp.o:     file format elf32-i386
    
    
    Disassembly of section .text:
    
    00000000 <.text>:
       0:   51                      push   %ecx
    

    Hope this helps.