You are on page 1of 15

Basic Assembler Functions

Function of an assembler
Assembly language program Machine language and other information for the loader

Assembler

Database

Basic assembler functions

Fundamental assembler functions


Translate mnemonic operation codes their machine language equivalents. Assign machine addresses to symbolic labels used by the programmer.

The features and design of an assembler depend heavily on


the source language it translates and the machine language it produces. e.g., the instruction format and addressing modes
Basic assembler functions 3

Assembler directives

The assembler can also process assembler directives.

Assembler directives (or pseudo-instructions)

provide instructions to the assembler itself. They are not translated into machine instructions.

Basic assembler functions

Assembler directives (contd)


The SIC assembler language has the following assembler directives.

START END BYTE constant WORD RESB RESW

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

Basic assembler functions

Example of a SIC assembler language program


a forward reference: a reference to a label that is defined later in the program

Basic assembler functions

Example of a SIC assembler language program (contd)


Lines beginning with . contain comments only.

Basic assembler functions

Example of a SIC assembler language program (contd)

Basic assembler functions

Translation of source program to object code


Require to accomplish the following functions

Convert mnemonic operation codes to their machine language equivalents

e.g. translate STL to 14 process assembler directives e.g. translate RETADR to 1033 handle forward references

Convert symbolic operands to their equivalent machine addresses

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

e.g. translate EOF to 454F46 Object program format


Basic assembler functions 9

Write the object program and the assembly listing

Program with object codes


STL

RETADR

Basic assembler functions

10 a large memory space

Program with object codes (contd)

Basic assembler functions

11

Program with object codes (contd)

Basic assembler functions

12

Format of SIC object program


Header record: Col. 1 Col. 2-7 Col. 8-13 Col. 14-19 Text record: Col. 1 Col. 2-7 Col. 8-9 Col. 10-69 End record: Col. 1 Col. 2-7 E Address of first executable instruction in object program (hex)
Basic assembler functions 13

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

A simple two-pass assembler


Pass 1 (define symbols)

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.

Pass 2 (assemble instructions and generate object program)


Assemble instructions.

translate operation codes look up addresses

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.

Basic assembler functions

15

Assembler Algorithm and Data Structures

Internal data structures


the Operation Code Table (OPTAB)

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.

the Symbol Table (SYMTAB) a Location Counter (LOCCTR)

Basic assembler functions

17

Internal data structures (contd)


Intermediate file

Source Program

Pass 1 of assembler

Pass 2 of assembler

Object Program

OPTAB LOCCTR SYMTAB


Basic assembler functions 18

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.

Basic assembler functions

19

OPTAB (contd)
class optab_entry { public: char mnemonic[6]; char opcode; };

Basic assembler functions

20

10

SYMTAB

SYMTAB includes the name and value (address) for each label in the source program together with flags to indicate error conditions.

e.g., a symbol defined in two different places

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.

the label is the key of SYMTAB.

non-random key

Basic assembler functions

21

SYMTAB (contd)
class symtab_entry { public: char symbol[8]; int }; address;

Basic assembler functions

22

11

Intermediate file

Pass 1 usually generates an intermediate file that contains

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.

Basic assembler functions

24

12

Algorithm for Pass 1

Basic assembler functions

25

Algorithm for Pass 1 (contd)

Basic assembler functions

26

13

Algorithm for Pass 2

Basic assembler functions

27

Algorithm for Pass 2 (contd)

trace an object program


Basic assembler functions 28

14

Table processing

Searching in a table

Key: symbol name Methods:


the table Binary search:

Linear search: compare exhaustively every entry in


ordered table sorting

Hash

Basic assembler functions

29

15

You might also like