开发者

from C to assembly

can somebody please explain, I have a program on C, can I convert it to assembly? if yes, h开发者_如何学JAVAow?


If you use gcc you can do gcc -O2 -S -c foo.c according to this page to get the output assembly in a human readable form.


With gcc, you can use the -S option:

gcc -O2 -S myfile.c

Most other options (such as the -O2) can be used here as well, to determine what kind of assembly code gets produced.

Note, however, that this is simply exposing an intermediate step that the compiler goes through anyway (not the generation of an assembly source file, that is, but the machine code that it represents). The end result, after passing this code through an assembler won't differ in any way from simply compiling directly to machine code.


Your compiler should have some option to do that. For instance, for gcc you can use the -S option. A short example:

// test.c

#include <stdio.h>

int
main ()
{
  printf ("hello, world\n");
  return 0;
}

Compile it with the -S option. This will produce a test.s file which will contain the assembly:

.file   "test.c"
    .section    .rodata
.LC0:
    .string "hello, world"
    .text
.globl main
    .type   main, @function
main:
    leal    4(%esp), %ecx
    andl    $-16, %esp
    pushl   -4(%ecx)
    pushl   %ebp
    movl    %esp, %ebp
    pushl   %ecx
    subl    $4, %esp
    movl    $.LC0, (%esp)
    call    puts
    movl    $0, %eax
    addl    $4, %esp
    popl    %ecx
    popl    %ebp
    leal    -4(%ecx), %esp
    ret
    .size   main, .-main
    .ident  "GCC: (Ubuntu 4.3.3-5ubuntu4) 4.3.3"
    .section    .note.GNU-stack,"",@progbits


That's what your compiler does.

The compiler compiles your C program to machine language which is a binary representation of the machine program. When a human wants to write machine language, s/he writes it in assembler, which gets translated to the binary machine language.

The assembler code is simply a human readable form of the binary machine language.

The C program:

$ cat test.c 
#include <stdio.h>

int main(int argc, char **argv) {
    int a = 11+12;
    printf("a = %d\n", a);
    return 0;
}

compile it

$ gcc -c test.c

disassemble it:

$ objdump -d test.o

test.o:     file format elf32-i386


Disassembly of section .text:

00000000 <main>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   83 e4 f0                and    $0xfffffff0,%esp
   6:   83 ec 20                sub    $0x20,%esp
   9:   c7 44 24 1c 17 00 00    movl   $0x17,0x1c(%esp)
  10:   00 
  11:   b8 00 00 00 00          mov    $0x0,%eax
  16:   8b 54 24 1c             mov    0x1c(%esp),%edx
  1a:   89 54 24 04             mov    %edx,0x4(%esp)
  1e:   89 04 24                mov    %eax,(%esp)
  21:   e8 fc ff ff ff          call   22 <main+0x22>
  26:   b8 00 00 00 00          mov    $0x0,%eax
  2b:   c9                      leave  
  2c:   c3                      ret    


Most of the compilers have some option to generate assembly listings along with the binary files. In Visual Studio you can find it under "code generation" section in the file properties.
With gcc you can use -S switch (capital S)
Finally if you have a binary you can use objdump -S (capital S).


Since you mentioned Dev-C++ it is worth mentioning that the -S flag also works there. Assuming Windows, Dev-C++ will still name the output .exe, but the result won't be an executable file so just change the extension to .txt or whatever so that it can be read in the editor of your choice. Add options/flags under Project>Project Options>Parameters>Compiler or Tools>Compiler Options.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜