0% found this document useful (0 votes)
27 views

Module2 Assembler

1. An assembler is a program that translates assembly language code into machine code. It performs this translation by using tables to look up the machine code equivalent for each assembly language element like mnemonics, operands, and symbols. 2. The assembler's job is to create tables to store information about elements in the assembly code and use these tables to generate the corresponding machine code. It handles tasks like resolving forward references, determining instruction lengths, and keeping track of the location counter. 3. There are two main types of assemblers - single-pass assemblers which perform translation in one pass, and two-pass assemblers which make two passes, using the first to collect symbol information and the second to generate

Uploaded by

gowtham313754
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Module2 Assembler

1. An assembler is a program that translates assembly language code into machine code. It performs this translation by using tables to look up the machine code equivalent for each assembly language element like mnemonics, operands, and symbols. 2. The assembler's job is to create tables to store information about elements in the assembly code and use these tables to generate the corresponding machine code. It handles tasks like resolving forward references, determining instruction lengths, and keeping track of the location counter. 3. There are two main types of assemblers - single-pass assemblers which perform translation in one pass, and two-pass assemblers which make two passes, using the first to collect symbol information and the second to generate

Uploaded by

gowtham313754
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Assembler

Module-2
Why We Need Translator?

A computer will not understand any program written in a


language, other than its machine language. The programs written
in other languages must be translated into the machine language.

Such translation is performed with the help of software.


Example: Compiler, Assembler, Interpreter.

A program which translates an assembly language program into a


machine language program is called an assembler.
Introduction

Basic • Assembler is a translator that accepts


assembly language as input and converts it
Concept of into machine code.
Assembler: • Easy rather than binary and use mnemonic.

The • Create table data structure for each


operator, pseudo code keyword, symbol,
assembler's constants, and register used in the assembly
program.
job is to • Generate machine code
• Location Counter (LC): Indicate next instruction to
be executed
• Literals: Constant value. Example: R1=a+8, 8 is
literal. Literals are stored in literal table data
structure.
• Symbols/labels: Symbols or labels are just like
variables in any other language.
• Example int a=5; Symbol table is used to
handle such things
• Mnemonics: A small word that acts as an identifier
for the instruction. The mnemonics are written in
code segment.
Terminologies ▪ In following examples mov, sub, add, jmp, call,
and mul are the mnemonics:
MOV Move/assign one value to another
(label)
SUB Subtract one value from another
ADD Adds two values
JMP Jump to a specific location
CALL Call a procedure/module
MUL Multiply two values
Example: ADD R1, R2
• Advantages
▪ Since mnemonics replace machine
instruction it is easy to write, debug and
understand in comparison to machine
codes.
▪ Useful to write lightweight application
(in embedded system like traffic light)
because it needs fewer codes than high
Advantages and level language.
Disadvantages • Disadvantages
▪ Mnemonics in assembly language are in
of assembly abbreviated form and in large number, so
they are hard to remember.
language ▪ Program written in assembly language
are machine dependent, so are
incompatible for different type of
machines.
▪ A program written in assembly language
is less efficient to same program in
machine language.
▪ Mnemonics can be different for different
machines according to manufacturers so
assembly language suffers from the
defect of non-standardization
A mnemonic is an abbreviation
for an operation.

Machine-Opcode
Table (MOT) or This table consists of the fields:
(Operation Code Name of mnemonic, binary
Table) or value, instruction length, format
Mnemonics Table of instruction.

MOT table is used to look up


mnemonic operation codes and
translate them to their machine
language equivalents.
Opcode
Table (MOT) • Assembler need not to enter
information in this table. This table
or with information is available with the
assembler.
(Operation • The assembler does not enter
Code Table) information in this table but uses this
data structure
or ▪ To determine valid imperative
statement.
Mnemonics ▪ Find length of instruction.
Table ▪ Fine corresponding machine
Opcode.
• Fields of Opcode Table:
a) Opcode
b) Machine code
c) Type of Opcode
d) Length of Opcode
This table is used to record information about symbols
in program. Symbols can be
Storage variable
Label appearing on Constant declared by
declared by ‘DS’
any statement ‘DC’ statement
statement

Symbol This table is created by pass-I and used by pass-II of


Two-pass assembler.
Table

Fields of Symbol Table:

Symbol no Symbol name Symbol address Symbol length


Literal Table

This table is used to store the information about


literals. This table is created by pass-I and used
by pass-II of Two-pass assembler.

Fields of Literal Table:

Literal no Literal Literal address


Pool Table
This table stores information about literal pools in the assembly
program. No of literal pools is always 1 greater than number of
LTORG statements in assembly program.

This table is created bypass-I and used by pass-II of Two-pass


assembler.

Pool table can be implemented as one dimensional array where


index of array indicates literal pool no and POOLTABLE[x] indicates
literal number of starting literal in literal pool ‘x’.
• Purpose:
▪ Single pass assembler converts assembly
code into machine language just using single
scan.
▪ Define symbols and literals
▪ Determine length of machine instruction
▪ Keep track of location counter ( LC).
Single-pass • Single pass assembler is the assembler which
assigns the memory addresses to the variables (i.e.
assembler label definitions) and translates the source code
into machine code (i.e. assembly) in the first pass
simultaneously. A single pass assembler passes
over the source file exactly once, in the same pass
collecting the labels, resolving future references
and doing the actual assembly. The difficult part is
to resolve future label references (the problem of
forward referencing) and assemble code in one
pass.
• Single Pass Translation
▪ LC processing and construction of the
symbol table proceeds as in two pass
translation.
▪ The problem of forward references is
Pass tacked using a process called
“backpatching”
Structure of ▪ The operand field of an instruction
Assembler containing a forward reference is left
blank initially. The address of the
forward referenced symbol is put into
this field when its definition is
encountered.
▪ MOVER BREG, ONE [ONE is
forward reference]
▪ Table of Incomplete Instruction (TII)
Two Pass Assembler

A two-pass assembler makes two passes over the source code.

During the first pass, it reads the entire source code and builds a symbol
table that contains information about all labels and their addresses.

It also resolves any forward references encountered.

In the second pass, it generates the machine code using the information
collected in the first pass.
• Two Pass Translation
▪ It can handle forward references
easily.
▪ LC processing is performed in
the first pass and symbol
Pass defined in the program are
Structure of entered into the symbol table.
▪ The second pass synthesizes the
Assembler target form using the address
information found in the symbol
table.
▪ In effect, the first pass performs
analysis of the source program
while the second pass performs
synthesis of the target program.
Two-Pass Assembler
Data
Structure
for Pass-1
Data
Structures
of
assembler
Pass I
Pass II of an assembler
Data Structure for Pass-2
Pass II of an assembler
Pass II of an assembler
• Both types of assemblers have their own
advantages and disadvantages.
▪ Single-pass assemblers are faster but
less flexible, while multi-pass
assemblers are more flexible but
slower.
▪ The choice between the two depends
on the specific requirements of the
Advantages and program being assembled.
Disadvantages ▪ For example, if speed is a priority and
all symbols are defined before, they
are used, a single-pass assembler
would be the better choice.
▪ If flexibility is more important, or if
symbols are used before, they are
defined, a multi-pass assembler would
be more suitable.

You might also like