7

My understanding is that a compiler converts the high level language into machine code. I have a question as to whether a compiler(say VC++) in-turn uses an assembler too? I remember seeing assembly code, whenever there is a crash or something like that.

2
  • 1
    Assembler is just a one-to-one mapping of machine instructions (bits of code) to some mnemonic codes (like mov ax, bx). That's why you can translate assembly codes to machine codes and back without any big effort. Compiler doing much more complicated work and there is no one-to-one relation between source code and produced code (no matter is it direct machine code or assembly instructions). That's why you usually can't see source code from binary code.
    – ony
    Jun 20, 2010 at 18:28
  • ony: that goes for a non symbolic, non macro backend assembler. But even GAS output is symbolic. Jul 2, 2010 at 4:22

5 Answers 5

5

It depends on the compiler; many compilers can compile to assembly. For instance, if you pass the '-S' flag to gcc, like:

gcc -S -o test.S test.c

That will output assembly for your test.c file into the file test.S which you can look at. (I recommend using -O0 if you're gonna be trying to read the assembly, because compiler optimizations in there will likely confuse the heck out of you).

Since you mentioned Visual C++ in your question, Paul Dixon points out below that Visual C++ uses the /FA flag to accomplish the same thing.

1
  • 1
    Well, actually, mainly GCC does that :-) Most commercial ones have it buildin to avoid the slow generating/reparsing text step, with only an option to generate assembler if needed. Jul 2, 2010 at 4:21
5

In general, a compiler translates one language into another. Most compilers target machine code; here are the most common ways of doing this, in order from lowest-level to highest-level:

  1. Produce machine code directly, by including back-ends for each instruction set.
  2. Produce assembly language, which is passed to an external assembler. This is what gcc does. After all, why not use existing assemblers? Generating opcodes directly is a pain.
  3. Generate a low-level intermediate form of the code, such as LLVM bytecode, which gets transformed into machine code. LLVM is a godsend for compiler-writers, because it handles a vast number of optimizations and supports a lot of back-ends, while letting you write at a higher level than assembly.
  4. Transform the code into a language like C, and then pass it to another compiler. Facebook does this to PHP: they compile it to C++, and then use gcc to compile that to assembly, which is assembled to native code by GNU as.

If you'd like to try writing a compiler, I'd recommend starting off with something like C as the back-end. If you'd like to get more elaborate later, try LLVM. If you try to produce assembly language or machine code directly, it'll probably be less efficient than what a C compiler or LLVM would produce. I speak from embarrassing personal experience on this one.

1

Most compilers will have an option to view an assembly listing

  • VC can be told to output an assembler listing with /FA
  • with gcc, use the -S switch
0

A compiler translates some high level programming language to some lower level language (as machine code, assembly or even another high level language, for example from Vala to C, or from uC++ to C++).

I don't know about VC++, but most compilers, as GNU GCC doensn't include an Assembler, but invoke an external Assembler program (as NASM, or GNU AS).

Cheers

0

A compiler goes directly to machine code.

The debugger will show the machine code instructions as assembler, but the debugger can also show other code which wasn't compiled by the compiler.

The compiler may also include an assembler so you can include inline assembler code in with the higher level language or generate assembler output.

2
  • 1
    not correct.... I could program a compiler that outputs only assembly source, to be assembled apart with an assembler... moreover, compiler hardly goes so directly to machine code... internal representation (different from target machine cod) of the code (!) could be very useful to do optimization and so on... ok, everything likely is hidden, but "directly" is too much anyway! :D Jun 25, 2010 at 16:15
  • You could but to answer the OP's question Visual Studio doesn't output assembler and then call MASM to generate the exe Jun 25, 2010 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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