2.1basic Assemblers Functions
2.1basic Assemblers Functions
Function of an assembler
Assembly language program Machine language and other information for the loader
Assembler
Database
Translate mnemonic operation codes their machine language equivalents. Assign machine addresses to symbolic labels used by the programmer.
Assembler directives
provide instructions to the assembler itself. They are not translated into machine instructions.
Specify name and staring address for the program Indicate the end of the source program and (optionally) specify the first executable instruction in the program Generate character or hexadecimal constant, occupying as many bytes as needed to represent the Generate one-word integer constant Reserve the indicated number of bytes for a data area Reserve the indicated number of words for a data area
e.g. translate STL to 14 process assembler directives e.g. translate RETADR to 1033 handle forward references
two passes the first pass scans the source program for label definitions and assigns addresses the second performs most of the actual translation.
Build the machine instructions in the proper format Convert the data constants specified in the source program into their internal machine representation
RETADR
11
12
H Program name Starting address of object program (hex.) Length of object program in bytes (hex.) T Starting address for object code in this record (hex.) Length of object code in this record in bytes (hex.) Object code, represented in hexadecimal (2 columns per byte of object code)
Object program
No object code corresponds to addresses 1033-2038. This storage is reserved by the loader for use by the program during execution.
Basic assembler functions 14
Assign addresses to all statements in the program. Save the values(addresses) assigned to all labels for use in Pass 2. Perform some processing of assembler directives.
Include processing that affects address assignment such as determining the length of data areas defined by BYTE, RESW, etc.
Generate data values defined by BYTE, WORD, etc. Perform processing of assembler directives not done during Pass 1. Write the object program and the assembly listing.
15
OPTAB is used to look up mnemonic operation codes and translate them to their machine language equivalents. SYMTAB is used to store values (addresses) assigned to labels. This is a variable that is used to help in the assignment of address.
17
Source Program
Pass 1 of assembler
Pass 2 of assembler
Object Program
OPTAB
In most cases, OPTAB is a static table. OPTAB must contain the mnemonic operation code and its machine language equivalent In more complex assemblers, OPTAB also contains information about instruction format and length. OPTAB is usually organized as a hash table, with mnemonic operation code as the key.
19
OPTAB (contd)
class optab_entry { public: char mnemonic[6]; char opcode; };
20
10
SYMTAB
SYMTAB includes the name and value (address) for each label in the source program together with flags to indicate error conditions.
This table may also contain information, such as type or length, about the data area or instruction labeled. SYMTAB is usually organized as a hash table for efficiency of insertion and retrieval.
non-random key
21
SYMTAB (contd)
class symtab_entry { public: char symbol[8]; int }; address;
22
11
Intermediate file
each source statement together with its assigned address, error indicators, etc.
This file is used as the input to Pass 2. This file retains some results of operations performed during Pass 1
the scanned operand field for symbols and addressing flags pointers into OPTAB and SYMTAB for each operation code and symbol used.
Basic assembler functions 23
LOCCTR
LOCCTR is a variable. LOCCTR is initialized to the beginning address specified in the START statement. After each source statement is processed, the length of the assembled instruction or data area to be generated is added to LOCCRT. When a label is reached, the current value of LOCCTR gives the address to be associated with that label.
24
12
25
26
13
27
14
Table processing
Searching in a table
Hash
29
15