Text preview for : CSE-V-SYSTEMS SOFTWARE [10CS52]-NOTES.pdf part of B.E (Engineering) Computer Science (CSE) B.E (Engineering) » Computer Science (CSE) » Sem 5 » System Software (SS)



Back to : CSE-V-SYSTEMS SOFTWARE [1 | Home

SYSTEM SOFTWARE

10CS52

SYLLABUS
PART ­ A UNIT ­ 1 6 Hours Machine Architecture: Introduction, System Software and Machine Architecture, Simplified Instructional Computer (SIC) - SIC Machine Architecture, SIC/XE Machine Architecture, SIC Programming Examples.

UNIT ­ 5 6 Hours Editors and Debugging Systems: Text Editors - Overview of Editing Process, User Interface, Editor Structure, Interactive Debugging Systems - Debugging Functions and Capabilities, Relationship With Other Parts Of The System, User-Interface Criteria

CI
CITSTUDENTS.IN

UNIT ­ 6 8 Hours Macro Processor: Basic Macro Processor Functions - Macro Definitions and Expansion, Macro Processor Algorithm and Data Structures, Machine- Independent Macro Processor Features - Concatenation of Macro Parameters, Generation of Unique Labels, Conditional Macro Expansion, Keyword Macro Parameters, Macro Processor Design Options - Recursive Macro Expansion, General-Purpose Macro Processors, Macro Processing Within Language Translators, Implementation Examples - MASM Macro Processor, ANSI C Macro Processor.

TS

TU D

UNIT ­ 4 8 Hours Loaders and Linkers: Basic Loader Functions - Design of an Absolute Loader, A Simple Bootstrap Loader, Machine-Dependent Loader Features ­ Relocation, Program Linking, Algorithm and Data Structures for a Linking Loader; Machine-Independent Loader Features - Automatic Library Search, Loader Options, Loader Design Options - Linkage Editor, Dynamic Linkage, Bootstrap Loaders, Implementation Examples - MS-DOS Linker.

EN
PART ­ B

6 Hours UNIT ­ 3 Assemblers -2: Machine Independent Assembler Features ­ Literals,Symbol-Definition Statements, Expression, Program Blocks, Control Sections and Programming Linking, Assembler Design Operations - One- Pass Assembler, Multi-Pass Assembler, Implementation Examples ­ MASM Assembler.

TS .IN

6 Hours UNIT ­ 2 Assemblers -1: Basic Assembler Function - A Simple SIC Assembler, Assembler Algorithm and Data Structures, Machine Dependent Assembler Features - Instruction Formats & Addressing Modes, Program Relocation.

SYSTEM SOFTWARE

10CS52

6 Hours UNIT ­ 7 Lex and Yacc ­ 1: Lex and Yacc - The Simplest Lex Program, Recognizing Words With LEX, Symbol Tables, Grammars, Parser-Lexer Communication, The Parts of Speech Lexer, A YACC Parser, The Rules Section, Running LEX and YACC, LEX and Hand- Written Lexers, Using LEX ­ Regular Expression, Examples of Regular Expressions, A Word Counting Program, Parsing a Command Line. UNIT ­ 8 6 Hours Lex and Yacc - 2 : Using YACC ­ Grammars, Recursive Rules, Shift/Reduce Parsing, What YACC Cannot Parse, A YACC Parser - The Definition Section, The Rules Section, Symbol Values and Actions, The LEXER, Compiling and Running a Simple Parser, Arithmetic Expressions and Ambiguity, Variables and Typed Tokens. Text Books: 1. Leland.L.Beck: System Software, 3rd Edition, Pearson Education, 1997. (Chapters 1.1 to 1.3, 2 (except 2.5.2 and 2.5.3), 3 (except 3.5.2 and 3.5.3), 4 (except 4.4.3)) 2. John.R.Levine, Tony Mason and Doug Brown: Lex and Yacc, O'Reilly, SPD, 1998. (Chapters 1, 2 (Page 2-42), 3 (Page 51-65))

Reference Books: 1. D.M.Dhamdhere: System Programming and Operating Systems, 2nd Edition, Tata McGraw - Hill, 1999.

CI
CITSTUDENTS.IN

TS

TU D

EN

TS .IN

SYSTEM SOFTWARE

10CS52

TABLE OF CONTENTS
1. Machine Architecture 1.1 Introduction. 1.2 System Software and Machine Architecture. 1.3 Simplified Instructional Computer (SIC). 1.3.1 SIC Machine Architecture. 1.3.2 SIC Programming Examples. 1.3.3 SIC/XE Machine Architecture. 1.4 Recommended Questions 2. Assemblers -1 01 01 02 02 05 08 14

3. Assemblers -2

CI

4. Loaders and Linkers 68 70 73 74 75 77 87 92 92

4.1 Basic Loader Functions 4.1.1 Design of an Absolute Loader 4.1.2 A Simple Bootstrap Loader 4.2 Machine-Dependent Loader Features 4.2.1Relocation 4.2.2Program Linking 4.2.3 Algorithm and Data Structures for a Linking Loader 4.3 Machine-Independent Loader Features 4.3.1 Automatic Library Search

CITSTUDENTS.IN

TS

3.1 Machine Independent Assembler Features 3.1.1 Literals 3.1.2 Symbol-Definition Statements 3.1.3 Expression, Program Blocks 3.1.4 Control Sections and Programming Linking 3.2 Assembler Design Operations 3.2.1 One-Pass Assembler 3.2.2 Multi-Pass Assembler 3.3 Implementation Examples ­ MASM Assembler. 3.4 Recommended Questions

TU D

EN

2.1 Basic Assembler Function 2.1.1 A Simple SIC Assembler 2.1.2Assembler Algorithm and Data Structures 2.2 Machine Dependent Assembler Features 2.2.1 Instruction Formats & Addressing Modes 22.2. Program Relocation. 2.3 Recommended Questions

TS .IN

15 20 22 30 30 36 39

40 40 42 47 48 59 60 64 66 67

SYSTEM SOFTWARE 4.3.2 Loader Options 4.4 Loader Design Options 4.4.1 Linkage Editor 4.4.2 Dynamic Linkage 4.4.3Bootstrap Loaders 4.5Implementation Examples 4.5.1 MS-DOS Linker. 4.6 Recommended Questions 5. Editors and Debugging Systems 5.1 Text Editors 5.1.1 Overview of Editing Process 5.2.2 User Interface 5.2.3 Editor Structure 5.2 Interactive Debugging Systems 5.2.1Debugging Functions and Capabilities 5.2.2 Relationship with Other Parts of the System 5.2.3User-Interface Criteria 5.3 Recommended Questions 6. Macro Processor

10CS52 92 93 94 95 96 96 96 97

TS .IN

98 98 99 100 103 103 105 106

CI

6.1 Basic Macro Processor Functions 6.1.1 Macro Definitions and Expansion 6.1.2 Macro Processor Algorithm and Data Structures 6.2 Machines-Independent Macro Processor Features 6.2.1 Concatenation of Macro Parameters 6.2.2 Generation of Unique Labels 6.2.3 Conditional Macro Expansion 6.2.4 Keyword Macro Parameters 6.3 Macro Processor Design Options 6.3.1 Recursive Macro Expansion 6.3.2 General-Purpose Macro Processors 6.3.3 Macro Processing Within Language Translators 6.4 Implementation Examples 6.5 Recommended Questions

EN

108 108 111 118 118 120 122 127 129 129 132 132 133 134

7. Lex and Yacc ­ 1 7.1 Lex and Yacc 7.2 A YACC Parser 7.3 Using LEX 7.4 Recommended Questions 135 136 142 157

CITSTUDENTS.IN

TS

TU D

SYSTEM SOFTWARE 8. Lex and Yacc ­ 2 8.1 Using YACC 8.2 A YACC Parser 8.3 The LEXER 8.4 Compiling and Running a Simple Parser 8.5 Arithmetic Expressions and Ambiguity 8.6 Variables and Typed Tokens 8.7 Recommended Questions

10CS52

158 160 164 166 168 173 201

CI
CITSTUDENTS.IN

TS

TU D

EN

TS .IN

SYSTEM SOFTWARE

10CS52

UNIT - 1

MACHINE ARCHITECTURE
1.1 Introduction:
The Software is set of instructions or programs written to carry out certain task on digital computers. It is classified into system software and application software. System

Application software focuses on an application or problem to be solved. System software consists of a variety of programs that support the operation of a computer.

Examples for system software are Operating system, compiler, assembler, macro processor, loader or linker, debugger, text editor, database management systems (some of them) and, software engineering tools. These softwares make it possible for the user to focus on an application or other problem to be solved, without needing to know the details of how the machine works internally.

One characteristic in which most system software differs from application software is machine dependency.

System software supports operation and use of computer. Application software provides solution to a problem. Assembler translates mnemonic instructions into machine code. The instruction formats, addressing modes etc., are of direct concern in assembler design. Similarly,

CI

hardware characteristics as the number and type of registers and the machine instructions available. Operating systems are directly concerned with the management of nearly all of the resources of a computing system.

TS

Compilers must generate machine language code, taking into account such

TU D

1.2 System Software and Machine Architecture:

EN

TS .IN

software consists of a variety of programs that support the operation of a computer.

CITSTUDENTS.IN

1

SYSTEM SOFTWARE

10CS52

There are aspects of system software that do not directly depend upon the type of computing system, general design and logic of an assembler, general design and logic of a compiler and code optimization techniques, which are independent of target machines. Likewise, the process of linking together independently assembled subprograms does not usually depend on the computer being used.

Simplified Instructional Computer (SIC) is a hypothetical computer that includes the hardware features most often found on real machines. There are two versions of SIC, they are, standard model (SIC), and, extension version (SIC/XE) (extra equipment or extra expensive).

We discuss here the SIC machine architecture with respect to its Memory and Registers, Data Formats, Instruction Formats, Addressing Modes, Instruction Set, Input and Output Memory:

There are 215 bytes in the computer memory, that is 32,768 bytes. It uses Little memory contains 8-bit bytes. Registers:

CI

given in the following table.

TS

Endian format to store the numbers, 3 consecutive bytes form a word , each location in

There are five registers, each 24 bits in length. Their mnemonic, number and use are

TU D

EN

1.3.1 SIC Machine Architecture:

TS .IN

1.3 The Simplified Instructional Computer (SIC):

CITSTUDENTS.IN

2

SYSTEM SOFTWARE Mnemonic A X L PC SW Number 0 1 2 8 9 Data Formats: Use Accu mulator; used for arithmetic operations Index register; used for addressing Linka ge register; JSUB Progr am counter Statu s word, including CC

10CS52

Integers are stored as 24-bit binary numbers. 2s complement representation is used for negative values, characters are stored using their 8-bit ASCII codes.No floating-point hardware on the standard version of SIC. Instruction Formats: Opcode(8) x

All machine instructions on the standard version of SIC have the 24-bit format as shown above

Addressing Modes:

TS
Mode Indication x=0 x=1 Direct

CI
Indexed

TU D
TA = address

Target address calculation

TA = address + (x)

EN
Address (15)

TS .IN

CITSTUDENTS.IN

3

SYSTEM SOFTWARE

10CS52

There are two addressing modes available, which are as shown in the above table. Parentheses are used to indicate the contents of a register or a memory location. Instruction Set :

1. SIC provides, load and store instructions (LDA, LDX, STA, STX, etc.). Integer arithmetic operations: (ADD, SUB, MUL, DIV, etc.). 2. All arithmetic operations involve register A and a word in memory, with the result

3. COMP compares the value in register A with a word in memory, this instruction sets a condition code CC to indicate the result. There are conditional jump instructions: (JLT, JEQ, JGT), these instructions test the setting of CC and jump accordingly.

4. JSUB jumps to the subroutine placing the return address in register L, RSUB returns by jumping to the address contained in register L.



Input and Output:

rightmost 8 bits of register A (accumulator). The Test Device (TD) instruction tests whether the addressed device is ready to send or receive a byte of data. Read Data (RD), Write Data (WD) are used for reading or writing the data.

TS



Data movement and Storage Definition X­

LDA, STA, LDL, STL, LDX, STX ( A- Accumulator, L ­ Linkage Register,

Index Register), all uses3-byte word. LDCH, STCH associated with characters uses 1-byte.

CI


There are no memory-memory move instructions. Storage definitions are WORD - ONE-WORD CONSTANT RESW - ONE-WORD VARIABLE BYTE - ONE-BYTE CONSTANT RESB - ONE-BYTE VARIABLE

TU D

Input and Output are performed by transferring 1 byte at a time to or from the

EN

TS .IN

being left in the register. Two instructions are provided for subroutine linkage.

CITSTUDENTS.IN

4

SYSTEM SOFTWARE

10CS52

1.3.2Example Programs (SIC):
Example 1: Simple data and character movement operation LDA FIVE STA ALPHA LDCH STCH ALPHA FIVE CHARZ C1 RESW WORD BYTE CZ RESB 1 CHARZ C1 1 5

Example 2: Arithmetic operations LDA ALPHA ADD INCR SUB ONE

STA BETA ........

CI
ONE ALPHA BEETA INCR

TS
........ ........ WORD 1 RESW 1 RESW RESW 1 1

TU D

EN
5

CITSTUDENTS.IN

TS .IN

SYSTEM SOFTWARE
Example 3: Looping and Indexing operation

10CS52

LDX

ZERO

; X=0 ; LOAD A FROM STR1

MOVECH LDCH STR1, X STCH STR2, X TIX JLT . . . STR1 STR2 ZERO ELEVEN BYTE RESB WORD ELEVEN MOVECH ;

; STORE A TO STR2

C ,,HELLO WORLD 11 0

WORD 11

Example 4: Input and Output operation INLOOP TD INDEV : TEST INPUT DEVICE : LOOP UNTIL DEVICE IS READY : READ ONE BYTE INTO A : STORE A TO DATA

CI
RD . . OUTLP TD

TS
JEQ INLOOP INDEV STCH DATA OUTDEV

TU D
: TEST OUTPUT DEVICE

EN
6

CITSTUDENTS.IN

TS .IN

ADD 1 TO X, TEST

SYSTEM SOFTWARE
JEQ OUTLP : LOOP UNTIL DEVICE IS READY : LOAD DATA INTO A : WRITE A TO OUTPUT DEVICE

10CS52

LDCH DATA WD . . OUTDEV

OUTDEV BYTE DATA RESB

X ,,08

: OUTPUT DEVICE NUMBER

1: ONE-BYTE VARIABLE

Example 5: To transfer two hundred bytes of data from input device to memory LDX ZERO CLOOP TD JEQ INDEV CLOOP RD INDEV

STCH RECORD, X TIX JLT . . B200

CI
INDEV BYTE RECORD RESB ZERO B200 WORD WORD

TS
CLOOP X ,,F5 200 0 200

TU D

EN
7

CITSTUDENTS.IN

TS .IN

INDEV

BYTE

X ,,F5

: INPUT DEVICE NUMBER

SYSTEM SOFTWARE

10CS52

1.3.3SIC/XE Machine Architecture:
Memory Maximum memory available on a SIC/XE system is 1 Megabyte (220 bytes). Registers Additional B, S, T, and F registers are provided by SIC/XE, in addition to the

Mnemonic B S T F

Number 3 4 5 6

Special use

Base register



Floating-point data type:

TS
1 s 11

There is a 48-bit floating-point data type, F*2(e-1024) 36 fraction

CI


Instruction Formats:

The new set of instruction formats fro SIC/XE machine architecture are as follows. Format 1 (1 byte): contains only operation code (straight from table).

TU D
exponent

EN

General working register General working register Floating-point accumulator (48 bits)

TS .IN
8

registers of SIC.

CITSTUDENTS.IN

SYSTEM SOFTWARE

10CS52

Format 2 (2 bytes): first eight bits for operation code, next four for register 1 and following four for register 2. The numbers for the registers go according to the numbers indicated at the registers section (ie, register T is replaced by hex 5, F is replaced by hex 6).



Format 3 (3 bytes): First 6 bits contain operation code, next 6 bits contain flags, last 12 bits contain displacement for the address of the operand. Operation code uses only 6 bits, thus the second hex digit will be affected by the values of the first two flags (n

next section. The last flag e indicates the instruction format (0 for 3 and 1 for 4). Format 4 (4 bytes): same as format 3 with an extra 2 hex digits (8 bits) for addresses that require more than 12 bits to be represented.

8 op

Format 2 (2 bytes) 8 op

CI
Format 3 (3 bytes) 6 op

Formats 1 and 2 are instructions do not reference memory at all

TS
r1 1 n i

TU D
4 4 r2 1 1 1 1 1 12 x b p e disp

EN
9

Format 1 (1 byte)

CITSTUDENTS.IN

TS .IN

and i). The flags, in order, are: n, i, x, b, p, and e. Its functionality is explained in the

SYSTEM SOFTWARE
Format 4 (4 bytes) 6 op 1 n 1 1 1 1 1 20 i x b p e address

10CS52



Addressing modes & Flag Bits Five possible addressing modes plus the combinations are as follows.

1. Direct (x, b, and p all set to 0):

operand address goes as it is. n and i are both set to

the same value, either 0 or 1. While in general that value is 1, if set to 0 for format 3 we can assume that the rest of the flags (x, b, p, and e) are used as a part of the address of the operand, to make the format compatible to the SIC format.

should be added to the current value stored at the B register (if b = 1) or to the value stored at the PC register (if p = 1)

3. Immediate(i = 1, n = 0): The operand value is already enclosed on the instruction (ie. lies on the last 12/20 bits of the instruction)

CI
immediate.

5. Indexed (x = 1): value to be added to the value stored at the register x to obtain real address of the operand. This can be combined with any of the previous modes except

e - > e = 0 means format 3, e = 1 means format 4
CITSTUDENTS.IN

TS

4. Indirect(i = 0, n = 1): The operand value points to an address that holds the address for the operand value.

The various flag bits used in the above formats have the following meanings

TU D

EN

2. Relative (either b or p equal to 1 and the other one to 0): the address of the operand

TS .IN

10

SYSTEM SOFTWARE

10CS52

Bits x,b,p : Used to calculate the target address using relative, direct, and indexed addressing Modes. Bits i and n: Says, how to use the target address b and p - both set to 0, disp field from format 3 instruction is taken to be the target address. For a format 4 bits b and p are normally set to 0, 20 bit address is the target address

x - x is set to 1, X register value is added for target address calculation

i=1, n=0 Immediate addressing, TA: TA is used as the operand value, no memory reference i=0, n=1 Indirect addressing, ((TA)): The word at the TA is fetched. Value of TA is taken as the address of the operand value

i=0, n=0 or i=1, n=1 Simple addressing, (TA):TA is taken as the address of the operand value Two new relative addressing modes are available for use with instructions assembled using

Mode

Base relative

TS
Instruction Set:

Program-counter relative

CI


SIC/XE provides all of the instructions that are available on the standard version. In addition we have, Instructions to load and store the new registers LDB, STB, etc, Floatingpoint arithmetic operations, ADDF, SUBF, MULF, DIVF, Register move instruction : RMO,
CITSTUDENTS.IN

TU D
Indication b=1,p=0 b=0,p=1

format 3.

EN
TA=(B)+ disp (0disp 4095) TA=(PC)+ disp (-2048disp 2047)

Target address calculation

TS .IN

11

SYSTEM SOFTWARE

10CS52

Register-to-register arithmetic operations, ADDR, SUBR, MULR, DIVR and, Supervisor call instruction : SVC. Input and Output: There are I/O channels that can be used to perform input and output while the CPU is executing other instructions. Allows overlap of computing and I/O, resulting in more efficient system operation. The instructions SIO, TIO, and HIO are used to start, test and halt

Example Programs (SIC/XE)

Example 1: Simple data and character movement operation

STA LDA STCH

ALPHA #90 C1

TS
ALPHA C1 RESW RESB

CI
LDS INCR

Example 2: Arithmetic operations

LDA ALPHA

TU D
. . 1 1

EN
12

LDA

#5

CITSTUDENTS.IN

TS .IN

the operation of I/O channels.

SYSTEM SOFTWARE
ADD S,A SUB #1 STA BETA ............. ..............

10CS52

BETA INCR

RESW 1 RESW 1

Example 3: Looping and Indexing operation

LDT LDX #0

#11 : X=0

MOVECH

LDCH STR1, X

STCH STR2, X

TS
TIXR JLT T

CI
STR1 STR2

.......... .......... .........

BYTE RESB

TU D
MOVECH C ,,HELLO WORLD 11

: LOAD A FROM STR1

: STORE A TO STR2

: ADD 1 TO X, TEST (T)

EN
13

CITSTUDENTS.IN

TS .IN

ALPHA RESW 1

SYSTEM SOFTWARE
RECOMMENDED QUESTIONS:

10CS52

1. Bring out the differences b/w System software and Application software.( 5) 2. Give the SIC machine architecture with all options? (10) 3. Suppose alpha is an array of 100 words. Write a sequence of instructions for SIC\XE to set all 100 elements to 0. (6) 4. Write a sequence of instructions for SIC to clear a 20 byte string to all blanks.(6) 5. Give the machine architecture of SIC/XE? (10)

6. With an example, explain simple I/O operation of SIC/XE? (5)

CI

TS

TU D

EN
CITSTUDENTS.IN

TS .IN
14

SYSTEM SOFTWARE

10CS52

CHAPTER -2

ASSEMBLERS-1
2.1 Basic Assembler Functions:
The basic assembler functions are:



Assigning machine addresses to symbolic labels.

SOURCE PROGRAM

ASSEMBLER

· The design of assembler can be to perform the following: ­ ­ ­ ­ ­ Scanning (tokenizing)

Parsing (validating the instructions) Creating the symbol table

Resolving the forward references



TS

Converting into the machine language

SIC Assembler Directive: ­ ­ START: Specify name & starting address.

CI

END: End of the program, specify the first execution instruction. BYTE, WORD, RESB, RESW End of record: a null char(00)

­

­

End of file: a zero length record · The design of assembler in other words: ­ Convert mnemonic operation codes to their machine language equivalents

TU D

EN
15

CITSTUDENTS.IN

TS .IN
OBJECT CODE



Translating mnemonic language code to its equivalent object code.

SYSTEM SOFTWARE ­ ­ ­ Convert symbolic operands to their equivalent machine addresses

10CS52

Decide the proper instruction format Convert the data constants to internal machine representations Write the object program and the assembly listing

So for the design of the assembler we need to concentrate on the machine architecture of the SIC/XE machine. We need to identify the algorithms and the various data structures to be used. According to the above required steps for assembling the assembler also has to handle assembler directives, these do not generate the object code but directs the assembler to perform certain operation. These directives are: The assembler design can be done: Single pass assembler Multi-pass assembler

In this case the whole process of scanning, parsing, and object code conversion is done in single pass. The only problem with this method is resolving forward reference. This is shown with an example below: 10 ---

TS
1000 1033

TU D
FIRST STL RETADR RESW

Single-pass Assembler:

EN
RETADR 141033 1

CI
--95

TS .IN

CITSTUDENTS.IN

16

SYSTEM SOFTWARE

10CS52

In the above example in line number 10 the instruction STL will store the linkage register with the contents of RETADR. But during the processing of this instruction the value of this symbol is not known as it is defined at the line number 95. Since I single-pass assembler the scanning, parsing and object code conversion happens simultaneously. The instruction is fetched; it is scanned for tokens, parsed for syntax and semantic validity. If it valid then it has to be converted to its equivalent object code. For this the object code is generated for the opcode STL and the value for the symbol RETADR need to be added,

Due to this reason usually the design is done in two passes. So a multi-pass assembler resolves the forward references and then converts into the object code. Hence the process of the multi-pass assembler can be as follows: Pass-1 Assign addresses to all the statements

Save the addresses assigned to all labels to be used in Pass-2 Perform some processing of assembler directives such as RESW, RESB to find the length of data areas for assigning the address values. Defines the symbols in the symbol table(generate the symbol table)

Pass-2



CI

Assembler Design: The most important things which need to be concentrated is the generation of Symbol table and resolving forward references.

TS

Assemble the instructions (translating operation codes and looking up addresses). Generate data values defined by BYTE, WORD etc. Perform the processing of the assembler directives not done during pass-1. Write the object program and assembler listing.

TU D

EN

TS .IN

which is not available.

CITSTUDENTS.IN

17

SYSTEM SOFTWARE · Symbol Table: ­ ­ ­ · ­ ­ This is created during pass 1 All the labels of the instructions are symbols Table has entry for symbol name, address value.

10CS52

Forward reference: Symbols that are defined in the later part of the program are called forward referencing.

pass 1.

Example Program:

The example program considered here has a main module, two subroutines · Purpose of example program

- Reads records from input device (code F1)

- At the end of the file, writes EOF on the output device, then RSUB to the operating system ·

Data transfer (RD, WD)

CI


TS
-RDREC, WRREC

-A buffer is used to store record -Buffering is necessary for different I/O rates -The end of each record is marked with a null character (00)16 -The end of the file is indicated by a zero-length record

Subroutines (JSUB, RSUB)

-Save link register first before nested jump

TU D

- Copies them to output device (code 05)

EN
18

CITSTUDENTS.IN

TS .IN

There will not be any address value for such symbols in the symbol table in

SYSTEM SOFTWARE

10CS52

addresses allocated to each instruction. The third column indicates the labels given to the statement, and is followed by the instruction consisting of opcode and operand. The last

The object code later will be loaded into memory for execution. The simple object program we use contains three types of records: · Header record - Col. 1 H

- Col. 2~7 Program name

CI
· Text record - Col. 1 T

- Col. 8~13 Starting address of object program (hex)

- Col. 14~19 Length of object program in bytes (hex)

- Col. 2~7 Starting address for object code in this record (hex)

TS

TU D

column gives the equivalent object code.

EN

The first column shows the line number for that instruction, second column shows the

TS .IN

CITSTUDENTS.IN

19

SYSTEM SOFTWARE - Col. 8~9 Length of object code in this record in bytes (hex) - Col. 10~69 Object code, represented in hex (2 col. per byte) · End record - Col.1 E

10CS52

- Col.2~7 Address of first executable instruction in object program (hex) "^" is only for separation only

2.1.1 Simple SIC Assembler

The program below is shown with the object code generated. The column named LOC gives the machine addresses of each part of the assembled program (assuming the program is starting at location 1000). The translation of the source program to the object program requires us to accomplish the following functions:

1. Convert the mnemonic operation codes to their machine language equivalent.

3. Build the machine instructions in the proper format. 4. Convert the data constants specified in the source program into their internal machine representations in the proper format. 5. Write the object program and assembly listing.

All these steps except the second can be performed by sequential processing of the source

CI
10 1000

program, one line at a time. Consider the instruction LDA ALPHA 00-----

yet defined. If the program is processed ( scanning and parsing and object code conversion) is done line-by-line, we will be unable to resolve the address of this symbol. Due to this problem most of the assemblers are designed to process the program in two passes.

TS

This instruction contains the forward reference, i.e. the symbol ALPHA is used is not

TU D

2. Convert symbolic operands to their equivalent machine addresses.

EN

TS .IN

CITSTUDENTS.IN

20

SYSTEM SOFTWARE

10CS52

In addition to the translation to object program, the assembler has to take care of handling assembler directive. These directives do not have object conversion but gives direction to the assembler to perform some function. Examples of directives are the statements like BYTE and WORD, which directs the assembler to reserve memory locations without generating data values. The other directives are START which indicates the beginning of the program and END indicating the end of the program. The assembled program will be loaded into memory for execution. The simple object program contains three types of records: Header record, Text record and end record. The header record contains the starting address and length. Text record contains the translated instructions and data of the program, together with an indication of the addresses where these are to be loaded. The end record marks the end of the object program and specifies the address where the execution is to begin. The format of each record is as given below. Header record: Col 1 Col. 2-7 Col 8-13 Col 14-19 Text record: Col. 1 H

CI
Col 2-7. Col 8-9 Col 10-69

TS
T Starting address for object code in this record (hexadecimal) Length off object code in this record in bytes (hexadecimal) Object code, represented in hexadecimal (2 columns per byte of object code)

TU D
Program name

Starting address of object program (hexadecimal) Length of object program in bytes (hexadecimal)

EN
21

CITSTUDENTS.IN

TS .IN

SYSTEM SOFTWARE End record: Col. 1 Col 2-7 E Address of first executable instruction in object program (hexadecimal)

10CS52

The assembler can be designed either as a single pass assembler or as a two pass

·

Pass 1 (define symbols) ­ ­ ­

Assign addresses to all statements in the program

Save the addresses assigned to all labels for use in Pass 2

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

·

Pass 2 (assemble instructions and generate object program) ­ ­ ­ ­ Assemble instructions (generate opcode and look up addresses) Generate data values defined by BYTE, WORD Perform processing of assembler directives not done during Pass 1 Write the object program and the assemblylisting

2.1.2. Algorithms and Data structure

Table (OPTAB) and the Symbol Table (SYMTAB).

CI


OPTAB:

TS

The simple assembler uses two major internal data structures: the operation Code

It is used to lookup mnemonic operation codes and translates them to their machine

language equivalents. In more complex assemblers the table also contains information about instruction format and length.

TU D

EN

TS .IN

assembler. The general description of both passes is as given below:

CITSTUDENTS.IN

22

SYSTEM SOFTWARE

10CS52

In pass 1 the OPTAB is used to look up and validate the operation code in the source program. In pass 2, it is used to translate the operation codes to machine language. In simple SIC machine this process can be performed in either in pass 1 or in pass 2. But for machine like SIC/XE that has instructions of different lengths, we must search OPTAB in the first pass to find the instruction length for incrementing LOCCTR.



In pass 2 we take the information from OPTAB to tell us which instruction format to use in assembling the instruction, and any peculiarities of the object code instruction.



OPTAB is usually organized as a hash table, with mnemonic operation code as the key. The hash table organization is particularly appropriate, since it provides fast

table- that is, entries are not normally added to or deleted from it. In such cases it is possible to design a special hashing function or other data structure to give optimum

SYMTAB:

This table includes the name and value for each label in the source program, together with flags to indicate the error conditions (e.g., if a symbol is defined in two different places).



CI


TS
optimization.

During Pass 1: labels are entered into the symbol table along with their assigned address value as they are encountered. All the symbols address value should get resolved at the pass 1.

During Pass 2: Symbols used as operands are looked up the symbol table to obtain the address value to be inserted in the assembled instructions. SYMTAB is usually organized as a hash table for efficiency of insertion and retrieval. Since entries are rarely deleted, efficiency of deletion is the important criteria for

TU D

performance for the particular set of keys being stored.

EN

retrieval with a minimum of searching. Most of the cases the OPTAB is a static

TS .IN

CITSTUDENTS.IN

23

SYSTEM SOFTWARE

10CS52

Both pass 1 and pass 2 require reading the source program. Apart from this an intermediate file is created by pass 1 that contains each source statement together with its assigned address, error indicators, etc. This file is one of the inputs to the pass 2.



A copy of the source program is also an input to the pass 2, which is used to retain the operations that may be performed during pass 1 (such as scanning the operation field for symbols and addressing flags), so that these need not be performed during pass 2.

and symbol used. This avoids need to repeat many of the table-searching operations.

LOCCTR:

Apart from the SYMTAB and OPTAB, this is another important variable which helps in the

the START statement of the program. After each statement is processed, the length of the assembled instruction is added to the LOCCTR to make it point to the next instruction.

associated with that label.

The Algorithm for Pass 1: Begin

read first input line

if OPCODE = ,,START then begin

CI
read next line

save #[Operand] as starting addr initialize LOCCTR to starting address write line to intermediate file

end( if START)
CITSTUDENTS.IN

TS

TU D

Whenever a label is encountered in an instruction the LOCCTR value gives the address to be

EN

assignment of the addresses. LOCCTR is initialized to the beginning address mentioned in

TS .IN

Similarly, pointers into OPTAB and SYMTAB is retained for each operation code

24

SYSTEM SOFTWARE else initialize LOCCTR to 0 While OPCODE != ,,END do begin if this is not a comment line then begin

10CS52

if there is a symbol in the LABEL field then begin search SYMTAB for LABEL if found then

set error flag (duplicate symbol) else

(if symbol)

search OPTAB for OPCODE

CI

TS

if found then

add 3 (instr length) to LOCCTR

else if OPCODE = ,,WORD then add 3 to LOCCTR

else if OPCODE = ,,RESW then add 3 * #[OPERAND] to LOCCTR else if OPCODE = ,,RESB then

TU D

EN
25

CITSTUDENTS.IN

TS .IN

SYSTEM SOFTWARE add #[OPERAND] to LOCCTR else if OPCODE = ,,BYTE then begin find length of constant in bytes add length to LOCCTR end else set error flag (invalid operation code) end (if not a comment) write line to intermediate file read next input line end { while not END}

10CS52

write last line to intermediate file

Save (LOCCTR ­ starting address) as program length



CI


TS

End {pass 1}

The algorithm scans the first statement START and saves the operand field (the

address) as the starting address of the program. Initializes the LOCCTR value to this address. This line is written to the intermediate line.

If no operand is mentioned the LOCCTR is initialized to zero. If a label is

encountered, the symbol has to be entered in the symbol table along with its associated address value. If the symbol already exists that indicates an entry of the same symbol already exists. So an error flag is set indicating a duplication of the symbol.

TU D

EN
26

CITSTUDENTS.IN

TS .IN

SYSTEM SOFTWARE

10CS52

It next checks for the mnemonic code, it searches for this code in the OPTAB. If found then the length of the instruction is added to the LOCCTR to make it point to the next instruction.



If the opcode is the directive WORD it adds a value 3 to the LOCCTR. If it is RESW, it needs to add the number of data word to the LOCCTR. If it is BYTE it adds a value one to the LOCCTR, if RESB it adds number of bytes.



If it is END directive then it is the end of the program it finds the length of the

operand field of the END directive. Each processed line is written to the intermediate file.

The Algorithm for Pass 2: Begin read 1st input line if OPCODE = ,,START then begin

write listing line

CI
begin begin
CITSTUDENTS.IN

while OPCODE != ,,END do

TS
end

read next input line

write Header record to object program

initialize 1st Text record

if this is not comment line then

TU D

EN
27

TS .IN

program by evaluating current LOCCTR ­ the starting address mentioned in the

SYSTEM SOFTWARE search OPTAB for OPCODE if found then begin if there is a symbol in OPERAND field then begin search SYMTAB for OPERAND field then if found then begin store symbol value as operand address else begin store 0 as operand address

10CS52

CI

TS
begin

convert constant to object code if object code doesnt fit into current Text record then

TU D
end end (if symbol)

set error flag (undefined symbol)

else store 0 as operand address assemble the object code instruction

else if OPCODE = ,,BYTE or ,,WORD" then

EN
28

CITSTUDENTS.IN

TS .IN

SYSTEM SOFTWARE Write text record to object code initialize new Text record

10CS52

end add object code to Text record end {if not comment} write listing line read next input line end write listing line read next input line write last listing line End {Pass 2}

Here the first input line is read from the intermediate file. If the opcode is START, then this line is directly written to the list file. A header record is written in the object program which gives the starting address and the length of the program (which is calculated during pass 1). Then the first text record is initialized. Comment lines are ignored. In the instruction, for the opcode the OPTAB is searched to find the object code.

CI

address value for this which gets added to the object code of the opcode. If the address not

found then zero value is stored as operands address. An error flag is set indicating it as undefined. If symbol itself is not found then store 0 as operand address and the object code instruction is assembled.

TS

If a symbol is there in the operand field, the symbol table is searched to get the

TU D

EN
29

CITSTUDENTS.IN

TS .IN

SYSTEM SOFTWARE

10CS52

If the opcode is BYTE or WORD, then the constant value is converted to its equivalent object code( for example, for character EOF, its equivalent hexadecimal value ,,454f46 is stored). If the object code cannot fit into the current text record, a new text record is created and the rest of the instructions object code is listed. The text records are written to the object program. Once the whole program is assemble and when the END directive is encountered, the End record is written. Design and Implementation Issues Some of the features in the program depend on the architecture of the machine. If the program is for SIC machine, then we have only limited instruction formats and hence limited addressing modes. We have only single operand instructions. The operand is always a memory reference. Anything to be fetched from memory requires more time. Hence the improved version of SIC/XE machine provides more instruction formats and hence more

number of instruction formats and the addressing modes changes. Therefore the design usually requires considering two things: Machine-dependent features and Machine-

2.2. Machine-Dependent Assembler Features:

CI

2.2.1 .Instruction formats and Addressing Modes
The instruction formats depend on the memory organization and the size of the memory.

In SIC machine the memory is byte addressable. Word size is 3 bytes. So the size of the memory is 212 bytes. Accordingly it supports only one instruction format. It has only two registers: register A and Index register. Therefore the addressing modes supported by this architecture are direct, indirect, and indexed. Whereas the memory of a SIC/XE machine is 220 bytes (1 MB). This supports four different types of instruction types, they are:
CITSTUDENTS.IN

TS
Program relocation.



Instruction formats and addressing modes

TU D

independent features.

EN

addressing modes. The moment we change the machine architecture the availability of

TS .IN

30

SYSTEM SOFTWARE 1 byte instruction 2 byte instruction 3 byte instruction 4 byte instruction

10CS52

·

Instructions can be: ­ ­ ­ Instructions involving register to register Instructions with one operand in memory, the other in Accumulator (Single operand instruction) Extended instruction format

·

Addressing Modes are: ­ ­ ­ ­ ­

Index Addressing(SIC): Opcode m, x Indirect Addressing: Opcode @m PC-relative: Opcode m

Base relative: Opcode m

Immediate addressing: Opcode #c

1. Translations for the Instruction involving Register-Register addressing mode:

During pass 1 the registers can be entered as part of the symbol table itself. The value for these registers is their equivalent numeric codes. During pass2, these values are assembled along with the mnemonics object code. If required a separate table can be created with the

CI

register names and their equivalent numeric values. 2. Translation involving Register-Memory instructions:

In SIC/XE machine there are four instruction formats and five addressing modes. For formats and addressing modes Among the instruction formats, format -3 and format-4 instructions are Register-Memory type of instruction. One of the operand is always in a register and the other operand is in the
CITSTUDENTS.IN

TS

TU D

EN
31

TS .IN

SYSTEM SOFTWARE

10CS52

memory. The addressing mode tells us the way in which the operand from the memory is to be fetched. There are two ways: Program-counter relative and Base-relative. This addressing mode can be represented by either using format-3 type or format-4 type of instruction format. In format-3, the instruction has the opcode followed by a 12-bit displacement value in the address field. Where as in format-4 the instruction contains the mnemonic code followed by a 20-bit displacement value in the address field.

Program-Counter Relative:

In this usually format-3 instruction format is used. The instruction contains the opcode followed by a 12-bit displacement value.

The range of displacement values are from 0 -2048. This displacement (should be small enough to fit in a 12-bit field) value is added to the current contents of the program counter to get the target address of the operand required by the instruction.

counter. Hence the displacement of the operand is relative to the current program counter value. The following example shows how the address is calculated:

CI

TS

TU D

This is relative way of calculating the address of the operand relative to the program

EN

TS .IN

CITSTUDENTS.IN

32

SYSTEM SOFTWARE

10CS52

Base-Relative Addressing Mode:

In this mode the base register is used to mention the displacement value. Therefore the target address is TA = (base) + displacement value

This addressing mode is used when the range of displacement value is not sufficient. Hence the operand is not relative to the instruction as in PC-relative addressing mode. Whenever this mode is used it is indicated by using a directive BASE.



The moment the assembler encounters this directive the next instruction uses baserelative addressing mode to calculate the target address of the operand. When NOBASE directive is used then it indicates the base register is no more used to calculate the target address of the operand. Assembler first chooses PC-relative,

CI
:

TS
NOBASE

when the displacement field is not enough it uses Base-relative.

LDB #LENGTH (instruction)

BASE LENGTH (directive)

TU D



EN
33

CITSTUDENTS.IN

TS .IN

SYSTEM SOFTWARE For example: 12 13 :: 100 105 :: 160 165 104E STCH 1051 TIXR BUFFER, T 0033 LENGTH 0036 BUFFER RESW RESB 1 0003 LDB BASE #LENGTH LENGTH 69202D

10CS52

In the above example the use of directive BASE indicates that Base-relative addressing mode is to be used to calculate the target address. PC-relative is no longer used. The value of the LENGTH is stored in the base register. If PC-relative is used then the target address



The LDB instruction loads the value of length in the base register which 0033. BASE directive explicitly tells the assembler that it has the value of LENGTH.

BUFFER is at location (0036)16 (B) = (0033)16

CI

disp = 0036 ­ 0033 = (0003)16

20

TS
000A

TU D
LDA

calculated is:

EN
LENGTH

TS .IN
4096 X 57C003 B850
032026

CITSTUDENTS.IN

34

SYSTEM SOFTWARE
:: 175 1056 EXIT STX LENGTH 134000

10CS52

Consider Line 175. If we use PC-relative Disp = TA ­ (PC) = 0033 ­1059 = EFDA PC relative is no longer applicable, so we try to use BASE relative addressing mode. Immediate Addressing Mode

In this mode no memory reference is involved. If immediate mode is used the target address is the operand itself.

If the symbol is referred in the instruction as the immediate operand then it is immediate with PC-relative mode as shown in the example below:

CI

TS

TU D

EN
CITSTUDENTS.IN

TS .IN

35

SYSTEM SOFTWARE Indirect and PC-relative mode:

10CS52

In this type of instruction the symbol used in the instruction is the address of the location which contains the address of the operand. The address of this is found using PC-relative addressing mode. For example:

The instruction jumps the control to the address location RETADR which in turn has the address of the operand. If address of RETADR is 0030, the target address is then 0003 as calculated above.

2.2.2Program Relocation

Sometimes it is required to load and run several programs at the same time. The system must be able to load these programs wherever there is place in the memory. Therefore the exact starting is not known until the load time. Absolute Program

In this the address is mentioned during assembling itself. This is called Absolute Assembly. Consider the instruction:

CI
55

TS

101B LDA THREE

This statement says that the register A is loaded with the value stored at location 102D. Suppose it is decided to load and execute the program at location 2000

instead of location 1000. Then at address 102D the required value which needs to be loaded in the register A is no more available. The address also gets changed relative to the displacement

TU D
00102D

EN
36

CITSTUDENTS.IN

TS .IN

SYSTEM SOFTWARE

10CS52

of the program. Hence we need to make some changes in the address portion of the instruction so that we can load and execute the program at location 2000. Apart from the instruction which will undergo a change in their operand address value as the program load address changes. There exist some parts in the program which will remain same regardless of where the program is being loaded. Since assembler will not know actual location where the program will get loaded, it cannot make the necessary changes in the addresses used in the program.

need modification.

An object program that has the information necessary to perform this kind of modification is called the relocatable program.

CI


TS
The above diagram shows the concept of relocation. Initially the program is loaded at location 0000. The instruction JSUB is loaded at location 0006. The address field of this instruction contains 01036, which is the address of the instruction labeled RDREC. The second figure shows that if the program is to be loaded at new location 5000.

TU D

EN
CITSTUDENTS.IN

TS .IN

However, the assembler identifies for the loader those parts of the program which

37

SYSTEM SOFTWARE

10CS52

The address of the instruction JSUB gets modified to new location 6036. Likewise the third figure shows that if the program is relocated at location 7420, the JSUB instruction would need to be changed to 4B108456 that correspond to the new address of RDREC.



The only part of the program that require modification at load time are those that specify direct addresses. The rest of the instructions need not be modified. The instructions which doesnt require modification are the ones that is not a memory



From the object program, it is not possible to distinguish the address and constant The assembler must keep some information to tell the loader.The object program that contains the modification record is called a relocatable program.



For an address label, its address is assigned relative to the start of the program (START 0). The assembler produces a Modification recordto store the starting location and the length of the address field to be modified. The command for the loader must also be a part of the object program. The Modification has the following format:

Modification record Col. 1 Col. 2-7 M

CI

Col. 8-9

One modification record is created for each address to be modified The length is stored in half-bytes (4 bits) The starting location is the location of the byte containing the leftmost bits of the address field to be modified. If the field contains an odd number of half-bytes, the starting location begins in the middle of the first byte.

TS

Starting location of the address field to be modified, relative to the

beginning of the program (Hex) Length of the address field to be modified, in half-bytes (Hex)

TU D

EN

TS .IN

address (immediate addressing) and PC-relative, Base-relative instructions.

CITSTUDENTS.IN

38

SYSTEM SOFTWARE

10CS52

In the above object code the red boxes indicate the addresses that need modifications. The object code lines at the end are the descriptions of the modification records for those instructions which need change if relocation occurs. M00000705 is the modification suggested for the statement at location 0007 and requires modification 5-half bytes. Similarly

RECOMMENDED QUESTIONS:

1. What are the fundamental functions of assembler? With an example, give the list of assembler directives?(6)

2. Explain the data structures used in Assemblers (8). 3. what is program relocation? Explain the problem associated with it and solutions? (6) 4. Give the format of the following (8)

CI
a. b. c.

5. Explain the function of each pass of an 2 pass assembler.(5) 6. Explain the following (8)
SYMTAB LOCCTR OPTAB

7. Give the algorithm for pass1 of an 2 pass assembler. (8) 8. Give the algorithm for pass2 of an 2 pass assembler (8)
CITSTUDENTS.IN

TS
b. Text record c. End record

a. Header record

d. Modification record

TU D

EN

the remaining instructions indicate.

TS .IN

39

SYSTEM SOFTWARE

10CS52

CHAPTER -3

Assembler-2
3.1 Machine-Independent features:
These are the features which do not depend on the architecture of the machine. These are: Literals

Program blocks Control sections

3.1.1 Literals:

Example: 45 93 002D * 001A ENDFIL

TS CI
215 1062 WLOOP

TU D
LTORG =CEOF TD =X05

LDA =CEOF

The example above shows a 3-byte operand whose value is a character string EOF.

The object code for the instruction is also mentioned. It shows the relative displacement

value of the location where this value is stored. In the example the value is at location (002D)

and hence the displacement value is (010). As another example the given statement below shows a 1-byte literal with the hexadecimal value ,,05. E32011

EN
032010 454F46

A literal is defined with a prefix = followed by a specification of the literal value.

TS .IN

Expressions

CITSTUDENTS.IN

40

SYSTEM SOFTWARE

10CS52

It is important to understand the difference between a constant defined as a literal and a constant defined as an immediate operand. In case of literals the assembler generates the specified value as a constant at some other memory location In immediate mode the operand value is assembled as part of the instruction itself. Example 55 0020 LDA #03 010003

All the literal operands used in a program are gathered together into one or more

program containing literals usually includes a listing of this literal pool, which shows the assigned addresses and the generated data values. In some cases it is placed at some other location in the object program. An assembler directive LTORG is used. Whenever the LTORG is encountered, it creates a literal pool that contains all the literal operands used since the beginning of the program. The literal pool definition is done after LTORG is

A literal table is created for the literals which are used in the program. The literal table contains the literal name, operand value and length. The literal table is usually created as a hash table on the literal name. Implementation of Literals: During Pass-1:

action is taken; if it is not present, the literal is added to the LITTAB and for the address value it waits till it encounters LTORG for literal definition. When Pass 1 encounters a

CI
literal.

LTORG statement or the end of the program, the assembler makes a scan of the literal table. At this time each literal currently in the table is assigned an address. As addresses are assigned, the location counter is updated to reflect the number of bytes occupied by each

TS

The literal encountered is searched in the literal table. If the literal already exists, no

TU D

EN

encountered. It is better to place the literals close to the instructions.

TS .IN

literal pools. This is usually placed at the end of the program. The assembly listing of a

CITSTUDENTS.IN

41

SYSTEM SOFTWARE During Pass-2:

10CS52

The assembler searches the LITTAB for each literal encountered in the instruction and replaces it with its equivalent value as if these values are generated by BYTE or WORD. If a literal represents an address in the program, the assembler must generate a modification relocation for, if it all it gets affected due to relocation. The following figure shows the difference between the SYMTAB and LITTAB

3.1.2 Symbol-Defining Statements: EQU Statement:

Most assemblers provide an assembler directive that allows the programmer to define symbols and specify their values. The directive used for this EQU (Equate). The general form of the statement is Symbol

TS
+LDT #4096

This statement defines the given symbol (i.e., entering in the SYMTAB) and assigning to it

CI

the value specified. The value can be a constant or an expression involving constants and any other symbol which is already defined. One common usage is to define symbolic names that

can be used to improve readability in place of numeric values. For example

TU D
EQU

EN
value

TS .IN
CITSTUDENTS.IN

42

SYSTEM SOFTWARE

10CS52

This loads the register T with immediate value 4096, this does not clearly what exactly this value indicates. If a statement is included as: MAXLEN EQU +LDT 4096 and then #MAXLEN

Then it clearly indicates that the value of MAXLEN is some maximum length value. When the assembler encounters EQU statement, it enters the symbol MAXLEN along with its value in the symbol table. During LDT the assembler searches the SYMTAB for its entry and its equivalent value as the operand in the instruction. The object code generated is the same for both the options discussed, but is easier to understand. If the maximum length is changed from 4096 to 1024, it is difficult to change if it is mentioned as an immediate value wherever required in the instructions. We have to scan the whole program and make changes wherever 4096 is used. If we mention this value in the instruction through the symbol defined by EQU, we may not have to search the whole program but change only the value of MAXLENGTH in the EQU statement (only once).

purpose registers. The assembler can use the mnemonics for register usage like a-register A , X ­ index register and so on. But there are some instructions which requires numbers in place of names in the instructions. For example in the instruction RMO 0,1 instead of RMO A,X. The programmer can assign the numerical values to these registers using EQU directive.

TS
A EQU X EQU

CI
EQU statements. BASE
CITSTUDENTS.IN

These statements will cause the symbols A, X, L... to be entered into the symbol

table with their respective values. An instruction RMO A, X would then be allowed. As another usage if in a machine that has many general purpose registers named as R1, R2,...,

some may be used as base register, some may be used as accumulator. Their usage may change from one program to another. In this case we can define these requirement using

TU D
0 EQU R1

Another common usage of EQU statement is for defining values for the general-

EN
1 and so on

TS .IN

43

SYSTEM SOFTWARE INDEX COUNT EQU EQU R2 R3

10CS52

One restriction with the usage of EQU is whatever symbol occurs in the right hand side of the EQU should be predefined. For example, the following statement is not valid: BETA ALPHA EQU RESW ALPHA

As the symbol ALPHA is assigned to BETA before it is defined. The value of ALPHA is not known. ORG Statement:

This directive can be used to indirectly assign values to the symbols. The directive is usually called ORG (for origin). Its general format is: ORG value

Where value is a constant or an expression involving constants and previously defined symbols. When this statement is encountered during assembly of a program, the assembler resets its location counter (LOCCTR) to the specified value. Since the values of symbols used as labels are taken from LOCCTR, the ORG statement will affect the values of all labels defined until the next ORG is encountered. ORG is used to control assignment storage in the object program. Sometimes altering the values may result in incorrect assembly. ORG can be useful in label definition. Suppose we need to define a symbol table with

CI
SYMBOL VALUE FLAG

the following structure: 6 Bytes 3 Bytes 2 Bytes

The table looks like the one given below.

TS

TU D

EN
44

CITSTUDENTS.IN

TS .IN

1

SYSTEM SOFTWARE

10CS52

The symbol field contains a 6-byte user-defined symbol; VALUE is a one-word representation of the value assigned to the symbol; FLAG is a 2-byte field specifies symbol type and other information. The space for the ttable can be reserved by the statement:

If we want to refer to the entries of the table using indexed addressing, place the offset value of the desired entry from the beginning of the table in the index register. To refer to the fields SYMBOL, VALUE, and FLAGS individually, we need to assign the values first as shown below:

TS
SYMBOL VALUE FLAGS EQU EQU

CI
statement: LDA

To retrieve the VALUE field from the table indicated by register X, we can write a

The same thing can also be done using ORG statement in the following way:
CITSTUDENTS.IN

TU D
STAB STAB+6 STAB+9 EQU VALUE, X

STAB

RESB

EN
1100

TS .IN

45

SYSTEM SOFTWARE STAB RESB ORG SYMBOL VALUE FLAG RESB RESW RESB ORG 1100 STAB 6 1 2 STAB+1100

10CS52

The first statement allocates 1100 bytes of memory assigned to label STAB. In the second statement the ORG statement initializes the location counter to the value of STAB. Now the LOCCTR points to STAB. The next three lines assign appropriate memory storage to each of SYMBOL, VALUE and FLAG symbols. The last ORG statement reinitializes the

(i.e., STAB+1100).

While using ORG, the symbol occurring in the statement should be predefined as is required in EQU statement. For example for the sequence of statements below: ORG BYTE1 BYTE2 BYTE3 ALPHA

RESB RESB

TS
RESB ORG RESB

CI
ALPHA

The sequence could not be processed as the symbol used to assign the new location counter value is not defined. In first pass, as the assembler would not know what value to assign to ALPHA, the other symbol in the next lines also could not be defined in the symbol table. This is a kind of problem of the forward reference.

TU D
1 1 1 1

EN

LOCCTR to a new value after skipping the required number of memory for the table STAB

TS .IN

CITSTUDENTS.IN

46

SYSTEM SOFTWARE 3.1.3 .Expressions:

10CS52

Assemblers also allow use of expressions in place of operands in the instruction. Each such expression must be evaluated to generate a single operand value or address. Assemblers generally arithmetic expressions formed according to the normal rules using arithmetic operators +, - *, /. Division is usually defined to produce an integer result. Individual terms may be constants, user-defined symbols, or special terms. The only special term used is * ( the current value of location counter) memory location. Thus the statement BUFFEND EQU * which indicates the value of the next unassigned

Assigns a value to BUFFEND, which is the address of the next byte following the buffer area. Some values in the object program are relative to the beginning of the program and some are absolute (independent of the program location, like constants). Hence, expressions are classified as either absolute expression or relative expressions depending on the type of value they produce. Absolute Expressions:

expression. Absolute expression may contain relative term provided the relative terms occur in pairs with opposite signs for each pair. Example: MAXLEN EQU BUFEND-BUFFER

depend on the location of the program and hence gives an absolute immaterial o the relocation of the program. The expression can have only absolute terms. Example:

CI
MAXLEN STAB

TS
EQU EQU

In the above instruction the difference in the expression gives a value that does not

Relative Expressions: All the relative terms except one can be paired as described in "absolute". The remaining unpaired relative term must have a positive sign. Example: OPTAB + (BUFEND ­ BUFFER)

TU D
1000

The expression that uses only absolute terms is absolute

EN

TS .IN

CITSTUDENTS.IN

47

SYSTEM SOFTWARE

10CS52

Handling the type of expressions: to find the type of expression, we must keep track the type of symbols used. This can be achieved by defining the type in the symbol table against each of the symbol as shown in the table below:

3.1.4 Program Blocks:

Program blocks allow the generated machine instructions and data to appear in the object program in a different order by Separating blocks for storing code, data, stack, and larger data block. Assembler Directive USE: USE [blockname]

At the beginning, statements are assumed to be part of the unnamed (default) block. If no USE statements are included, the entire program belongs to this single block. Each program block may actually contain several separate segments of the source program. Assemblers rearrange these segments to gather together the pieces of each block and assign address. Separate the program into blocks in a particular order.Large buffer area is moved to the end of the object program. Program readability is betterif data areas are placed in the source program close to the statements that reference them.

CI

In the example below three blocks are used : Default: executable instructions CDATA: all data areas that are less in length CBLKS: all data areas that consists of larger blocks of memory

TS

TU D

EN
48

CITSTUDENTS.IN

TS .IN

SYSTEM SOFTWARE

10CS52

Example Code

CI

TS

TU D

EN
CITSTUDENTS.IN

TS .IN
49

SYSTEM SOFTWARE

10CS52

CI
Pass 1

Arranging code into program blocks:

TS

A separate location counter for each program block is maintained. Save and restore LOCCTR when switching between blocks. At the beginning of a block, LOCCTR is set to 0. Assign each label an address relative to the start of the block.

TU D

EN
CITSTUDENTS.IN

TS .IN
50

SYSTEM SOFTWARE

10CS52

Store the block name or number in the SYMTAB along with the assigned relative address of the label Indicate the block length as the latest value of LOCCTR for each block at the end of Pass1 Assign to each block a starting address in the object program by concatenating the program blocks in a particular order

Pass 2

Calculate the address for each symbol relative to the start of the object program by adding The starting address of this block The location of the symbol relative to the start of its block

Control Sections:

control section can be loaded and relocated independently of the others. Different control sections are most often used for subroutines or other logical subdivisions. The programmer can assemble, load, and manipulate each of these control sections separately.

example, instructions in one control section may refer to the data or instructions of other control sections. Since control sections are independently loaded and relocated, the assembler

CI
­

is unable to process these references in the usual way. Such references between different

control sections are called external references. The assembler generates the information about each of the external references that

will allow the loader to perform the required linking. When a program is written using multiple control sections, the beginning of each of the control section is indicated by an assembler directive assembler directive: CSECT

TS

Because of this, there should be some means for linking control sections together. For

TU D

A control section is a part of the program that maintains its identity after assembly; each

EN

TS .IN

CITSTUDENTS.IN

51

SYSTEM SOFTWARE The syntax secname CSECT ­ separate location counter for each control section

10CS52

Control sections differ from program blocks in that they are handled separately by the assembler. Symbols that are defined in one control section may not be used directly another control section; they must be identified as external reference for the loader to handle. The



EXTDEF (external Definition):

It is the statement in a control section, names symbols that are defined in this section but may be used by other control sections. Control section names do not need to be named in the EXTREF as they are automatically considered as external symbols. EXTREF (external Reference):

It names symbols that are used in this section but are defined in some other control

The order in which these symbols are listed is not significant. The assembler must include proper information about the external references in the object program that will cause the loader to insert the proper value where they are required.

CI

TS

TU D

section.

EN

TS .IN

external references are indicated by two assembler directives:

CITSTUDENTS.IN

52

SYSTEM SOFTWARE
..- Implicitly defined as an external symbol
START· EXTDEF EXTREF STL i-fbSUB LDA COfvlP JEQ r+bsuB

10CS52

COPY

/

....---;;- first control sectron
0 BUFFER BUFEND LENGTH RDRECWRREC RETADR RDREC LENGTH #0 ENDFIL WRREC CLOOP =C'EOF' BUFFER #3 LENGTH WRREC @RETADR

COPY FILE FROM INPUT TO OUTPUT

FIRST CLOOP

SAVE RETURN ADDRESS READ INPUT RECORD TEST FOR EOF (LENGTH=O) EXIT IF EOF FOUND WRITE OUTPUT RECORD LOOP INSERT END OF FL fvlARKER IE

J
ENDFIL LDA STA LDA STA [:±bsuB J RESW RESW LTORG RESB EQU EQU

RETADR LENGTH BUFFER BUFEND MAXLEN

1 1
4096

*

BUFFEND-BUFFER

Rolf

Implicitly defined as an external symbol CSECT second control section
SUBROUT NE TO READ RECORD INTO BUFFER IEXTREF CLEAR CLEAR CLEAR LDT TD JEQ RD COIIJPR I JEQ +STCH TIXR JLT +STX RSUB BYTE WORD

TU D
s
fvlAXLEN INPUT RLOOP INPUT A,S EXIT BUFFER,X

BUFFER,LENGTH,BUFFEND X A

RLOOP

EXIT

CI
INPUT MAXLEN

TS
T

RLOOP LENGTH

X'F1 ' BUFFEND-BUFFER

EN
CLEAR LOOP COUNTER CLEAR A TO ZERO CLEAR S TO ZERO TEST NPUT DEVICE LOOP UNTIL READY READ CHARACTER