2. Assembler
An assembler is a program that accepts as input an assembly
language program (source) and produces its machine language
equivalent (object code) along with the information for the loader.
Assembler
Object
Code
Loader
Executable
CodeLinker
Fig. : Role of Assemblers
3. An assembler does the following:
1. Generate machine instructions
evaluate the mnemonics to produce their machine code
evaluate the symbols, literals, addresses to produce their
equivalent machine addresses
convert the data constants into their machine representations
5. Single pass assembler
A single pass assembler scans the program only once and creates
the equivalent binary program.
The assembler substitute all of the symbolic instruction with
machine code in one pass
6. Single Pass Assemblers
Single pass assemblers are used when
it is necessary or desirable to avoid a second pass over the source program
the external storage for the intermediate file between two passes is slow
or is inconvenient to use
Main problem
Forward references
Data items
Labels on instructions
7. Forward reference problem
Rules for an assembly program states that the symbol should be
defined 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. c=a + b;
int a;
int b;
int c;
8. Forward reference
For any symbol that has not yet been defined
1. omit the address translation
2. insert the symbol into SYMTAB, and mark this symbol undefined
3. the address that refers to the undefined symbol is added to a list of
forward references associated with the symbol table entry
4. when the definition for a symbol is encountered, the proper address for
the symbol is then inserted into any instructions previous generated
according to the forward reference list
9. Solutions for one pass assembler
Two methods can be used:
Eliminating forward references:
Either all labels used in forward references are defined in the source program
before they are referenced, or forward references to data items are prohibited.
Generating the object code in memory:
No object program is written out and no loader is needed. The program needs to
be re-assembled every time.