What converts Assembly language to machine code

48,521

Solution 1

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.

Solution 2

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

Solution 3

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 !!

Share:
48,521
Karim K.
Author by

Karim K.

Updated on July 21, 2020

Comments

  • Karim K.
    Karim K. almost 4 years

    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.

  • Karim K.
    Karim K. almost 10 years
    But if so... why don't all compilers convert high-level programming language directly to machine code and not assembly? wouldn't it be better? I mean, from what I've read, assembly language makes it easier for the user to read the low-level code but not for the computer to run a program.
  • Martin Rosenau
    Martin Rosenau over 8 years
    You need the "assembler" tool anyway because some kind of code (in the OS kernel) cannot be written in high-level language. Assembler code is easier to generade and easier to debug so writing assembler code makes the compiler development simpler. Some CPU types use different machine code but the same assembler instructions (historical example: 6800/6809; modern examples: mixed-endian CPUs, ARM abi/thumb). In these cases you need only one compiler for two CPUs.
  • Peter Cordes
    Peter Cordes almost 8 years
    clang/LLVM also emits machine-code directly, but the way they talk about it is that it uses a built-in internal assembler. It can still compile code that uses GNU C inline asm syntax. However, compiler-generated instructions might not ever go through a text representation; I'm not sure. clang and MSVC absolutely can output asm for users to read (even with comments); you don't have to just disassemble the object files.
  • Gajanan Kulkarni
    Gajanan Kulkarni over 3 years
    HI @MartinRosenau , I want to understand when is assembly code converted into machine code. Because Kind of Cpu it will run on is only known at run time, So how is this handled. Does it mean, Assembler convert into machine code , when user double click exe file. I am talking in the context of c language. And second Microsoft compilers which converts into directly machine code handle this situation
  • Martin Rosenau
    Martin Rosenau almost 3 years
    @GajananKulkarni Sorry for the late answer. The EXE file already contains machine code, not assembly code. One exception: .NET EXE files (for example created by C#) do not contain machine code but a ".NET assembly". However, ".NET assembly" has nothing to do with assembly language or assembly code (only the word "assembly" is common)...