CSC 218 Notes 3
CSC 218 Notes 3
Translators
Purpose of Translator
It translates high-level language program into a machine language program
that the central processing unit (CPU) can understand. It also detects errors in the
program.
Compiler
A compiler is a translator used to convert high-level programming language to
low-level programming language. It converts the whole program in one session
and reports errors detected after the conversion. Compiler takes time to do its
work as it translates high-level code to lower-level code all at once and then saves
it to memory. A compiler is processor-dependent and platform-dependent. But it
has been addressed by a special compiler, a cross-compiler and a source-to-source
compiler. Before choosing a compiler, user has to identify first the Instruction Set
Architecture (ISA), the operating system (OS) and the programming language that
will be used to ensure that it will be compatible.
1
2
Fig.: Compiler
Interpreter
Just like a compiler, is a translator used to convert high-level programming
language to low-level programming language. It converts the program one at a
time and reports errors detected at once, while doing the conversion. With this, it
is easier to detect errors than in a compiler. An interpreter is faster than a
compiler as it immediately executes the code upon reading the code.
It is often used as a debugging tool for software development as it can execute a
single line of code at a time. An interpreter is also more portable than a compiler
as it is not processor-dependent, you can work between hardware architectures.
Fig.: Interpreter
Assembler
An assembler is is a translator used to translate assembly language to machine
language. It is like a compiler for the assembly language but interactive like an
interpreter. Assembly language is difficult to understand as it is a low-level
programming language. An assembler translates a low-level language, an
assembly language to an even lower-level language, which is the machine code.
The machine code can be directly understood by the CPU.
Fig.: Assembler
2
3
Examples of Translators
Here are some examples of translators per type:
3
4
• You can work on small parts of the program and link them later into a
whole program.
4
5
Example:
Find the below steps to print “Hello world” in Windows
1. Open the notepad.
2. Write below code
global _main
extern _printf
section .text
_main:
push message
call _printf
add esp, 4
ret
message:
db 'Hello, World!', 10, 0
3. Save the file with any name example XYZ.asm, the extension should be
“.asm”.
4. The above file needs to compile with the help of an assembler that is
NASM (Netwide Assembler).
5. Run the command nasm –f win32 XYZ.asm
6. After this, Nasm creates one object file that contains machine code but not
the executable code that is XYZ.obj
7. To create the executable file for windows Minimal GNU is used that
provides the GCC compiler.
8. Run the command gcc –o XYZ.exe XYZ.obj
5
6
6
7
Features
The features of the assembly language are mentioned below:
1. It can use mnemonic than numeric operation code and it also provides the
information of any error in the code.
2. This language helps in specifying the symbolic operand that means it does
not need to specify the machine address of that operand. It can be
represented in the form of a symbol.
3. The data can be declared by using decimal notation.
Assemblers
The assemblers are used to translate the assembly language into machine
language. There are two types of assembler are:
1. Single-pass assembler: A single assembler pass is referred as the complete
scan of source program input to assembler or equivalent representation and
translation by the statement on the basis of statement called as single pass
assembler or one pass translation. It isolates the label, mnemonics and
operand field of the system. It validates the code instructions by looking it
up in mnemonic code table. It enters the symbol found in the label field and
the address of the text available machine word into the symbol table. This
pass is fast and effected, and no need to construct the intermediate code.
2. Multi-pass assembler: In this, an assembler goes through assembly
language several times and generates the object code. In this last pass is
7
8
Advantages
Below are the advantages:
1. It allows complex jobs to run in a simpler way.
2. It is memory efficient, as it requires less memory.
3. It is faster in speed, as its execution time is less.
4. It is mainly hardware oriented.
5. It requires less instruction to get the result.
6. It is used for critical jobs.
7. It is not required to keep track of memory locations.
8. It is a low-level embedded system.
Disadvantages
Below mentioned are the disadvantages:
1. It takes a lot of time and effort to write the code for the same.
2. It is very complex and difficult to understand.
3. The syntax is difficult to remember.
4. It has a lack of portability of program between different computer
architectures.
5. It needs more size or memory of the computer to run the long programs
written in Assembly Language.