sysprog assembler

28
1 Assembler Sarmistha Neogy Dept. of C. S. E. Jadavpur University

Upload: tuhin-chakrabarty

Post on 18-Apr-2015

54 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: SysProg Assembler

1

Assembler

Sarmistha Neogy

Dept. of C. S. E.

Jadavpur University

Page 2: SysProg Assembler

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

Page 3: SysProg Assembler

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

Page 4: SysProg Assembler

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

Page 5: SysProg Assembler

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)

Page 6: SysProg Assembler

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

Page 7: SysProg Assembler

7

Data structures for assembler

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

Page 8: SysProg Assembler

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)

Page 9: SysProg Assembler

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

Page 10: SysProg Assembler

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)

Page 11: SysProg Assembler

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.

Page 12: SysProg Assembler

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

Page 13: SysProg Assembler

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

Page 14: SysProg Assembler

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

Page 15: SysProg Assembler

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.

Page 16: SysProg Assembler

16

1-pass assembler…

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

Page 17: SysProg Assembler

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

Page 18: SysProg Assembler

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.

Page 19: SysProg Assembler

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.

Page 20: SysProg 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

Page 21: SysProg Assembler

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

Page 22: SysProg Assembler

22

Some notes…

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

Page 23: SysProg Assembler

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

Page 24: SysProg Assembler

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

Page 25: SysProg Assembler

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

Page 26: SysProg Assembler

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??

Page 27: SysProg Assembler

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)

Page 28: SysProg Assembler

28

Acknowledgement

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