36
.K. WAGH POLYTECHNIC, NASHIK-03 DEPARTMENT OF COMPUTER TECHNOLOGY Second Shift) Chapter-02 Assembler

Spr ch-02

Embed Size (px)

Citation preview

Page 1: Spr ch-02

K.K. WAGH POLYTECHNIC, NASHIK-03 DEPARTMENT OF COMPUTER TECHNOLOGY

(Second Shift)

Chapter-02

Assembler

Page 2: Spr ch-02

An Assembly language is the lowest level programming language for a computer and machine dependent .

Assembly language provides the following three basic features which make programming a lot easier than in machine language.

1. Mnemonic Operation Codes.2. Symbolic Operand Specification.3. Declaration of data/storage area:

Assembly Language

Page 3: Spr ch-02

1.Mnemonic Operation Codes.

Instead of using numeric opcodes (i.e. pattern of 0 & 1), mnemonics are used e.g. ADD, SUB etc.

2. Symbolic Operand Specification.Symbolic names can be associated with

data or instructions . Operands can be specified in the form of symbolic references, rather than as machine addresses of data or instructions.

Assembly Language

Page 4: Spr ch-02

3. Declaration of data/storage area:

Data can declared using the decimal notation. This avoids the manual conversion of constants into their internal machine representation.e.g 5 into (0101)2

10.5 into (41A80000)16

Assembly Language

Page 5: Spr ch-02

Assembly language consist of three kinds of statements

1. Imperative statementsImperative assembly language

statements indicates actions to be performed during the execution of the assembled program. Hence each imperative statement translates into machine instruction.General Instruction format is Label Opcode Operand [,Operand…]

Assembly Language

Page 6: Spr ch-02

Assembly Language- Imperative statements Following are the some instructions with opcode:InstructionOpcode

Assembly Mnemonic

Remark

00 STOP Stops program.

01 ADD First Operand is assumed to be in the accumulator.02 SUB

03 MULT

04 LOAD Load into accumulator

05 STORE Store accumulator into storage location.

06 TRANS Transfer control to address mentioned.

07 TRIM Transfer only if accumulator < 0

08 DIV Divide accumulator by storage location contents.

09 READ Read a card into storage location

10 PRINT Print storage location contents.

Page 7: Spr ch-02

InstructionOpcode

Assembly Mnemonic

Remark

11 LIR Load index register with last three digits of storage operand.

12 IIR Increment index register with last three digits of storage operand.

13 LOOP Decrement index register. If new contents >0 then same as TRANS.

Assembly Language- Imperative statements

Page 8: Spr ch-02

Assembly Language-Declarative statements 2. Declarative statements:

A declarative statement assembly language statement declares constants or storage areas in a program.e.g.

i) A DS 1 ii) G DS 200These statements simply declares a storage area of 1 word and block of 200 words respectively.

Constants are declared using Declare Constant (DC) statement.e.g. ONE DC ‘1’

Page 9: Spr ch-02

Assembly Language-Assembler Directives 3. Assembler Directives:

Assembler Directives neither represent machine instructions instead of that they direct the assembler to take certain actions during the process of assembling a program.e.g. START 100This statement indicates that the first word of the object program generated by the assembler should be placed in the memory location with address ‘100’

ENDIndicates end of assembly language program

Page 10: Spr ch-02

Assembly Language-Program

Program to calculate factorial of a number:

START 100LIR 4,ALOAD ONESTORE

RESULTSTORE TERM

AGAIN LOAD RESULTMULT TERMSTORE RESULTLOAD TERMADD ONE STORE TERM

LOOP 4,AGAIN PRINT RESULT STOP A DS 1 ONE DC ‘1’ TERM DS 1 RESULT DS 1

END

Page 11: Spr ch-02

The Process of Translation The general model for the translation process can be represented as follows:

Analysis of Synthesis of Translation from Source Source Text + Source Text = to Target Text

Analysis Phase:

In the analysis phase , we are concerned with the determination of meaning of a source language text, for that we should know the grammar of source language . Further we should also know how to determine the meaning of a statement once its grammatical structure becomes known. The rules of grammar and rules of meanings are known as syntax and semantics of language respectively.

Page 12: Spr ch-02

The Process of Translation Synthesis Phase:

In the synthesis phase , the source language statements are replaced by target language statements.

Page 13: Spr ch-02

General Design Procedure of Assembler Following fig. shows the general procedure for translation of assembly program

Which consist of two phases:

1. Analysis Phase.2. Synthesis Phase.

Assembly

Language

Program

Equivalent Target

Code

Analysis

Phase

Synthesis

Phase

Page 14: Spr ch-02

General Design Procedure of Assembler1. Analysis Phase.

In this phase following functions are performed.

i. Isolate the label, mnemonic operation code and operand fields of a statement

ii. Enter the symbol found in label field (if any) and its address into Symbol Table.

iii. Validate the mnemonic operation code by looking it up in the Mnemonic Table.

iv. Determine the storage requirements of the statement by considering the mnemonic operation code and operand fields of the statement. Calculate the address of the first machine word following the target code generated for this statement (Location Counter processing).

Page 15: Spr ch-02

General Design Procedure of Assembler2. Synthesis Phase.

In this phase following functions are performed.

i. Obtain the machine operation code corresponding to the mnemonic operation code by searching the Mnemonic table.

ii. Obtain address of the operand from Symbol table.

iii. Synthesis the machine instruction or the machine form of the constant, as the case may be.

Page 16: Spr ch-02

Some Assembler Directives1. ORIGIN

This directive sets the location counter to the given address.e.g. ORIGIN 200

sets the location counter(LC) value to 200.

2. EQUThe EQU statement simply defines a new

symbol and gives it the value indicated by operand expressions.e.g. FIRTST EQU LAST

Page 17: Spr ch-02

Some Assembler Directives3. LTORG

While assembling, a reference to literal, the following care should be taken:i ) Allocation of machine location to contain the value of the literal during execution and

ii) Use of the address of this location as the operand address in the statement referencing the literal.

At every LTORG , statement assembler allocates all literals used in the program .

Page 18: Spr ch-02

Program to calculate factorial of a number:

START 100

LOAD =‘5’STORE ALOAD =‘1’STORE ONESTORE RESULTSTORE TERM

AGAIN LOAD RESULTMULT TERMSTORE RESULTLOAD TERMADD =‘1’ STORE TERM

LOOP 4,AGAIN PRINT RESULT STOP LTORG

=‘5’ =‘1’ A DS 1 ONE DC ‘1’ TERM DS 1 RESULT DS 1

END =‘1’

Some Assembler Directives

Page 19: Spr ch-02

Types of Assembler1. Multi-pass Assembler2. Single-pass Assembler

An Assembler pass is one complete scan of the source program input an assembler.

1.Single-pass Assembler:- Single-pass assembler have the advantage that every source statement processed only once.

- Single pass assembler would face a problem while translating forward references.

- This problem can be solved as below:

Page 20: Spr ch-02

Types of Assembler- Instructions containing forward references

can be left incomplete until address of the referenced symbol becomes known. These incomplete instructions are placed into a table called as Table of Incomplete Instructions (TII).

- On encountering its definition, its address can be filled into that instruction.

Page 21: Spr ch-02

Types of Assembler2. Multi-pass Assembler- Multi pass assemble resolves the problem of forward reference by using more than one passes.- In first pass analysis is takes place in which LC processing is performed and symbols defined in the program are entered into the symbol table.- During the second pass, statements are processed or synthesized to generate machine instructions.Assembly

Language Program

Equivalent Target

Code

Pass I

Pass IIIntermediat

e Code

Page 22: Spr ch-02

Data Structures Used in Pass-I of an AssemblerFollowing Data Structures are used in Pass-I of an Assembler:

1. OPTAB (Opcode Table)2. SYMTAB ( Symbol Table)3. LITTAB (Literal Table)

Page 23: Spr ch-02

Data Structures Used in Pass-I of an Assembler1. OPTAB (Opcode Table)

Mnemonic Opcode

Class Machine Opcode/Routine ID

Length

LOAD 1 (Imperative) 04 1

DS 2 (Declarative) R#7 -

START 3 ( Directive) R#11 -

STORE 1 (Imperative) 05 1

Page 24: Spr ch-02

Data Structures Used in Pass-I of an Assembler2. SYMTAB (Symbol Table)

Symbol Address Length Other Information

A 110 1 -

Page 25: Spr ch-02

Literal Address

=‘5’ 110

=‘1’ 112

=‘1’ 120

Data Structures Used in Pass-I of an Assembler3. LITTAB (Literal Table)

POOL TAB

Next Free entryCurrent Pool Pointer

Page 26: Spr ch-02

Pass 1 Pass 2 Intermediate

fileObject codes

Sourceprogram

OPTAB SYMTAB SYMTAB

Design of Two Pass Assembler

Pass-I

-Separate the symbol, mnemonic opcode and operand fields.

-Determine the storage required for every assembly language statement and update the location counter.

- Build the symbol table.

- Construct intermediate code.

Pass-II- Synthesize the target code by processing intermediate code generated during Pass-I

Page 27: Spr ch-02

Pass-I of an Assembler

Page 28: Spr ch-02

START 100READ AREAD BLOAD ASUB ATRIM LARGEBPRINT ASTOP

LARGEB PRINT BSTOPEND FIRST DS 1A DS 1B DS 1

Page 29: Spr ch-02

Intermediate Code

Program to calculate factorial of a number:

START 100

READ ALIR 4,ALOAD =‘1’STORE RESULTSTORE TERM

AGAIN LOAD RESULTMULT TERMSTORE RESULTLOAD TERMADD ONE STORE TERM

LOOP 4,AGAIN PRINT RESULT STOP LTORG A DS 1 ONE DC ‘1’ TERM DS 1 RESULT DS 1

END =‘1’

Page 30: Spr ch-02

Intermediate Code:AD#5 100

09 S#111 04,S#104 L#105 S#205 S#3

04 S#203 S#305 S#204 S#301 S#405 S#513 04,S#06

10 S#2 00 AD#7

Page 31: Spr ch-02

Pass-II of an Assembler

Page 32: Spr ch-02

Hashing or Random Entry Search

To put elements in the order into the table (SYMTAB,LITTAB etc.) slows down the process.

A considerable improvement can be achieve by inserting element in random way.

The random entry- number K is generated from the key by using function h(k) called as hashing function.

There is probability that same random number may be generated for two or more keys such condition is called as collision.

Hashing function should be fast and reduced collision

Page 33: Spr ch-02

Hashing or Random Entry Search

Two popular hashing functions are as below:1. Multiplicative Method:

e= h(ki ) = (a . ki + b) mod m

where ki is the symbol(key) and (m-1) is the maximum integer number which the computer can store.

2. Divisive method:In divisive methods, the symbol (key)

is divided by N, (N is table size), and the entry number is called as

e=remainder of (ki /N) + 1

Page 34: Spr ch-02

Collision Handling Methods

1. Rehashing technique:In this technique ,when a collision occurs, a

new entry number is obtained for the colliding symbol by applying another hashing function.

Thus , if collision occurs on applying the hashing function hi (k), a function hi +1(k) is applied and a probe (entry) is made at hi +1(k) th

entry in the table .

Page 35: Spr ch-02

Collision Handling Methods

2. Overflow chaining technique:In this technique ,all the symbols (keys) which

encounter a collision at first hash are store in a separate table called as overflow table . All overflow table entries belonging to the same hash table entry are chained together.A pointer in the hash table points to be the first of these overflow entry.

Hash Table Overflow Table

Symbol Pointer

COUNT

MAX

VALUE

Symbol Pointer

NEXT1

TEMP

BIG

Page 36: Spr ch-02

Thank You