Sie sind auf Seite 1von 21

a

The assembler
Teodor Rus
rus@cs.uiowa.edu

The University of Iowa, Department of Computer Science


a
These slides have been developed by Teodor Rus. They are copyrighted materials and may not be used in other course settings outside of the University of Iowa in
their current form or modified form without the express written permission of the copyright holder. During this course, students are prohibited from selling notes to or being
paid for taking notes by any person or commercial firm without the express written permission of the copyright holder.

Introduction to System Software – p.1/21


Assembler : AL −→ M L
The assembler is defined by a pair of mappings
Assembler = hHA, T Ai,

HA : ALSem −→ M LSem , T A : ALSyn −→ M LSyn ,

which make the communication diagram in Figure 1 commutes:

LearnAL EvalAL
ALSem - ALSyn - ALSem
6 6 6
HM HA TM TA HM HA
? EvalM L ? LearnM L ?
M LSem  M LSyn  M LSem

Figure 1: The assembler

Introduction to System Software – p.2/21


Facts
1. The mappings hHA, T Ai in Figure 1 support the communication
from the assembly language programmer to the machine.
2. The mappings hHM, T M i in Figure 1 support the communication
from the machine to the assembly language programmer.
3. Since ALSem = M LSem , HM and HA are the identity mappings.

Introduction to System Software – p.3/21


Example assembler
Oolong : OolongP rograms → CF F
Oolong Program: a stream of Oolong statements
Oolong statement: [Label:] Mnemonic [Operand] [;Comment]
CFF:

Magic# Minor Major CnstPool Class Super Interfaces Fields Methods

Introduction to System Software – p.4/21


Implementation
To design and implement an assembler one
needs to:
• Develop a finite specification of the machine language;
• Develop a finite specification of the assembly language;
• Implement the mapping that performs:
1. read an assembly language program statement by statement;
2. map each statement into machine language representation;
3. generate a machine language module.

Introduction to System Software – p.5/21


Constructing TA (the assembler)
Figure 2
AL program ML program
Statement 1 Instruction/Data
HH *


Statement 2 j
H  Instruction/Data
... PP
q
P 
1 ...
Statement i - TA - Instruction/Data
*
 HH

... ...
j
H
Statement n Instruction/Data

Figure 2:

Introduction to System Software – p.6/21


C language representation
Assembler(FILE *ALP, FILE *MLP)
{
struct AssemblyStatement ALS_BUFFER, *APC;
struct MachineStatement MLS_BUFFER, *MPC;
APC = read(ALP, ALS_BUFFER);
MPC = &MLS_BUFFER;
while (APC->Opcode != End)
{
map (APC, MPC);
write (MPC, MLP);
APC = read(ALP, ALS_BUFFER);
MPC = &MLS_BUFFER;
}
map (APC, MPC);
write (MPC, MLP);
}

Introduction to System Software – p.7/21


Assembler components
• read() reads program statements;
• map() maps assembly statements into
instruction and data words; it is specified by
the diagram in Figure 3;
• write() writes statement translation.
Label Mnemonic Operand Comment
H

 j
H
? ?
Address Opcode Operand Result

Figure 3: Mapping assembly statements

Introduction to System Software – p.8/21


Generator translation
Each generator class in the assembly language
is mapped into binary code in machine language.

1. Mnemonics are mapped into binary operation codes


2. Operands are mapped into operand-specifiers which are
address expressions of the form:
AE(address, register, index, modif ier, immediate)
3. User defined symbols are mapped into addresses, registers,
binary numbers, modifiers, as appropriate.

Example: labels are mapped into memory locations holding the


elements they labels.

Introduction to System Software – p.9/21


Statement translation
Each assembly language statement is mapped
into a sequence of one or more machine
language instructions or data.
Examples:
1. dc 1234 is mapped into an address that stores binary
representation of 1234;
2. dv x is mapped into the address allocated to variable x usually
initialized to zero;
3. Add x, y is mapped into the code 001 A(x) A(y) stored into
a memory location allocated to Add x, y where 001 is the
machine code represented by Add and A(x), A(y) are memory
locations allocated to x and y respectively.

Introduction to System Software – p.10/21


Program translation pattern
Assembly language statement translations are
assembled into a predefined machine language
program pattern.
Example: CFF is an example of such pattern.

Introduction to System Software – p.11/21


Conclusions
The assembler can be implemented by three
readings of the assembly language program
(called passes):

1. Pass 1: translates all generators and store their translations in


appropriate tables;
2. Pass 2: Uses generator translations generated by Pass 1 to
translate all statements and stores their translations in appropriate
tables;
3. Pass 3: Assembles statement translations generated in Pass 2 into
a machine language program according to the predefined
machine language program pattern.

Introduction to System Software – p.12/21


Example passes
Consider Oolong : OolongP rogram → CF F
1. Pass 1: maps Oolong program generators into appropriate
constants;
2. Pass 2: constructs CnstPool, Class representation, Superclass
representation, Interfaces representations and Method
representations. All are predefined patterns using index to
CnstPool;
3. Pass 3: assemble result of Pass 2 into CFF.

Introduction to System Software – p.13/21


Optimization
Assembly language programs are obtained by
concatenating assembly language statements.
Therefore program translation can be performed
during Pass 2, i.e.:

1. Pass 1: translate generators and store their translations into


appropriate tables;
2. Pass 2: translate statements and assemble their translations into
the ML program.

Introduction to System Software – p.14/21


Two pass assembler
Pass 1: for each statement S:
translates S’s generators and store their translations into the
symbol table, SYMTAB.

Pass 2: for each statement S:


1. Identifies the machine language pattern M (S) holding the
translation of S;
2. Fill-out M (S) with generator translations taken from SYMTAB;
3. Assemble M (S) into the ML program according to the ML
program pattern.

Introduction to System Software – p.15/21


Feasibility
• A statement translation depends only on its
generators which are in SYMTAB;
• ML program generation is determined by its
instruction and data concatenation patterns;
• Conclusion: A two pass assembler is always
feasible.

Introduction to System Software – p.16/21


One pass assembler
If each statement of an assembly language pro-
gram is expressed only in terms of symbols previ-
ously defined in the program then the assembler
can be implemented in one pass.

Introduction to System Software – p.17/21


One pass assembler:implementation
One pass assembler performs as follows:
For each statement S:
1. Perform generator translation and store them in SYMTAB;
2. Identify machine language statement pattern, M (S), holding the
translation of S;
3. Fill-out M (S) using SYMTAB. This is feasible because every
statement is expressed only in terms of previously defined
symbols;
4. Assemble M (S) accordingly.

Introduction to System Software – p.18/21


Data structures
Data structures supporting assembler
implementation are:
1. OPTAB: the assembler predefined table that holds the mnemonics
and their translations. Each mnemonic is however associated with
AL statement holding it and its ML translation pattern;
2. SYMTAB: the assembler constructed table that holds the
translations of user defined symbols;
3. IFAS: internal form of assembly statement (for optimization purpose);
4. FIF: file of internal form (prevents two-readings of the source);
5. FOG: file of the object generated by the assembler (holds CFF).

Introduction to System Software – p.19/21


Relationship
The relationship between the data structures sup-
porting assembler implementation is shown in
Figure 4.

Introduction to System Software – p.20/21


Figure 4
* OPTAB

IFAS FIF
... rMnem Label k r Body
r r ...
r
SYMTAB 1 2 ... k
? TDT
j j j
ODT
+,-,*,etc
-

 U Symbols

Figure 4: Relationship between data structures

Introduction to System Software – p.21/21

Das könnte Ihnen auch gefallen