Sei sulla pagina 1di 63

Unit – 4

Unit – 3
Pushdown
Assembler
Automata
Prof. Dixita Kagathara
dixita.kagathara@darshan.ac.in

System
Unit –– 33Programming
System
Unit Assembler (2150708)
:: Assembler
Programming (2150708) 11 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Topics to be covered
• Elements of assembly language programming
• Types of assembly language statements
• Design of the assembler & assembler design criteria
• Advanced assembler directives
• Types of assemblers
• Two-Pass assemblers
• One-Pass assemblers
• Algorithm of single pass assembler
• Variants of assemblers
• Design of two pass assembler

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 22 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Assembler
• Assembler is a language processor that converts assembly
language program to machine language program.

MOV id3, R1 0000 1100 0010


MUL #2.0, R1 0111 1000 0001
MOV id2, R2 1111 0101 1110
MUL R2, R1 Assembler 1100 0000 1000
MOV id1, R2 1011 0010 1010
ADD R2, R1 1001 1000 1001
MOV R1, id1 1100 0000 1000

Assembly Error Machine


language Messages (If any) language
program program

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 33 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Assembly language
• An assembly language is machine dependent.
• It is low level programming language that is specific to certain
computer or to the certain family of computers.
• Assembly language provides following three basic facilities:
1. Mnemonic operation codes
2. Symbolic operands
3. Data declarations

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 44 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Facilities of assembly language
Basic facilities of assembly
language

Mnemonics Symbolic Data


Operation Operand Declaration
Code

• Consider following assembly statement:


MOVE AREG, X

Mnemonics
Register operand Memory operand
in symbolic
Operation Code form in symbolic form

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 55 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Facilities of assembly language
Basic facilities of assembly
language

Mnemonics Symbolic Data


Operation Operand Declaration
Code

• Consider another assembly statement:


X DS, 5

SymbolicDeclare
Name storage
Reserves 5 words of
memory for X

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 66 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Assembly language statement format
• An assembly language statement has the following format:
[Label] <Opcode> <operand specification>
• Example:
AGAIN MULT BREG, A

It is a symbolic name.
It is an optional field.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 77 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Assembly language statement format
• An assembly language statement has the following format:
[Label] <Opcode> <operand specification>
Instruction opcode Assembly mnemonics Remarks
00 STOP Stop execution
01 ADD Perform addition
02 SUB Perform subtraction
03 MULT Perform multiplication
04 MOVER Memory to Register move
05 MOVEM Register to Memory move
06 COMP Compare & set condition
07 BC Branch on condition
08 DIV Perform division
09 READ 1. LT 2. LE Read
3. EQainto Register
4. GT 5. GE 6. ANY
10 PRINT Print content of Register

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 88 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Assembly language statement format
• An assembly language statement has the following format:
[Label] <Opcode> <operand specification>
• <operand specification> has following syntax:
<symbolic name> [± <displacement>] [(<index register>)]
Example:
AREA: It returns a memory word with which name AREA is associated.
AREA+5: Refers a memory word 5 word away from the word with
name AREA. ‘5’ is displacement.
AREA(4): It implies indexing the operand AREA with index register 4.
That is the operand address is obtained by adding the content of
index register 4 to the address of AREA.
AREA+5(4): It is a combination of previous two specifications.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 99 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Types of assembly language statements
• Three types of assembly statements are:
1. Imperative statement
2. Declaration statement
3. Assembler directive

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 11
11 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Imperative statement
• An imperative statement indicates an action to be performed
during the execution of the assembled statement.
• Each imperative statement converted into one machine
instruction.
• These are executable statements.
• Examples:
MOVER BREG,X
ADD AREG,Y
PRINT RESULT
STOP

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 12
12 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Declarative statement
• Declaration statements are used for reserving memory for
variables.
• The syntax of declaration statement is as follow:
[Label] DS <constant>
[Label] DC ‘<value>’
Where DS stands for Declare storage and DC stands for Declare
constant.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 13
13 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Declarative statement
DS DC
The DS statement reserves area The DC statement constructs
of memory and associates memory words containing
name with them. constants.
A DS 10 ONE DC ‘1’
Above statement reserves 10 Above statement associates the
words of memory for variable name ONE with a memory word
A. containing the value ‘1’.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 14
14 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Assembler directives
• Assembler directives instruct the assembler to perform certain
action during the assembly program.
• Assembler directives are not converted into machine code rather
they give instruction to assembler itself.
1. START
2. END

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 15
15 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Assembler directives
START
START <constant>
• This directive instructs the assembler to place first word of the
target program in the memory having the address <constant>.
• Example:
START 100
• First word of the target program is stored in the memory location
100 onwards.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 16
16 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Assembler directives
END
END [<operand specification>]
• This directive indicates end of the source program.
• The operand specification indicates address of the instruction
from where the execution of program should begin.
• Execution control should transfer to label given in operand field.
• By default, execution begins with the first instruction of the
assembly program.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 17
17 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Conversion from Assembly
language to Machine language

System
Unit –– 33Programming
System
Unit Assembler (2150708)
:: Assembler
Programming (2150708) 18
18 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Statement formats
Assembly statement format:

Label Opcode Operand specifications

Machine statement format:

Opcode Register Memory operand


operand

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 19
19 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Assembly Statements Memory Opcode Register Memory
Location Operand Operand
START 101
READ N 101) 09 0 113
MOVER BREG, ONE 102) 04 2 115
MOVEM BREG, TERM 103) 05 2 116
AGAIN MULT BREG,TERM 104) 03 2 116
MOVER CREG, TERM 105) 04 3 116
ADD CREG, ONE 106) 01 3 115
MOVEM CREG, TERM 107) 05 3 116
COMP CREG, N 108) 06 3 113
BC LE, AGAIN 109) 07 2 104
MOVEM BREG, RESULT 110) 05 2 114
PRINT RESULT 111) 10 0 114
STOP 112) 00 0 000
N DS 1 113) No code generation for DS
RESULT DS 1 114) No code generation for DS
ONE DC ‘1’ 115) 00 0 001
TERM DS 1 116) No code generation for DS
System END
System Programming
Programming (2150708)
(2150708) 20
20 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Simple assembly scheme

System
Unit –– 33Programming
System
Unit Assembler (2150708)
:: Assembler
Programming (2150708) 21
21 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Design specification for an assembler
• Following steps are used to develop a design specification for an
assembler.
1. Identify the information required to perform a task.
2. Design a suitable data structure for recording the information.
3. Determine the processing required for obtaining and maintaining the
information.
4. Determine the processing required for performing the task by using
the recorded information.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 22
22 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Design of an assembler
mnemonic opcode length Fields of
Mnemonics table
Mnemonics Table ADD 01 1
SUB 02 1
Data access
Control transfer

Source Data structures of an Target


Analysis Phase Assembler Synthesis Phase
Program Program

mnemonic opcode Fields of


Symbol Table AGAIN 104 Symbol table

N 113

Phases of an Assembler

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 23
23 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Analysis phase
• The primary function performed by the analysis phase is to build
the symbol table.
• Addresses of symbolic names are required to build a symbol
table.
• It is possible to determine some address directly, however others
must be inferred. This function is called memory allocation.
• Location Counter (LC) is used to implement a memory allocation.
• The processing involved in maintaining the location counter is
called LC processing.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 24
24 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Tasks of analysis phase
1. Separate the content of label, mnemonics opcode, and operand
fields of a statement.
2. If a label is present, enter the pair (symbol, <LC content>) in a
new entry of the symbol table.
3. Check validity of mnemonics opcode.
4. Perform LC processing.
Example:
ONE MULT BREG, A
label Address
ONE 100

Symbol table

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 25
25 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Synthesis phase
• Consider following assembly statement:
MOVER BREG, ONE
• We require following information to synthesize the machine
instruction corresponding to above statement:
1. Address of variable ONE: depends on the source program;
hence it must be available by analysis phase.
2. Machine operation code of MOVER: does not depend on the
source program; it depends on the assembly language.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 26
26 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Tasks of synthesis phase
1. Obtain machine opcode from the mnemonics table.
2. Obtain address of memory operand from the symbol table.
3. Synthesize a machine instruction. (conversion from assembly
language to machine language)
Example: (Conversion from assembly language to machine language)
MOVER BREG, ONE 04 02 100
Opcode Mnemonics Opcode Mnemonics
00 STOP Code Register
06 COMP
01 ADD 01 AREG
07 BC
Opcode of MOVER Consider
04BREG =address
Code =for 2 02 SUB 02 BREG
08 DIV
of ONE = 100 03 MULT 03 CREG
09 READ
04 MOVER04 DREG
10 PRINT
05 MOVEM

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 27
27 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Pass structure of assembler

System
Unit –– 33Programming
System
Unit Assembler (2150708)
:: Assembler
Programming (2150708) 28
28 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Pass structure of assembler
• A complete scan of the program is called pass.
• Types of assembler are:
1. Two pass assembler (Two pass translation)
2. Single pass assembler (Single pass translation)

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 29
29 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Two pass assembler (Two pass translation)

l l l l l

Data structures

Source Target
Pass I Pass II
Program Program

Intermediate code

Data access
Control transfer

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 30
30 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Two pass assembler (Two pass translation)
Source Intermediate Target
Pass I Pass II
Program representation Program

Intermediate Data
code structures

• The first pass performs analysis of the source program.


• The first pass performs Location Counter processing and records
the addresses of symbols in the symbol table.
• It constructs intermediate representation of the source program.
• Intermediate representation consists of following two
components:
1. Intermediate code
2. Data structures
Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 31
31 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Two pass assembler (Two pass translation)
Source Intermediate Target
Pass I Pass II
Program representation Program

Intermediate Data
code structures

• The second pass synthesizes the target program by using address


information recorded in the symbol table.
• Two pass translation handles a forward reference to a symbol naturally
because the address of each symbol would be known before program
synthesis begins.

Use of a Symbol that precedes


its definition in a program.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 32
32 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
One pass assembler (One pass translation)
• A one pass assembler requires one scan of the source program to
generate machine code.
• LC processing, symbol table construction and target code
generation proceed in single pass.
• The issue of forward references can be solved using a process
called back patching.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 33
33 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Example: Back patching
Assembly Statements Memory Opcode Register Memory
Location Operand Operand
START 101
READ N 101) 09 0 115
MOVER BREG, ONE 102) 04 2 116
. .
. .
. .
N DC ‘1’ 115) 00 0 001
ONE DS 1 116) No code generation for DS
END

Instruction address Symbol


101 N
102 ONE
Table of incomplete instructions (TII)

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 34
34 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Resolving forward references using back patching
• The operand field of an instruction containing forward
references is left blank initially.
• It builds a Table of Incomplete Instructions (TII) to record
information about instructions whose operand fields were left
blank.
• Each entry in TII contains a pair in the form of (instruction
address, symbol)
• When END statement is processed, The symbol table would
contain addresses of all symbols defined in the source program.
• TII would contain information describing all forward references.
• The assembler can now process each entry in TII to complete
the concerned instruction.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 35
35 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Advanced assembler directives

System
Unit –– 33Programming
System
Unit Assembler (2150708)
:: Assembler
Programming (2150708) 36
36 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Advanced assembler directives
• Advanced assembler directives are:
1. ORIGIN
2. EQU
3. LTORG

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 37
37 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
ORIGIN
• Syntax:
ORIGIN <address specification>
• Where <address specification> is an <operand specification> or
<constant>.
• This directive indicates that LC should be set to the address given
by <address specification>.
• The ORIGIN statement is useful when the target program does not
consist of consecutive memory words.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 38
38 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Example: ORIGIN
Assembly Statements Memory Opcode Register Memory
Location Operand Operand
START 200
….
LOOP MOVER AREG, A 210) 04 1 217
…. ….
ORIGIN LOOP + 2 ….
MULT CREG, B 212) 03 3 218

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 39
39 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
EQU
• Syntax
<symbol> EQU <address specification>
• Where <address specification> is either a <constant> or
<symbolic name> ± <displacement>.
• The EQU statement simply associates the name <symbol> with
the address specified by <address specification>.
• However, the address in the location counter is not affected.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 40
40 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Example: EQU
Assembly Statements Memory Opcode Register Memory
Location Operand Operand
START 100
….
LOOP MOVER AREG, A 102) 04 1 117
.
BC LT, BACK 115) 07 1 102

BACK EQU LOOP No LC counter is processed

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 41
41 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
LTORG
• The LTORG directive, stands for 'origin for literals'.
• LTORG allows a programmer to specify where literals should be
placed.
• The assembler uses the following scheme for placement of literals:
1. When the use of a literal is seen in a statement, the assembler
enters it into a literal pool unless a matching literal already
exists in the pool.
2. At every LTORG statement also at the END statement, the
assembler allocates memory to the literals of the literal pool
then clears the literal pool.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 42
42 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Example: LTORG
Assembly Statements Memory Opcode Register Memory
Location Operand Operand
START 200
LOOP MOVER AREG, =’5’ 200) 04 1 211
ADD CREG, =’1’ 201) 01 3 212
….
LTORG
=’5’ 211) 00 0 005
=’1’ 212) 00 0 001
SUB AREG, =’1’ 214) 02 1 219
END
=’1’ 219) 00 0 001

Literal pool
‘5’
‘1’
‘1’

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 43
43 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Intermediate code forms

System
Unit –– 33Programming
System
Unit Assembler (2150708)
:: Assembler
Programming (2150708) 44
44 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Intermediate code forms
• The Intermediate code consists of a sequence of intermediate
code units. (IC units)
• Each unit consists of the following three fields:
1. Address
2. Representation of mnemonics opcode
3. Representation of operands
• There are two variants of intermediate code:
1. Variant I
2. Variant II
• Address and mnemonics opcode fields are assumed to contain
identical information in both variants.
• But, both variants differ in information contained in operand field.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 45
45 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Format of intermediate code
Address Mnemonics Opcode Operands

Statement class Code

Imperative Declaration Assembler Instruction


Code for
statement statement directive opcode for
AD & DL
(IS) (DL) (AD) IS

Examples: Code for AD Code for DL


START: (AD, 01) START 01 EQU 04 DC 01
END 02 LTORG 05 DS 02
DS: (DL, 02)
ORIGIN 03
READ: (IS, 09)
Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 46
46 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Format of intermediate code (Variant I)
Address Mnemonics Opcode Operands Code for
Condition
LT 01
LE 02
First operand Second operand EQ 03
GT 04
GE 05
Code for Code for Operand ANY 06
Code
register condition class

Constant(C) Symbol(S) Literal (L) For symbol


Constant
and literal
Examples: itself for
entry no. in
AREG, A GT, LOOP AREG, =‘1’ START 200 constant
SYMTAB &
(1) (S, 01) (4) (S, 02) (1) (L, 01) (C, 200) LITTAB
Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 47
47 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Format of intermediate code (Variant II)
• In variant II symbols, condition codes and CPU registers are not
processed.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 48
48 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Variant I & variant II
Assembly statements Variant I Variant II
START 200 (AD,01) (C, 200) (AD,01) (C, 200)
READ A (IS, 09) (S, 01) (IS, 09) A
LOOP MOVER AREG, A (IS, 04) (1)(S, 01) (IS, 04) AREG, A
. . .
SUB AREG, =’1’ (IS, 02) (1)(L, 01) (IS, 02) AREG,(L, 01)
BC GT, LOOP (IS, 07) (4)(S, 02) (IS, 07) GT, LOOP
STOP (IS, 00) (IS, 00)
A DS 1 (DL, 02) (C,1) (DL, 02) (C,1)
LTORG (AD, 05) (AD, 05)

Symbols, condition codes and CPU register are not processed in Variant II.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 49
49 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Exercise: Write variant I & variant II for given program.
Assembly statements Variant I Variant II
1 START 101 (AD, 01) (C, 101) (AD, 01) (C, 101)
2 READ N (IS, 09) (S, 02) (IS, 09) N
3 MOVER BREG, ONE (IS, 04) (2)(S, 04) (IS, 04) BREG, ONE
4 MOVEM BREG, TERM (IS, 05) (2)(S, 05) (IS, 05) BREG, TERM
5 AGAIN MULT BREG,TERM (IS, 03) (2)(S, 05) (IS, 03) BREG, TERM
6 MOVER CREG, TERM (IS, 04) (3)(S, 05) (IS, 04) CREG, TERM
7 ADD CREG, ONE (IS, 01) (3)(S, 04) (IS, 01) CREG, ONE
12 MOVEM CREG, TERM (IS, 05) (3)(S, 05) (IS, 05) CREG, TERM
13 COMP CREG, N (IS, 06) (3)(S, 02) (IS, 06) CREG, N
14 BC LE, AGAIN (IS, 07) (2)(S, 01) (IS, 07) LE, AGAIN
15 MOVEM BREG, RESULT (IS, 05) (2)(S, 03) (IS, 05) BREG, RESULT
16 PRINT RESULT (IS, 10) (S, 03) (IS, 10) RESULT
17 STOP (IS, 00) (IS, 00)
18 N DS 1 (DL, 02) (C, 1) (DL, 02) (C, 1)
19 RESULT DS 1 (DL, 02) (C, 1) (DL, 02) (C, 1)
20 ONE DC ‘1’ (DL, 01) (C, 1) (DL, 01) (C, 1)
21 TERM DS 1 (DL, 02) (C, 1) (DL, 02) (C, 1)
22 END (AD, 02) (AD, 02)

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 50
50 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Design of a two pass assembler

System
Unit –– 33Programming
System
Unit Assembler (2150708)
:: Assembler
Programming (2150708) 51
51 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Design of a two pass assembler
• Tasks performed by the passes of a two pass assembler are as
follows:
• Pass I (Analysis phase)
1. Separate the symbol, mnemonics opcode and operand fields.
2. Build the symbol table.
3. Perform Location Counter (LC) processing.
4. Construct intermediate representation.
• Pass II (Synthesis phase)
1. Synthesize the target program.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 52
52 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Data structures of assembler - Pass I
• Pass I uses the following data structures:
1. OPTAB - A table of mnemonics opcode and related information
2. SYMTAB - A table of symbols used in the program.
3. LITTAB - A table of literals used in the program
4. POOLTAB – A table of information concerning literal pools

Mnemonics opcode Mnemonics info Class


MOVER (04,1) IS (Imperative statement)
DS R#7 DL (Declaration statement)
START R#11 AD (Assembler directive)

• If an imperative statement, then mnemonics info field contains the pair


(machine code, instruction length).
• Else it contains the id of a routine to handle the declaration or assembler
directive statement.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 53
53 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Data structures of assembler - Pass I
• Pass I uses the following data structures:
1. OPTAB - A table of mnemonics opcode and related information
2. SYMTAB - A table of symbols used in the program.
3. LITTAB - A table of literals used in the program
4. POOLTAB – A table of information concerning literal pools

Symbol Address Length


LOOP 202 1
NEXT 214 1
LAST 216 1

• A SYMTAB entry contains the symbol name, field address and length.
• Some addresses can be determined directly, e.g. the address of the first
instruction in the program, however others must be inferred.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 54
54 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Data structures of assembler - Pass I
• Pass I uses the following data structures:
1. OPTAB - A table of mnemonics opcode and related information
2. SYMTAB - A table of symbols used in the program.
3. LITTAB - A table of literals used in the program
4. POOLTAB – A table of information concerning literal pools

literal Address
1 =’5’
2 =’1’
3 =’1’

• A LITTAB entry contains the field literal and address.


• The first pass uses LITTAB to collect all literals used in a program.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 55
55 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Data structures of assembler - Pass I
• Pass I uses the following data structures:
1. OPTAB - A table of mnemonics opcode and related information
2. SYMTAB - A table of symbols used in the program.
3. LITTAB - A table of literals used in the program
4. POOLTAB – A table of information concerning literal pools

Literal no
#1
#3
-
• This table contains the literal number of the starting literal of each literal pool.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 56
56 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
1 START 200 Symbol Address Length
2 MOVER AREG, =’50’ 200) +04 1 211 LOOP 202 1
3 MOVEM AREG, A 201) +05 1 217 NEXT 214 1
4 LOOP MOVER AREG, A 202) +04 1 217 LAST 216 1
5 MOVER CREG, B 203) +04 3 218
A 217 1
6 ADD CREG, =’10’ 204) +01 3 212
BACK 202 1
7 …
B 218 1
12 BC ANY, NEXT 210) +07 6 214 SYMTAB
13 LTORG
=’50’ 211) +00 0 005
=’10’ 212) +00 0 001 No. literal Address
14 … 1 =’50’ 211
15 NEXT SUB AREG, =’20’ 214) +02 1 219 2 =’10’ 212
16 BC LT, BACK 215) +07 1 202 3 =’20’ 219
17 LAST STOP 216) +00 0 000 LITTAB
18 ORIGIN LOOP + 2
19 MULT CREG, B 204) +03 3 218
20 ORIGIN LAST + 1 Literal no
21 A DS 1 217) #1
22 BACK EQU LOOP
23 B DS 1 218) #3
24 END POOLTAB
25 =’20’ 219) +00 0 001
System
System Programming
Programming (2150708)
(2150708) 57
57 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Assembler pass I Algorithm
1. loc_cntr=0(default value) Location Counter
pooltab_ptr=1; POOLTAB[1]=1; Points to an entry in LITTAB
littab_ptr=1; Points to an entry in POOLTAB
2. While next statement is not END statement
a) If a label is present then Enter label in symbol table.
I. this_label=symbol in label field
II. Enter (this_label, loc_cntr) in SYMTAB Allocate memory for literals
b) If an LTORG statement then and increment pooltab_ptr
I. Process literals LITTAB to allocate memory and put the address field.
update loc_cntr accordingly
II. pooltab_ptr= pooltab_ptr+1;
III. POOLTAB[ pooltab_ptr]= littab_ptr
c) If a START or ORIGIN statement then Memory allocation process
I. loc_cntr=value specified in operand field; Update the symbol table
d) If an EQU statement then entry for label
I. this_address=value specified in <address spec>;
II. Correct the symtab entry for this_label to (this_label, this_address);

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 58
58 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Assembler pass I Algorithm
2. While next statement is not END statement
Consider size require by
e) If a declaration statement then DC/DS and according to
I. code= code of the declaration statement that • update
Take location
machine
II. size= size of memory area required by DC/DS counter,opcode
then generate and
III. loc_cntr=loc_cntr+size; intermediate code to that
acoording
IV. Generate IC ’(DL,code)’.. update location
f) If an imperative statement then counter.
• If operand is literal
I. Code= machine opcode from OPTAB
make entry in
II. loc_cntr=loc_cntr+instruction length from OPTAB; literal table and
III. if operand is a literal then update literal
this_literal=literal in operand field; table pointer
LITTAB[littab_ptr]=this_literal; • If operand is label
make entry in
littab_ptr= littab_ptr +1;
symbol table, then
else generate IC code.
this_entry= SYMTAB entry number of operand
generate IC ‘(IS, code)(S, this_entry)’;

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 59
59 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Assembler pass I Algorithm
3. Processing END statement
a) Perform step 2(b) Allocate memory for literals
b) Generate IC ‘(AD,02)’
IC unit for END
c) Go to pass II

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 60
60 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Assembler pass II Algorithm
• loc_cntr : Location Counter
• littab_ptr : Points to an entry in LITTAB
• pooltab_ptr : Points to an entry in POOLTAB
• machine_code_buffer : Area for constructing code for one statement
• code_area : Area for assembling the target program
• code_area_address : Contains address of code_area
1. Code_area_adress= address of code_areas;
Pooltab_ptr=1;
Loc_cntr=0;
2. While next statement is not an END statement
a) Clear machine_code_buffer;
Process literals and generate
b) if an LTORG statement target code also increment
i. Process literals in LITTAB and assemblepooltab
the literals in machine_code_buffer.
pointer
ii. size= size of memory area required for literals
iii. pooltab_ptr=pooltab_ptr +1;

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 61
61 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Assembler pass II Algorithm
2. While next statement is not an END statement
c) If a START or ORIGIN statement Update location counter
I. loc_cntr=value specified in operand field;
II. size=0;
d) If a declaration statement Generate target code for DC
I. If a DC statement then assemble the constant in machine_code_buffer;
II. size= size of memory area required by DC/DS;
e) If an imperative statement
Get addresses from
I. Get operand address from SYMTAB or LITTAB symbol table and literal
II. Assemble instruction in machine_code_buffer; table then generate target
III. size=size of instruction; code
f) If size≠ 0 then
Move thetotarget
I. Move contents of machine_code_buffer code to memory location
the address
code_area_address+loc_cntr; assigned for it and update location counter.
II. loc_cntr=loc_cntr+size;

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 62
62 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Assembler pass II Algorithm
3. Processing END statement
Generate code for LTORG and
a) Perform steps 2(b) and 2(f). place at proper memory location
b) Write code_area into output file.
Write entire target code to output file.

Unit –– 33Programming
System
Unit
System :: Assembler
Assembler (2150708)
Programming (2150708) 63
63 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
End of Unit - 3

System
Unit –– 33Programming
System
Unit Assembler (2150708)
:: Assembler
Programming (2150708) 64
64 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology

Potrebbero piacerti anche