tags:

views:

298

answers:

7
+5  Q: 

from C to assembly

can somebody please explain, I have a program on C, can I convert it to assembly? if yes, how?

+2  A: 

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    
Johannes Weiß
for example, I'm using dev-cpp, where can I find this option?
lego69
look in the project's compiler/code generator options, search for something like "generate assembly listings"
garph0
+15  A: 

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.

Anders Abel
and the page explains also how to get source code and generated asm :-) +1
garph0
@Anders Abel: if I need something special? for example pdp-11?
lego69
Use a crosscompiler, that produces code for the arch you would like.
Johan
can You give me an example, thanks in advance, what exactly does it mean?
lego69
Why -O2? Surely you'd want no optimisation so you could understand the thing.
paxdiablo
@paxdiablo I didn't really pay attention that switch, I just copied the sample from the referenced site, but you're definitely right - disabling optimisation will produce assembly which is easier to understand.
Anders Abel
gcc supports pdp-11, so you should be able to use it.the --target option should be useful here.the usual target syntax is processor-format so, supposing you are using an elf format for pdp-11 (which is not probable, but I know nothing of pdp-11 executables formats) you should add the option--target pdp11-elf
garph0
+4  A: 

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.

Marcelo Cantos
+2  A: 

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
Vijay Mathew
@Vijay Mathew: if I need something special? for example pdp-11?
lego69
+1  A: 

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).

garph0
A: 

See this question. This answer might be helpful when you intend to read the assembly. Also, you can compile a program, and then use disasm to get assembly output.

Yktula
A: 

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.

Evan Huddleston