You are on page 1of 45

ASSEMBLERS

Prepared By:
Shifali Sharma
Apex Institute of Technology- MBA
Apex Institute of Technology- CSE
Definition
• An assembler is a translator that translates source
instructions (in symbolic language) into target
instructions (in machine language), on a one to one
basis.
• An assembler is a translator that translates a
machine oriented language into machine language.

Apex Institute of Technology- CSE


Assembly Language

[label] mnemonic [operand] [;]


• label : Symbolic name for memory location
• mnemonic : operation to be performed
• operands : values or variables on which
operation is to be performed
• after ; : comment

Apex Institute of Technology- CSE


Assembly Language Terms
• LC (Location Counter) : LC contains the address of next
instruction to be executed.
• Literals : the constant values present in the assemble
code.
ADD R1, 4
Here 4 is a literal.
• Symbols : the variable names we use to hold some
value.
A DC ‘1’
Here A is a symbol.
• Procedures : these are the functions in assembly
language.

Apex Institute of Technology- CSE


Assembly Language Statements

• Imperative Statements
– An Imperative statement indicates an action to be
performed during the execution of assembled
program.
– MOVER AREG A
– PRINT B

Apex Institute of Technology- CSE


Assembly Language Statements

• Declarative Statements
– DS : reserves areas of memory and associates
name with them
– DC : constructs memory word containing
constants
– A DS 5
– B DC 3

Apex Institute of Technology- CSE


Assembly Language Statements

• Assembler Directive:
– Instruct the assembler to perform certain actions
during assembly of program.
– START : directive instruct the assembler to place
first word of the target program to generate by it
in memory word having the address (constant).
• START 100  Set LC=100
– END : directive indicate the end of the source
program.

Apex Institute of Technology- CSE


Assembly Language Statements

– ORIGIN : directive instruct the assembler to put


the address given by address specification in the
location counter.
• ORGIN 200  Set LC = 200
– EQU : directive assigns the address of one symbol
to other label, location counter is not affected due
to EQU directive.
• LABEL EQU LOOP  Here LABEL will get the
address of LOOP

Apex Institute of Technology- CSE


Assembly Language Statements
– LTORG : ‘ORIGIN FOR LITERALS’. LTORG specify where literals
should be placed. After LTORG and END statements
assembler allocates memory to the literal of literal pool and
clears the literal pool.
– USING : The USING statement tells the assembler to
associate the address in R with label. It also tells the
assembler which register to use when converting implicit
addresses to explicit addresses.
• USING label, R
– DROP : The DROP informs the assembler that registers R1,
R2, … , RN are no longer to be associated with the label or
that the specified register is not supposed to be used to
convert implicit addresses to explicit addresses.

Apex Institute of Technology- CSE


Databases used by Assemblers
• LC : Location Counter, used to keep track of
next instruction to be executed.
• MOT : Machine operation table, contain a field
[mnemonic opcode, class and mnemonic
information (information code of IS, length)].
It is of 6 bytes. It is a fixed length table.

Apex Institute of Technology- CSE


Databases used by Assemblers
• POT : Pseudo Op-code Table, contain a field
[mnemonic op-code, class and mnemonic
information (routine number)]. Each entry is
of 8 bytes. It is a fixed length table.

Apex Institute of Technology- CSE


Databases used by Assemblers
• ST or SYMTAB : Symbol Table, is for storing
each label (variable name) and it’s value. Used
to keep track of symbols that are defined in
the program and giving them a location in
memory.

Apex Institute of Technology- CSE


Databases used by Assemblers

• LT or LTTAB : Literal Table, is for storing each


literal and it’s location in memory. Literals will
be always encountered in the operation field
of an instruction.

Apex Institute of Technology- CSE


Databases used by Assemblers

• POOLTAB : The literals processed by the assembler are


collected and placed in a special area called the literal
pool.
– Awareness of different literal pools is maintained using the
auxiliary table POOLTAB.
– POOLTAB contains the literal number of the starting literal
of each literal pool.
– At any stage, the current literal pool is the last pool in the
LITTAB.
– On encountering LTORG statement (or the END statement),
literals in the current pool are allocated addresses starting
with the current value in LC and LC is appropriately
incremented.

Apex Institute of Technology- CSE


Databases used by Assemblers

• MOT + POT = OPTAB:


– OPTAB contains the field mnemonics op-codes, class
and mnemonics information.
– The Class field indicates whether the op-code belongs
to an imperative statement (IS), a declarative
statement (DS) or an assembler directive (AD).
– If an imperative, the mnemonics information field
contains the pair (machine code, instruction length),
else it contains the id of routine to handle the
declaration statement or assembler directive
statement.

Apex Institute of Technology- CSE


TWO PASS ASSEMBLER

Apex Institute of Technology- MBA


Apex Institute of Technology- CSE
Two Pass Assembler

• Convert mnemonic operation codes to their


machine language equivalents.
• Convert symbolic operands to their equivalent
machine addresses.
• Build the machine instructions in the proper
format.
• Convert the data constants to internal machine
representations.
• Write the object program and the assembly
listing.

Apex Institute of Technology- CSE


Two Pass Assembler

PASS 1 is the ANALYSIS PHASE


AND
Source PASS 2 is the SYNTHESIS PHASE
Program

PASS 1 Intermediate PASS 2 Object


Code Code

OPTAB SYMTAB SYMTAB

Apex Institute of Technology- CSE


Design of Two Pass Assembler

• Pass 1:
– Separate the symbols, literals, mnemonics and
operand fields.
– Build the Symbol and Literal Table.
– Perform LC processing
– Construct intermediate representation
• Pass 2:
– Synthesize the target code

Apex Institute of Technology- CSE


Two Pass Assembler

Pool Table

Apex Institute of Technology- CSE


Two Pass Assembler

Pool Table
Pool Table
0
1
2
3
4
5
5
Apex Institute of Technology- CSE
Two Pass Assembler

Apex Institute of Technology- CSE


Two Pass Assembler

Apex Institute of Technology- CSE


Assembly Codes for Practice
• JOHN START 0
• START 200
• USING *, 15
• MOVER AREG, =‘5’ • L 1, FIVE
• MOVEM AREG, X • A 1, FOUR
• L MOVER BREG, =‘2’ • ST 1, TEMP
• ORIGIN L + 3 • FOUR DC F’4’
• LTORG • FIVE DC F’5’
• TEMP DS 1F
• X DS 1
• END
• END

Apex Institute of Technology- CSE


Pass 1 Flowchart

Apex Institute of Technology- CSE


Pass 2 Flowchart

Apex Institute of Technology- CSE


Pass 1 Algorithm

Apex Institute of Technology- CSE


Pass 1 Algorithm

Apex Institute of Technology- CSE


Pass 1 Algorithm

Apex Institute of Technology- CSE


Pass 2 Algorithm

Apex Institute of Technology- CSE


Pass 2 Algorithm

Apex Institute of Technology- CSE


Pass 2 Algorithm

Apex Institute of Technology- CSE


SINGLE PASS ASSEMBLER
or
ONE PASS ASSEMBLER

Apex Institute of Technology- MBA


Apex Institute of Technology- CSE
Single Pass Assembler

• A single pass assembler scans the program


only once and creates the equivalent binary
program.
• The assembler substitute all the symbolic
instruction with machine code in one pass.

Apex Institute of Technology- CSE


Single Pass Assembler

Source POOLTAB
Program
LITTAB

Object
SINGLE PASS
Code
ASSEMBLER

SYMTAB

MOT POT

TII

Apex Institute of Technology- CSE


Single Pass Assembler

Apex Institute of Technology- CSE


Single Pass Assembler

Apex Institute of Technology- CSE


Single Pass Assembler

Apex Institute of Technology- CSE


Forward Reference Problem

• Rules for an assembly program states that the


symbol should be defines somewhere in the
program.
• But in some cases a symbol may be used prior
to its definition. Such a reference is called
forward reference.
• Due to this assembler cannot assemble the
instructions and such a problem is called
forward reference problem.

Apex Institute of Technology- CSE


Solution of FRP for One-Pass
Assembler
• The process of forward references is tackled using a
process called back patching.
• The operand field of an instruction containing forward
references is left blank initially.
• A table of instruction containing forward references is
maintained separately called table of incomplete
instruction (TII).
• This table can be used to fill up the addresses in
incomplete instruction.
• The address of the forward references symbols is put
back in the blank field with the help of back patching
list.

Apex Institute of Technology- CSE


Solution of FRP for Two-Pass
Assembler
• During the first pass
– Symbol table and is maintained.
– When ever a undefined symbol is encountered it
is inserted into the Symbol table without the
address of that symbol.
– When symbol declaration is found, then its
address is stored in the symbol table.
• During the second pass
– Translation from source to machine language
actually take place. Symbol addresses are then
used from symbol table instead of their names.
Apex Institute of Technology- CSE
Looking for Modularity while
designing the passes

• While designing an assembler some functions


can be isolated.
• Such functions fall into two categories:
– Multi-use
– Unique

Apex Institute of Technology- CSE


Functions that can be isolated in Pass
1

Apex Institute of Technology- CSE


Looking for Modularity while designing
the passes

• Each of these functions should independently go


through the entire design process (problem statement,
databases, algorithm, modularity etc.
• These functions can be implemented as separate
external subroutines, as internal subroutines, or as
sections of the pass 1 and pass 2 programs.
• In any case, the ability to treat functions separately
makes it much easier to design the structure of
assembler and each of its parts.
• Thus, rather than viewing the assembler as a single
program, we view it as a coordinated collection of
routines each of relatively minor size and complexity.

Apex Institute of Technology- CSE


Functions that can be isolated in Pass
2

Apex Institute of Technology- CSE

You might also like