Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm currently learning ASM programming, and I would like to disassembly a file .c. However after execute the command line

set disassembly-flavor test.c

I use the command:

disassembly test.c

and my terminal displayed

disassemble: command not found

Does anyone know can I get the command "disassemble" on my computer ?

Thanks

share|improve this question

First, a file with a .c extension most likely is a C source file. No use in disassembling it. To verify that a file actually is a program, use the file command:

$ file test.c
test.c: ASCII text
$ file /bin/bash
/bin/bash: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), dynamically linked
(uses shared libs), for GNU/Linux 2.6.24,
BuildID[sha1]=54967822da027467f21e65a1eac7576dec7dd821, stripped

As for disassembling, use objdump instead. From the manpage:

NAME
       objdump - display information from object files.

SYNOPSIS
       objdump [...]
               [-d|--disassemble]

So to disassemble, let's say, a file called a.out, use

$ objdump -d a.out 

a.out:     file format elf64-x86-64


Disassembly of section .init:

00000000004003a8 <_init>:
  4003a8:   48 83 ec 08             sub    $0x8,%rsp
  4003ac:   48 8b 05 45 0c 20 00    mov    0x200c45(%rip),%rax        # 600ff8 <_DYNAMIC+0x1d0>
  4003b3:   48 85 c0                test   %rax,%rax
  4003b6:   74 05                   je     4003bd <_init+0x15>
  4003b8:   e8 33 00 00 00          callq  4003f0 <__gmon_start__@plt>
  4003bd:   48 83 c4 08             add    $0x8,%rsp
  4003c1:   c3                      retq   
[...] and so on...
share|improve this answer

I think what you want is a function of gdb, the gnu debugger, which has a disassemble command. First, you would compile test.c with debugging flags on, like this: gcc -Wall -g -o test test.c

or something along those lines. Then, you would run the application with gdb as an argument:

gdb test

once you are in gdb, you can use the disassemble command like this:

(gdb) break main
(gdb) disassemble main

Take this trivial program named test.cpp:

#include <iostream>
using namespace std;
int main ()
{
    int a=0;
    for (int b = 0; b < 11; b++){
        a=b+2;
        cout << a << "\n\t";
    }
cout << "Hello World \n";     
return 0;
}

now I will compile it, with debug flags:

g++ -Wall -g test.cpp -o test

Then you say, but j0h, this is a C question... to which I say, gdb doesn't care what language you use. you can debug and disassemble c, c++, assembly, fortran, and a variety of other things, just compile with the -g option, thats what is important.

once we load the program into gdb, and break main, we cam run disassemble:

Breakpoint 1 at 0x4007da
(gdb) disassemble main
Dump of assembler code for function main:
   0x00000000004007d6 <+0>: push   %rbp
   0x00000000004007d7 <+1>: mov    %rsp,%rbp
   0x00000000004007da <+4>: sub    $0x10,%rsp
   0x00000000004007de <+8>: movl   $0x0,-0x4(%rbp)
   0x00000000004007e5 <+15>:    movl   $0x0,-0x8(%rbp)
   0x00000000004007ec <+22>:    jmp    0x400817 <main+65>
   0x00000000004007ee <+24>:    mov    -0x8(%rbp),%eax
   0x00000000004007f1 <+27>:    add    $0x2,%eax
   0x00000000004007f4 <+30>:    mov    %eax,-0x4(%rbp)
   0x00000000004007f7 <+33>:    mov    -0x4(%rbp),%eax
   0x00000000004007fa <+36>:    mov    %eax,%esi
   0x00000000004007fc <+38>:    mov    $0x601080,%edi
   0x0000000000400801 <+43>:    callq  0x400670 <_ZNSolsEi@plt>
   0x0000000000400806 <+48>:    mov    $0x400914,%esi
   0x000000000040080b <+53>:    mov    %rax,%rdi
---Type <return> to continue, or q <return> to quit---

Here is a tutorial: http://www.unknownroad.com/rtfm/gdbtut/gdbadvanced.html

objdump can list the contents, but I don’t think you’ll be able to interact with your code the same way as if you use the debugger.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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