9

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

lego69
  • 1,403
  • 3
  • 12
  • 15
  • I'm sure dev-cpp has options (similar to gcc) to produce assembly output. I don't know if dev-cpp has the same hooks for _intermediate_ output, but I don't think you really need those for what you're asking. – Tim Post May 22 '10 at 11:07
  • Also related [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116) for more about making asm output of C compilers interesting to look at. – Peter Cordes Nov 29 '21 at 05:36

6 Answers6

23

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
  • 67,989
  • 17
  • 150
  • 217
  • and the page explains also how to get source code and generated asm :-) +1 – garph0 May 22 '10 at 11:09
  • 1
    @Anders Abel: if I need something special? for example pdp-11? – lego69 May 22 '10 at 11:18
  • 2
    Use a crosscompiler, that produces code for the arch you would like. – Johan May 22 '10 at 11:31
  • can You give me an example, thanks in advance, what exactly does it mean? – lego69 May 22 '10 at 11:34
  • 4
    Why -O2? Surely you'd want no optimisation so you could understand the thing. – paxdiablo May 22 '10 at 11:57
  • @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 May 22 '10 at 12:01
  • 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 May 22 '10 at 13:50
5

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
  • 181,030
  • 38
  • 327
  • 365
3

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 Weiss
  • 52,533
  • 16
  • 102
  • 136
3

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
  • 26,737
  • 4
  • 62
  • 93
1

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
  • 1,700
  • 1
  • 13
  • 16
1

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
  • 756
  • 4
  • 9
  • HAHA! alright!! awesome! i had been searching for just this for about half an hour, and yesterday all evening. here it is finally! No where did i find that you have to rename it to a document format! Damn i was trolled! Thank you very much sir! – bad_keypoints Sep 25 '12 at 05:18