Download as pdf or txt
Download as pdf or txt
You are on page 1of 109

lOMoARcPSD|15994706

SPOS UNIT 1 INTRODUCTION NOTES

System Programming and Operating System (Savitribai Phule Pune University)

StuDocu is not sponsored or endorsed by any college or university


Downloaded by Anthony Gonsalves (anthony07856@gmail.com)
lOMoARcPSD|15994706

INTRODUCTION TO SYSTEMS PROGRAMMING

1.1 INTRODUCTION
System Software
• System software consists of a variety of programs that support the operation of a
computer.
• It is a set of programs to perform a variety of system functions as file editing, resource
management, I/O management and storage management.
• The characteristic in which system software differs from application software is machine
dependency.
• An application program is primarily concerned with the solution of some problem, using
the computer as a tool.
• System programs on the other hand are intended to support the operation and use of
the computer itself, rather than any particular application.
• For this reason, they are usually related to the architecture of the machine on which they
are run.
• For example, assemblers translate mnemonic instructions into machine code. The
instruction formats, addressing modes are of direct concern in assembler design.
• There are some aspects of system software that do not directly depend upon the type of
computing system being supported. These are known as machine-independent features.
• For example, the general design and logic of an assembler is basically the same on most
computers.

1.1.1 Components of System Software


1. Operating system
2. Language translators
a. Compilers
b. Interpreters
c. Assemblers
d. Preprocessors

UNIT I | 1.1

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


3. Loaders
4. Linkers
5. Macro processors
Operating System
• It is the most important system program that act as an interface between the users and
the system. It makes the computer easier to use.
• It provides an interface that is more user-friendly than the underlying hardware.
• The functions of OS are :
1. Process management
2. Memory management
3. Resource management
4. I/O operations
5. Data management
6. Providing security to user’s job.
Language Translators
It is the program that takes an input program in one language and produces an output in
another language.

Fig. 1.1
Compilers
• A compiler is a language program that translates programs written in any high-level
language into its equivalent machine language program.
• It bridges the semantic gap between a programming language domain and the execution
domain.
• Two aspects of compilation are :
1. Generate code to increment meaning of a source program in the execution domain.
2. Provide diagnostics for violation of programming language, semantics in a source
program.
• The program instructions are taken as a whole.

Fig. 1.2

UNIT I | 1.2

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


Interpreters
• It is a translator program that translates a statement of high-level language to machine
language and executes it immediately. The program instructions are taken line by line.
• The interpreter reads the source program and stores it in memory.
• During interpretation, it takes a source statement, determines its meaning and performs
actions which increments it. This includes computational and I/O actions.
• Program counter (PC) indicates which statement of the source program is to be
interpreted next. This statement would be subjected to the interpretation cycle.
• The interpretation cycle consists of the following steps :
1. Fetch the statement.
2. Analyze the statement and determine its meaning.
3. Execute the meaning of the statement.
• The following are the characteristics of interpretation :
1. The source program is retained in the source form itself, no target program exists.
2. A statement is analyzed during the interpretation.

Fig. 1.3
Assemblers
• Programmers found it difficult to write or red programs in machine language. In a quest
for a convenient language, they began to use a mnemonic (symbol) for each machine
instructions which would subsequently be translated into machine language.
• Such a mnemonic language is called Assembly language.
• Programs known as Assemblers are written to automate the translation of assembly
language into machine language.

Fig. 1.4
• Translating mnemonic operation codes to their machine language equivalents.
• Assigning machine addresses to symbolic tables used by the programmers.
Loader
• A loader is a system software program that performs the loading function.

UNIT I | 1.3

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


• Loading is the process of placing the program into memory for execution.
• Loader is responsible for initiating the execution of the process.
• Fundamental process of Loaders consists of :
1. Allocation : The space for program is allocated in the main memory, by calculating
the size of the program.
2. Loading : Brings the object program into memory for execution.
3. Relocation : Modifies the object program so that it can be loaded at an address
different from the location originally specified.
4. Linking : Which combines two or more separate object programs and supplies the
necessary information.
Linker : In high level languages, some built in header files or libraries are stored. These
libraries are predefined and these contain basic functions which are essential for executing
the program. These functions are linked to the libraries by a program called Linker. If linker
does not find a library of a function then it informs to compiler and then compiler generates
an error. The compiler automatically invokes the linker as the last step in compiling a
program.
Not only built in libraries but also it links the user defined functions to the user defined
libraries. Usually a longer program is divided into smaller subprograms called modules. And
these modules must be combined to execute the program. The process of combining the
modules is done by the linker.
Macro Processors
• A macro instruction (macro) is a notational convenience for the programmer which allow
the programmer to write a shorthand version of a program
• A macro represents a commonly used group of statements in the source programming
language.
• Expanding the macros : The macro processor replaces each macro instruction with the
corresponding group of source language statements.
• A macro processor :
1. Essentially involve the substitution of one group of characters or lines for another.
2. Normally, it performs no analysis of the text it handles.
3. It doesn’t concern the meaning of the involved statements during macro expansion
4. The design of a macro processor generally is machine independent.
• A macro processor designed for use by assembler language programmers.
• Used with a high-level programming language.
UNIT I | 1.4

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


• General-purpose macro processor, which is not tied to any particular language.

Fig. 1.5

1.1.2 Language Processing Activities


Language Processing activities arises due to the difference between the manner in which a
software designer describes the ideas concerning the behavior of a software and the manner
in which these ideas are implemented in computer system. The designer expresses the ideas
in terms related to the application domain of the software. To implement these ideas, their
description has to be interpreted in terms related to the execution domain of the computer
system. We use the term semantics to represent the rules of meaning of a domain, and the
term semantic gap to represent the difference between the semantics of two domains.

Fig. 1.6 : Semantic gap


The semantic gap has many consequences, some of the important ones being large
development time, large development efforts, and poor quality of software.
These issues are handled by software engineers through the use of methodologies and
programming languages (PLs). The software engineering steps aimed at the use of a PL can
be grouped into :
1. Specification, design and coding steps.
2. PL implementation steps.
Software implementation using a PL is called PL domain.
The semantic gap between the application domain and execution domain is bridged by
software engineering steps. The first step bridges the gap between the application and PL
domains (called as specification gap). The second step bridges the gap between the PL and
execution domains.
The specification gap is bridged by software development team, whereas execution gap is
bridged by designer of the programming language processor ( that is translator or
interpreter).

UNIT I | 1.5

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING

Fig. 1.7 : Specification and execution gaps


Specification Gap : It is the semantic gap between the two specifications of the same task.
Execution Gap : The gap between the semantics of programs written in the different
programming languages.
1.1.2.1 Language Processors
Language processor is a software which bridges a specification or execution gap. A spectrum
of language processors is defined to meet practical requirements :
• A language translator bridges an execution gap to the machine language (or assembly
language) of a computer system. An assembler is a language translator whose source
language is assembly language. A compiler is any language translator which is not an
assembler.
• A detranslator bridges the same execution gap as the language translator, but in the
reverse direction.
• A preprocessor is a language processor which bridges an execution gap but is not a
language translator.
• A language migrator bridges the specification gap between two PLs.

(a)

(b)
Fig. 1.8 : Language processors
The interpreter is a language translator. This leads to many similarities between translators
and interpreters. From a practical viewpoint many differences also exist between translators
and interpreters. The absence of a target program implies the absence of an output interface
UNIT I | 1.6

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


the interpreter. Thus the language processing activities of an interpreter cannot be separated
from its program execution activities. Hence we say that an interpreter 'executes' a program
written in a PL.
Problem Oriented Languages
PLs used for specific application are called problem oriented languages .They are more close
to the application domain. They have more execution gap and it is bridged by a translator or
interpreter.
Procedure Oriented Languages
Provides general purpose facilities required in most application domains .Such a language is
independent of specific application domains and result in large specification gap which has
to be bridged by an application designer.

Fig. 1.9 : Problem oriented language domain

1.1.2.2 Language Processing Activities


The fundamental language processing activities can be divided into those that bridge the
specification gap and those that bridge the execution gap. We name these activities as :
1. Program generation activities.
2. Program execution activities.
A program generation activity aims at automatic generation of a program. The source
languages specification language of an application domain and the target language is
typically a procedure oriented PL. A Program execution activity organizes the execution of a
program written in a PL on computer system. Its source language could be a procedure
oriented language or a problem oriented language.
Program Generation
The program generator is a software system which accepts the specification of a program to
be generated, and generates program in the target PL. In effect, the program generator
introduces a new domain between the application and PL domains we call this the program
generator domain. The specification gap is now the gap between the application domain and
the program generator domain. This gap is smaller than the gap between the application
UNIT I | 1.7

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


domain and the target PL domain. Reduction in the specification gap increases the reliability
of the generated program.

Fig. 1.10 : Program generation

Fig. 1.11 : Program generator domain


Since the generator domain is close to the application domain, it is easy for the designer or
programmer to write the specification of the program to be generated. The harder task of
bridging the gap to the PL domain is performed by the generator. This arrangement also
reduces the testing effort. Proving the correctness of the program generator amounts to
proving the correctness of the transformation .This would be performed while implementing
the generator. To test an application generated by using the generator, it is necessary to only
verify the correctness of the specification input to the program generator. This is a much
simpler task than verifying correctness of the generated program. This task can be further
simplified by providing a good diagnostic (i.e. error indication) capability in the program
generator, which would detect inconsistencies in the specification. It is more economical to
develop a program generator than to develop a problem-oriented language. This is because
a problem oriented language suffers a very large execution gap between the PL domain and
the execution domain whereas the program generator has a smaller semantic gap to the
target PL domain, which is the domain of a standard procedure oriented language. The
execution gap between the target PL domain and the execution domain is bridged by the
compiler or interpreter for the PL.
Program Execution
Two popular models for program execution are translation and interpretation.
Program Translation
The program translation model bridges the execution gap by translating a program written in
a PL, called the source program(SP), into an equivalent program in the machine or assembly
language of the computer system, called the target program (TP) Characteristics of the
program translation model are :
• A program must be translated before it can be executed.
UNIT I | 1.8

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


• The translated program may be saved in a file. The saved program may be executed
repeatedly.
• A program must be retranslated following modifications.

Fig. 1.12 : Program translation model


Program Interpretation
The interpreter reads the source program and stores it in its memory. During interpretation it
takes a source statement, determines its meaning and performs actions which implement it.
This includes computational and input-output actions. The CPU uses a program counter (PC)
to note the address of the next instruction to be executed. This instruction is subjected to the
instruction execution cycle consisting of the following steps :
1. Fetch the instruction.
2. Decode the instruction to determine the operation to be performed, and also its
operands.
3. Execute the instruction.
At the end of the cycle, the instruction address in PC is updated and the cycle is repeated for
the next instruction. Program interpretation can proceed in an analogous manner. Thus, the
PC can indicate which statement of the source program is to be interpreted next. This
statement would be subjected to the interpretation cycle, which could consist of the
following steps :
Fetch the Statement
Analyze the statement and determine its meaning, viz. the computation to be performed and
its operands.
Execute the meaning of the statement.

Fig. 1.13 : Schematic of (a) Interpretation (b) Execution


UNIT I | 1.9

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


Comparison
A fixed cost (the translation overhead) is incurred in the use of the program translation
model. If the source program is modified, the translation cost must be incurred again
irrespective of the size of the modification. However, execution of the target program is
efficient since the target program is in the machine language. Use of the interpretation
model does not incur the translation overheads. This is advantageous if a program is
modified between executions, as in program testing and debugging.

1.1.3 Fundamentals of Language Processing


Definition
Language Processing = Analysis of SP + Synthesis of TP.
Where SP : Source Program and TP : Target Program
We refer to the collection of language processor components engaged in analyzing a source
program as the analysis phase of the language processor. Components engaged in
synthesizing a target program constitute the synthesis phase. A specification of the source
language forms the basis of source program analysis. The specification consists of three
components :
1. Lexical rules, which govern the formation of valid lexical units in the source language.
2. Syntax rules which govern the formation of valid statements in the source language.
3. Semantic rules which associate meaning with valid statements of the language.
The analysis phase uses each component of the source language specification to determine
relevant information concerning a statement in the source program. Thus, analysis of a
source statement consists of lexical, syntax and semantic analysis. The synthesis phase is
concerned with the construction of target language statement(s) which have the same
meaning as a source statement. Typically, this consist of two main activities :
• Creation of data structures in the target program.
• Generation of target code.
We refer to these activities as memory allocation and code generation, respectively.
Lexical Analysis (Scanning)
Lexical analysis identifies the lexical units in a source statement. It then classifies the units
into different lexical classes e.g. id’s, constants etc. and enters them into different tables. This
classification may be based on the nature of string or on the specification of the source
language. (For example, while an integer constant is a string of digits with an optional sign, a
reserved id is an id whose name matches one of the reserved names mentioned in the
language specification.) Lexical analysis builds a descriptor, called a token, for each lexical
unit. A token contain two fields class code, and number in class, class code identifies the class
UNIT I | 1.10

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


to which a lexical unit belongs, number in class is the entry number of the lexical unit in the
relevant table.
Syntax Analysis (Parsing)
Syntax analysis processes the string of tokens built by lexical analysis to determine the
statement class, e.g. assignment statement, if statement, etc. It then builds an intermediate
code(IC) which represents the structure of the statement. The IC is passed to semantic
analysis to determine the meaning of the statement.
Semantic Analysis
Semantic analysis of declaration statements differs from the semantic analysis of imperative
statements. The former results in addition of information to the symbol table, e.g. type,
length and dimensionality of variables. The latter identifies the sequence of actions necessary
to implement the meaning of a source statement. In both cases the structure of a source
statement guides the application of the semantic rules. When semantic analysis determines
the meaning of a sub tree in the IC. It adds information table or adds an action to the
sequence. It then modifies the IC to enable further semantic analysis. The analysis ends when
the tree has been completely processed.
Fundamentals of Language Specification
The lexical and syntactic features of a programming language are specified by its grammar. A
language L can be considered to be a collection of valid sentences. Each sentence can be
looked upon as a sequence of words, and each word as a sequence of letters or graphic
symbols acceptable in L. A language specified in this manner is known as a formal language.
A formal language grammar is a set of rules which precisely specify the sentences of L. It is
clear that natural languages are not formal languages due to their rich vocabulary. However,
PLs are formal languages.
Terminal Symbols, Alphabet and Strings
The alphabet of L, denoted by the Greek symbol Σ , is the collection of symbols in its
character set. We will use lower case letters a, b, c, etc. to denote symbols in Σ .
A symbol in the alphabet is known as a terminal symbol (T) of L. The alphabet can be
represented using the mathematical notation of a set, e.g. Σ ≡ {a, b, ….z, 0,1....9}
Here the symbols {, ',' and} are part of the notation. We call them metasymbols to
differentiate them from terminal symbols. It is assumed that met symbols are distinct from
the terminal symbols. If this is not the case, i.e. if a terminal symbol and a met symbol are
identical, we enclose the terminal symbol in quotes to differentiate it from the met symbol.
For example, the set of punctuation symbols of English can be defined as {:, ;, ‘ ,’,...} Where ','
denotes the terminal symbol 'comma'.
A string is a finite sequence of symbols. We will represent strings by Greek symbols α, β, γ ,
etc. Thus α = axy is a string over Σ . The length of a string is the Number of symbols in it.

UNIT I | 1.11

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


Note that the absence of any symbol is also a string, the null string ε. The concatenation
operation combines two strings into a single string.

1.2 ASSEMBLERS
1.2.1 Introduction to Assembly languages
There are two main classes of programming languages : high level (e.g., C, Pascal) and low
level. Assembly Language is a low level programming language. Programmers code symbolic
instructions, each of which generates machine instructions.
An assembler is a program that accepts as input an assembly language program (source) and
produces its machine language equivalent (object code) along with the information for the
loader.

Fig. 1.14 : Executable program generation from an assembly source code


Advantages of Coding in Assembly Language are :
• Provides more control over handling particular hardware components.
• May generate smaller, more compact executable modules.
• Often results in faster execution.
Disadvantages of Coding in Assembly Language are :
• Not portable.
• More complex.
• Requires understanding of hardware details (interfaces).
Assembler
An assembler does the following :
1. Generate machine instructions.
• Evaluate the mnemonics to produce their machine code.
• Evaluate the symbols, literals, addresses to produce their equivalent machine
addresses.
• Convert the data constants into their machine representations.
2. Process pseudo operations.
1.2.2 Elements of Assembly Language Programming
An Assembly Language is a low-level programming language for a computer, or other
programmable device, in which there is a very strong (generally one-to-one)
correspondence between the language and the architecture's machine code
UNIT I | 1.12

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


instructions. Each assembly language is specific to a particular computer architecture, in
contrast to most high-level programming languages, which are generally portable
across multiple architectures, but require interpreting or compiling.
Assembly language is converted into executable machine code by a utility program
referred to as an assembler; the conversion process is referred to as assembly, or
assembling the code.
Assembly language uses a mnemonic to represent each low-level machine instruction or
operation. Typical operations require one or more operands in order to form a complete
instruction, and most assemblers can therefore take labels, symbols and expressions as
operands to represent addresses and other constants, freeing the programmer from tedious
manual calculations. Macro assemblers include a macroinstruction facility so that
(parameterized) assembly language text can be represented by a name, and that name can
be used to insert the expanded text into other code. Many assemblers offer additional
mechanisms to facilitate program development, to control the assembly process, and to aid
debugging.
A program written in assembly language consists of a series of (mnemonic) processor
instructions and meta-statements (known variously as directives, pseudo-instructions and
pseudo-ops), comments and data. Assembly language instructions usually consist of an
opcode mnemonic followed by a list of data, arguments or parameters. These are translated
by an assembler into machine language instructions that can be loaded into memory and
executed.
1.2.2.1 Basic Elements
Integer Constants
An integer constant (or integer literal) is made up of an optional leading sign, one or more
digits, and an optional suffix character (called a radix) indicating the number’s base :
[{+| -}] digits [radix]
Radix may be one of the following (uppercase or lowercase) :
h Hexadecimal r Encoded real
q/o Octal t Decimal (alternate) t Decimal (alternate)
d Decimal y Binary (alternate) y Binary (alternate)
b Binary
If no radix is given, the integer constant is assumed to be decimal. Here are some examples
using different radixes :
26 Decimal 42o Octal

UNIT I | 1.13

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING

26d Decimal 1Ah Hexadecimal


11010011b Binary 0A3h Hexadecimal
42q Octal
A hexadecimal constant beginning with a letter must have a leading zero to prevent the
assembler from interpreting it as an identifier.
Integer Expressions
An integer expression is a mathematical expression involving integer values and arithmetic
operators. The expression must evaluate to an integer, which can be stored in 32 bits
(0 through FFFFFFFFh). The arithmetic operators are listed in Table 1.1 according to their
precedence order, from highest (1) to lowest (4).
Table 1.1
Operator Name Precedence Level
() Parentheses 1
+, – Unary plus, minus 2
*, / Multiply, divide 3
MOD Modulus 3
+, – Add, subtract 4
Table Arithmetic Operators
Precedence refers to the implied order of operations when an expression contains two or
more operators. The order of operations is shown for the following expressions :
4+5*2 Multiply, add
12 - 1 MOD 5 Modulus, subtract
-5 + 2 Unary minus, add
(4 + 2) * 6 Add, multiply
The following are examples of valid expressions and their values :
Expression Value
16 / 5 3
–(3 + 4) * (6 – 1) –35
–3 + 4 * 6 – 1 20
25 mod 3 1
Real Number Constants

UNIT I | 1.14

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


Real number constants are represented as decimal reals or encoded (hexadecimal) reals. A
decimal real contains an optional sign followed by an integer, a decimal point, an optional
integer that expresses a fraction, and an optional exponent :
[sign]integer.[integer][exponent]
Following are the syntax for the sign and exponent :
sign {+,-}
exponent E[{+,-}]integer
Following are examples of valid real number constants :
2.
+3.0
-44.2E+05
26.E5
At least one digit and a decimal point are required.
Encoded Reals : An encoded real represents a real number in hexadecimal, using the IEEE
floating-point format for short reals. The binary representation of decimal +1.0.
For example, is
0011 1111 1000 0000 0000 0000 0000 0000
The same value would be encoded as a short real in assembly language as :
3F800000r
Character Constants
A character constant is a single character enclosed in single or double quotes. MASM stores
the value in memory as the character’s binary ASCII code. Examples are :
'A'
"d"
A complete list of ASCII codes is printed on the inside back cover of this book.
String Constants
A string constant is a sequence of characters (including spaces) enclosed in single or double
quotes :
'ABC'
'X'
"Goodnight, Gracie"
'4096'

UNIT I | 1.15

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


Embedded quotes are permitted when used in the manner shown by the following examples
:
"This isn't a test"
'Say "Goodnight," Gracie'
Reserved Words
Reserved words have special meaning in MASM and can only be used in their correct
context. There are different types of reserved words :
• Instruction mnemonics, such as MOV, ADD, and MUL.
• Directives, which tell MASM how to assemble programs.
• Attributes, which provide size and usage information for variables and operands.
Examples are BYTE and WORD.
• Operators, used in constant expressions.
• Predefined symbols, such as @data, which return constant integer values at assembly
time.
Identifiers
An identifier is a programmer-chosen name. It might identify a variable, a constant, a
procedure, or a code label. Keep the following in mind when creating identifiers :
• They may contain between 1 and 247 characters.
• They are not case sensitive.
• The first character must be a letter (A..Z, a..z), underscore (_), @ , ?, or $. Subsequent
characters may also be digits.
• An identifier cannot be the same as an assembler reserved word.
The @ symbol is used extensively by the assembler as a prefix for predefined symbols, so
avoid it in your own identifiers. Make identifier names descriptive and easy to understand.
Here are some valid identifiers :
var1 Count $first
_main MAX open_file
@@myfile xVal _12345
b Binary
Directives
A directive is a command embedded in the source code that is recognized and acted upon
by the assembler. Directives do not execute at run time, whereas instructions do. Directives
can define variables, macros, and procedures. They can assign names to memory segments
and perform many other housekeeping tasks related to the assembler. In MASM, directives
are case insensitive. It recognizes .data, .DATA, and .Data as equivalent.
UNIT I | 1.16

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


The following example helps to show that directives do not execute at run time. The DWORD
directive tells the assembler to reserve space in the program for a doubleword variable. The
MOV instruction executes at run time, copying the contents of myVar to the EAX register :
myVar DWORD 26 ; DWORD directive
mov eax, myVar ; MOV instruction
Each assembler has a different set of directives. TASM (Borland) and NASM (Netwide
Assembler), for example, share a common subset of directives with MASM. The GNU
assembler, on the other hand, has almost no directives in common with MASM.
Defining Segments One important function of assembler directives is to define program
sections, or segments.
The .DATA directive identifies the area of a program containing variables :
.data
The .CODE directive identifies the area of a program containing instructions :
.code
The .STACK directive identifies the area of a program holding the runtime stack, setting its
size :
.stack 100h
Instructions
An instruction is a statement that becomes executable when a program is assembled.
Instructions are translated by the assembler into machine language bytes, which are loaded
and executed by the CPU at run time. An instruction contains four basic parts :
• Label (optional)
• Instruction mnemonic (required)
• Operand(s) (usually required)
• Comment (optional)
This is the basic syntax :
[label : ] mnemonic operand(s) [;comment]
Let’s explore each part separately, beginning with the label field.
Label
A label is an identifier that acts as a place marker for instructions and data. A label placed just
before an instruction implies the instruction’s address. Similarly, a label placed just before a
variable implies the variable’s address.
Data Labels : A data label identifies the location of a variable, providing a convenient way to
reference the variable in code. The following, for example, defines a variable named count :
count DWORD 100

UNIT I | 1.17

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


The assembler assigns a numeric address to each label. It is possible to define multiple data
items following a label. In the following example, array defines the location of the first
number (1024). The other numbers following in memory immediately afterward :
Array DWORD 1024, 2048
DWORD 4096, 8192
Code Labels : A label in the code area of a program (where instructions are located) must
end with a colon (:) character. In this context, labels are used as targets of jumping and
looping instructions.
For example, the following JMP (jump) instruction transfers control to the location marked by
the label named target, creating a loop :
target :
mov ax, bx
...

jmp target
A code label can share the same line with an instruction, or it can be on a line by itself :
L1 : mov ax, bx
L2 :
Instruction Mnemonic
An instruction mnemonic is a short word that identifies an instruction. In English, a
mnemonic is a device that assists memory. Similarly, assembly language instruction
mnemonics such as mov, add, and sub provide hints about the type of operation they
perform :
Instruction Operation
mov Move (assign) one value to another
add Add two values
sub Subtract one value from another
mul Multiply two values
jmp Jump to a new location
call Call a procedure
Operands Assembly language instructions can have between zero and three operands, each
of which can be a register, memory operand, constant expression, or I/O port. A memory
operand is specified by the name of a variable or by one or more registers containing the
address of a variable. A variable name implies the address of the variable and instructs the

UNIT I | 1.18

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


computer to reference the contents of memory at the given address. The following table 1.2
contains several sample operands :
Table 1.2
Example Operand Type
96 Constant (immediate value)
2+4 Constant expression
eax Register
count Memory
Following are examples of assembly language instructions having varying numbers of
operands.
The STC instruction, for example, has no operands :
stc ; set Carry flag
The INC instruction has one operand :
Inc eax ; add 1 to EAX
The MOV instruction has two operands :
mov count, ebx ; move EBX to count
In a two-operand instruction, the first is called the destination or target. The second operand
is the source. In general, the contents of the destination operand are modified by the
instruction. In a MOV instruction, for example, data is copied from the source to the
destination.
Comments
Comments are an important way for the writer of a program to communicate information
about how the program works to a person reading the source code. The following
information is typically included at the top of a program listing :
• Description of the program’s purpose.
• Names of persons who created and/or revised the program.
• Program creation and revision dates.
• Technical notes about the program’s implementation.
Comments can be specified in two ways :
• Single-line comments, beginning with a semicolon character (;). All characters following
the semicolon on the same line are ignored by the assembler.
• Block comments, beginning with the COMMENT directive and a user-specified symbol.
All subsequent lines of text are ignored by the assembler until the same user-specified
symbol appears.

UNIT I | 1.19

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


For example,
COMMENT !
This line is a comment.
This line is also a comment.
!
We can also use any other symbol :
COMMENT &
This line is a comment.
This line is also a comment.
&
The NOP (No Operation) Instruction
The safest instruction you can write is called NOP (no operation). It takes up 1 byte of
program storage and doesn’t do any work. It is sometimes used by compilers and assemblers
to align code to even-address boundaries. In the following example, the first MOV instruction
generates three machine code bytes. The NOP instruction aligns the address of the third
instruction to a double word boundary (even multiple of 4) :
00000000 66 8B C3 mov ax,bx
00000003 90 nop ; align next instruction
00000004 8B D1 mov edx,ecx
Example : Adding Three Integers
We now introduce a short assembly language program that adds and subtracts integers.
Registers are used to hold the intermediate data, and we call a library subroutine to display
the contents of the registers on the screen. Here is the program source code :
TITLE Add and Subtract (AddSub.asm).
; This program adds and subtracts 32-bit integers.
INCLUDE Irvine32.inc
.code
main PROC
mov eax, 10000h ; EAX = 10000h
add eax, 40000h ; EAX = 50000h
sub eax, 20000h ; EAX = 30000h
call DumpRegs ; display registers
exit

UNIT I | 1.20

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


main ENDP
END main
Let’s go through the program line by line. In each case, the program code appears before its
explanation :
TITLE Add and Subtract (AddSub.asm)
The TITLE directive marks the entire line as a comment. You can put anything you want on
this line.
This program adds and subtracts 32-bit integers.
All text to the right of a semicolon is ignored by the assembler, so we use it for comments.
INCLUDE Irvine32.inc
The INCLUDE directive copies necessary definitions and setup information from a text file
named Irvine32.inc, located in the assembler’s INCLUDE directory.
.code
The .code directive marks the beginning of the code segment, where all executable
statements in a program are located.
main PROC
The PROC directive identifies the beginning of a procedure. The name chosen for the only
procedure in our program is main.
mov eax,10000h ; EAX = 10000h
The MOV instruction moves (copies) the integer 10000h to the EAX register. The first
operand.
(EAX) is called the destination operand, and the second operand is called the source operand.
add eax,40000h ; EAX = 50000h
The ADD instruction adds 40000h to the EAX register.
sub eax,20000h ; EAX = 30000h
The SUB instruction subtracts 20000h from the EAX register.
Call DumpRegs; display registers
The CALL statement calls a procedure that displays the current values of the CPU registers.
This canbe a useful way to verify that a program is working correctly.
exit
main ENDP
The exit statement (indirectly) calls a predefined MS-Windows function that halts the
program. The ENDP directive marks the end of the main procedure. Note that exit is not a

UNIT I | 1.21

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


MASM keyword; instead, it’s a command defined in Irvine32.inc that provides a simple way to
end a program.
END main
The END directive marks the last line of the program to be assembled. It identifies the name
of the program’s startup procedure (the procedure that starts the program execution).
Program Output : The following is a snapshot of the program’s output, generated by the
call to DumpRegs :
EAX = 00030000 EBX = 7FFDF000 ECX = 00000101 EDX = FFFFFFFF
ESI = 00000000 EDI = 00000000 EBP = 0012FF0 ESP = 0012FFC4
EIP = 00401024 EFL = 00000206 CF = 0 SF = 0 ZF = 0 OF = 0 AF = 0 PF = 1
The first two rows of output show the hexadecimal values of the 32-bit general-purpose
registers. EAX equals 00030000h, the value produced by the ADD and SUB instructions in the
program. The third row shows the values of the EIP (extended instruction pointer) and EFL
(extended flags) registers, as well as the values of the Carry, Sign, Zero, Overflow, Auxiliary
Carry, and Parity flags.

1.2.3 A Simple Assembly Scheme


The four step approach is used to develop deign specification for an assembler.
1. Identify the information needed for performing a task.
2. Design a suitable data structure for recording information.
3. Determine the processing needed for obtaining and maintain the information.
4. Determine the processing needed for performing the task by using the recorded
information.
Program 1.1 : An Assembly Program and Corresponding Machine Language Program
Assembly Statement Machine Instruction
START 101
READ N 101) + 09 0 113
MOVER BREG, ONE 102) + 04 2 115
MOVEM BREG, TERM 103) + 05 2 116
AGAIN MULT BREG, TERM 104) + 03 2 116
MOVER CREG, TERM 105) + 04 3 116
ADD CREG, ONE 106) + 01 3 115
MOVEM CREG, TERM 107) + 05 3 116

UNIT I | 1.22

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING

COMP CREG, N 108) + 06 3 113


BC LE, AGAIN 109) + 07 2 104
MOVEM BREG, RESULT 110) + 05 2 114
PRINT RESULT 111) + 10 0 114
STOP 112) + 00 0 000
N DS 1 113)
RESULT DS 1 114)
ONR DC ‘1’ 115) + 00 0 001
TERM DS 1 116)
END
The primary information requirements arises when the assembler is engaged in generating
instructions in the synthesis phase. Hence it is better to start with information requirements
in the synthesis phase. We then consisder whether the information should be collected
during analysis phase or synthesis phase itself.
Synthesis Phase
Consider the assembly statement
MOVER BREG, ONE
For synthesizing the machine instruction corresponding to this statement, the assembler
must know the address of the memory word with which the symbol ONE is associated, and
the numeric operation code of the computer that corresponds to the mnemonic MOVER.
Following data structures are used for this purpose :
1. Symbol Table : Each entry has the fields name and address.
2. Mnemonics Table : Each entry has the fields mnemonic, opcode and length.
The memory address of a symbol depends on how it is defined in the source program.
Analysis phase shold determine this information and record it in the symbol table. The
synthesis phase can search the symbol table using te name field as the key to obtain the
address associated with a name. Information concerning numeric operation codes of
mnemonics does not depend on the source program., it merely depends on the assembly
lanuage and the instructionsin the computer.
Hence the designer of an assembler can constitute the mnemonics table as a fixed table. The
synthesis phase can search it by using the mnemonics field as the key field.
Analysis Phase

UNIT I | 1.23

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


The primary function of analysis phase is build the symbol table. It must determine they
Memory address with which each symbolic name used in a program is associated. In the
assembly program of the address of N would be known only after fixing the address of all
program elements whether instructions or memory areas that precede it. This function is
called memory allocation.
Memory allocation is performed by using a data structure called location counter (LC). The
analysis phase ensures the the location counter always contains the address that the next
memory word in the target program should have. At start of its processing, it initializes the
location counter to the constant specified in the START statement. While processing a
statement, it checks whether the statement has a label. If so, it enters and the address
contained in the location counter in a new entry of the symbol table. It then finds how many
memory words are needed for the instruction or data represented by the assembly statement
and updates the address in the location counter by that number. The amount of memory
needed for each assembly statement depends on the mnemonics of an assembly statement.
It obtains this information from the length field in the mnemonics table. For DC and DS
statements, the memory requirement further depends on the constant appearing in the
operand field, so the analysis phase should determine it appropriately. We refer to all actions
involved in managing the location counter collectively as LC (Location Counter) processing.

Fig. 1.15
Above Fig. 1.15 illustrates use of the symbol and mnemonics tables by the analysis and
synthesis phases. The symbol table is constructed during analysis and used during synthesis.
The tasks performed by analysis and synthesis phase are as follows :
Analysis Phase :
1. Isolates the label, mnemonic opcode, and operands fields of a statement.

UNIT I | 1.24

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


2. If a label is present, enter the pair (symbol, <LC contents>) in a new entry of symbol
table.
3. Check validity of the mnemonic opcode through a look-up in the mnemonics table.
4. Perform LC processing, i.e. update the value contained in LC by considering the
opcode and operands of the statement.
Synthesis Phase :
1. Obtain the machine opcode corresponding to the mnemonic from the mnemonics
table.
2. Obtain address of a memory operand from the Symbol table.
3. Synthesis a machine instruction or the machine form of a constant, as the case may
be.
1.2.4 Pass Structure of Assembler
Assemblers (Two Passes)
• An assembler’s first pass reads each line of an assembly file and breaks it into its
component pieces. These pieces, which are called lexemes, are individual words,
numbers, and punctuation characters.
For Example :
ble $t0, 100, loop (6 lexemes)

Fig. 1.16 : Overview of two pass assembler


• If a line begins with a label, the assembler records in its symbol table the name of the
label and the address of the memory word that the instruction occupies (the first pass).
• The assembler uses the information in the symbol table during a second pass over the
file, which actually produces machine code.
Assemblers (One Pass)
• If an assembler’s speed is important, this two-step process can done in one pass over the
assembly file with a technique known as backpatching.

UNIT I | 1.25

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


• The assembler builds a (possibly incomplete) binary representation of every instruction. If
the instruction references a label that has not yet been defined, the assembler records
the label and instruction in a table. When a label is defined, the assembler consults this
table to find all instructions that contain a forward reference to the label.
• The assembler goes back and corrects their binary representation to incorporate the
address of the label.
• Backpatching speeds assembly because the assembler only reads its input once.
However, it requires an assembler to hold the entire binary representation of a program
in memory so instructions can be backpatched.
1.2.5 Single Pass Assembler
Single Pass Translation
The main problem in designing the assembler using single pass was to resolve forward
references. We can avoid to some extent the forward references by :
• Eliminating forward reference to data items, by defining all the storage reservation
statements at the beginning of the program rather at the end.
• Unfortunately, forward reference to labels on the instructions cannot be avoided.
(forward jumping).
• To provide some provision for handling forward references by prohibiting forward
references to data items.
There are two types of one pass assemblers :
• One that produces object code directly in memory for immediate execution (Load and go
assemblers).
• The other type produces the usual kind of object code for later execute.
Load-and-Go Assembler
The simplest assembler program is the load and go assembler. It accepts as input a program
whose instructions are essentially one to one correspondence with those of machine
language but with symbolic names used for operators and operands. It produces machine
language as output which are loaded directly in main memory and gets executed. The
translation is usually performed in a single pass over the input program text. The resulting
machine language program occupies storage locations which are fixed at the time of
translation and cannot be changed subsequently. The program can call library subroutines,
provided that they occupy other locations than those required by the program. No provision
is made for combining separate subprograms translated in this manner. The load and go

UNIT I | 1.26

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


assembler forgoes the advantages of modular program development. Among the most of
these are :
• The ability to design code and test different program components in parallel.
• Change in one particular module does not require scanning the rest of program.
• 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 reassembled nearly every time they are run; efficiency of the assembly
process is an important consideration.
Forward Reference in One Pass Assemblers : In load and Go assemblers when a forward
reference is encountered.
• Omits the operand address if the symbol has not yet been defined.
• Enters this undefined symbol into SYMTAB and indicates that it is undefined.
• Adds the address of this operand address to a list of forward references associated with
the SYMTAB entry.
• Scans the reference list and inserts the address when the definition for the symbol is
encountered.
• Reports the error if there are still SYMTAB entries indicated undefined symbols at the end
of the program.
• Search SYMTAB for the symbol named in the END statement and jumps to this location
to begin execution if there is no error.
mov ax, 0
cmp ax, bx
jge next ;can’t assemble
….
next :
When a forward reference is found :
• Put the statement in a table
• At the end of the pass, assemble the statements in the table
• Disadvantage : complex pieces must be put back in the right order.

UNIT I | 1.27

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


• The problem of forward reference can be handled using a technique called as back
patching.

Fig. 1.17
START 100
MOVER AREG, X 100 04 1 _ _ _
L1 ADD BREG, ONE 101 01 2 _ _ _
ADD CREG, TEN 102 06 3 _ _ _
STOP 103 00 0 000
X DC ‘5’ 104
ONE DC ‘1’ 105
TEN DC ‘10’ 106

Intruction Address Symbol Making a Forward Reference


100 X
101 One
102 TEN

Machine Instruction after Backpatching


04 1 104
01 2 105
06 2 106
00 0 000

1.2.6 Design of a Two Pass Assembler


UNIT I | 1.28

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


1.2.6.1 Data Structure of Two Pass Assembler

Fig. 1.18 : Data structure of two pass assembler


 Following are the tables used by the assembler :
1. Machine Opcode Table (MOT)
2. Pseudo Opcode Table (POT)
3. Symbol Table (ST)
4. Base Register Table (BRT)
5. Literal Table (LT)
1. Machine Opcode Table
The table consist of the fields : Name of mnemonic i.e. machine opcode, equiv. byte
opcode, format of instruction and length of instruction.
Name of Bype Opcode Format of Length of
Mnemonic Instruction Instruction

2. Pseudo Opcode Table (POT)


This table consist of the fields :
Name of Pseudo opcode
Routine associated with the Pseudo Opcode
Name of Pseudo Associated Routine
Opcode

3. Symbol Table (ST)

UNIT I | 1.29

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


The symbol table keeps track of the symbols used in the ALP . It has the following fields .
Name of symbol
Value of symbol
Type of symbol
Name of Symbol Its Value Type of Symbol

4. Base Register Table


This table keeps the track of registers which are used as base register.
The table has the following format.
Statement No. of Base Reg. Contents
Program

5. Literal Table
The literal table keep list of all the literals used in the program.
The table has the following format.
Literal Symbol Value Legth of Inst. Characteristics

1.2.6.2 Design of Two Pass Assembler


The entire process of translating an ALP to its equivalent machine language code is divided
into two passes.
1. Pass 1 Assembler.
2. Pass 2 Assembler.
Pass 1 Assembler
The Function of Pass 1 Assembler is :
• To define and compute the value of symbols .
• Assigning and storing the addresses to literals.
So output of pass 1 is Symbol table and Literal table.
Processing of Pass 1 with :

UNIT I | 1.30

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


• Pseudo - Opcodes :
1. USING and DROP : No processing is done.
2. EQU : Processed for evaluation of operands.
3. DC/DS : Processed for the allocation of storage and LC is updated.
4. START : LC is incremented by the value of the operand.
5. LTORG : Storage is assigned from the start of the program or from previous LTORG.
6. END : Assembler allocates all the literals before going for pass 2.
• Machine Opcode :
Get the length of instruction. If label/literal is present enter it in LT/ST and increment LC .

Pass 1 Data Bases :

1. Input Source Program.

2. LC : Used to keep track of each instruction.

3. MOT : Indicates Symbolic mnemonic for each instruction and its length.

4. POT : Indicates Symbolic mnemonic and action to be taken for each Pseudo Opcode.

5. ST : Store each label and its value.

6. LT : Store each literal and its corresponding location.

7. A Copy of input later used by pass2.

UNIT I | 1.31

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING

Fig. 1.19 : Flow chart of pass1 of two pass assembler

UNIT I | 1.32

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


Pass 2 Assembler
• Second pass synthesis Target Program.

Fig. 1.20 : Flow chart of pass2 of two pass assembler

UNIT I | 1.33

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


Table 1.3 : Machine Opcode Table (MOT)
Mnemonic of Code Class Opcode Length
STOP IS 00 1
ADD IS 01 1
SUB IS 02 1
MULT IS 03 1
MOVER IS 04 1
MOVEM IS 05 1
COMP IS 06 1
BC IS 07 1
DIV IS 08 1
READ IS 09 1
PRINT IS 10 1
START AD 01 –
END AD 02 –
ORIGIN AD 03 –
E&U AD 04 –
LTORG AD 05 –
DS DL 01 –
DC DL 02 1
AREG RG 01 –
BREG RG 02 –
CREG RG 03 –
EQ CC 01 –
LT CC 02 –
GT CC 03 –
LE CC 04 –
GE CC 05 –
NE CC 06 –
ANY CC 07 –

UNIT I | 1.34

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


Example 1.1 :
START 200 LC
MOVER AREG, = ‘5’ 200
MOVEM AREG, X 201
L1 MOVER BREG, = ‘2’ 202
ORIGIN L1 + 3
LTORG 205
206
NEXT ADD AREG, = ‘1’ 207
SUB BREG = ‘2’ 208
BC LT, BACK 209
LTORG 210
211
BACK EQU L1 212
ORIGIN NEXT + 5
MULT CREG, = ‘4’ 212
STOP 213
X DS 1 214
END
Solution :
Symbol Address Literal Address Pool Table
START 200
0
MOVER AREG, = ‘5’ 200
= ‘5’ --- 0
MOVER AREG, = X 201
X --- = ‘5’ --- 0
L1 MOVER BREG, = ‘2’ 202
X --- = ‘5’ --- 0
L1 202 = ‘2’ ---
ORIGIN L1 + 3 203

UNIT I | 1.35

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING

X --- = ‘5’ --- 0


L1 202 = ‘2’ ---
LTORG 205
206

X --- = ‘5’ 205 0


L1 202 = ‘2’ 206 2
NEXT ADD AREG, = ‘1’ 207
X --- = ‘5’ 205 0
L1 202 = ‘2’ 206 2
NEXT 207 = ‘1’ ---
SUB BREG, = ‘2’ 208
X --- = ‘5’ 205 0
L1 202 = ‘2’ 206 2
NEXT 207 = ‘1’ ---
= ‘2’ ---
BC LT, BACK 209
X --- = ‘5’ 205 0
L1 202 = ‘2’ 206 2
NEXT 207 = ‘1’ ---
BACK --- = ‘2’ ---
LTORG 210
211
X --- = ‘5’ 205 0
L1 202 = ‘2’ 206 2
NEXT 207 = ‘1’ 210 4
= ‘2’ 211
BACK EQU L1 212
X --- = ‘5’ 205 0
L1 202 = ‘2’ 206 2
NEXT 207 = ‘1’ 210

UNIT I | 1.36

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


BACK 202 = ‘2’ 211
ORIGIN NEXT + 5 213
X --- = ‘5’ 205 0
L1 202 = ‘2’ 206 2
NEXT 207 = ‘1’ 210 4
BACK 202 = ‘2’ 211
MULT CREG, = ‘4’ 212
X --- = ‘5’ 205 0
L1 202 = ‘2’ 206 2
NEXT 207 = ‘1’ 210 4
BACK 202 = ‘2’ 211
= ‘4’ ---
STOP 213
X --- = ‘5’ 205 0
L1 202 = ‘2’ 206 2
NEXT 207 = ‘1’ 210 4
BACK --- = ‘2’ 211
= ‘4’ ---
X DS 1 214
X 214 = ‘5’ 205 0
L1 202 = ‘2’ 206 2
NEXT 207 = ‘1’ 210 4
BACK 202 = ‘2’ 211
= ‘4’ ---
END
X 214 = ‘5’ 205 0
L1 202 = ‘2’ 206 2
NEXT 207 = ‘1’ 210 4
BACK 202 = ‘2’ 211 5
= ‘4’ 215

UNIT I | 1.37

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


Example 1.2 : For the following assembly language code show the content of Symbol table,
literal table and also generate intermeadiate and target code.
(Assume suitable op-codes and instruction length and clearly indicate the assumptions
made).
START 1000
READ N
MOVER B, = ‘1’
MOVEM B, TERM
AGAIN MUL B, TERM
MOVER C, TERM
COMP C, N
BC LE, AGAIN
MOVEM B, RESULT
LTORG
PRINT RESULT
STOP
N DS 1
RESULT DS 20
TERM DS 1
END
Solution : C : constant, S : symbol, L : literal
Assembly Program LC Intermidate Code
1 START 1000 (AD, 01) (C, 1000)
2 READ N 1000 (IS, 09) (S, 0)
3 MOVER B, = ‘1’ 1001 (IS, 04) (RG, 02) (L, 0)
4 MOVEM B,TERM 1002 (IS, 05) (RG, 02) (S, 1)
5 AGAIN MUL B, TERM 1003 (IS, 03) (RG, 02) (S, 1)
6 MOVER C, TERM 1004 (IS, 04) (RG, 03) (S, 1)
7 COMP C, N 1005 (IS, 06) (RG, 03) (S, 0)
8 BC LE, AGAIN 1006 (IS, 07) (CC, 04) (S, 2)
9 MOVEM B, RESULT 1007 (IS, 05) (RG, 02) (S, 3)

UNIT I | 1.38

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING

10 LTORG 1008 (DL, 02) (C, 1)


11 PRINT RESULT 1009 (IS, 10) (S, 3)
12 STOP 1010 (IS, 00)
13 N DS 1 1011 (DL, 01) (C, 1)
14 RESULT DS 20 1012 (DL, 01) (C, 20)
15 TERM DS 1 1032 (DL, 01) (C, 1)
16 END 1033 (AD, 02)

Symbol Table Literal Table Pool Table


Symbol Address Literal Address 0 0
0 N 1011 0 = ‘1’ 1008
1 TERM 1032
2 AGAIN 1003
3 RESULT 1012

Intermidate Code LC Machine Code


1 (AD, 01) (C, 1000)
2 (IS, 09) (S, 0) 1000 09 00 1011
3 (IS, 04) (RG, 02) (L, 0) 1001 04 02 1008
4 (IS, 05) (RG, 02) (S, 1) 1002 05 02 1032
5 (IS, 03) (RG, 02) (S, 1) 1003 03 02 1032
6 (IS, 04) (RG, 03) (S, 1) 1004 04 03 1032
7 (IS, 06) (RG, 03) (S, 0) 1005 06 03 1011
8 (IS, 07) (CC, 04) (S, 2) 1006 07 04 1003
9 (IS, 05) (RG, 02) (S, 3) 1007 05 02 1012
10 (DL, 02) (C, 1) 1008 00 00 0001
11 (IS, 10) (S, 3) 1009 10 00 1012
12 (IS, 00) 1010 00 00 0000
13 (DL, 01) (C, 1) 1011
14 (DL, 01) (C, 20) 1012
15 (DL, 01) (C, 1) 1032
16 (AD, 02)
UNIT I | 1.39

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


Example 1.3 : Following assembly program show that.
START 100
MOVER AREG, = 5
ADD CREG, = 1
A DS 3
L1 MOVER AREG, B
ADD AREG, C
MOVEM AREG, D
L TORG
D EQ0 A+1
L2 PRINT D
ORIGIN A–1
SUB AREG, = 1
MULT CREG, B
C DC ‘5’
ORIGIN L2 + 1
STOP
B DC 19
END
Find Out :
1. Show the contentS of symbol table, literal table, & pool table at the end of pass 1.
2. Show the intermidiate code genrated for the program.
3. Show the machine code generate for the program.
Assembly Code LC Intermediate Code
1 START 100 (AD, 01) (C, 100)
2 MOVER AREG, = 5 100 (IS, 04) (RG, 01) (L, 0)
3 ADD CREG, = 1 101 (IS, 01) (RG, 03) (L, 1)
4 A DS S 102 (DL,01)(C,3)
5 L1 MOVER AREG, B 105 (IS, 04) (RG, 01) (S,2)
6 ADD AREG, C 106 (IS, 01) (RG, 01) (S,3)
7 MOVEM AREG, D 107 (IS, 05) (RG, 01) (S,4)

UNIT I | 1.40

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING

8 LTORG 108 (DL, 02) (C, 5)


- 109 (DL, 02) (C, 1)
9. D EQU A + 1 (AD, 04) (C, 103)
10. L2 PRINT D 110 (IS, 10) (S, 4)
11 ORIGIN A – 1 (AD, 03) (C, 101)
12 SUB AREG, = 1 101 (IS, 02) (RG, 01) (L, 2)
13 MULT CREG, B 102 (IS, 03) (RG, 03) (S, 2)
14 C DC ‘5’ 103 (DL, 02) (C, 5)
15 ORIGIN L2 + 1 (AD, 03) (C, 111)
16 STOP 111 (IS, 00)
17 B DC 19 112 (DL, 02) (C, 19)
18 END (AD, 02)
19 LTORG 113 (DL, 02) (C, 1)

Symbol Table Literal Table Pool Table


Symbol Address Literal Address
0 A 102 0 =5 108 0 0
1 L1 105 1 =1 109 1 2
2 B 112 2 =1 113
3 C 103
4 D 108

Intermediate Code LC Machine Code


1. (AD, 01) (C, 100)
2. (IS, 04) (RG, 01) (L, 0) 100 04 01 108
3. (IS, 01) (RG, 03) (L, 1) 101 01 03 109
4. (DL, 01) (C, 3) 102
5. (IS, 04) (RG, 01) (S, 2) 105 04 01 112
6. (IS, 01) (RG, 01) (S, 3) 106 01 01 103
7. (IS, 05) (RG, 01) (S, 4) 107 05 01 103

UNIT I | 1.41

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING

8. (DL, 02) (C, 5) 108 00 00 005


(DL, 02) (C, 1) 109 00 00 001
9. (AD, 04) (C, 103)
10. (IS, 10) (S,4) 110 10 00 103
11. (A0, 03) (C, 101)
12. (IS, 02) (RG, 01) (L, 2) 101 02 01 113
13. (IS, 03) (RG, 03) (S, 2) 102 03 03 112
14. (DL, 02) (C, 5) 103 00 00 005
15. (AD, 03) (C, 111)
16. (IS, 00) 111 00 00 000
17. (DL, 02) (C, 19) 112 00 00 019
18. (AD, 02)
19. (DL, 02) (C, 1) 113 00 00 001
1.2.6.3 Implementation of two pass Assembler with Hypothetical Instruction
Set
#include<conio.h>
#include<stdio.h>
#define MAX 20
#include<string.h>
struct symbol
{
Char sym[10];
Int addr;
}S[MAX];
Struct litab
{
char lit[10];
int addr;
}L[MAX];
void print_file(char *);

UNIT I | 1.42

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


void print_symtab();
void print_littab();
char optab[][6]={"STOP", "ADD", "SUB", "MULT", "MOVER", "MOVEM", "COMP", "BC",
"DIV", "READ", "PRINT"};
char regtab[][5]={"AREG","BREG","CREG","DREG"};
char adtab[][7]={"START","END","ORIGIN","EQU","LTORG"};
char condtab[][4]={"LT","LE","EQ","GT","GE","ANY"};
FILE *fs, *ft;
char buffer[80], source[80], tok1[10], tok2[10], tok3[10], tok4[10], tok5[10];
int lc=0, sc=0, poolcnt=0, litcnt=0;
int pooltab[10];
int search_optab(char *s)
{
int i;
for(i=0;i<11;i++)
{
if(strcmp(optab[i],s)==0)
{
return i;
}
}
return -1;
}
int search_regtab(char *s)
{
int i;
for(i=0;i<4;i++)
{
if(strcmp(regtab[i],s)==0)
{

UNIT I | 1.43

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


return i+1;
}
}
return -1;
}
int search_condtab(char *s)
{
int i;
for(i=0;i<6;i++)
{
if(strcmp(condtab[i],s)==0)
{
return (i+1);
}
}
return -1;
}
int search_adtab(char *s)
{
int i;
for(i=0;i<5;i++)
{
if(strcmp(adtab[i],s)==0)
{
return i+1;
}
}
return -1;
}
int search_symtab(char *s)

UNIT I | 1.44

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


{
int i;
for(i=0;i<sc;i++)
{
if(strcmp(S[i].sym,s)==0)
{
return i;
}
}
return -1;
}
int search_littab(char *s)
{
int i;
for(i=pooltab[poolcnt];i<litcnt;i++)
{
if(strcmp(L[i].lit,s)==0)
{
return i;
}
}
return -1;
}
void pass1()
{
int p;
int n,i=0,j=0,k=0;
fs = fopen(source,"r");
if(fs==NULL)
{

UNIT I | 1.45

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


printf("\n file does not exist!!!!");
getch();
exit(0);
}
ft =fopen("id.txt", "w");
while(fgets(buffer,80,fs))
{
n=sscanf(buffer,"%s%s%s%s",tok1,tok2,tok3,tok4);
switch(n)
{
case 1 : //ltorg,end
i=search_adtab(tok1);
if(i==2 || i==5)
{
for(j=pooltab[poolcnt];j<litcnt;j++)
{
L[j].addr=lc++;
}
lc--;
pooltab[++poolcnt]=litcnt;
fprintf(ft,"(AD, %02d)\n",i);
break;
}
case 2 : //start
i=search_adtab(tok1);
if(i==1)
{
lc=atoi(tok2)-1;
fprintf(ft,"(AD, %02d) (C, %s)\n",i,tok2);
break;

UNIT I | 1.46

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


}
case 3 : i=search_optab(tok1);
if(i>=1 && i<=8)
{
tok2[strlen(tok2)-1]='\0';
k=search_regtab(tok2);
//mover areg,='5'
if(tok3[0]=='=')
{
j=search_littab(tok3);
if(j==-1)
{
strcpy(L[litcnt].lit,tok3);
fprintf(ft,"(IS, %02d) %d (L, %02d)\n",i,k,litcnt);
litcnt++;
}
else
{
fprintf(ft,"(IS, %02d) %d (L, %02d)\n",i,k,j);
}
break;
}
else//mover areg,A
{
p=search_symtab(tok3);
if(p==-1)
{
strcpy(S[sc].sym,tok3);
fprintf(ft,"(IS, %02d) %d (S, %02d)\n",i,k,sc);
sc++;

UNIT I | 1.47

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


}
else
{
fprintf(ft,"(IS, %02d) %d (S, %02d)\n",i,k,p);
}
break;
}
}
//A DS 2
if(strcmp(tok2,"DS")==0)
{
p=search_symtab(tok1);
if(p==-1)
{
strcpy(S[sc].sym,tok1);
S[sc].addr=lc;
fprintf(ft,"(DL, 2) (C, %s)\n",tok3);
sc++;
}
else
{
S[p].addr=lc;
fprintf(ft,"(DL, 2) (C, %s)\n",tok3);
}
lc=lc+atoi(tok3)-1;
break;
}
}
lc++;
}

UNIT I | 1.48

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


fcloseall();
}
void print_file(char *target)
{
FILE *fp;
fp=fopen(target,"r");
if(fp==NULL)
{
printf("\nfile does not exist!!!");
getch();
exit(0);
}
printf("\n\n");
while(fgets(buffer,80,fp))
{
printf("%s",buffer);
}
fclose(fp);
getch();
}
void print_littab()
{
int i;
printf("\n LITERAL\tADDRESS\n");
for(i=0;i<litcnt;i++)
{
printf("%s\t%d\n",L[i].lit,L[i].addr);
}
}
void print_symtab()

UNIT I | 1.49

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


{
int p=0;
printf("\n SYMBOL\tADDRESS\n");
while(p<sc)
{
printf("%s\t%d\n",S[p].sym,S[p].addr);
p=p+1;
}
}
void pass2()
{
inti,j,k,n,p;
char temp[20];
if((fs=fopen("id.txt","r"))==NULL)
{
printf("\n\nerror in opening file..");
getch();
exit(0);
}
if((ft=fopen("tar.txt","w"))==NULL)
{
printf("\n\nerror in opening file..");
getch();
exit(0);
}
lc=0;
while(fgets(buffer,80,fs))
{
n=sscanf(buffer,"%s%s%s%s%s",tok1,tok2,tok3,tok4,tok5);
switch(n)

UNIT I | 1.50

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


{
case 1 :
tok2[strlen(tok2)-1]='\0';
i=atoi(tok2);
if(i==2 || i==5)
{
for(j=0;j<poolcnt;j++)
{
if(L[pooltab[j]].addr==lc)
break;
}
for(k=pooltab[j];k<pooltab[j+1];k++)
{
strcpy(temp,L[k].lit);
temp[strlen(temp)-1]='\0';
fprintf(ft,"%d) %s \n",lc++,strstr(temp,"'")+1);
}
lc--;
break;
}
fprintf(ft,"%d) 00 0 00\n",lc);
break;
case 2 : if(strcmp(tok1,"(AD,")==0)
{
tok4[strlen(tok4)-1]='\0';
lc=atoi(tok4)-1;
break;
}
if(strcmp(tok1,"(DL,")==0)
{

UNIT I | 1.51

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


tok2[strlen(tok2)-1]='\0';
tok4[strlen(tok4)-1]='\0';
i=atoi(tok2);
j=atoi(tok4);
if(i==2) // A DS 2
{
for(k=0;k<j;k++)
{
fprintf(ft,"%d)\n",lc++);
}
lc--;
}
break;
}
case 3 : tok2[strlen(tok2)-1]='\0';
tok5[strlen(tok5)-1]='\0';
i=atoi(tok2);
j=atoi(tok3);
k=atoi(tok5);
if(strcmp(tok4,"(S,")==0)
{
fprintf(ft,"%d) %02d %d %03d\n",lc,i,j,S[k].addr);
}
else
{
fprintf(ft,"%d) %02d %d %03d\n",lc,i,j,L[k].addr);
}
break;
}
lc++;

UNIT I | 1.52

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


}//while
fcloseall();
}
void main()
{
clrscr();
printf("\n enter source file name : \n");
scanf("%s",source);
printf("\n source code is : \n ");
print_file(source);
pass1();
printf("\n\n literal table : \n");
print_littab();
printf("\n\n symbol table : \n");
print_symtab();
printf("\n intermediate code is : \n");
print_file("id.txt");
getch();
pass2();
printf("\n\n\n target code....");
print_file("tar.txt");
getch();
}
/* source file*/
START 200
MOVER AREG, ='5'
MOVEM BREG, ='1'
ADD BREG, A
LTORG
SUB CREG, B
A DS 2
B DS 2
END

UNIT I | 1.53

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


/* id file for intermediate code */
(AD, 01) (C, 200)
(IS, 04) 1 (L, 00)
(IS, 05) 2 (L, 01)
(IS, 01) 2 (S, 00)
(AD, 05)
(IS, 02) 3 (S, 01)
(DL, 2) (C, 2)
(DL, 2) (C, 2)
(AD, 02)
/* tar file for target code */
200) 04 1 203
201) 05 2 204
202) 01 2 206
203) 5
204) 1
205) 02 3 208
206)
207)
208)
209)

1.3 MACRO PROCESSOR


1.3.1 Macro Definition and Call, Macro Expansion
Macro processor is system software that replaces each macroinstruction with the
corresponding group of source language statements. This is also called as expanding of
macros.
Macro represents a commonly used group of statements in the source programming
language.
• A macro instruction (macro) is a notational convenience for the programmer
1. It allows the programmer to write shorthand version of a program (module
programming)
UNIT I | 1.54

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


• The macro processor replaces each macro instruction with the corresponding group of
source language statements (expanding) :
1. Normally, it performs no analysis of the text it handles.
2. It does not concern the meaning of the involved statements during macro expansion.
• The design of a macro processor generally is machine independent!
• Two new assembler directives are used in macro definition.
1. MACRO : identify the beginning of a macro definition.
2. MEND : identify the end of a macro definition.
• Prototype for the macro
Each parameter begins with „&‟
name MACRO parameters
:
:
body
:
:
MEND
Body : the statements that will be generated as the expansion of the macro.
Basic Macro Processor Functions :
• Macro Definition and Expansion.
• Macro Processor Algorithms and Data structures.
Macro Definition and Expansion :
The Table 1.4 shows the MACRO expansion. The left block shows the MACRO definition and
the right block shows the expanded macro replacing the MACRO call with its block of
executable instruction.
M1 is a macro with two parameters D1 and D2. The MACRO stores the contents of register A
in D1 and the contents of register B in D2. Later M1 is invoked with the parameters DATA1
and DATA2, Second time with DATA4 and DATA3. Every call of MACRO is expended with the
executable statements.
Table 1.4
Source Expanded Source
M1 MACRO &D1, & D2 .
STA &D1 .
STB &D2 .
MEND STA DATA1
. STB DATA2
M1 DATA1, DATA2 . STA DATA4
. STB DATA3
M1 DATA4, DATA3 .

UNIT I | 1.55

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


The statement M1 DATA1, DATA2 is a macro invocation statements that gives the name of
the macro instruction being invoked and the arguments (M1 and M2) to be used in
expanding. A macro invocation is referred as a Macro Call or Invocation.
Macro Expansion
The program with macros is supplied to the macro processor. Each macro invocation
statement will be expanded into the statement s that form the body of the macro, with the
arguments from the macro invocation substituted for the parameters in the macro prototype.
During the expansion, the macro definition statements are deleted since they are no longer
needed.
The arguments and the parameters are associated with one another according to their
positions. The first argument in the macro matches with the first parameter in the macro
prototype and so on.
After macro processing the expanded file can become the input for the Assembler. The
Macro Invocation statement is considered as comments and the statement generated from
expansion is treated exactly as though they had been written directly by the programmer.
The difference between Macros and Subroutines is that the statement s from the body of the
Macro is expanded the number of times the macro invocation is encountered, whereas the
statement of the subroutine appears only once no matter how many times the subroutine is
called. Macro instructions will be written so that the body of the macro contains no labels.
• Problem of the label in the body of macro :
1. If the same macro is expanded multiple times at different places in the program.
2. There will be duplicate labels, which will be treated as errors by the assembler.
• Solutions :
1. Do not use labels in the body of macro.
2. Explicitly use PC-relative addressing instead.
Source Expanded Source
:
INCR Macro :
A 1, data :
A 2, data :
A 3, data :
MEND :
: A 1, Data
: A 2, Data

UNIT I | 1.56

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING

INCR A 3, Data
: :
: :
INCR :
: A 1, Data
: A 2, Data
data DC F’5’ A 3, Data
: data Dc F’5’
:
END

1.3.2 Nested Macro Calls and Definition

Fig. 1.21

• Macro-definition, which itself is a sequence of instruction, can be abbreviated by using


macro.
• It is important to note that inner macro is not defined until the outer macro is called.
• The user might call the above macro with the statement
• Define-macro add1
• When this call is executed then the macro ADD1 will be defined. So after the above call
user an call the ADD1 macro in following way :
• ADD1 data1.
• The macro processor will generate the sequence.
L 1, data1
A 1, = F’0’
ST 1, data1.
Macro-Calls within Macro
UNIT I | 1.57

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


• Macros are abbreviations of instruction sequence. Such abbreviations are also present
within other macro definition.
Source Code Expanded Code (Level 1) Expanded Code (Level 2)
:
:
ADD MACRO &arg
L 1, &arg
A 1, = F ‘10’
ST 1, &arg
MEND
ADDS MACRO &arg1,
&arg2, arg3
ADD1 &arg1
ADD1 &arg2 Expansion of ADD1
ADD1 &arg3
MEND L 1 data1
Expansion of ADDS A 1, = F ‘10’
MAIN : ST 1, data1
: :
: ADD1 data1 L 1 data2
ADDS data1, data2, data3 ADD1 data2 A 1, = F ‘10’
: ADD1 data3 ST 1, data2
: :
data1 DC F’5’ : L 1, data3
data2 DC F’6’ : A 1, = F’10
data3 DC F’7’ : ST 1, data3
END data1 DC F’5’
data2 DC F’6’
data3 DC F’7’ data1 DC F’5’
END data2 DC F’6’
data3 DC F’7’
END

UNIT I | 1.58

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


1.3.2.1 How to Handle Nested Macro Calls
There are three ways to handle nested macro calls :
1. Several levels of expansion
2. Recursive expansion
3. Use of stack during expansion
1. Several Levels of Expansion
Let’s consider macro call
COMPUTE X ,Y, Z
Call can be expanded using the algorithm of macro expansion which will divided above call
into following calls :
COMPUTE X
COMPUTE Y
COMPUTE Z
The expanded code itself contains macro calls. The macro expansion algorithm can be
applied to the first level expanded code to expand these macro calls and so on, until we
obtain a code which does not contain any macro calls.
This method requires several passes of expansion that’s why not preferred.
2. Recursive Expansion
To handle nested macro calls, the macro expansion function should be able to work
recursively. During recursion, while processing one macro the processing of inner macro can
begin and after the expansion of inner macro finishes, the processing of outer macro will
continue.
Recursion is handled through stack, where local variables and return address are stored onto
the stack before making a recursive call.
3. Use of Stack During Expansion
• Nested macro calls can be handled through explicit stack.
• Macro calls are handled in LIFO manner.
• Stack can be used to accommodate the expansion time data structures.
• Expansion time data structures include :
(a) MEC : Macro expansion counter.
(b) Actual parameter table.
Expansion time data structure is stored in an activation record.

Fig. 1.22 : Activation record

UNIT I | 1.59

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


Every call to macro involves pushing and activation record onto the stack.
At the end of the macro expansion and activation record is removed from the stack. The top
of the stack can be shifted to the next record the following operation.
Top= stack[top] -1

1.3.3 Advance Macro Facility


Macro Instructions Arguments
Source Expanded Source
:
INCR MACRO &arg (Macro name with one argument) :
A1, &arg :
A2, &arg :
A3, &arg :
MEND A1, data1
: A2, data1
: A3, data1
INCR data1 (use of data1 as operand) :
: A1, data2
INCR data2 (use of data2 as operand) A2, data2
data1 DC F’5’ A 2, data2
data2 DC F’6’ A 3, data2
: :
END :
: data1 DC F’5’
data2 DC F’5’
Example 1.4 : More than one arguments.
Source Expanded Source
:
INCR MACRO &arg1, &arg2, &arg3 :
A1, &arg1 :
A2, &arg2 :

UNIT I | 1.60

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING

A3, &arg3 :
MEND :
: :
: :
: :
LOOP1 INCR data1, data2, data3 LOOP1 A1, data1
: A2, data2
: A3, data3
: :
: :
LOOP2 INCR data3, data2, data1 LOOP2 A1, data1
: A2, data2
: A3, data3
data1 DC F’5’ :
data1 DC F’6’ data1 DC F’5’
data1 DC F’7’ data1 DC F’6’
data1 DC F’7’
1.3.3.1 Two ways of Specifying Arguments to a Macro Call
(A) Positional Argument
Argument are matched with dummy arguments according to order in which they appear.
• INCR A,B,C
• ‘A’ replaces first dummy argument.
• ‘B’ replaces second dummy argument.
• ‘C’ replaces third dummy argument.
(B) Keyword Arguments
All the macro instruction definitions used positional parameters. Parameters and arguments
are matched according to their positions in the macro prototype and the macro invocation
statement. The programmer needs to be careful while specifying the arguments. If an
argument is to be omitted the macro invocation statement must contain a null argument
mentioned with two commas.
Positional parameters are suitable for the macro invocation. But if the macro invocation has
large number of parameters, and if only few of the values need to be used in a typical
UNIT I | 1.61

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


invocation, a different type of parameter specification is required (for example, in many cases
most of the parameters may have default values, and the invocation may mention only the
changes from the default values).
Example : XXX MACRO &P1, &P2, ….,&P20, ….
XXX A1, A2,,,,,,,,,,…,,A20,…..
Null arguments
1.3.3.2 Keyword Parameters
• Each argument value is written with a keyword that names the corresponding parameter.
• Arguments may appear in any order.
• Null arguments no longer need to be used.
• Ex : XXX P1=A1, P2=A2, P20=A20.
• It is easier to read and much less error-prone than the positional method.
• This allows reference to dummy arguments by name as well as by position.
Example : INCR &arg1 = A,&arg3 = C, &arg2 =’B’
Example : INCR &arg1 = &arg2 = A, &arg2 =’C’
1.3.3.3 Concatenation of Unique Labels
Most macro processor allows parameters to be concatenated with other character strings.
Suppose that a program contains a series of variables named by the symbols XA1, XA2,
XA3,…, another series of variables named XB1, XB2, XB3,…, etc. If similar processing is to be
performed on each series of labels, the programmer might put this as a macro instruction.
The parameter to such a macro instruction could specify the series of variables to be
operated on (A, B, etc.). The macro processor would use this parameter to construct the
symbols required in the macro expansion (XA1, Xb1, etc.).
Suppose that the parameter to such a macro instruction is named &ID. The body of the
macro definition might contain a statement like :
LDA X&ID1
TOTAL MACRO &ID LAD XA1
LAD X&ID1 TOTAL A ADD XA2
ADD X&ID2 STA XA3
STA X&ID3
MEND
and is the starting character of the macro instruction; but the end of the parameter is not
marked. So in the case of &ID1, the macro processor could deduce the meaning that was

UNIT I | 1.62

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


intended. If the macro definition contains contain &ID and &ID1 as parameters, the situation
would be unavoidably ambiguous.
Most of the macro processors deal with this problem by providing a special concatenation
operator.
In the SIC macro language, this operator is the character → Thus the statement.
LDA X&ID1 can be written as :
LDA X&ID →.
ID123 MACRO &ID
LAD X&ID→1
ADD X&ID→2
STA X&ID→3
MEND
1 SUM MACRO &ID
2 LDA X&ID→1
3 ADD X&ID→2
4 ADD X&ID→3
5 STA X&ID→S
6 MEND
SUM A SUM BETA
 
LDA XA1 LDA XBEATA1
ADD XA2 ADD XBEATA2
ADD XA3 ADD XBEATA3
In above example the statement SUM A and SUM BETA shows the invocation statements and
the corresponding macro expansion.
1.3.3.4 Generation of Unique Labels
As discussed it is not possible to use labels for the instructions in the macro definition, since
every expansion of macro would include the label repeatedly which is not allowed by the
assembler. This in turn forces us to use relative addressing in the jump instructions. Instead
we can use the technique of generating unique labels for every macro invocation and
expansion. During macro expansion each $ will be replaced with $XX, where xx is a two
character alphanumeric counter of the number of macro instructions expansion.

UNIT I | 1.63

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


For example, XX = AA, AB, AC… This allows 1296 macro expansions in a single program. The
following program shows the macro definition with labels to the instruction.
25 RDBUFF MACRO &INDEV, &BUFADR, & RECLTH
30 CLEAR X CLEAR LOOP COUNTER
35 CLEAR A
40 CLEAR S
45 +LDT #4096 SET MAXIMUM RECORD LENGTH
50 $LOOP TD =X’&INDEV’ TEST INPUT DEVICE
55 JEQ $LOOP LOOP UNTIL READY
60 RD =X’&INDEV’ READ CHARACTER INTI REG A
65 COMPR A, S TEST FOR END OF RECORD
70 JEQ $EXIT EXIT LOOP IF EOR
75 STCH &BUFADR, X STORE CHARACTHER IN BUFFER
80 TIXR $LOOP HAS BEEN REACHED
90 $EXIT STX &RECLTH SAVE RECORD LENGTH
MEND
The following table shows the macro invocation and expansion first time.
 RDBUFF F1, BUFFER, LENGTH
30 CLEAR X CLEAR LOOP COUNTER
35 CLEAR A
40 CLEAR S
45 +LDT #4096 SET MAXIMUM RECORD LENGTH
50 $AALOOP TD =X’F1’ TEST INPUT DEVICE
55 JEQ $AALOOP LOOP UNTIL READY
60 RD =X’F1’ READ CHARACTER INTI REG A
65 COMPR A, S TEST FOR END OF RECORD
70 JEQ $AAEXIT EXIT LOOP IF EOR
75 STCH BUFFER, X STORE CHARACTHER IN BUFFER
80 TIXR $AALOOP HAS BEEN REACHED
90 $AAEXIT STX &RECLTH SAVE RECORD LENGTH
MEND

UNIT I | 1.64

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


If the macro is invoked second time the labels may be expanded as $ABLOOP $ABEXIT.
1.3.3.5 Conditional Macro Expansion
There are applications of macro processors that are not related to assemblers or assembler
programming. Conditional assembly depends on parameters provides
MACRO &COND
……..
IF (&COND NE „‟)
part I
ELSE
part II
ENDIF
………
ENDM
Part I is expanded if condition part is true, otherwise part II is expanded.
Compare operators : NE, EQ, LE, GT.
1.3.3.6 Macro-Time Variables
Macro-time variables (often called as SET Symbol) can be used to store working values
during the macro expansion. Any symbol that begins with symbol & and not a macro
instruction parameter is considered as macro-time variable. All such variables are initialized
to zero.
25 RDBUFF MACRO &INDEV, &BUFADR, &REACH, &FOR, &MAXITH
26 IF (&EOR NE ‘ ‘)
27 &EORCK SET 1
28 ENDIF
30 CLEAR X CLEAR LOOP COUNDTER
35 CLEAR A
38 IF (&EORCK EQ
1)
40 Macro- LDCH =X’&EOR’ SET FOR COUNTER
Time
42 Variable RMO A, S

UNIT I | 1.65

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING

43 ENDIF
44 IF (&MAXLTH
EQ ‘ ‘)
45 +LDT #4096 SET MAX LENGTH = 4096
46 $AAEXIT ELSE
47 +LDT #&MAXLTH SET MAXIMUM RECORD LENGTH
48 ENDIF
50 $LOOP TD =X’&INDEV’ TEST INPUT DEVICE
55 JEQ $LOOP LOOP UNTIL READY
60 RD =X’&INDEV’ READ CHARACTER INTI REG A
63 IF (&EORCK EQ
1)
65 COMPR A, S TEST FOR END OF RECORD
70 JEQ $EXIT EXIT LOOP IF EOR
73 ENDIF
75 STCH &BUFADR, X STORE CHRACTHER IN BUFFER
80 TIXR T LOOP UNLESS MAXIMUM LENGTH
85 JLT $LOOP HAS BEEN REACHED
90 $EXIT SIX &RECLTH SAVE RECORD LENGTH
95 MEND
Above Program a gives the definition of the macro RDBUFF with the parameters &INDEV,
&BUFADR, &RECLTH, &EOR, &MAXLTH. According to the above program if &EOR has any
value, then &EORCK is set to 1 by using the directive SET, otherwise it retains its default value
0.
Use of Macro-Time Variable with EOF being Not Null
 RDBUFF F31 BUF, RECL, 04, 2048
30 CLEAR X CLEAR LOOP COUNTER
35 CLEAR A
40 LDTH =X’04’ SET FOR CHARACTHER
42 RMO A, S
47 +LDT #2048 SET MAXIMUM RECORD LENGTH

UNIT I | 1.66

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING

50 $AALOOP TD =X’F3’ TEST INPUT DEVICE


55 JEQ $AALOOP LOOP UNTIL READY
60 RD =X’F3’ TEST INPUT DEVICE
65 COMPR A, S TEST FOR END OF RECORD
70 JEQ $AAEXIT EXIT LOOP IF EOR
75 STCH BUF, X STORE CHARACTE IN BUFFER
80 TIXR T LOOP UNLESS MAXIMUM LENGTH
85 JLT $AALOOP HAS BEEN REACHED
90 $AAEXIT STX RECL SAVE RECORD LENGTH
Use of Macro-Time Conditional Statement with EOF being Null
 RDBUFF OE, BUFFER, LENGTH, , 80
30 CLEAR X CLEAR LOOP COUNTER
35 CLEAR A
47 +LDT #80 SET MAXIMUM RECORD FOR LENGTH
50 $ABLOOP TD =X’0E’ TEXT INPUT DEVICE
55 JEQ $ABLOOP LOOP UNTIL READY
60 RD =X’0E’ READ CHARACTER IN REG A
75 STCH BUFFER, X STORE CHARACTER IN BUFFER
80 TIXR T LOOP UNLESS MAXIMUM LENGTH
87 JLT $ABLOOP HAS BEEN REACHED
90 $ABEXIT STX LENGTH SAVE RECOD LENGTH
Use of Time-Variable with EOF Not Null and Maxlength Being Null
 RDBUFF F1. BUF, ELENG, 04
30 CLEAR X CLEAR LOOP COUNTER
35 CLEAR A
40 LDCH =X’04’ SET EOR CHARACTER
42 RMO A, S
45 +LDT #4096 SET MAX LENGTH = 4096
50 $ACLOOP TD =X’F1’ TEST INPUT DEVICE
55 JEQ $ACLOOP LOOP UNTIL READY

UNIT I | 1.67

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING

60 RD =X’F1’ READ CHARATHER INTI REG A


65 COMPR A.S TEST FOR END OF RECORD
70 JEQ $ACEXIT EXIT LOOP IF EOR
75 STCH BUF, X STORE CHARACTE IN BUFFER
80 TIXR T LOOP UNLESS MAXIMUM LENGTH
85 JLT $ACLOOP HAS BEEN REACHED
90 $ACEXIT STX RLENG SAVE RECORD LENGTH
The above programs show the expansion of Macro invocation statements with different
values for the time variables. In figure b the &EOF value is NULL. When the macro invocation
is done, IF statement is executed, if it is true EORCK is set to 1, otherwise normal execution of
the other part of the program is continued.
The macro processor must maintain a symbol table that contains the value of all macro-time
variables used. Entries in this table are modified when SET statements are processed. The
table is used to look up the current value of the macro-time variable whenever it is required.
When an IF statement is encountered during the expansion of a macro, the specified Boolean
expression is evaluated.
If the value of this expression TRUE,
• The macro processor continues to process lines from the Macro Definition Table (MDT)
until it encounters the
• ELSE or ENDIF statement.
• If an ELSE is found, macro processor skips lines in MDT until the next ENDIF.
• Once it reaches ENDIF, it resumes expanding the macro in the usual way.
If the value of the expression is FALSE,
• The macro processor skips ahead in MDT until it encounters next ELSE or ENDIF
statement.
• The macro processor then resumes normal macro expansion.
The macro-time IF-ELSE-ENDIF structure provides a mechanism for either generating(once)
or skipping selected statements in the macro body. There is another construct WHILE
statement which specifies that the following line until the next ENDW statement, are to be
generated repeatedly as long as a particular condition is true. The testing of this condition,
and the looping are done during the macro is under expansion. The example shown below
shows the usage of Macro-Time Looping statement.
1.3.3.7 WHILE-ENDW Structure

UNIT I | 1.68

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


• When an WHILE statement is encountered during the expansion of a macro, the specified
Boolean expression is evaluated.
• TRUE
1. The macro processor continues to process lines from MDT until it encounters the
next ENDW statement.
2. When ENDW is encountered, the macro processor returns to the preceding WHILE,
reevaluates the Boolean expression, and takes action based on the new value.
• FALSE
1. The macro processor skips ahead in MDT until it finds the next ENDW statement and
then resumes normal macro expansion.
25 RDBUFF MACRO &INDEV, &BUFADR, &RECLTH, &EOR
27 &EORCT SET %NITEMS (&EOR) Macro processor function
30 CLEAR X CLEAR LOOP COUNTER
35 CLEAR A
45 +LDT #4096 SET MAX LENGTH = 4096
50 $LOOP TD =X’&INDEV’ TEST INPUT DEVICE
55 JEQ $LOOP LOOP UNTIL READY
60 RD =X’&INDEV’ READ CHARACTER INTO REG A
63 &CTR SET 1 TEST INPUT DEVICE
64 WHILE (&CTR LE TEST FOR END OF RECORD
&EORCT)
70 COMPR =X’0000&EOR[&CTR] List index
71 &CTR SET &CTR + 1
73 ENDW
75 STCH &BUFADR, X STORE CHARACTER IN BUFFER
80 TIXR T LOOP UNLESS MAXIMUM LENGTH
85 JLT $LOOP HAS BEEN REACHED
90 $EXIT STX &RECLTH SAVE RECORTD LENGTH

. REDBUFF F2, BUFFER, LENGTH, (00, 03, 04)


30 CLEAR X CLEAR LOOP COUNTER
35 CLEAR A

UNIT I | 1.69

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


45 +LDT #4096 SET MAX LENGTH = 4096
50 $AALOOP TD =X’F2’ TEST INPUT DEVICE
55 JEQ $AALOOP LOOP UNTIL READY
60 RD =X’F2’ READ CHARACTER INTO REG A
65 COMP =X’000000’
70 JEQ $AAEXIT
65 COMP =X’000003’
70 JEQ $AAEXIT
65 COMP =X’000004’
70 JEQ $AAEXIT
75 STCH BUFFER, X STORE CHARACTER IN BUFFER
80 TIXR T LOOP UNLESS MAXIMUM LENGTH
85 JLT $AALOOP HAS BEEN REACHED
90 $AAEXIT STX LENGTH SAVE RECORTD LENGTH

1.3.4 Design of Macro Processor


Macro pre-processor takes a source program containing macro definitions and macro calls
and translates into an assembly language program without any macro definitions or calls.
This program can now be handled over to a conventional assembler to obtain the target
language as shown in following Fig. 1.23.
It is possible to implement a macro pre-processor which processes macro definition and
macro calls for the purpose of expansions.

Fig. 1.23 : A scheme for a macro pre-processor


There are four basic task that a macro-processor must perform :
1. Recognize Macro Definition

UNIT I | 1.70

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


A macro instruction processor must recognize macro definition identified by MACRO &
MEND pseudo – operations.
2. Save the Definition
The processor must store the macro – definitions which will be needed at the time of
expansion of calls.
3. Recognize the Macro Call
The processor must recognize macro calls that appear as operation mnemonics.
4. Expand Calls & Substitute Arguments
The macro call is replaced by the macro definition. The dummy arguments are replaced
by the actual data.
General Design Steps
Step 1 : Specification of Problem.
Step 2 : Specification of databases.
Step 3 : Specification of database formats.
Step 4 : Algorithm.
Specify the Problem
• In Pass-1 the macro definitions are searched and stored in the macro definition table and
the entry is made in macro name table
• In Pass-2 the macro calls are identified and the arguments are placed in the appropriate
place and the macro calls are replaced by macro definitions.
1.3.4.1 Specification of Databases
Pass 1 :
• The input macro source program.
• The output macro source program to be used by Pass 2.
• Macro-Definition Table (MDT), to store the body of macro definitions.
• Macro-Definition Table Counter (MDTC), to mark next available entry MDT.
• Macro- Name Table (MNT), used to store names of macros.
• Macro Name Table counter (MNTC), used to indicate the next available entry in MNT.
• Argument List Array (ALA), used to substitute index markers for dummy arguments
before storing a macro- definitions.
Pass 2 :
• The copy of the input from Pass 1.

UNIT I | 1.71

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


• The output expanded source to be given to assembler.
• MDT, created by Pass 1.
• MNT, created by Pass 1.
• Macro-Definition Table Pointer (MDTP), used to indicate the next line of text to be used
during macro-expansion.
• Argument List Array (ALA), used to substitute macro-call arguments for the index markers
in the stored macro-definitions.
Specification of Database Format
Macro Definition Table 80 bytes per entry
Index Card
:
15 &Lab INCR &arg1, &arg2, & arg3
16 #0 A 1, # 1
17 A 2, # 2
18 A 3, # 3
19 MEND
: :
• MDT is a table of text lines.
• Every line of each macro definition except the MACRO line, is stored in the MDT (MACRO
line is useless during macro-expansion).
• MEND is kept to indicate the end of the definition.
• The macro-name line is retained to facilitate keyword argument replacement.
Macro Names Table (MNT)
Macro Name Table
Index 8bytes 4bytes
Name MIDT index
: :
3 “INCR” 15
: :
: :
• Each MNT entry consists of

UNIT I | 1.72

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


1. A character string (the macro name) and
2. A pointer (index) to the entry in MDT that corresponds to the beginning of the
macro-definition.(MDT index).
1.3.4.2 Argument List Array (ALA)
• ALA is used during both Pass 1 & Pass 2 but for some what reverse functions.
• During Pass 1, in order to simplify later argument replacement during macro expansion,
dummy arguments are replaced with positional indicators when definition is stored.
Ex. # 1, # 2, # 3 etc.
• The ith dummy argument on the macro-name is represented in the body by #i.
• These symbols are used in conjunction with ALA prepared before expansion of a macro-
call.
• Symbolic dummy argument are retained on macro-name to enable the macro processor
to handle argument replacement byname rather by position.
During Pass 1
Macro Definition Table MDT
:
& lab INCR & arg1, & arg 2, & arg3
#0 A 1, # 1
A 2, # 2
A 3, # 3
MEND
:
During Pass-2
• During Pass2 it is necessary to substitute macro call arguments for the index markers
stored in the macro.
• Thus upon encountering the call expander would prepare a ALA as shown below :
(1) LOOP 1NCR DATA1, DATA2, DATA3,}
(2) Call in main program.
(3) ALA is prepare of after this call is encountered.
Argument List Arry
Index Argument
0 “LOOP 1bbb”

UNIT I | 1.73

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


1 “DATA 1bbb”
2 “DATA 2bbb”
3 “DATA 3bbb”
Algorithm for Pass 1
• Pass 1 of macro processor makes a line-by-line scan over its input.
• Set MDTC = 1 as well as MNTC = 1.
• Read next line from input program.
• If it is a MACRO pseudo-op, the entire macro definition except this (MACRO) line is
stored in MDT.
• The name is entered into Macro Name Table along with a pointer to the first location of
MDT entry of the definition.
• When the END pseudo-op is encountered all the macro-defination have been processed,
so control is transferred to pass 2

UNIT I | 1.74

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING

Fig. 1.24 : Pass 1 of two pass macro processor


Algorithm for Pass 2
• This algorithm reads one line of input program at a time.
• for each Line it checks if op-code of that line matches any of the MNT entry.
• When match is found (i.e. when call is pointer called MDTF to corresponding macro
defination stored in MDT.
• The initial value of MDTP is obtained from MDT index field of MNT entry.
• The macro expander prepares the ALA consisting of a table of dummy argument indices
& corresponding arguments to the call.
• Reading proceeds from the MDT, as each successive line is read, The values form the
argument list one substituted for dummy arguments indices in the macro defination.
• Reading MEND line in MDT terminates expansion of macro & scanning continues from
the input file.
UNIT I | 1.75

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


• When END pseudo-op encountered, the expanded source program is given to the
assembler.

Fig. 1.25 : Pass 2 of two pass macro processor


Example 1.5 : Show macro name table, macro definition table and expanded code for the
following assembly language code
MACRO
CLEAR &X, &N
LCL &M
&M SET 0
MOVER AREG, = ‘0’
MORE MOVEM AREG, &X + & M
&M SET &M + 1
AIF (&M NE & N)  MORE
UNIT I | 1.76

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


MEND
MACRO
MAIN &W
SUB AREG, &W
CLEAR &W, 3
MULT AREG, = ‘5’
MEND
START
MAIN B
B DS 4
END
Solution :
Macro Name Table :
NAME MOTP SSTP
CLEAR 0 30
MAIN 8 –
macro definition table :
Label Opcode Rest
0 CLEAR &X, &N
1 LCL (E, 1)
2 (E, 1) SET 0
3 MOVER AREG, = ‘0’
4 MOVEM AREG, #0 + (E, 1)
5 (E, 1) SET (E, 1) + 1
6 AIF ((E, 1) NE #1) (S : 1)
7 MEND
8 MAIN &W
9 SUB AREG, #0
10 CLEAR #0, 3
11 MULT AREG, = ‘5’

UNIT I | 1.77

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING

12 MEND
Where SSTP : Sequencing Symbol Table Points
(E, 1) First Expansion Variable
(S, 1) First Sequency Symbol
#0, #1 : parameter
Expanded Code
START
+SUB AREG, B
+ MOVER AREG, = ‘0’
+ MOVERM AREG, B
+ MOVEM AREG, B+ 1
+ MULT AREG, = ‘5
B DS 4
END
Example 1.6 : Consider the following code segment and show the content of
(a) Macro name table
(b) Macro definition table
(c) Argument list any
1 MACRO
2 INCR &A, &B, & REG
3 MOVER &REG, &A
4 ADDS &A, &B
5 MOVEM &REG, &A
6 MEND
7 MACRO
8 ADDS &F, &S
9 MOVER AREG, &F
10 ADD AREG, &S
11 MOVEM AREG, &S
12 WRITE &S
13 MEND

UNIT I | 1.78

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


14 MACRO
15 SUBS &F, &S
16 MOVER BREG, &F
17 SUB BREG, &S
18 MOVEM BREG, &S
19 WRITE &S
20 MEND
21 START 200
22 READ N1
23 READ N2
24 ADDS N1, N2
25 SUBS N1, N2
26 INCR N1, N2, DREG
27 STOP
28 N1 DS 2
29 N2 DS 2
At the end of pass 1
Solution :
MNT
NAME MDTP
0 INCR 0
1 ADDI 5
2 SUBS 11
MDT
OPCODE RESB
0 INCR & A, & B, & REG
1 MOVER #2, #0
2 ADDS #0, #1
3 MOVEM #2, #0
4 MEND
5 ADDS &F, &S
6 MOVER AREG, #0
7 ADD AREG, #1

UNIT I | 1.79

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


8 MOVEM AREG, #1
9 WRITE #1
10 MEND
11 SUBS &F, &S
12 MOVER BREG, #0
13 SUB BREG, #1
14 MOVEM BREG, #1
15 WRITE #1
16 MEND
on the expansion of line 24- ADDS N1,N2 :
ALA 0 N1
1 N2
on expansion of line 25- SUBS N1,N2
ALA
0 N1
1 N2
on expansion of line 26- INCR N1,N2,DREG
ALA 0 N1
1 N2
2 DREG
Example 1.7 : Consider the following program segment
MACRO
DEFINE & XYZ
MACRO
&XYZ &X, &Y, &OP
MOVER AREG, &X
&OP AREG, &Y
MOVEM AREG, &X
MEND

UNIT I | 1.80

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


MEND
MACRO
COMPUTE &F, &S
MOVEM BREG, TMP
INCRM &F, &S, BREG
MOVER BREG, TMP
MEND
MACRO
INCRM &M, &I, & R
MOVER &R, &M
ADD &R, &I
MOVEM &R, &M
MEND
START 100
DEFINE CACL
COMPUTE X, Y
CALC A, B, MULT
END
(a) Show the contents of MDT and MNT after macro processing..
(b) Show expanded assembly language program.
Solution :
MNT
NAME MDTP
DEFINE 0
COMPUTE 8
INCRM 13
CALC 18
MDT
OPCODE RESB

UNIT I | 1.81

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING

0 DEFINE & XYZ


1 MACRO
2 #0 &X, &Y, &OP
3 MOVER AREG, &X
4 &OP AREG, &Y
5 MOVEM AREG, &Z
6 MEND
7 MEND
8 COMPUTE &F, &S
9 MOVEM BREG, TMP
10 INCRM #0, #1, BREG
11 MOVER BREG, TMP
12 MEND
13 INCRM &M, &I, &R
14 MOVER #2, #0
15 ADD #2, #1
16 MOVEM #2, #0
17 MEND
18 CALC &X, &Y, &OP
19 MOVER AREG, #0
20 #2 AREG, #1
21 MOVEM AREG, #0
22 MEND
Expanded assembly language program.
START 100
MOVEM BREG, TMP
MOVER BREG, X
ADD BREG, Y
MOVEM BREG, X
MOVER BREG, TMP
MOVER AREG, A
MULT AREG, B

UNIT I | 1.82

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


MOVEM AREG, A
END
Because : on calling macro DEFINE CALG on code will be generated.
on calling COMPUTE X, Y following code will get generated
MOVEM BREG, TMP
MOVER BREG, X
on calling
ADD BREG, Y
INCR M
MOVEM BREG, X
MOVER BREG, TNP
Now on executing CALC A, B, MULT it calls CALC inside DIFINE as &XYZ so it becomes
MOVER AREG, A
MULT AREG, B
MOVEM AREG, A
Example 1.8 : Consider the following code, show the content of macro name table R mean
definition table.
START 100
SR 2, 2
USING *, 15
MACRO
XYZ &A
A 1, &A
AR 2, 2
MEND
L 1, D1
MACRO
ABC &Z
SR 3, 3
MACRO
DISPLAY
XYZ B
MEND
UNIT I | 1.83

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING

L 1, &Z
MEND
XYZ B1
SR 4, 4
ABC B1
D1 DC F’4’
B1 DC F’5’
END
Solution :
MNT
NAME MDTP
XYZ 0
ABC 4
DISPLAY 12
MDT
OPCODE RESB
0 XYZ &A
1 A 1, # 1
2 AR 2, 2
3 MEND
4 ABC &Z
5 SR 3, 3
6 MACRO
7 DISPLAY
8 XYZ B
9 MEND
10 L 1, #1
11 MEND
12 DISPLAY
13 XYZ B

UNIT I | 1.84

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING

14 MEND
Expanded Code
START 100
SR 2, 2
USING *, 15
L 1, D1
+A 1, B1 ; xyz is expanded
+AR 2, 2
SR 4, 4
+SR 3, 3 ; ABC B1 is expanded
+L 1, B1
D1 DC F’4’
B1 DC F’5’
END

1.3.5 A One-Pass Macro Processor


One-Pass Macro Processor
• A one-pass macro processor that alternate between macro definition and macro
expansion in a recursive way is able to handle recursive macro definition.
• Restriction :
1. The definition of a macro must appear in the source program before any statements
that invoke that macro.
2. This restriction does not create any real inconvenience.
• The design considered is for one-pass assembler. The data structures required are :
1. MDT (Definition Table) :
• Stores the macro definition including macro prototype and macro body
• Comment lines are omitted.
• References to the macro instruction parameters are converted to a positional
notation for efficiency in substituting arguments.
2. MNT (Name Table) :
• Stores macro names.

UNIT I | 1.85

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


• Serves as an index to MDT.
• Pointers to the beginning and the end of the macro definition (MDT).
3. ALA (Argument Table) :
• Stores the arguments according to their positions in the argument list.
• As the macro is expanded the arguments from the Argument table are
substituted for the corresponding parameters in the macro body.

Fig. 1.26
Algorithm :
begin {macro processor}
EXPANDING : = FALSE
while OPCODE ≠ ‘END’ do
begin
GETLINE
PROCESSLINE
end {while}
end {macro processor}
procedure PROCESSLINE
begin
search MNT for OPCODE
if found then
EXPAND
else if OPCODE = ‘MACRO’ then

UNIT I | 1.86

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


DEFINE
else write source line to expanded file
end {PROCESSLINE}
Procedure DEFINE :
begin
enter macro name into MNT
enter macro prototype into MDT
LEVEL = 1;
while (LEVEL > 0) do
begin
GETLINE
if this is not comment line
begin
substitute positional notation for parameters
enter line into MDT
if (OPCODE = ‘MACRO’) then
LEVEL = LEVEL +1;
else if (OPCODE = ‘MEND’) then
LEVEL = LEVEL –1;
end {if not comment}
end{while}
store in MNT pointer to beginning and end of definition
end {DEFINE}
procedure EXPAND
begin
EXPANDING : = TRUE
get first line of macro definition {prototype} from MDT
set up arguments from macro invocation in ALA
write macro invocation to expanded file as a comment
while not end of macro definition do
begin
GETLINE
PROCESSLINE
UNIT I | 1.87

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


end {while}
EXPANDING : = FALSE
end {EXPAND}
procedure GETLINE
begin
if EXPANDING then
begin
get next line of macro definition from MDT
substitute arguments from ALA for positional notation
end {if}
else
read next line from input file
end {GETLINE}
Comparison of Macro Processor Design
One-Pass Algorithm
• Every macro must be defined before it is called.
• One-pass processor can alternate between macro definition and macro expansion.
• Nested macro definitions are allowed but nested calls are not allowed.
Two-Pass Algorithm
• Pass 1 : Recognize macro definitions
• Pass 2 : Recognize macro calls
• Nested macro definitions are not allowed.
Program for Two Pass Macro Processor
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct macroNameTab
{
char macroName[11];
int no_of_pp, mdtp;

UNIT I | 1.88

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


}mnt[20];
struct pnTab
{
char pName[8];
}pntab[30];
struct macroDefTab
{
char label[8], opcode[11], operand[40];
}mdt[80];
struct apTab
{
char value[8];
}aptab[10];
//global variables
char srcfile[11], buffer[80];
char tok[12][40];
int mdt_ptr, mnt_ptr, pntab_ptr;
int mec, aptab_ptr;
FILE *fp,*fd;
int num,ntok,srch,i,j,k;
/*--------------------------------------------------------------------
function to process the macro definition
--------------------------------------------------------------------*/
void show_file(charfname[])
{
FILE *fp;
if((fp = fopen(fname, "r"))== NULL)
{
printf("\n\tError : Can't open file ..!");
getch();

UNIT I | 1.89

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


exit(0);
}
while(fgets(buffer, 80, fp))
printf("%s", buffer);
}
/*--------------------------------------------------------------
to set the pntab tables
--------------------------------------------------------------*/
void set_tables()
{
int pp=0;
fgets(buffer, 80, fp);
ntok = sscanf(buffer, "%s%s%s%s%s%s",tok[0],tok[1],tok[2],
tok[3], tok[4], tok[5]);
strcpy(mnt[mnt_ptr].macroName, tok[0]);
mnt[mnt_ptr].mdtp = mdt_ptr;
strcat(tok[ntok-1], ",");
for(num =1 ; num<ntok; num++)
{
k=strlen(tok[num])-1;
tok[num][k] = '\0';
strcpy(pntab[pntab_ptr++].pName, tok[num]);
pp++;
}
mnt[mnt_ptr].no_of_pp = pp;
}
/*--------------------------------------------------------------------
function to process the macro definition
-------------------------------------------------------------------*/
void pass_one()

UNIT I | 1.90

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


{
if( (fp = fopen("newmac.asm", "r"))==NULL )
{
printf("\n\tError : Can't open file ..!");
getch();
exit(0);
}
//to init all the pointers
mdt_ptr = mnt_ptr=0;
while(fgets(buffer, 80, fp))
{
if(strstr(buffer, "MACRO"))
{
pntab_ptr = 0;
set_tables();
while(fgets(buffer, 80, fp))
{
ntok = sscanf(buffer, "%s%s%s%s%s%s",tok[1],tok[2],tok[3]);
if(strstr(buffer, "MEND"))
{
strcpy(mdt[mdt_ptr].label, "");
strcpy(mdt[mdt_ptr].opcode, "MEND");
strcpy(mdt[mdt_ptr++].operand, "");
mnt_ptr++;
break;
}
else
{
if(tok[2][0] == '&')
{

UNIT I | 1.91

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


k=strlen(tok[2])-1;
tok[2][k] = '\0';
k = search_pntab(tok[2]);
sprintf(tok[2], "(P, %d),", k);
}
if(tok[3][0] == '&')
{
k = search_pntab(tok[3]);
sprintf(tok[3], "(P, %d)", k);
}
strcpy(mdt[mdt_ptr].label, "");
strcpy(mdt[mdt_ptr].opcode, tok[1]);
sprintf(mdt[mdt_ptr++].operand, "%s %s", tok[2], tok[3]);
}
}
}
}
}
/*--------------------------------------------------------------
to search pntab
--------------------------------------------------------------*/
int search_pntab(charstr[])
{
for(i=0; i<pntab_ptr; i++)
if(!strcmpi(str, pntab[i].pName))
return i;
return -1;
}
/*--------------------------------------------------------------
to print mdt

UNIT I | 1.92

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


--------------------------------------------------------------*/
void print_mdt()
{
printf("\n\t---------------------------------------------------------");
printf("\n\t| No| LABEL | OPCODE | OPERANDS |");
printf("\n\t---------------------------------------------------------");
for(i=0; i<mdt_ptr; i++)
printf("\n\n\t|%2d]|%7s|%8s|%35s |", i, mdt[i].label, mdt[i].opcode,
mdt[i].operand);
}
/*--------------------------------------------------------------
to print mnt
--------------------------------------------------------------*/
void print_mnt()
{
printf("\n\t-------------------------");
printf("\n\t| Name | #PP | MDTP |");
printf("\n\t-------------------------");
for(i=0; i<mnt_ptr; i++)
printf("\n\t| %8s|%5d|%5d|"//%5d|%6d|%7d|%6d |"
, mnt[i].macroName, mnt[i].no_of_pp, mnt[i].mdtp);
}
/*-----------------------------------------------------------
to search the mnt for a macro name
-----------------------------------------------------------*/
int search_mnt(charstr[])
{
for(i=0; i<mnt_ptr; i++)
if(!strcmpi(str, mnt[i].macroName))
return i;

UNIT I | 1.93

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


return -1;
}
/*-----------------------------------------------------------
to expand the macro
-----------------------------------------------------------*/
int cal(chartok[])
{
char name[20];
for(j=0; tok[j] != ')';j++)
name[j] = tok[j];
name[j] = '\0';
return(atoi(name));
}
void pass_two()
{
if((fp = fopen("newmac.asm", "r"))== NULL)
{
printf("\n\tError : cannot open file..!");
exit(0);
}
if((fd = fopen("inter.int","w"))== NULL)
{
printf("\n\tError : cannot open file..!");
exit(0);
}
while(fgets(buffer, 80, fp))
{
if(strstr(buffer, "MACRO")!=NULL)
{
while(fgets(buffer,80,fp) )

UNIT I | 1.94

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


{
if((strstr(buffer, "MEND"))!=NULL)
break;
}
continue;
}
ntok = sscanf(buffer, "%s%s%s%s%s%s%s",tok[1], tok[2],
tok[3],tok[4],tok[5],tok[6],tok[7]);
srch = search_mnt(tok[1]);
if(srch== -1)
{
fprintf(fd, "%s", buffer);
continue;
}
strcat(tok[ntok],",");
tok[2][strlen(tok[2])-1] ='\0';
tok[3][strlen(tok[3])-1] ='\0';
tok[4][strlen(tok[4])-1] ='\0';
for(j=0,i=2;j<mnt[srch].no_of_pp;j++,i++)
{
strcpy(aptab[j].value, tok[i]);
}
mec = mnt[srch].mdtp;
fprintf(fd, "\n" );
while(strcmpi(mdt[mec].opcode, "MEND"))
{
//for model statements
ntok=sscanf(mdt[mec].operand, "%s%s%s%s%s%s%s%s%s",tok[0],tok[1], tok[2],
tok[3],tok[4], tok[5], tok[6],
tok[7], tok[8], tok[9]);

UNIT I | 1.95

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


if(strcmp(tok[0],"(P,")==0)
{
i = cal(tok[1]);
strcpy(tok[0], aptab[i].value);
sprintf(buffer, "%s %s %s ",
mdt[mec].opcode,tok[0],tok[2]);
}
else
if(strcmp(tok[1],"(P,")==0)
{
i = cal(tok[2]);
strcpy(tok[1], aptab[i].value);
sprintf(buffer, "%s %s %s ",mdt[mec].opcode,tok[0],tok[1]);
}
else
sprintf(buffer, "%s %s %s ", mdt[mec].opcode,tok[0],tok[1]);
fprintf(fd, "\t+ %s\n", buffer);
mec++;
}
fprintf(fd, "\n");
}
fcloseall();
}
void main()
{
clrscr();
pass_one();
printf("\n\n\n\tMacro Name Table............\n\n");
print_mnt();
getch();

UNIT I | 1.96

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


printf("\n\n\n\tMacro Definition Table............\n\n");
print_mdt();
getch();
clrscr();
pass_two();
printf("\n\t----------------------------------------------------------");
printf("\n\tSOURCE CODE.....\n");
show_file("newmac.asm");
getch();
printf("\n\t----------------------------------------------------------");
printf("\n\tEXPANDED CODE.....\n");
show_file("inter.int");
getch();
}
/*
Macro Name Table............
-------------------------
| Name | #PP | MDTP |
-------------------------
| EVAL| 3| 0|
| INCR| 2| 5|
Macro Definition Table............
---------------------------------------------------------
| No| LABEL | OPCODE | OPERANDS |
---------------------------------------------------------
| 0]| | MOVER| AREG, "5" |
| 1]| | ADD| AREG, (P, 0) |
| 2]| | MULT| AREG, (P, 1) |
| 3]| | MOVEM| AREG, (P, 2) |
| 4]| | MEND| |
| 5]| | ADD| (P, 0), ="1" |

UNIT I | 1.97

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


| 6]| | SUB| (P, 1), ="1" |
| 7]| | MEND| |
SOURCE CODE.....
MACRO
EVAL &P, &Q, &R
MOVER AREG, "5"
ADD AREG, &P
MULT AREG,&Q
MOVEM AREG,&R
MEND
MACRO
INCR &A,&B
ADD &A, ="1"
SUB &B, ="1"
MEND
START 100
ADD BREG,A
EVAL A, B, C
SUB BREG,A
INCR AREG, BREG
ADD BREG,P
EVAL P, Q, R
A DS 1
B DS 1
C DS 1
END
----------------------------------------------------------
EXPANDED CODE.....
START 100
ADD BREG,A
+ MOVER AREG, "5"
+ ADD AREG, A
+ MULT AREG, B
+ MOVEM AREG, C
UNIT I | 1.98

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


SUB BREG,A
+ ADD AREG ="1"
+ SUB BREG ="1"
ADD BREG,P
+ MOVER AREG, "5"
+ ADD AREG, P
+ MULT AREG, Q
+ MOVEM AREG, R
A DS 1
B DS 1
C DS 1
END
*/

SOLVED UNIVERSITY QUESTIONS


1. Differentiate linkers and loaders.
Ans. : Linker : Linker is a program that takes one or more objects generated by a compiler
and combines them into a single executable program.
Linker is a program that takes one or more objects generated by a compiler and combines
them into a single executable program.
Once a linker has scanned all of the input files to determine segment sizes, symbol
definitions and symbol references, figured out which library modules to include, and decided
where in the output address space all of the segments will go, the next stage the heart of the
linking process, relocation is. We use relocation to refer both to the process of adjusting
program addresses to account for non-zero segment origins, and the process of resolving
references to external symbols, since the two are frequently handled together. The linker's
first pass lays out the positions of the various segments and collects the segment-relative
values of all global symbols in the program. Once the linker determines the position of each
segment, it potentially needs to fix up all storage addresses to reflect the new locations of
the segments. On most architecture, addresses in data are absolute, while those embedded
in instructions may be absolute or relative. The linker needs to fix up accordingly, as we'll
discuss later.
Loaders : Loader is the part of an operating system that is responsible for loading programs
from executable (i.e., executable files) into memory, preparing them for execution and then
executing them.

UNIT I | 1.99

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


Loader is the part of an operating system that is responsible for loading programs from
executables (i.e., executable files) into memory, preparing them for execution and then
executing them. In computing, a loader is the part of an operating system that is responsible
for loading programs. It is one of the essential stages in the process of starting a program, as
it places programs into memory and prepares them for execution. Loading a program
involves reading the contents of executable file, the file containing the program text, into
memory, and then carrying out other required preparatory tasks to prepare the executable
for running. Once loading is complete, the operating system starts the program by passing
control to the loaded program code.
2. Difference between two pass and single pass assembler [Dec. 2013, May 2014 -6M]
Ans. :
Two Pass Assembler Single Pass Assembler
• Performs two passes. • Performs single pass.
• In first pass it collets the labels and • In first pass itself its collects the symbols
symbols and in second pass it and labels and assembles the instruction.
• It stores mnenmonics and • It stores all mnenmonics and
pseudocodes separately i.e. MOT and pseudocodes in a single table MOT itselt.
POT respectively.
• Literals and symbols are stored in • All entries for symbols and literals are
symbol and literal table respectively. entered into symbol table only
3. What is the data structure used to organize the SYMTAB and OPCODE table in a simple
assembler? Why it is preferred? [Dec. 2012 -8M]
Ans. : OPTAB is used to look up mnemonic operation codes and translate them to their
machine language equivalents.
SYMTAB is used to store values (addresses) assigned to labels.
4. Consider the following sequence
X EQU Y
Y EQU Z
Z RESW 1
In a 2-pass assembler can resolve such a sequence of definition. Justify your answer.
[Dec. 2013 -6M]
Ans. : No. The symbol Y cannot be assigned a value when it is encountered during the first
pass because Z has not yet been defined. As a result, X cannot be evaluated during the

UNIT I | 1.100

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


second pass. This means that any assembler that makes only two sequential passes over the
source program cannot resolve such a sequence of definitions.
5. What is SYMTAB? How it is useful? [Dec. 2011, May 2012 -6M]
Ans. : The Symbol table (SYMTAB) includes the name and value (addresses) for each label in
the source program, together with flags to indicate error conditions.
SYMTAB is usually organized as a hash table for efficiency of insertion and retrieval.
6. List out the machine independent assembler features. [Dec. 2011 -3M]
Ans. : The machine independent assembler features are
1. Literals
2. Symbol defining statements
3. Expressions
4. Program blocks
5. Control Sections and Program Linking.
7. What is the need for an assembler directive? [Dec. 2012 -2M]
Ans. : In addition to translating the instructions of the source program, the assembler must
process statements called assembler directives or pseudo-instructions. These statements are
not translated into machine instructions. Instead, they provide instructions to the assembler
itself.
Examples :
1. BYTE and WORD : direct the assembler to generate constants as part of the object
program.
2. RESB and RESW → instruct the assembler to reserve memory locations without
generating data values.
3. START → specifies the starting memory address for the object program.
4. END → marks the end of the program.
8. What is the use of location counter?
Ans. : LOCCTR is initialized to the beginning address specified in the START statement. After
each source statement is processed, the length of the assembled instruction or data area to
be generated is added to LOCCTR. Thus whenever we reach a label in the source program,
the current value of LOCCTR gives the address to be associated with that label.
9. What does an assembler perform when it encounters LTORG assembler directive.
Ans. : When the assembler encounters a LTORG statement, it creates a literal pool that
contains all of the literal operands used since the previous LTORG (or the beginning of the
UNIT I | 1.101

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


program). This literal pool is placed in the object program at the location where the LTORG
directive was encountered.
10. List the types of assembler.
Ans. : 1. one pass assembler
2. two pass assembler
3. multipass assembler.
11. How can the assembler overcome the problem of forward reference?
Ans. : The assembler overcome the problem of forward reference through two pass and
multipass assemblers.
The first pass does little more than scan the source program for label definitions an assign
addresses. The second pass performs most of the actual translation previously described.
12. Write the uses of program relocation.
Ans. : 1. Sharing the memory location for the different programs.
2. Avoid the wastage of processing or execution time.
3. Reduce the wastage of memory space.
13. How are jump instructions assembled?
Ans. : Jump instructions are assembled in two different ways, depending on whether the
target of the jump is in the same code segment as the jump instruction. A near jump is a
jump to a target in the same code segment. A far jump is a jump to a target in a different
code segment.
14. What are the functions required to translate source program to object code?
Ans. : It requires the following functions
1. Convert mnemonic operation codes to their machine language equivalents.
2. Convert symbolic operands to their equivalent machine addresses.
3. Build the machine instructions in the proper format.
4. Convert the data constants specified in the source program into their internal
machine representations.

5. Write the object program and the assembly listing.

15. What is object program?

Ans. : The object program is a program written with the generated object codes for an
assembly language program.

UNIT I | 1.102

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


16. What are the records used to write an object program?

Ans. : There are three types of records

1. Header record

2. Text record and

3. End record.

17. What are the functions performed by pass 1 of two pass assembler?

Ans. : 1. Assign addresses to all statements in the program.

2. Save the values (addresses) assign to all labels for use in pass-2.

18. What is a literal?

Ans. : It is often convenient for the programmer to be able to write the value of a constant
operand as a part of the instruction that uses it. This avoids having to define the constant
elsewhere in the program and makeup a label for it. Such an operand is called a literal
because the value is stated “literally” in the instruction.

19. What is literal table and literal pool?

Ans. : For each literal used, literal table contains the literal name, the operand value and
length, and the address assigned to the operand when it is placed in the literal pool. All of
the literal operands used in a program are gathered together into one or more literal pools.

20. What do macro expansion statements mean? [May 2011, 2014 -3M]

Ans. : These statements give the name of the macroinstruction being invoked and the
arguments to be used in expanding the macros. These statements are also known as macro
call.

21. Mention the tasks involved in macro expansion. [May 2011, 2014 -3M]

Ans. : 1. Identify the macro calls in the program.


2. The values of formal parameters are identified.
3. Maintain the values of expansion time variables declared in a macro.
4. Expansion time control flow is organized.
5. Determining the values of sequencing symbols.
UNIT I | 1.103

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


6. Expansion of a model statement is performed.

22. What are known as nested macro call?


Ans. : The statement, in which a macro calls on another macro, is called nested macro all. In
the nested macro call, the call is done by outer macro and the macro called is the inner
macro.

23. How the nested macro calls are executed?


Ans. : The execution of nested macro call follows the LIFO rule. In case of nested macro calls
the expansion of the latest macro call is completed first.

24. What are the statements used for conditional macro expansion?

Ans. : IF-ELSE-ENDIF statement

WHILE-ENDW statement

25. What is meant by positional parameters? [May 2011, -3M]


Ans. : If the parameters and arguments were associated with each other according to their
positions in the macro prototype and the macro invocation statement, then these parameters
in macro definitions are called as positional parameters.
26. Consider the macro definition #define DISPLAY (EXPR) Printf (“EXPR = %d\n”, EXPR)
Expand the macro instruction DISPLAY (ANS)
Ans. : Printf (“EXPR = %d\n”, ANS)
27. What are the data structures used in macro processor?
Ans. : MDT (Macro Definition Table) : the macro definitions are stored in a definition table
i.e. it contains a macro prototype and the statements that make up the macro body.
MNT (Macro Name Table) : it is used to store the macro names and it contains two pointers
for each macro instruction which indicate the starting and end location of macro definition
MDTP (Macro Definition Table Pointer) : It also serves as an index to MDT. ALA(argument
list array)– it is used to store the arguments during the expansion of macro invocations.

28. What is the difference between macro and procedure.


Ans.
MACRO PROCEDURE
1. The corresponding machine code is 1. The corresponding m/c code is written
written every time a macro is called in only once in memory.

UNIT I | 1.104

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


a program.
2. Program takes up more memory 2. Program takes up comparatively less
space. memory space.
3. No transfer of program counter. 3. Transferring of program counter is
required.
4. No overhead of using stack for 4. Overhead of using stack for transferring
transferring control. control.
5. Execution is fast. 5. Execution is comparatively slow.
6. Assembly time is more. 6. Assembly time is comparatively less.
7. More advantageous to the programs 7. More advantageous to the programs
when repeated group of instruction is when repeated group of instructions is
too short. quite large.
29. Compare and contrast the properties of macro and subroutines with respect to the
following :
1) Code space requirements
2) Execution speed
3) Processing requires by the assembler
4) Flexibility and generality.
Ans.
Sr. Parameters Macro Subroutine
No.
1. Code space More, it is expanded Less, it is called not
requirements inline expanded
2. Execution speed More, no calling Less, there is calling
overhead (like passing overhead
parameters, saving
return address)
3. Processing requires by As code size increases Assembler has to handle
the assembler assembler takes more parameter passing
time for processing overhead.
4. Flexibility and generality It cannot handle labels It can handle labels.

UNIT I | 1.105

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING

QUESTIONS
1. Write down the pass numbers either (Pass 1 or Pass 2) of the following activities that
occur in a 2-pass assembler.
(a) Object code generation
(b) Literals added to literal table
(c) Printing assembly listing
(d) Address resolution of local symbols

2. What is system software?

3. What are the different components of system software?

4. Differentiate linkers and loaders.

5. What are Language Processing Activities?

6. Explain Fundamentals of Language Processing

7. What is assembler and what are its functions?

8. What are Elements of Assembly Language Programming.

9. Write short note on simple Assembly Scheme

10. Explain Pass structure of assembler

11. How single pass assembler works? Explain in detail

12. What are the Data Structures of two pass assembler.

13. Explain the design of pass1 of two pass assembler.

14. Discuss the flowchart of pass 2 of two pass assembler.

15. Compare two pass ans single pass assembler.

16. Explain with suitable example macro definition, call and expansion.

17. How to handle nested macro calls?

18. How unique labels are generated in macro.


19. Explain conditional macro expansion with respect to macro time variables.
20. Explain the data structures of macro processor with their formats.

UNIT I | 1.106

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


21. Discuss the design of macro processor with respect to pass1.
22. Discuss the design of macro processor with respect to pass2.
23. Explain in detail one pass macro processor.
24. What is system software?
25. What are the different components of system software?
26. Differentiate linkers and loaders.
27. What are Language Processing Activities?
28. Explain Fundamentals of Language Processing
29. What is assembler and what are its functions?
30. What are Elements of Assembly Language Programming
31. Write short note on simple Assembly Scheme
32. Explain Pass structure of assembler
33. How single pass assembler works? Explain in detail.
34. What is Load-and-go assembler.
35. How to handle problem of Forward Reference in One Pass Assemblers.
36. What is backpatching.
37. What are the Data Structures of two pass assembler.
38. Explain the design of pass1 of two pass assembler.
39. Discuss the flowchart of pass 2 of two pass assembler.

40. Compare two pass ans single pass assembler.

41. What is the data structure used to organize the SYMTAB and OPCODE table in a simple
assembler? Why it is preferred?

42. What is SYMTAB? How it is useful?

43. List out the machine independent assembler features

44. What is the need for an assembler directive?


45. What is the use of location counter?
46. What does an assembler perform when it encounters LTORG assembler directive.
47. List the types of assembler.

UNIT I | 1.107

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)


lOMoARcPSD|15994706

SYSTEMS PROGRAMMING (TE IT SEM. II – PU) INTRODUCTION TO SYSTEMS PROGRAMMING


48. How can the assembler overcome the problem of forward reference?
49. Write the uses of program relocation.
50. How are jump instructions assembled?
51. What are the functions required to translate source program to object code?
52. What is object program?



UNIT I | 1.108

Downloaded by Anthony Gonsalves (anthony07856@gmail.com)

You might also like