Search code examples
assemblyatt

How to view machine code?


I've written a program in assembly (at&t syntax) and I want to see how the machine code looks. This is how I get executable code:

as -g -o p1.o p1.s --32 -gstabs
ld -o p1 p1.o -m elf_i386

Solution

  • Assuming that you are on a Linux or BSD platform (based on the fact that you're using as), you might want to try objdump.

    objdump -d <binary file> will disassemble the object file, showing you machine code hex bytes on the left and the disassembled matching assembly mnemonics on the right. Here's an example:

    $ objdump -d factorial
    
    factorial:     file format elf64-x86-64
    
    Disassembly of section .init:
    
    00000000004003f0 :
      4003f0:   48 83 ec 08             sub    $0x8,%rsp
      4003f4:   e8 73 00 00 00          callq  40046c
      ..
    Disassembly of section .plt:
    
    0000000000400408 :
      400408:   ff 35 e2 0b 20 00       pushq  0x200be2(%rip)        # 600ff0
      40040e:   ff 25 e4 0b 20 00       jmpq   *0x200be4(%rip)        # 600ff8
      400414:   0f 1f 40 00             nopl   0x0(%rax)
    

    objdump is a part of the binutils package on Linux platforms.