sysprog assembler

Post on 18-Apr-2015

29 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

Assembler

Sarmistha Neogy

Dept. of C. S. E.

Jadavpur University

2

Introduction

Basic assembler functions: Translating mnemonic operation codes to their

machine language equivalents Assigning machine addresses to symbolic labels Generating machine language program equivalent to

the input assembly language program

3

Assembler directive

Assembler directives are pseudo instructions They provide instructions to the assembler itself.

They are not translated into machine operation codes.

Examples: START, END, BYTE, WORD etc

4

Basic functions

Functions of an assembler: Convert mnemonic operation codes to their machine

language equivalents Convert symbolic operands to their equivalent

machine addresses Decide the proper instruction format Convert the data constants to internal machine

Representations Write the object program and the assembly listing

5

Types of assembler

Two pass - Performs the functions mentioned in two distinct passes

over the input program, intermediate file, etc. One pass – Performs the functions mentioned in one pass over the

input program. Difference between types of assemblers – functionality

in processing of labels (definition and usage)

6

Labels in assembly program

Forward reference – Label/item defined later in the program with respect

to its reference (as operand) in the program

Backward reference – Label/item defined earlier in the program with respect

to its reference (as operand) in the program

Problem lies with the forward reference

7

Data structures for assembler

OPTAB: operation code table SYMTAB: symbol table LOCCTR: location counter

8

Data structures contd..

OPTAB: Mnemonic operation codes ⇔ Machine code

Contain instruction format and length LOCCTR ← LOCCTR + (instruction length) Implementation of opcode table: It is a static table Array or hash table Usually use a hashtable (mnemonic opcode as key)

9

Data structures contd..

LOCCTR: Initialize to be the beginning address specified in the

“START” statement LOCCTR ← LOCCTR + (instruction length) The current value of LOCCTR gives the

address to the label encountered

10

Data structures contd..

SYMTAB: Label name ⇔ label address, type, length, flag To

indicate error condition Operations in SYMTAB Usually hash table The hash function should perform non-random key

(Ex: LOOP1, LOOP2, X, Y, Z)

11

Forward reference handling in 2-pass assembler

In SYMTAB:» The symbol name» The defining expression» The number of undefined symbols in the defining expression

The undefined symbol (marked with a flag *) associated with a list of symbols depend on this undefined symbol.

When a symbol is defined, we can recursively evaluate the symbol expressions depending on the newly defined symbol.

12

Two pass assembler

Pass 1 (define symbols, check validity etc) Initialize location counter, i.e., the starting address Scan input statement Search opcode table to find mnemonic If match occurs, advance location counter by the no. of

bytes mentioned as length of the current instruction, thus generating address for each machine instruction

Assign addresses to all statements in the program as mentioned above

13

2-pass assembler..

Save the addresses assigned to all labels for use in Pass 2 (symbol table)

Perform assembler directives, including those for address assignment, such as BYTE and RESW

Pass 2 (assemble instructions and generate object program):

Assemble instructions (generate machine code and look up addresses)

Generate data values defined by BYTE, WORD Perform processing of assembler directives not done

during Pass 1

14

2-pass assembler..

For instructions with forward reference, look up symbol table for substitution of label addresses

Generate error message if necessary Write the object program and the assembly listing

15

One pass assembler

Load-and-go assembler generates their object code in memory for immediate execution.

No object program is written out, no loader is needed.

It is useful in a system oriented toward program development and testing such that the efficiency of the assembly process is an important consideration.

Programs are re-assembled nearly every time they are run, efficiency of the assembly process is an important consideration.

16

1-pass assembler…

Problem is Forward reference Solution: eliminate forward referencing !! Not possible, so..

17

1-pass assembler..

Omits the operand address if the symbol has not yet been defined

Enters this undefined symbol into SYMTAB and indicates that it is undefined

Remember where it is referred

18

1-pass assembler..

When the definition for the symbol is encountered, inserts the address.

At the end of the program, reports error, if any.

19

1-pass assembler…

NOT the Load-and-Go type: If the operand contains an undefined symbol, use 0

as the address and Text record is written to the object program.

Forward references are handled like load-and-go assembler.

20

1-pass assembler…

When the definition of a symbol is encountered, the assembler generates another Text record

When loaded, the incorrect address 0 will be updated

21

Points to remember …

Assemblers may allow “the use of expressions as operand”

The assembler evaluates the expressions and produces a single operand address or value

Expressions may consist of Operator +,-,*,/ (division is usually defined to

produce an integer result) Individual terms: Constants, User-defined

symbols Special terms, e.g., *, the current value of LOCCTR

22

Some notes…

Immediate operands: The operand value is assembled as part of the machine instruction

23

Some notes…

Program block: Allow the generated machine instructions and data to

appear in the object program in a different/particular order» Separate blocks for storing code, data, stack, and larger

data block

Segments of code that are rearranged within a single object program unit

24

Some notes…

Control sections Segments of code that are translated into

independent object program unit (subroutines/logical subdivisions)

Can be loaded and relocated independently of the other control sections

Programmer can assemble, load, and manipulate each of the control sections separately

25

Points to remember..

Instructions in one control section may need to refer to instructions or data located in another section

External definition: Definition names symbols that are defined in this

control section and may be used by other sections External reference: Reference names symbols that are used in this

control section and defined elsewhere

26

Points to remember..

External symbol reference: Assembler may not have any idea about its

occurrence Assembler must generate information for each

external reference – WHY??

27

Points to remember..

In an object program : There may be DEFINE and REFER records (for

external symbols) There may be MODIFICATION record involving

modification of operand (can be addresses)

28

Acknowledgement

course.ipv6.club.tw/SP.941/sp2-4.pdf

top related