Sei sulla pagina 1di 156

Macros and macro processors:-Macro

definition and call, Macro expansion,


Nested macro calls, Advanced macro
facilities, Design of macro preprocessor.
Linker- Relocation and linking concepts-
Self relocating programs.
Loader – Types of loaders.
Editor – Types of editors- Components of
editor- Debug monitor.

Module II
Macros
A macro is a facility for extending a programming
language.

Definition: A macro is a unit of


specification for program generation
through expansion.
 A macro consists of a name, a set of formal
parameters and a body of code that defines a new
operation or a new method of declaring data.
 Statements in the body of code may use the
formal parameters.
 Use of the macro’s name in the mnemonic field of
an assembly statement constitutes a macro call.
 The operand field of the macro call statement
indicates the actual parameters of the call.
The use of a macro name with a set of actual
parameters is replaced by some code generated by
its body. This is called macro expansion.
Two types of expansion:
1. Lexical expansion: It implies replacement of a
character string by another character string during
program generation. It is typically employed to
replace occurrences of formal parameters by
corresponding actual parameters.

2. Semantic expansion: It implies generation of


instructions tailored to the requirements of a specific
usage. For eg, generation of type specific instructions
for manipulation of byte and word operands.
Example:

The following sequence of statements is used to increment the


value stored in a memory word by a given constant:
1.Move the value from memory word into a register
2.Increment the value in a register
3.Move the new value into the memory word.
If a program need to increment many values stored in memory,
it would be useful to define new operation named INCR.

Using lexical substitution, the macro call INCR A, B, AREG can


lead to the generation of a sequence of statements that
increment A by the value of B by using the register AREG to
perform arithmetic.

Using semantic expansion, the sequence of generated


statements can be adapted to suit the types of actual parameters
A and B. (The instruction could be generated if A is a byte
operand and B has a constant value.
Macro Definition and Call
Macro Definition

A macro definition in a program defines either a


new operation or a new method of declaring data.
A macro definition is enclosed between a macro
header statement and a macro end statement.
Located at the start of a program.

It consists of

1. A macro prototype statement


2. One or more model statements
3. Macro preprocessor statements
The macro prototype statement declares the
name of a macro and the names and kinds of its
parameters.
Syntax

<macro name> [<formal parameter spec>[,…]]

Where <macro name> appears in the mnemonic field of an assembly


statement and the <formal parameter spec> appears in the operand
field of the statement.
 A <formal parameter spec> is of the form

&<parameter name>[<parameter kind>]

 A model statement is a statement from which


an assembly language statement
may be generated during macro expansion.

 A preprocessor statement is used to perform


auxiliary functions during macro expansion.
Macro call
A macro call in the program is an invocation of the new operation
or the new method of declaring data defined in the macro.
A macro is called by writing the macro name in the mnemonic field
of an assembly stmnt.

Syntax

<macro name> [<actual parameter spec>[,…]]


Where an actual parameter resembles an operand
specification in an assembly language statement.
Eg:
MACRO
INCR &MEM_VAL, &INCR_VAL, &REG
MOVER &REG, &MEM_VAL
ADD &REG, &INCR_VAL
MOVEM &REG, &MEM_VAL
MEND

 MACRO and MEND are the macro header and macro end
statements.
The prototype statement indicates that INCR has 3
parameters called
MEM_VAL,INCR_VAL and REG.
Statements with the operation codes MOVER, ADD and
MOVEM are model statements.
No preprocessor statements are used in this macro.
MACRO
EXPANSION
 A macro call leads to macro expansion
 During macro expansion, the macro call
statement is replaced by a sequence of
assembly statements
 Each expanded statement is marked with a
‘+’ preceding its label field

Key notions concerning macro


expansion
1. Expansion Time Control Flow.
This determines the order in which model
statements
are visited during macro expansion.
2. Lexical Substitution.
Lexical substitution is used to generate an
assembly
statement from a model statement.
Expansion Time Control Flow

 The default flow of control of the macro


expansion
is sequential.
 A preprocessor statement can alter the flow
of control during expansion such that some
model statements either never visited
during expansion are called conditional
expansion, or repeatedly visited during
expansion called expansion time loops.
 The flow of control during macro expansion
is implemented using a macro expansion
counter (MEC).
1. MEC:= statement number of first
statement
 Algorithm of Outline of macro expansion
following the prototype
statement .
2. While statement pointed by MEC is not a
MEND statement
(a) If a model statement then
(1) Expand the statement.
(2) MEC:=MEC+1;
(b) Else(i.e a preprocessor statement)
MEC:= New value specified in the
statement .
3. Exit from the macro expansion.
Lexical Substitution
 A model statement consist of three types of
strings

1. An ordinary string which stands for itself.

2. The name of the formal parameter which


is preceded by a character ‘&’.

3. The name of the preprocessor variable


which
is also preceded by a character ‘&’.
During lexical expansion the strings of
type 1 are retained without substitution.
Strings of type2 & type 3 are replaced by
the values of the formal parameters or
the preprocessor variables.
The value of a formal parameter is the
corresponding actual parameter string.
The rules for determining the value of a formal parameter
depend on the kind of parameter.

Different kinds of parameters.


1. Positional Parameters.
2. keyword parameters.
Positional Parameters
 A positional formal parameter is
written as &<parameter name>.
eg, &SAMPLE , where SAMPLE is the
name of the parameter.

 The <actual parameter spec> in a call


on a macro using positional
parameters is simply an <ordinary
string>.
To find the value of the positional
parameter XYZ
1. Find the actual parameter specification
occupying the same
ordinal position in the list of actual
parameters in the macro
call statement .

E.g.
Let’s consider a macro

MACRO
INCR &MEM_VAL, &INCR_VAL, &REG
MOVER &REG, &MEM_VAL
ADD &REG, &INCR_VAL
MOVEM &REG, &MEM_VAL
MEND
Consider the macro call
INCR A, B, AREG;
Now values of formal parameters are

Formal parameter Value


MEM_VAL A
INCR_VAL B
REG AREG

Lexical expansion of the model statement now leads


to the code
+ MOVER AREG,A
+ ADD AREG,B
+ MOVEM AREG,A
keyword parameters
For keyword parameters, <parameter
name> is an ordinary string and
<parameter kind> is the string ‘=‘. The
<actual parameter spec> is written as
<formal parameter name>=<ordinary
string>.

The value of a formal parameter XYZ is


determined by the rule of keyword association as
follows:

1. Find the actual parameter specification


which has the form XYZ=<ordinary
string>.
2. Let <ordinary string> in the
specification be the string ABC .
Then the value of the formal
parameter XYZ is ABC.
Then the macro calls will be
INCR MEM_VAL=A,INCR_VAL=B, REG=AREG
and
macro definition using key word
parameters is
MACRO
INCR &MEM_VAL=,
&INCR_VAL=, &REG=
MOVER &REG, &MEM_VAL
ADD &REG, &INCR_VAL
MOVEM &REG, &MEM_VAL
MEND
Default specification of the
parameters
Default specification of parameters is
useful in situations where a parameter
has the same value in most calls.

Syntax
&<parameter name>[<parameter kind>[<default
value>]]

e.g.
INCR &MEM_VAL=, &INCR_VAL=, &REG=BREG
different macro calls
INCR MEM_VAL=A,INCR_VAL=B
INCR MEM_VAL=A,INCR_VAL=B, REG=AREG

The second call overrides the default value for


REG with the value BREG. Then BREG will be
Macro definition will be

MACRO
INCR &MEM_VAL=, &INCR_VAL=,
&REG=BREG
MOVER &REG, &MEM_VAL
ADD &REG, &INCR_VAL
MOVEM &REG, &MEM_VAL
MEND
Macros with mixed parameter list

A macro may be defined to use both


positional and keyword parameters

E.g. SUMUP A,B,G=20,H=X

A and B are positional parameters while


G,H are keyword parameters.
Other uses of the
parameters
Formal parameters can also appear in the
label and opcode field of model statements
 E.g.
MACRO
CALC &X,&Y,&OP=MULT,&LAB=
&LAB MOVER AREG, &X
&OP AREG, &Y
MOVEM AREG, &X
MEND
Expansion of the call CALC A, B, LAB=LOOP
leads to the following code:
+ LOOP MOVER AREG, A
+ MULT AREG, B
+ MOVEM AREG, A
NESTED MACRO CALLS
A model statement in a
macro constitute a call on
another macro.
 We refer to the macro
containing nested call as the
outer macro and the called
macros as the inner macro.
 The expansion of nested
macro calls follows LIFO
rule.
EXAMPLE:1
MACRO
EVAL &X,&Y,&Z;
LOAD &X
SUB &Y
ADD &Z
MEND

MACRO
MAJOR &W1,&W2,&W3,&W4,&W5,&W6
EVAL &W1,&W2,&W3
STORE &W6
EVAL &W4,&W5,&W6
MEND
-------------------
-------------------
-------------------
+LOAD A

+EVAL ABC +SUB B


+ADD C

MAJOR A,B,C,D,E,F +STORE F

+LOAD D

+EVAL D,E,F
+SUB E

+ADD F

CONT…………….
A NESTED
EXAMPLE MACRO
2: CALL
MACRO
COMPUTE &FIRST,&SECOND
MOVEM BREG,TMP
INCR &FIRST,&SECOND,REG=BREG
MOVER BREG TMP
MEND

+ MOVEM BREG, TMP 1

+MOVER 2
BREG,X 3
COMPUTE X,Y
+INCR X,Y +ADD
4
BREG,Y
5
+MOVEM
BREG X
ADVANCED MACRO
FACILITIES
 It aimed to support semantic expansion.
 It can be grouped into

1. Facilities for alteration of


flow of
control during expansion.
2 . Expansion time variables.
3. Attributes of parameters.
1. ALTERATION OF FLOW OF CONTROL
 Two features areDURING
provided toEXPANSION
facilitate alteration of
flow of control during expansion

1. Expansion time sequencing symbols


2.Expansion time statements AIF,AGO
and ANOP.
ALTERATION OF FLOW OF CONTROL
DURING EXPANSION ...
 A Sequencing Symbol (SS) has the syntax

. <ordinary string>

-: A sequencing symbol is defined by putting


it in the label field of a statement in the macro
body.

-: It is used as an operand in an AIF or


AGO statement to designate the destination
of an expansion time control transfer.
It never appears in the expanded form of
a model statement.
 AIF statement

AIF(< expression >)< Sequencing Symbol>

 AIF statement is similar to an IF statement ,used


to make selection.

 <expression> is a relational expression involving


ordinary strings ,formal parameters (e.g. &x) and
their attributes, and expansion time variables.

 If relational expression evaluates to true,


expansion time control is transferred to the
statement containing
<sequencing symbol> in its label field.
AGO statement
AGO< sequencing symbol>

 Similar to Go statement.

 Unconditionally transfers expansion


time control to the statement
containing <sequencing symbol> in
its label field.
ANOP statement

< sequencing symbol > ANOP

 ANOP statements represent NO operation.

 It has the effect of defining the sequencing


symbol.
2. EXPANSION TIME VARIABLE (EV’s)

 Variables which can only be used during the


expansion of macro calls.
 Have two type declarations

1.Local Expansion time variables


2.Global Expansion time variables

 Local EV used only for a particular macro call


 Global EV exist across all macro calls situated in a
program .
Syntax

Local EV’s

LCL<EV specification>[,<EV specification>…]

Global EV’s

GBL<EV specification>[,<EV specification>…]


<EV specification>
syntax
&<EV name>
 Where EV name is an ordinary string .
 Values of EV’s can be manipulated through the
preprocessor statement ‘SET’.
 A SET statement is written as
<EV specification> SET <SET-expression>
 Where <EV specification> appears in the label field and
SET in the mnemonic field.
 A SET statement assigns the value of <SET expression>
to the EV specified in <EV specification>. The value of
an EV can be used in any field of a model statement, and
in the expression of an AIF statement.
EXAMPLE:

MACRO
CONSTANTS
LCL &A
&A SET 1
DB &A
&A SET &A+1
DB &A
MEND

A call on macro CONSTANTS is expanded as


follows: The local EV A is created. The first SET
statement assigns the value ‘1’ to it. The first DB
statement thus declares a byte constant ‘1’. The
second SET statement assigns the value’2’ to A
and the second DB statement declares a
3.ATTRIBUTES OF FORMAL PARAMETERS

 SYNTAX
<attribute name>’<formal parameter spec>
 The type ,length and size attributes have the names T, L and
S

E.g. MACRO
DCL_CONST &A
AIF (L’&A EQ 1) .NEXT
------
.NEXT - - - - - -
------
MEND

Here expansion time control is transferred to the


statement having .NEXT in its label field only if the
actual parameter corresponding to the formal
parameter A has the length of ‘1’.
 Helps:-

 To ensure execution efficiency


 It ensure that a model statement is
visited only under specific conditions
 The AIF and AGO statements are used for
this purpose.

Conditional Expansion
 MACRO
EVAL &X, &Y, &Z
AIF (&Y EQ &X) .ONLY
MOVER AREG, &X
SUB AREG,&Y
ADD AREG,&Z
AGO .OVER
.ONLY MOVER AREG,&Z
.OVER MEND

To generate an efficient code to


evaluate
A-B+C in AREG
 AIF statement compare first two actual
parameters, if the they are same, control
is transferred to MOVER AREG,&Z
 If not the control is transferred to MOVE-
SUB-ADD sequence.
 OVER MEND terminates the expansion

Procedure:-
 There are many similar statements during
the expansion of a macro

 This can be achieved by writing similar


model statements in macro

Expansion time loops


MACRO
CLEAR &A
MOVER AREG,=’0’
MOVEM AREG,&A
MOVEM AREG,&A+1
MOVEM AREG,&A+2
MEND
When called as CLEAR B, the
MOVER statement puts ‘0’ in AREG, while
the three MOVEM statements store this
value in 3 consecutive bytes with the
address
Eg:- B,B+1,B+2.
 The same effect can be achieved by
writing an expansion time loop.

 Expansion time loops can be written using


expansion time variables and AIF and
AGO.
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
MEND
Procedure:-

Expansion of macro call CLEAR B, 3

 LCL statement declares M to be a local


EV.
 At start M is initialized to zero.
 The expansion of the statement
MOVEM AREG, &X+&M leads to
generation of the statement MOVEM
AREG, B
 The value of M is incremented by 1
and model statement MOVEM is
expanded repeatedly until its value
equals to N, which is 3 in this case.
Thus the macro call leads to the
generation of the statements

+ MOVER AREG, =‘0’


+ MOVEM AREG, B
+ MOVEM AREG, B+1
+ MOVEM AREG, B+2
 The REPT statement:
Syntax:-
REPT <expression>
 <expression>should evaluate to a
numerical value during macro expansion
 The statements between REPT and ENDM
statement would be processed for
expansion <expression>no. of times.

Other facilities for Expansion time


loops
MACRO
CONST10
LCL &M
&M SET 1
REPT 10
DC ‘&M’
&M SET &M+1
ENDM
MEND

Eg: use of REPT to declare 10


constants with values 1,2,…10.
 Syntax:-

IRP<formal
parameter>,<argument-list>

The formal parameter mentioned


in the statement takes successive
values from the argument list.
 For each value, the statements
between
Thestatementsthe IRP
IRP statement and ENDM
expanded once.
MACRO
CONSTS &M, &N, &Z
IRP &Z, &M, 7, &N
DC &Z
ENDM
MEND
A macro call CONSTS 4,10 leads to
declaration of 3 constants with values 4,7
and 10
 SEMANTIC
Semantic EXPANSION
Expansion is the generation of instructions
tailored to the requirements of a specific usage.

 It can be achieved by a combinations of advanced


macro facilities like AIF,AGO statements and
expansion time variables.

 CLEAR is an instance of semantic expansion.

 Macro EVAL is a conditional expansion.

 Semantic expansion is also used as type attribute.


Example :1
MACRO
CREATE_CONST &X,&Y
AIF (T’&X EQ B) .BYTE
&Y DW 25
AGO .OVER
.BYTE ANOP
&Y DB 25
.OVER MEND

 This macro creates a constant ‘25’ with the


name given by the 2nd parameter. The type
of the constant matches the type of the first
parameter.
DESIGN OF MACRO
PREPROCESSOS
MACRO PREPROCESSOR

 The macro preprocessor accept an


assembly program containing definitions
and calls and translates it into an
assembly program which does not contain
any macro definitions or calls.
---- Macro ---
Assembler
- --- preprocessor ---

Program Target
Program
With macro program
Without
definitions and
macros
calls

A schematic of a macro preprocessor


TASKS INVOLVED IN MACRO
EXPANSION

 Identify macro calls in the program.


 Determine the value of formal parameters.
 Maintains the value of expansion time
variables declared in a macro.
 Organize expansions time control flow.
 Determine the values of sequencing
symbols.
 Perform expansion of a model statement.
DESIGN SPECIFICATION FOR
EACH TASK
 Identify the information necessary to
perform a task.
 Design a suitable data structure to
record the information.
 Determine the processing necessary
to obtain the information.
 Determine the processing necessary
to perform the task.
1.Identify macro calls

 A table called the Macro Name Table (MNT) is


designed to hold the name of all macro
defined in a program.

 A macro name is entered in this table when a


macro definition is processed.

 While processing a statement in the source


program ,the preprocessor compares the
string found in its mnemonic field with the
macro names in the MNT. A match indicate
that the current statement is a macro calls.
2. Determine the value of formal parameter
 Actual Parameter Table (APT) is designed to
hold the values of parameters during the
expansion of a macro call.
(<formal parameter name>,<value>)
 Two items of information are needed to
construct this table, names of formal
parameters and default values of keyword
parameters.
 For this purpose parameter default table
(PDT) is used for each macro. This table would
be accessible from the MNT entry of a macro
and would contain pair of the form (<formal
para. Name>,<defa. Val>).
 If a macro call statement does not specify a
value for some parameter, its default value
would be copied from PDT to APT.
3. Maintain expansion time
variable
An expansion time variables table
(EVT) contain pair of the form
(<EV name>,<value>)
The value field of a pair is accessed
when a preprocessor statement or a
model statement under expansion
refers to an EV.
4. Organize expansion time
control flow
 The body of a macro or the set of
preprocessor statement and model
statements in it, is stored in a table
called the macro Definition
Table(MDT) for use macro expansion.
 The flow of control during macro
expansion determines when a model
statement is to be visited for
expansion.
 MEC (Macro Expansion Counter) is
initialized to the first statement of the
macro body in the MDT
5. Determine value of
sequencing symbols

 A sequencing symbol table (SST)


contain pair of the form
(<sequencing symbol name> <MDT
entry #>)
 Where <MDT entry #> is the number of
the MDT entry which contains the
model statement defining the
sequencing symbol.
 This entry is made on encountering a
statement which contains the
sequencing symbol in its label field.
6.Perform expansion of a model
statement
Tasks are
1. MEC points to the MDT entry
containing the model statement.
2. Value of formal parameters and EV’s
are available in APT and
EVT,respectively.
3. The model statement defining a
sequencing symbol can be identified
from SST.

Expansion of a model statement is achieved by performing a


lexical substitution for the parameters and EV’s used in the
model statement.
In the statement
MOVER AREG, &ABC
The value of parameter ABC is needed
while expanding a model statement.
Let the pair (ABC,ALPHA) occupy entry #5
in APT. The search in APT can be avoided
if the model statement appears as
MOVER AREG, (P, 5) in the MDT,
where (P, 5) stands for ‘parameter#5’
DATA STRUCTURE
 Macro expansion can be made more
efficient by storing an intermediate code
for a statement in the MDT.
 All parameters could be replaced by a
pair (p,n) in model statements and
preprocessor statements in MDT.
 Hence APT containing
(<for.para.name>,<value> is replaced by
another table called APTAB which only
contains <value>’s.
 To implement this ordinal numbers are
assigned to all parameters of a macro.
PNTAB is used for this purpose.
 Parameter names are entered in
PNTAB in the same order in which
they appear in the prototype
statement.
 The entry# of a parameter in PNTAB is
its ordinal number.
The information in APT has been split into
:-
1. PNTAB :-contain formal parameter
names.
Used while processing a MACRO
definition.
2. APTAB :-contain formal parameter
value
used during macro expansion.
Contd..............

EVT is split into


EVNTAB :- EV names entered while
processing EV declaration.
and
EVTAB :- value
SST is split into
SSNTAB :- name entered while processing
SS reference or definition.
and
SSTAB :- value
Contd..............
 The positional parameters of a macro appear
before keyword parameters in a prototype
statement.
Hence in a prototype statement which has p
positional parameters and k keyword
parameters, the keyword parameters have
ordinal numbers p+1….p+k.
Hence we replace the parameter default table
(PDT) by keyword parameter default table
(KPDTAB).
We store the number of positional parameters
p, in a new field of the MNT entry.
contd......

MNT has entries for all macro defined in


the program.
Each MNT contain three pointers
MDTP, KPDTP, SSTP which are
pointers to MDT,KPDTAB,and SSNTAB for
the macro.
Construction and use of macro
preprocessor data structure:-
 PNTAB and KPDTAB are constructed by
processing prototype statement
 MDT entries are constructed while
processing the model statement and
preprocessor statement in the macro body.
 An entry is added to SSTAB when the
definition of a sequencing symbol is
encounterd.
 APTAB is constructed while processing a
macro call.
 EVTAB is constructed at the start of
expansion of a macro.
Table Field in each entry

 MNT Macro name


(Macro No. of positional parameter
(#PP)
Name No. of keyword parameter
(#KP)
Table) No. of expansion time variable
(#EV)
MDTP (MDT Pointer)
KPDTP (KPDTAB Pointer)
SSTP (SSTAB Pointer)
Contd…….
 PNTAB:- Parameter name
 EVNTAB:- EV name
 SSNTAB:- SS name
 KPDTAB:- Parameter name, default
value
 MDT :- label,opcode,operands
 APTAB :- value
 EVTAB :- value
 SSTAB :- MDT entry#
 Example:-
MACRO
CLEARMEM
&X,&N,&REG=AREG
LCL &M
&M SET 0
MOVER &REG,=‘0’
. MORE MOVEM &REG, &X+&M
&M SET &M+1
AIF (&M NE N)
.MORE
MEND
Shows the contents of the data structures for the call
CLEARMEM AREA, 10
X

N
MORE
REG M
SSNTAB
EVNTAB
PNTAB
5 SSTAB
28
KPDTAB REG AREG
10

name # PP # # EV MDTP KPDTP SSTP


CLEARMEM KP
2 1 1 25 10 5

MNT
25 LCL (E,1) AREA
26 (E,1) SET 0 10
APTAB
MOVER (P,3),=‘0’
27 AREG
28 (S,1) MOVEM (P,3),(P,1)+(E,1)
(E,1) SET (E,1)+1
29

30 AIF ((E,1) NE (P,2)) (S,1)


EVTAB 0

31 MEND

MDT
PROCESSING
OF
MACRO DEFINITION
ALGORITHM

Before initiating the processing of


macro definitions in a program :-
KPDTAB_ptr := 1;
SSTAB_ptr := 1;
MDT_ptr := 1;
Algorithm (Processing of macro definition)

1. SSNTAB_ptr := 1;
PNTAB_ptr := 1;
2. Process the macro prototype
statement and form the MNT entry.
(a) name := macro name;
(b) For each positional parameter
(i) Enter parameter name in PNTAB
[PNTAB_ptr].
(ii) PNTAB_ptr := PNTAB_ptr+1;
(iii) #PP := #PP + 1;
(c) KPDTP := KPDTAB_ptr;
(d) For each keyword parameter
(i) Enter parameter name and default
value, in
KPDTAB [KPDTAB_ptr].
(ii) Enter parameter name in
PNTAB [PNTAB-ptr].
(iii) KPDTAB_ptr := KPDTAB_ptr + 1;
(iv) PNTAB_ptr := PNTAB_ptr + 1;
(v) #KP := #KP + 1;
(e) MDTP := MDT_ptr;
(f) #EV := 0;
(g) SSTP := SSTAB_ptr;
3. While not a MEND statement
(a) if an LCL statement then
(i) Enter the expansion time variable name in
EVNTAB.
(ii) #EV := #EV + 1;

(b) If a model statement then


(i) if label field contains a sequencing
symbol then
if symbol is present in SSNTAB then
q:= entry number in SSNTAB;
else
Enter symbol SSNTAB [SSNTAB_ptr].
q:= SSNTAB_ptr;
SSNTAB_ptr := SSNTAB_ptr + 1;
SSTAB [SSTP+q-1] := MDT_ptr;
(ii) For a parameter , generate the
specification (P, #n).
(iii) For an expansion variable, generate
the
specification (E, #m).
(iv) Record the IC in MDT [MDT_ptr];
(v) MDT_ptr := MDT_ptr + 1;
(c) If a preprocessor statement then
(i) If a SET statement
Search each expansion variable
name used in the statement in
EVNTAB and generate the
spec (E, #m).
(ii) If an AIF / AGO statement then
If sequencing symbol used in the
statement is present in SSNTAB
then
q := entry number in SSNTAB;
else
Enter symbol in SSNTAB [SSNTAB_ptr].
q := SSNTAB_ptr;
SSNTAB_ptr := SSNTAB_ptr + 1;
Replace the symbol by (S,SSTP +q-1)
(iii) Record the IC in MDT [MDT_ptr].
(iv) MDT_ptr := MDT_ptr + 1;
4. (MEND statement)
If SSNTAB_ptr = 1 (empty) then
SSTP = 0;
else
SSTAB_ptr =
SSTAB_ptr+SSNTAB_ptr - 1;
If #KP = 0 then KPDTP = 0;
Algorithm (Macro Expansion)
1.Perform initializations for the expansion of a macro
(a) MEC :=MDTP field of the MNT entry.

(b) Create EVTAB with #EV entries & set EVTAB_ptr.

(c) Create APTAB with #PP + #KP entries & set


APTAB_ptr.

(d) Copy keyword parameter defaults from the entries


KPDTAB [KPDTP] ……. KPDTAB [KPDTP + #KP-1]
into APTAB[#PP + 1] ……. APTAB[#PP + #KP].

(e) Process positional parameters in the actual


parameter list
& copy them into APTAB[1] ……. APTAB[#PP].
Algorithm contd……

(f) For keyword parameters in the actual parameter list


Search the keyword name in parameter name field of
KPDTAB [ KPDTP ] …… KPDTAB [ KPDTP+ #KP-1 ].
Let KPDTAB [q] contain a matching entry. Enter value
of the keyword parameter in the call (if any) in
APTAB [ #PP +q – KPDTP +1 ].

2.While statement pointed by MEC is not MEND statement


(a) If a model statement, then
(i) Replace operands of the form (P,#n) and (E,#m) by
values in APTAB[n] and EVTAB[m] respectively.
(ii) Output the generated statement.
(iii) MEC := MEC + 1.
Algorithm contd……
(b) If a SET statement with the specification (E,#m) in
the
label field, then
(i) Evaluate the expression in the operand field
and
set an appropriate value in EVTAB[m].
(ii) MEC := MEC +1.
(c) If an AGO statement with (S,#s) in operand field,
then
MEC := SSTAB [SSTP +s -1].
(d) If an AIF statement with (S,#s) in operand field,
then
If condition in the AIF statement is true, then
MEC := SSTAB [ SSTP +s -1 ].

3. Exit from macro expansion.


LINKERS
1. Translation of prgm
Program execution steps:-
2. Linking of prgm with other prgm needed
for execution.
3. Relocation of prgm to execute from the
specific memory area allocated to it.
4. Loading of prgm in the memory for the
purpose of execution.
 These 4 steps are performed by different
language processors
step 1 :- Translator
step 2, step 3 :- Linker
step 4 :- Loader
Data

Translato Linker Loader Binary


r program

Source Results
program

Object Binary
modules programs

Data flow
Control flow
Translated, linked & load
 Translated (translated time)address:-
time Address
Address assigned by the translator
 Linked address:-
Address assigned by the linker
 Load time(load) address:-
Address assigned by the loader
 Translated origin:-
Address of the origin assumed by
the translator. This is the address
specified in
the ORIGIN statement.
 Linked origin:-
Address of the origin assigned by
the linker while producing a binary
program.
 Load origin:-
Address of the origin assigned by
the loader while loading the
program for execution.
 Address sensitive program is a
program that assumes its instructions
and data to occupy memory words
with specific addresses.
 Address sensitive instruction : An
instruction which uses an absolute
address.

Relocation and Linking Concepts


Relocation…?

It is the process of
modifying the address used
in the address sensitive
instructions of a program
such that the program can
execute correctly from the
designated area of memory.
When relocation is needed..?
 If Linked origin = translator origin ,
relocation must be performed by linker.
 If Load origin = linked origin, relocation
must be performed by the loader.
 A linker always performs relocation,
whereas some loaders do not.
 Loaders do not perform relocation are
called absolute loaders.
Performing Relocation

The relocation factor of P is defined as

Relocation-factorP = l-originP - t-originP


Where l-originP and t-originP are linked and translated
origin of program P.
(Relocation-factor can be +ve,-ve or zero)
Consider a symbol symb in P. Let its translation address
be tsymb and link time address be lsymb . Then

tsymb = t-originP + dsymb

Where dsymb is the offset of symb in P. Hence

lsymb = l-originP + dsymb


So
lsymb = t-originP +Relocation-factorP + dsymb
= t-originP + dsymb + Relocation-factorP
= tsymb + Relocation-factorP
START 500
ENTRY TOTAL
EXTRN MAX, ALPHA
READ A 500) + 09 0 540
LOOP 501)
……
MOVER AREG, ALPHA 518) + 04 1 000
BC ANY , MAX 519) + 06 6 000
……
BC LT, LOOP 538) + 06 1 501
STOP 539) + 00 0 000
A DS 1 540)
TOTAL DS 1 541)
END

Program P
Here t_originp = 500.
If l_originp = 900
Relocation factor = 900-500=400
Let IRRp designate the set of Instructions
Requiring Relocation in program p. IRRp
contains instructions with translated addresses
500 and 538. The instruction with trans. Address
500 contains the address 540 in the operand
field. This address is changed to 540+400 = 940.
Let symb be 540
tsymb = t_originp +dsymb (500+40=540)
lsymb = l_originp +dsymb (900+40=940)

lsymb = tsymb + relocation_factorp

= 540 + 400 = 940


Linking
Consider an application program AP
consisting of a set of program units SP={
Pi }.

A program unit Pi interacts with another


program unit Pj by using addresses of Pj’s
instrns and data in its own instrns.

To realize such interactions, Pj and Pi


must contain public definitions and
external references.
Public Defintion: A symbol pub_symb
defined in
a program unit which
may be
referenced in other
program units.

External reference: A reference to a symbol


ext_symb
which is not defined in
the
program unit containing
the
reference.
EXTRN and ENTRY statements

The ENTRY statement lists the public


definitions of a program unit, i.e. it
lists those symbols defined in the
program unit which may be
referenced in other program units.

The EXTRN statement lists the


symbols to which external references
are made in the program unit.
In the eg. program, the ENTRY statement
indicates that a public definition of
TOTAL exists in the program.

The EXTRN statement indicates that the


program contains external references to
MAX and ALPHA. The assembler does not
know the address of an external symbol.
Hence it puts zeroes in the address
fields of the instructions corresponding
to the statements MOVER AREG, ALPHA and
BC ANY, MAX.
Definition of Linking

Linking is the process of binding an


external reference to the correct
link time address.

Note: An external reference is said to be


unresolved until linking is performed for
it. It is said to be resolved when its
linking is completed.
START 200
ENTRY ALPHA
-----
-----
ALPHA DS 25 231) + 00 0 025
END

Program Q
Let program unit P be linked with the program unit
Q.
P contains an external reference to symbol ALPHA
which is a public definition in Q with translation
address 231. Let the link origin of P be 900 and its
size is 42 words. The link origin of Q is therefore
942, and the link time address of ALPHA is 973.
Linking is performed by putting the link time address
of ALPHA in the instruction of P using ALPHA. Ie,
by putting the address 973 in the instruction with the
translation time address 518 in P.
Binary Programs
To form a binary program from a set of object
modules, the programmer invokes the linke
using the command:

Linker <link origin>, <object module names>


[,<execution start address>]

Where <link origin> specifies the memory


address to be given to the first word of the
binary program.

<execution start address> is usually a


pair(pgm unit name,offset in pgm unit).
The linker converts this into a linked start
address. This is stored along with the binary
program for use when the program is to be
executed.

If <execution start address> is omitted the


execution start address is assumed to be the
same as the linked origin.

The linker converts the object modules in the


set of program units into a binary program.
The object module of a program P contains all information
necessary to relocate & link the program with other
programs .

4 Components of Object Module


Header
Program
Relocation table (RELOCTAB)
Linking table (LINKTAB)

Object module
Header
Header contains translated origin, size &
execution start address of P.

Program
This component contains the machine language program
corresponding to p.

Relocation Table (RELOCTAB)


This table describes IRRp. Each RELOCTAB contains a
single field.
Translated address: Translated address of an address
sensitive instruction.
IRRp-designates the set of instructions requiring relocation
in pgm ‘p’.
Linking Table (LINKTAB)
This table contains information concerning the
public definitions & external references in P.

Each LINKTAB entry contains 3 fields

Symbol : Symbolic name

Type : PD/EXT indicating whether


public
definition or external
definition

Translated address : For a public definition, this


is the address of the first memory word allocated
to the symbol .For an external reference, it is the
address of the memory word which is required to
contain the address of the symbol.
START 500
ENTRY TOTAL
EXTRN MAX, ALPHA
READ A 500) + 09 0 540
LOOP 501)
……
MOVER AREG, ALPHA 518) + 04 1 000
BC ANY , MAX 519) + 06 6 000
……
BC LT, LOOP 538) + 06 1 501
STOP 539) + 00 0 000
A DS 1 540)
TOTAL DS 1 541)
END

Program P
Object module of Eg. program
contains

1.Translated origin=500, size=42,


execution start address=500
2.Machine language instructions
3.Relocation table
500
538
4.Linking table

ALPHA EXT 518


MAX EXT 519
TOTAL PD 541
Program Relocation Algorithm

1. program_linked _origin:=<link origin>


from linker command;
2. for each module
a) t_origin:=translated origin of the object
module;
OM_size := size of object module;
b) relocation_factor:=program_linked_origi
n – t_origin;
c) Read the machine language program in
work_area ;
d) Read RELOCTAB of the object module;
e) for each entry in RELOCTAB
i. translated_addr:=address in the
RELOCTAB entry;
ii. address_in_work_area:=address of
work_area + translated_address –
t_origin;
iii. Add relocation_factor to the operand
address in the word with the
address_in_work_area.
f) program_linked_origin:=program_link
ed_origin + OM_size;
Step 2 (f) increments
program_linked_origin so that the next
object module would be granted the next
available load address.
Eg. Let the address of work_area be 300.
While relocating the object module of the
eg. Program, relocation_factor=400. For
the first RELOCTAB entry,
address_in_work_area=300+500-
500=300. This word contains the
instruction for READ A. It is relocated
by adding 400 to the operand address in
it. For the second entry,
address_in_work_area=300+538-
500=338. The instruction in this word is
relocated by adding 400 to the operand
address in it.
Linking Requirements
The linker processes all object modules
being linked and builds a table of all Public
Definitions (PD) and their load time
addresses.
Linking of ALPHA is simply a matter of
searching for ALPHA in this table and
copying the linked address into the word
containing the external reference.
Name Table (NTAB)
A data structure used during
Program Linking. Each entry of this table
will have the following 2 fields-
Symbol : Symbolic name of an external
reference or an object module.
Linked-address : For a PD, it contains
linked-address of the symbol. For an OM, it
contains linked-origin of OM.
Program Linking-Algorithm
1.program-linked-origin := <link origin> from
linker command.
2.For each object module
a) t-origin := translated origin of the object
module.
OM-size := size of the object module.
b) relocation-factor := program_linked_origin –
t_origin.
c) Read the machine language program in work-
area.
d) Read LINKTAB of the object module.
e) For each LINKTAB entry with type = PD
name := symbol.
linked-address := translated-address +
relocation
Program linked factor.
origin of Q is 942. t_origin of Q is 200.
Enter =(name,
Hence relocation_factor linked-address)
942 – 200 = 742. in NTAB.
Linked address of ALPHA = 231 + 742 = 973.
f) Enter (object module name, program-linked-origin)
in NTAB.
g) program-linked-origin := program-linked-origin +
OM-size.

3. For each object module


a) t-origin := translated origin of the object module.
program_linked_origin := load_address from NTAB.
b) For each LINKTAB entry with type = EXT
i) address_in_work_area := address of work_area
+
program_linked_origin - <link_origin> +
translated address –
t_origin.
ii) Search symbol in NTAB and copy its linked
address.
Add the linked address to the operand address
in the
word with the address address-in-work-area.
address

EXT 518 ALPHA PD

EXT 519
PD 541

f Object Module P LINKTAB of Object


While linking P and Q, with linked origin 900,
NTAB

Symbol linked address

P 900
TOTAL 941
Q 942
ALPHA 973
….

NTAB entries created while Linking


Let the address of work_area be 300. When the
LINKTAB entry of ALPHA is processed during
linking,
address_in_work_area = 300+900-900+518-500
= 318

Hence , the linked address of ALPHA (ie, 973) is copied


from NTAB entry of ALPHA and added to the word in
address 318. (Step 3(b) (ii)).
SELF RELOCATING PROGRAMS

The manner in which a program can be modified, or can modify


itself, to execute from a given load origin can be used to classify
programs into one of the following forms:

1.Non relocatable pgms

2.Relocatable pgms

3.Self-relocating pgms
Non Relocatable programs

It is a program which cannot be executed


in memory area other than the area starting
on its translated origin.

Non relocatability is the result of address


sensitivity of a program and lack of
information concerning the address
sensitive instructions in the program.
Relocatable pgms
A relocatable program form is one which consists of a program and
relevant information for its relocation.

A relocatable program can be processed to relocate it to a desired area


of memory.

Hand coded machine language program is an eg for non relocatable


program and object modules are relocatable programs.
Self-relocating programs
Itis a program which can perform the relocation of its
own
address sensitive instructions.

It contains:
1.A table of information concerning the address
sensitive instructions exists as a part of the
program.
2.Code to perform the relocation of address
sensitive
instructions also exists as a part of the
program. This
is called the relocating logic.
The start address of the relocating logic is specified as
the execution start address of the program.
The relocating logic gains control when the program is
loaded in memory for execution.
It uses the load address and the information concerning
the address sensitive instructions to perform its own
relocation.
Execution control is now transferred to the relocated
program.
A self-relocating program can execute in any area of the
memory.
Relocating
Program Program Logic
+ +
Data Data
Non-relocatable pgm Program
+
Relocation Data
Relocatable pgm
Infmn

Relocation
Self-relocating
Infmn pgm
LOADER
S
A loader is a program that takes an input
as an object program, prepares these
programs for execution by the computer
and initiates execution.
Functions of a loader
1. Allocation : It allocates space in the
memory for the programs.
2. Linking : It resolves symbolic
references between objects
3. Relocation : It adjusts all address
dependent locations,such as address
constant etc, to correspond to the
allocated space.
4. Loading : It physically places the
machine instructions or data into the
memory.
Source Translat Object
Progra or Program
m 1 Object
programs
ready for
Loader execution
+

Source Object
Translat Program
Progra or
m 2 Loader

General Loader
Scheme
 Type of loaders
◦Assemble-and-go loader
(Compile-and-go loader)
◦Absolute loader (Bootstrap
loader)
◦Relocating loader (Relative
loader)
◦Direct linking loader
Compile-and-go-loader
One method of performing the loader
functions is to have the assembler run in
one part of memory and place the
assembled machine instructions and data,
as they are assembled, directly into their
assigned memory locations . When the
assembly is completed, the assembler
causes a transfer to the starting instruction
of the program. This is a simple solution,
involving no extra procedures. Such a
loading scheme is commonly called
"compile-and-go" or "assemble and-go." It
is relatively easy to implement. The
assembler simply places the code into core,
and the "loader" consists of one instruction
Disadvantages
 A portion of memory is wasted.

 It is necessary to retranslate (assemble)


the
user's program deck every time it is
run.

It is very difficult to handle multiple


segments,
especially if the source programs are in
different
languages.
Absolute Loader
An absolute loader is the simplest type of loader
scheme that fits the general model of loaders.
The assembler produces the output in the same
way as in the "complier and go loader“, except
that the data is punched on cards(object deck)
instead of being placed directly in memory .

An absolute loader can only load program with


load origin=linked origin.

Disadvantage: The programmer has to specify


the address to the assembler that where the
program is to be loaded.
It is very difficult to relocate in case of multiple
subroutine.
Programmer has to remember the address of
Absolute Loader …

The four loader functions are


accomplished as follows in an absolute
loading scheme.
Allocation – by programmer
Linking - by programmer
Relocation – by assembler
Loading – by loader
A bootstrap loader (Absolute loader)
is a program that resides in the
computers EPROM, ROM, or
other non-volatile memory that
automatically executed by the
processor when turning on the
computer. The bootstrap loader reads
the hard drives boot sector to
continue the process of loading the
computers operating system.
This bootstrap loads the first program to be
run by the computer -- usually an
Relocating loaders are used to avoid
possible reassembling of all subroutines
when a single subroutine is changed, and
to perform the tasks of allocation and
linking for the programmer.

Relocating loaders adjust addresses


(pointers) in the executable to
compensate for variations in the address
at which loading starts.
Relocating Loaders
The computers which need relocating
loaders are those in which pointers are
 The relocating loader will load the
program anywhere in memory,
altering the various addresses as
required to ensure correct referencing.
 The decision as to where in memory
the program is placed is done by the
Operating System, not the programs
header file.
 A Direct linking loader is a general
relocating loader and is the most popular
loading scheme presently used.
 This scheme has an advantage that it
allows the programmer to use multiple
procedure and multiple data segments.
 In addition, the programmer is free to
reference data or instructions that are
contained in other segments.
 The direct linking loaders provide flexible
intersegment referencing and accessing
ability.
Direct Linking Loaders
EDITOR
S
 LineEditors
 Stream Editors
 Screen Editors
 Word Processors
 Structure Editors

Different Types of Editors


 The scope of edit operations in a line
editor is limited to a line of text.
 The line is designated positionally.

Advantage:-
Simplicity
Disadvantage:-
Line Does not display the text
Editors
in the manner it would appear
if printed
 Views the entire text as a stream
of characters.

 This permits edit operations to cross


line
boundaries.

Support character, line and


context oriented commands based on
Stream Editors
the current editing context indicated by
the position of a text pointer.
Screen Editor

 A screen editor uses the ‘what you see is what


you get’ principle in editor design.

 The user can move the cursor over the screen,


position it at the point where he desires to
perform some editing and proceed with the
editing directly.

Advantage:-

 Useful while formatting the text to produce


printed documents.
 Basically document editors with
additional features to produce well
formatted hard copy output.

 Essentialfeatures of word
processors are commands for
moving sections of text from one
place to another, merging of text,
and searching and replacement
of words.
Word Processors
 Many word processors support a
spell check option. e.g. Wordstar,
MS Word

 Incorporates an awareness of the structure
of a document.

 Structure is specified by the user while


creating or modifying the document.

 Editing requirements are specified using the


structure.

 A special class of structure editors , called


Structure Editors
syntax directed editors, are used in
programming environments.
Debug monitors provide the following
facilities for dynamic debugging.
1. Setting breakpoints to the program
2. Initiating a debug conversation when
control reaches a breakpoint
3. Displaying values of variables
4. Assigning new values to variables
5. Testing user defined assertions and
predicates involving program variables.

Debug Monitors

Potrebbero piacerti anche