Compiler and Assembler plays an important role in the execution of a program. Some compilers directly generate the executable code instead of assembly code. The compiler takes the preprocessed source code and translates it into the assembly code. The assembler takes the assembly code from the compiler and translates it to the relocatable machine code. In this article, I have discussed the differences between compiler and assembler with the help of comparison chart shown below, just have a look.
Content: Compiler Vs Assembler
|Basis for Comparison||Compiler||Assembler|
|Basic||Generates the assembly language code or directly the executable code.||Generates the relocatable machine code.|
|Input||Preprocessed source code.||Assembly language code.|
|Phases/ Passes||The compilation phases are lexical analyzer, syntax analyzer, semantic analyzer, intermediate code generation, code optimization, code generation.||Assembler makes two passes over the given input.|
|Output||The assembly code generated by the compiler is a mnemonic version of machine code.||The relocatable machine code generated by an assembler is represented by binary code.|
Definition of Compiler
The compiler is a computer program that reads the program written in a source language, translates it into equivalent assembly language and forwards the assembly language code to the Assembler. While the translation of the source code to assembly code the compiler also reports the error in the source code to its user.
Compilers are also classified as single-pass, multi-pass, load-and-go, debugging and optimization. Classification is done on the basis of what function does a compiler perform and how it has been constructed. Despite these complexities, the basic task of compiler remains same.
The compilation is performed in two parts, analysis part and synthesis part. The analysis part breaks the source code into constituent pieces and forms the intermediate representation of the source code. The synthesis part forms the target code from the intermediate representation.
The compilation is performed in the following phases:
Lexical analyzer, syntax analyzer, semantic analyzer, intermediate code generator, code optimizer, code generator, Symbol table and error Handler.
- The lexical analyzer reads the characters of source code and groups them into the streams of tokens. Each token represents the logical sequence of characters like keyword, identifiers, operators. The sequence of character that forms a token is called lexeme.
- The syntax analyzer parses the token obtained from lexical analyzer and groups tokens into a hierarchical structure.
- The semantic analyzer checks the source code for any semantic error.
- Intermediate code generator generates the intermediate representation of the source code
- The code Optimizer optimizes the intermediate code into faster running machine code.
- The code generator finally generates the target code which is a relocatable machine code or an assembly code.
- The symbol table is a data structure which contains the record for each identifier in the source code.
- Error handler detects the error in each phase and handles those errors.
Definition of Assembler
Some compilers perform the task of assembler and directly generate a relocatable machine code instead of assembly code, which is further directly passed to linker /loader. The assembler takes as input the assembly code generated by the compiler and translates it into relocatable machine code.
Let us see how machine code is different from assembly code. Assembly code is the mnemonic version of machine code. It means it assembly code uses names for representing operations and it even gives names to the memory addresses. On the other hands, the machine code uses binary codes for representation of operations and memory addresses.
Even the simplest form of assembler performs two passes over the input. The first pass detects all the identifiers in the assembly code that denotes storage location and store them in the symbol table (other than compilers symbol table). The storage location is assigned to the identifier that is encountered in the first pass.
In the second pass, the input is scanned again, and this time the operation code are translated into a sequence of bits representing that operation in the machine code. The second pass also translates identifiers into the addresses defined in the symbol table. Thus the second pass generates the relocatable machine code.
Key Differences Between Compiler and Assembler
- The key difference between compiler and assembler is that the compiler generates assembly code and some compilers can also directly generate executable code whereas, the assembler generates relocatable machine code.
- The compiler takes as input the preprocessed code generated by preprocessor. On the other hands, the assembler takes assembly code as input.
- The compilation takes place in two phases that are analysis phase and synthesis phase. In analysis phase, the input goes through lexical analyzer, syntax analyzer, semantic analyzer whereas, the synthesis analysis takes place via intermediate code generator, code optimizer, code generator. On the other hands, assembler passes the input through two phases. The first phase detects the identifiers and allots addresses to them in the second phase the assembly code is translated to binary code.
- The assembly code generated by the compiler is a mnemonic version of machine code. However, the relocatable machine code generated by assembler is a binary relocatable code.
The assembler may not be required as some compilers directly generate executable code. If assembler is used it requires linker to link all the built-in libraries to the library functions used in the source code.