8
votes

I am new to programming and I started with C++ language, as far as I know C++ language is converted to assembly language by the C++ compiler (Ex:Visual Studio), but I tried looking up for what converts the assembly language into machine code to be understood and executed by the computer but I couldn't find an answer.

So the question is, where and how is assembly language converted to machine code? is it by some sort of compiler integrated in the OS?

Thanks in advance.

4
No, that only happened in the olden days. A typical bootstrapping step, but nobody wants to wait for the extra required assembler pass. All that remains is that C and C++ compilers still have the option to generate an assembly listing.Hans Passant
The Microsoft C++ compiler generates machine code directly, while the GNU C compiler generates assembler and then uses an assembler to turn it in to machine code.Ross Ridge
The Microsoft compilers have the ability to output assembly code. I don't know if there is an option to compile to assembly code, then assemble the assembly code. The Microsoft assembler is ML.EXE for 32 bit and ML64.EXE for 64 bit. There's also MASM.EXE, which in some cases refers to the MASM 5.x versions, while ML.EXE was/is the new name for MASM 6.x or later versions.rcgldr

4 Answers

11
votes

Some compilers (like GNU) convert the C/C++ code into assembly code. A tool called "assembler" converts the assembly code into machine code and a tool called "linker" connects multiple machine-code files into one single executable (.EXE under Windows) file. Most of these compilers allow you to write the resulting assembler code into a file so you can look at the assembler code or modify it.

The assembler and the linker are part of the tool chain which means that they are typically delivered together with the compiler.

Some compilers (like Microsoft) however directly convert C/C++ code into machine language so no assembler is needed any more. Many of these compilers are not able to create assembler code so you cannot write the assembler code into a file.

By the way: There are even compilers (not for C/C++, but for other programming languages) that directly create an .EXE file so no linker is required.

6
votes

When compiler converts high level code into assembly language which looks something like below -

  • MOV AX, [A] ; # Move A to Register Ax
  • ADD AX, [B] ; # Add B to A
  • IMUL AX ; # Square(A+B)
  • MOV [C], AX ;

An assembler converts these assembly instructions to machine code.

Assembly instruction has typical form like : opcode operand [operand]

And if you check microprocessor manual you will get to know how each instruction can be converted to a binary form like 1001000. Some bits are for opcode and some are for operands.

http://www.mathemainzel.info/files/x86asmref.html

2
votes

Assembly language is converted into executable machine code by a utility program referred to as an assembler; the conversion process is referred to as assembly, or assembling the code.

Read more here

Cheers !!

0
votes

Assembly code has a similar process for conversion to object/machine code, as it is run through something called an assembler (analogous to a compiler in your given example).