An assembler in a computer is a kind of language processor. It translates the assembly language into machine language. However, the program in assembly language may have some pseudo instructions. These instructions cannot be directly translated to the equivalent machine instruction. In this case, we need to scan the assembly code two or multiple times through the assembler.
Although we can classify the assembler into two types, i.e. single-pass assembler and two-pass/multi-pass assembler. In a single-pass assembler, the entire assembly language program is translated to its equivalent machine language program at once.
In a two-pass or multi-pass assembler, the assembly language program has to be scanned twice or multiple times to translate it completely to the equivalent machine language program. Now lets us learn more about the assembler in computer.
Content: Assembler in Computer
What is an Assembler in System Programming?
Assembler is a system program that accepts a program in assembly language and translates it to a relocatable machine code. Along with the machine language program, it also generates some information that is required by the loader. Let us try to understand what is assembly language and machine language.
What is Assembly Language?
Assembly language is a low-level programming language. One instruction of the assembly language program directs exactly one operation to the computer. However, the assembly language is close to the machine language but is quite simpler than machine language. As assembly language uses mnemonics to represent instructions.
Features of assembly language are:
- It uses mnemonics to write instructions.
- Addresses used in the instructions are symbolic, not absolute.
- The mnemonics used in assembly language are easy to understand and readable by programmers.
- Finally, it facilitates declaring data and reserving memory.
What is Machine Language?
Machine language is a language that can directly communicate with the hardware of your computer. The language includes strings of 0s and 1s, which are binary digits or bits. However, it is difficult for users to understand machine language.
The assembler analyzes the source program and identifies the symbolic names associated with the instructions or data of the program. It allocates the memory to the program instructions and associated data. So, the assembler knows the memory address of each instruction and data in the assembly code.
Further, the assembler also maintains a data structure that we refer to as the location counter (LC). LC keep track of the addresses that the next instruction or data in the target program would have. As the assembler processes assembly instructions one by one, it updates the location counter by the size of each instruction or data.
The assembler also maintains two tables mnemonic table and a symbol table. The mnemonic table helps in identifying which mnemonic operation code corresponds to which instruction or data. However, the symbol table helps in identifying which symbolic name corresponds to which instruction or data in the program.
Types of Assembler
One pass assembler or single pass assembler scans the program only once to create the equivalent binary program. In this one scan, the assembler substitutes all the symbolic instructions present in the assembly language program with machine code.
The two-pass assembler translates an assembly language program into a machine language program in two passes or multiple passes. Moreover, the sequential passes over the assembly language code and resolves the pseudo-op instructions present in the assembly language code.
- Identifies the symbols and opcode and records them in the symbol table.
- Keep a record of the location counter.
- Processes the pseudo instructions.
- Translate opcode into corresponding numeric opcode.
- Generate machine code according to the value of literals and symbols.
What are Pseudo-op Instructions?
Pseudo-op instructions are assembly language instructions. Moreover, the assembly instructions do not have their machine language equivalent. These instructions present in the assembly code are processed in the first pass and are resolved in the second pass.
How Does Assembler Work?
An assembler processes the assembly language code and translates it to the relocatable machine language code. Moreover, how the assembler does this depends on the number of phases used to convert the assembly code to machine code.
If the assembler is one pass assembler, then it would perform the entire conversion of assembly code to machine code in a single pass or single scan.
If the assembler is a two-pass assembler, then in the first pass, the assembler generates a symbol table. Consequently, in the second pass, it generates the machine code. Let us understand the working with the help of an example.
Consider an assembly language code that will be provided to the assembler.
JHON START 0 //identifies the name of the program USING *, 15 //register 15 is the base register L 1, FIVE //Load value of FIVE in register 1 A 1, FOUR //Add value in FOUR to the value in register 1 ST 1, TEMP // Store updated value of register 1 in TEMP FOUR DC F ‘4’ //Define constant 4 FIVE DC F ‘5’ //Define constant 5 TEMP DS 1F //Define storage of 1 full word END
Now in the first pass, when the assembler scans the assembly code above. It defines symbols and literals, traces the location counter and processes pseudo instructions. So, the intermediate code generated after first-pass is:
Additionally, the second pass generates the machine’s instructions and addresses. The figure below particularly illustrates the machine instructions for the intermediate code generated in the first pass.
Design of Assembler
To design an assembler, the programmer must be aware of the problem statement for which it has to design an assembler. The steps to design an assembler.
- Specify the problem
- Specify data structure
- Define the format of the data structure
- Specify algorithm
- Look for modularity (capability of one program to be subdivided into independent programming units)
- Repeat 1 through 5 on modules.
So, this is all about the assembler in computer that translates an assembly language program to a machine language program. Further, we have also learned how the assembler processes the pseudo instruction to generate the machine code.