165
UNIT II 1 Jagadish K Kamble (M.Tech, IIT Kharagpur) Dept. of Information Technology, Pune Institute of Computer Technology, Pune

Chapter 5 Macro and Macro Processors

  • Upload
    others

  • View
    37

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 5 Macro and Macro Processors

UNIT II

1

Jagadish K Kamble (M.Tech, IIT Kharagpur)

Dept. of Information Technology, Pune Institute of Computer Technology, Pune

Page 2: Chapter 5 Macro and Macro Processors

MACRO AND MACRO

PRE-PROCESSORS

Page 3: Chapter 5 Macro and Macro Processors

Introduction

Macro Facility provided in assembly languages, which

enables programmer to define new operations and

data structures of her own choice to simplify design and

coding of programs.

Macro are used to provide a program generation

facility through macro expansion.

A general purpose generator should provide features

for defining new operations and data.

A Macro is such feature provided in a programming

language.

Many programming language provide built in facilities

for writing macros. E.g. Ada,C and C++.

Page 4: Chapter 5 Macro and Macro Processors

Introduction

Functions

Macro Instructions are single line abbreviations for

multiple instructions.

Macro is a Pre-processor directive.

Advantage: Simplifies and reduces repetitive code

Increase speed of execution.

Time saving

More Readable

Disadvantage Size of program

Page 5: Chapter 5 Macro and Macro Processors

Recall

•Macro and Macro Preprocessor

Page 6: Chapter 5 Macro and Macro Processors

Cont.

“A macro is a unit of specification for program

generation through expansion.

Macro definition consist of name, a set of formal

parameters and a body of code.

It replaces macro call into macro definition.

“The use of macro name with a set of actual parameters

(macro call) is replaced by some code generated from its

body (macro definition), this is called macro expansion.”

Page 7: Chapter 5 Macro and Macro Processors

Cont.

Two kind of expansion

Lexical expansion:

◼ Lexical expansion implies replacement of character string by another character string during program generation.

◼ Lexical expansion is typically employed to replace occurrences of formal parameter by corresponding actual parameters.

Semantic Expansion:

◼ Semantic expansion implies generation of instructions tailored to the requirements of a specific usage

◼ Example: generation of type specific instruction for

manipulation of byte and word operands.

Page 8: Chapter 5 Macro and Macro Processors

Example: the following sequence of instruction is used to

increment the value in a memory word by a constant:

Move the value from the memory word into a machine

register.

Increment the value in the machine register.

Move the new value into the memory word.

Using lexical expansion the macro call INCR A,B,AREG

can lead to the generation of a MOVE-ADD-MOVE

instruction sequence to increment A by the value B using

AREG.

Page 9: Chapter 5 Macro and Macro Processors

Macro definition and call

Macro definition:

A macro definition is enclosed between a macro header

statement and macro end statement.

Macro definition are typically located at the start of

program .

Macro definition consist of

◼ A macro prototype statement

◼ One or more model statement

◼ Macro preprocessor statement

Page 10: Chapter 5 Macro and Macro Processors

Cont.

A macro prototype statement

◼ The macro prototype statement declares the name of a macro and the names and kinds of its parameters.

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

◼ Where name appears in the mnemonic field of assembly statement and <formal parameter spec> is of the form &<parameter name>[<parameter kind>]

Model statement

◼ A model statement is a statement from which an assembly language statement may be generated during macro expansion.

Macro preprocessor statement

◼ A preprocessor statement is used to perform auxiliary functions

during macro expansion.

Page 11: Chapter 5 Macro and Macro Processors

Example

Macro

INCR &MEM_VAL, &INCR_VAL, &REG

MOVER &REG, &MEM_VAL

ADD &REG,&INCR_VAL

MOVEM &REG, &MEM_VAL

MEND

Macro Prototype Statement Model Statement

Page 12: Chapter 5 Macro and Macro Processors

Macro call

A macro is called by writing the macro name in the

mnemonic field of an assembly statement.

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

Where an actual parameter typically an operand

specification in an assembly language statement.

INCR A, B, AREG

Page 13: Chapter 5 Macro and Macro Processors

Macro Expansion

A macro call leads to macro expansion, during macro

expansion, the macro call statement is replaced by a

sequence of assembly statements.

“+” is used to differentiate between the original

statement of program and macro statement.

Performed by two kinds of language processor

Macro Assembler:- performs expansion of each macro call

in a program into sequence of assembly statements and

assembles the resulting assembly program.

Page 14: Chapter 5 Macro and Macro Processors

Macro Preprocessor:-merely performs expansion of macro

calls in program.

Macro Preprocessor operates as follows: If the first statement in the program input to it is a macro statement, it knows

that one or more macro definitions exist in the program. It processes and stores all macro definitions in its own data structures. After all macro definitions have been processed , it produces output program

as follows: If the next statement in the program is not macro call, it merely copies the

program in to output program. If it is macro call, it visits the model statements find in the definition of the

called macro and generates assembly statements from them throughsubstitution of formal parameters

Distinguishing the original statements of program and statements generatedduring macro expansion by printing the symbol ‘+’ before the label field of eachgenerated statement.

Page 15: Chapter 5 Macro and Macro Processors

Macro Expansion

Two key notions concerning macro expansion are:

Expansion time control flow:

◼ This determines the order in which model statements are visited

during macro expansion.

Lexical substitution:

◼ Lexical substitution is used to generate an assembly

statement from a model statement.

Page 16: Chapter 5 Macro and Macro Processors

Flow of control during expansion

Flow of control during expansion

The default flow of control during macro expansion is sequential. its start with statement following the macro prototype statement and ending with the statement preceding the MEND statement.

A preprocessor statement can alter the flow of control during expansion such that some model statements are never visited during expansion is called conditional expansion.

Same statement are repeatedly visited during expansion is called loops expansion.

Page 17: Chapter 5 Macro and Macro Processors

Algorithm – Macro Expansion

Macro expansion is implemented using a macro expansion counter (MEC).

Algorithm: (Outline of macro expansion)

MEC=statement number of first statement following the prototype statement;

While statement pointed by MEC is not a MEND statement◼ (a) if a model statement then

◼ (i) Expand the statement◼ (ii) MEC=MEC+1;

◼ (b) Else (i.e. a preprocessor statement)◼ (i) MEC= new value specified in the statement;

Exit from macro expansion.

Page 18: Chapter 5 Macro and Macro Processors

Lexical Substitution

Lexical Substitution: Model statement consists of 3 type of strings

◼ An ordinary string, which stands for itself.

◼ The name of a formal parameter which is preceded by the character „&‟.

◼ The name of preprocessor variable, which is also preceded by the character „&‟.

During lexical expansion, string of type 1 are retainedwithout substitution.

String type 2 and 3 are replaced by the corresponding actual parameter values.

The value of formal parameter depends on the kind ofparameter.

Page 19: Chapter 5 Macro and Macro Processors

Types of Parameters

Positional parameters

Keyword parameters

Default specification of parameter

Macro with mixed parameter lists

Other uses of parameters

Page 20: Chapter 5 Macro and Macro Processors

Positional parameters

Positional parameters A positional formal parameter, the specification <parameter kind> is

simply omitted. A positional formal parameter is written as

&<parameter name>

e.g., &SAMPLE where SMAPLE is name of parameter.

The <actual parameter spec> in call on a macro using positional

parameters is simply an <ordinary string>.

The value of positional formal parameter XYZ is determined by the

rules of positional association as follows:

Step-1 find the ordinal position of XYZ in the list of formal

parameters in the macro prototype statement.

Step-2 find the actual parameter specification occupying the same

ordinal position in the list of actual parameters in macro call

statement.

Page 21: Chapter 5 Macro and Macro Processors

RECALL

MACRO DEFINITION

MACRO CALL

MACRO EXPANSION

TYPES OF PARAMETER

Page 22: Chapter 5 Macro and Macro Processors

Positional parameters – Example

INCR A, B, AREG

The rule of positional association values of the formal parameters are:

Formal parameterMEM_VAL

INCR_VAL

REG

valueA

B

AREG

Lexical expansion of model statement now leads to the code

Macro

INCR &MEM_VAL, &INCR_VAL, &REG

MOVER &REG, &MEM_VAL

ADD &REG,&INCR_VAL

MOVEM &REG, &MEM_VAL

MEND

+ MOVER AREG, A

+ ADD AREG, B

+ MOVEM AREG, A

Page 23: Chapter 5 Macro and Macro Processors

Keyword parameters

Keyword parameters

<parameter name > is an ordinary string and

<parameter kind> is the string “ = ”in syntax rule.

The <actual parameter spec> is written as <formal

parameter name>=<ordinary string>.

The keyword association rules:

◼ Step-1 find the actual parameter specification which has the

form XYZ=<ordinary string>

◼ Step-2 Let <ordinary string> in the specification be the string

ABC. Then the value of formal parameter XYZ is ABC.

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

&<parameter name>[<parameter kind>]

Page 24: Chapter 5 Macro and Macro Processors

Keyword parameters

Example :

Macro

INCR &MEM_VAL=, &INCR_VAL=, &REG=

MOVER &REG, &MEM_VAL

ADD &REG,&INCR_VAL

MOVEM &REG, &MEM_VAL

MEND

INCR MEM_VAL=A, INCR_VAL=B, REG=BREG

INCR INCR_VAL=B, REG=AREG, MEM_VAL=A

+ MOVER AREG, A

+ ADD AREG, B

+ MOVEM AREG, A

Page 25: Chapter 5 Macro and Macro Processors

Default specification of parameters

Default specification of parameters

A default is a standard assumption in the absence of an

explicit specification by programmer.

Default specification of parameters is useful in

situations where a parameter has the same value in

most calls.

When desired value is different from the default value,

the desired value can be specified explicitly in a macro

call.

Page 26: Chapter 5 Macro and Macro Processors

Default specification of parameters

Example:

INCR_D MEM_VAL=A, INCR_VAL=B

INCR_D INCR_VAL=B, MEM_VAL=A

INCR_D INCR_VAL=B, MEM_VAL=A, REG=BREG

MARCO DIFINITION

Macro

INCR &MEM_VAL=, &INCR_VAL=, &REG=AREG

MOVER &REG, &MEM_VAL

ADD &REG,&INCR_VAL

MOVEM &REG, &MEM_VAL

MEND

Call the macro

+ MOVER AREG, A

+ ADD AREG, B

+ MOVEM AREG, A

+ MOVER AREG, A

+ ADD AREG, B

+ MOVEM AREG, A

+ MOVER BREG, A

+ ADD BREG, B

+ MOVEM BREG, A

Page 27: Chapter 5 Macro and Macro Processors

Macro with mixed parameter lists

Macro with mixed parameter lists

A macro may be defined to use both positional and

keyword parameters.

All positional parameters must precede all keyword

parameters.

Example: SUMUP A,B,G=20,H=X

Where A,B are positional parameters while G,H are

keyword parameters.

Page 28: Chapter 5 Macro and Macro Processors

Mixed parameters

Example:

INCR_D A, B, REG=AREG

MARCO DIFINITION

Macro

INCR &MEM_VAL, &INCR_VAL, &REG=

MOVER &REG, &MEM_VAL

ADD &REG,&INCR_VAL

MOVEM &REG, &MEM_VAL

MEND

Call the macro

+ MOVER AREG, A

+ ADD AREG, B

+ MOVEM AREG, A

Page 29: Chapter 5 Macro and Macro Processors

Other uses of parameters

Other uses of parameters

The model statements have used formal parameters

only in operand fields.

Formal parameter can also appear in the label and

opcode fields of model statements.

Page 30: Chapter 5 Macro and Macro Processors

Other uses of parameters-Example

Macro

CALC &X, &Y, &OP=MULT, &LAB=

&LAB MOVER AREG, &X

&OP AREG, &Y

MOVEM AREG, &X

MEND

Expansion of the Macro Call

CALC A, B, LAB=LOOP leads to following code.

+ LOOP MOVER AREG, A

+ MULT AREG, B

+ MOVEM AREG, A

Page 31: Chapter 5 Macro and Macro Processors

Nested Macro Call

A model statement in macro may constitute a call on

another macro, such calls are known as nested

macro calls.

The macro containing the nested call is called outer

macro.

The called macro called inner macro.

Expansion of nested macro calls follows the last-in-

first-out(LIFO) rule.

Page 32: Chapter 5 Macro and Macro Processors

Nested Macro

Call - Example

Macro

COMPUTE &FIRST, &SECOND

MOVEM BREG, TMP

INCR_D &FIRST, &SECOND, REG=BREG

MOVER BREG, TMP

MEND

+ MOVEM BREG, TMP

INCR_D X, Y

+ MOVER BREG, TMP

COMPUTE X, Y

+ MOVER BREG, X

+ ADD BREG, Y

+ MOVERM BREG, X

+ MOVEM BREG, TMP

+ MOVER BREG, X

+ ADD BREG, Y

+ MOVEM BREG, X

+ MOVER BREG, TMP

Page 33: Chapter 5 Macro and Macro Processors

Compiler

Convert High level language into machine level

language

Machine Language is tedious and difficult to use.

PreprocessorSource program

CompilerPure HLL

AssemblerAssemblycode

Linker

M/C Code (relocatble)

LoaderM/C Language

Program

Results

Data

Page 34: Chapter 5 Macro and Macro Processors

Compiler

PhasesLexical Analysis

Source program

Syntax Analysis

Stream of tokens

Semantic Analysis

Parse Tree

Intermediate Code Generation

Parse Tree(Semantically)

Code Optimization

Target Code Generation

Target Code

Three Address Code

Optimized Code

Symbol TableError Handler

Page 35: Chapter 5 Macro and Macro Processors

Recall

•Types of Parameters

•Nested Macro Calls

Page 36: Chapter 5 Macro and Macro Processors

Advanced Macro Facilities

Advance macro facilities are aimed at supporting

semantic expansion.

Facilities for alteration of flow of control during

expansion.

Expansion time variables

Attributes of parameters.

Page 37: Chapter 5 Macro and Macro Processors

Alteration of flow of control during

expansion

Alteration of flow of control during expansion:

Flow of control during macro expansion can be altered by

performing an expansion time control flow, which directs the

preprocessor to particular model statement for expansion.

A Unique Sequencing Symbol(SS) is written in label field of

statement in macro definition. It provides convenient method for

identifying that statement for expansion time control flow.

The preprocessor statement with mnemonic AIF or AGO uses this

Sequencing Symbol as an operand. If the condition specified in the

AIF statement is satisfied, macro preprocessor puts the statement

number of the statement that has sequencing symbol in its label field

into MEC used in Algorithm.

The action performed unconditionally for AGO statement.

Page 38: Chapter 5 Macro and Macro Processors

Cont..

Expansion time statements AIF, AGO and ANOP.

Sequencing symbol(SS) has syntax

.<ordinary string>

A SS is defined by putting it in the label field of statement in the

macro body. It is used as operand in an AIF, AGO statement

for expansion control transfer.

Page 39: Chapter 5 Macro and Macro Processors

Cont.

An AIF statement has syntax

AIF (<expression>) <sequencing symbol>

Where, <expression> is relational expression involving ordinary strings,

formal parameters and their attributes, and expansion time variables.

If the relational expression evaluates to true, expansion time control is

transferred to the statement containing <sequencing symbol> in its

label field.

Attribute has syntax:

<attribute name>’ <formal Parameter Specification>

And represent information about value of formal parameter i.e. about

actual parameter. The Type, length and size attributes have the names

T, L and S.

Page 40: Chapter 5 Macro and Macro Processors

Example

Macro

DCL_CONST &A

AIF (L ‘ &A EQ 1) .NEXT

------

.NEXT ------

------

MEND

Page 41: Chapter 5 Macro and Macro Processors

Cont.

An AGO statement the syntax

AGO <sequencing symbol>

Unconditionally transfer expansion time control to the statement containing <sequencing symbol> in its label field.

An ANOP statement is written as

<sequencing symbol> ANOP

Simply has the effect of defining the sequencing symbol.

Page 42: Chapter 5 Macro and Macro Processors

Recall

AIF

AGO

ANOP

Page 43: Chapter 5 Macro and Macro Processors

Expansion Time Variable (EV’s)

Expansion Time Variable

Expansion time variable are variables which can only be

used during the expansion of macro calls.

Its value can be used only within macro definition preprocessor

statement assigns value to an expression variable, in model staement

and condtional expression.

Local EV is created for use only during a particular macro call.

Global EV exists across all macro calls situated in program and can

be used in any macro which has a declaration for it.

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

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

Page 44: Chapter 5 Macro and Macro Processors

Cont.

<EV specification>has syntax &<EV name>, where EV

name is ordinary string.

Initialize EV by preprocessor statement SET.

<EV Specification> SET <SET-expression>

Page 45: Chapter 5 Macro and Macro Processors

Example

Macro

CONSTANTS

LCL &A

&A SET 1

DB &A

&A SET &A+1

DB &A

MEND

Page 46: Chapter 5 Macro and Macro Processors

Attributes of formal parameters

Attributes of formal parameters:

<attribute name>‘<formal parameter spec>

Represents information about the value of the formal

parameter about corresponding actual parameter.

The type, length and size attributes have the name T,L

and S.

Page 47: Chapter 5 Macro and Macro Processors

Example

Page 48: Chapter 5 Macro and Macro Processors

Cont.

Conditional expansion:

Conditional expansion helps in generating assembly

code specifically suited to the parameters in macro call.

A model statement is visited only under specific

conditions during the expansion of a macro.

AIF and AGO statement used for this purpose.

Page 49: Chapter 5 Macro and Macro Processors

Cont.

Example: evaluate A-B+C in AREG.

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

Page 50: Chapter 5 Macro and Macro Processors

Cont.

Expansion time loop

To generate many similar statements during the expansion of a macro.

This can be achieved by similar model statements in the macro.

Example:

MACRO

CLEAR &A

MOVER AREG,=‘0’

MOVEM AREG, &A

MOVEM AREG, &A+1

MOVEM AREG, &A+2

MEND

Page 51: Chapter 5 Macro and Macro Processors

Cont.

Expansion time loops can be written using expansion time variables and expansion time control transfer statement AIF and AGO.

Example: 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

+ MOVER AREG, =‘0’

+ MOVEM AREG, B

+ MOVEM AREG, B+1

+ MOVEM AREG, B+2

CLEAR B, 3

Page 52: Chapter 5 Macro and Macro Processors

Cont.

Comparison with execution time loops:

Most expansion time loops can be replaced by

execution time loops.

An execution time loop leads to more compact

assembly programs.

In execution time loop programs would execute slower

than programs containing expansion time loops.

Page 53: Chapter 5 Macro and Macro Processors

Cont.

Other facilities for expansion time loops:

REPT statement

◼ Syntax: REPT <expression>

◼ <expression> should evaluate to a numerical value during

macro expansion.

◼ The statements between REPT and an ENDM statement

would be processed for expansion <expression> number of

times.

Page 54: Chapter 5 Macro and Macro Processors

Cont.

Example

MACRO

CONST10

LCL &M

&M SET 1

REPT 10

DC ‘&M’

&M SET &M+1

ENDM

MEND

Page 55: Chapter 5 Macro and Macro Processors

Cont.

Semantic Expansion:

Semantic expansion is the generation of instructions tailored to the requirements of a specific usage.

Example:

MACRO

CREATE_CONST &X, &Y

AIF (T‟&X EQ B) .BYTE

&Y DW 25

AGO .OVER

.BYTE ANOP

&Y DB 25

.OVER MEND

Page 56: Chapter 5 Macro and Macro Processors

DESIGN OF A MACRO PREPROCESSOR

The macro preprocessor accepts an assembly

program containing definitions and calls and

translates it into an assembly program which does

not contain any macro definition or call.

Macro

preprocessorAssembler

Program with

macro definitions

and callsProgram without

macros

Target program

Page 57: Chapter 5 Macro and Macro Processors

Cont.

Design overview

Listing all tasks involved in macro expansion

◼ Identify macro calls in the program.

◼ Determine the values of formal parameters.

◼ Maintain the values of expansion time variables declared in

a macro.

◼ Organize expansion time control flow.

◼ Determine the values of sequencing symbols.

◼ Perform expansion of a model statement.

Page 58: Chapter 5 Macro and Macro Processors

Cont.

The following 4 step procedure is followed to arrive

at a 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.

Page 59: Chapter 5 Macro and Macro Processors

Cont.

Identify macro calls: A table called the macro name table (MNT) is designed to hold the

name of all macro defined in program.

Determine values of formal parameters

A table called actual parameter table (APT) is designed to hold the values of formal parameters during the expansion of a macro call.

It contains (<formal parameter name>,<value>)

A table called parameter default table(PDT) is used for each macro.

Accessible from the MNT entry of macro.

It contain pairs of the form (<formal parameter name>,<default value>).

If macro call statement does not specify a value for some parameterthen its default value would be copied from PDT to APT.

Page 60: Chapter 5 Macro and Macro Processors

Cont.

Maintain expansion time variables:

An expansion time variables table (EVT) is maintained

for this purpose.

Table contain pairs of the form

(<EV name>,<value>)

It accessed when a preprocessor statement or model

statement under expansion refers to an EV.

Page 61: Chapter 5 Macro and Macro Processors

Cont.

Organize expansion time control flow

The body of macro contained set of model statements

and preprocessor statement in it, is stored in a table

called the macro definition table (MDT) for use during

macro expansion.

The flow of control during macro expansion determines

when a model statement is to be visited for expansion.

Page 62: Chapter 5 Macro and Macro Processors

Cont.

Determine values of sequencing symbols:

A sequencing symbol table (SST) is maintained to hold

this information

Table contains pairs 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.

Page 63: Chapter 5 Macro and Macro Processors

Cont.

Perform expansion of a model statement

Task are as follow

◼ MEC points to the MDT entry containing the model statement.

◼ Values of formal parameters and EV‟s are available in APT

and EVT, respectively

◼ 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.

Page 64: Chapter 5 Macro and Macro Processors

Copy code -- Example

Source

MACRO

STRG

STA DATA1

STB DATA2

STX DATA3

MEND

.

STRG

READ A

STRG

.

.

Expanded source

.

.

.

+ STA DATA1

+ STB DATA2

+ STX DATA3

READ A

+ STA DATA1

+ STB DATA2

+ STX DATA3

.

{

{

Page 65: Chapter 5 Macro and Macro Processors

Parameter Substitution -- Example

Source

MACRO

STRG &a1, &a2, &a3

STA &a1

STB &a2

STX &a3

MEND

.

STRG DATA1, DATA2, DATA3

PRINT DATA1

STRG DATA4, DATA5, DATA6

.

.

Expanded source

.

.

.

+ STA DATA1

+ STB DATA2

+ STX DATA3

PRINT DATA1

+ STA DATA4

+ STB DATA5

+ STX DATA6

.

{

{

Page 66: Chapter 5 Macro and Macro Processors

RECALL

•Macro pre-processor

•Different Tasks

•Procedures to perform task

•Macro Name Table(MNT)

•Actual parameter Table(APT)

•Parameter Default Table(PDT)

•Expansion Time Variable Table(EVT)

•Macro Definition Table(MDT)

•Sequencing Symbol Table(SST)

•Macro Expansion

Page 67: Chapter 5 Macro and Macro Processors

APT EVT

SST

X

N

REG

M

MORE

MNT CLEARMEM

PDT REG AREG

MDT LCL &M

&M SET 0

MOVEM &REG, =‘0’

.MORE MOVEM &REG, &X+&M

&M SET &M+1

AIF (&M NE N) .MORE

MACRO

CLEARMEM &X, &N, &REG=AREG

LCL &M

&M SET 0

MOVEM &REG, =‘0’

.MORE MOVEM &REG, &X+&M

&M SET &M+1

AIF (&M NE N) .MORE

MEND

0X

N

REG

1

2

3

4

5

6

#4

Page 68: Chapter 5 Macro and Macro Processors

Data structures

To obtain a detailed design of the data structure it

is necessary to apply the practical criteria of

processing efficiency and memory requirements.

The table APT,PDT and EVT contain pairs which are

searched using the first component of the pairs as a

key- the formal parameter name is used as the key

to obtain its value from APT.

Page 69: Chapter 5 Macro and Macro Processors

Cont.

This search can be eliminated if the position of an entity within a table is known when its value is accessed.

The value of formal parameter ABC is needed while expanding a model statement using it

MOVER AREG, &ABC

Let the pair (ABC,5) occupy entry #5 in APT. the search inAPT can be avoided if the model statement appears as

MOVER AREG, (P,5)

In the MDT, where (P,5) stand for the word “Parameter #5”

Page 70: Chapter 5 Macro and Macro Processors

Cont.

The first component of the pairs stored in APT is no longer used during macro expansion e.g. the information (P,5) appearing in model statement is sufficient to access the value of formal parameter ABC.

APT containing (<formal parameter name>,<value>) pairs is replaced by another table called APTAB which only contains <value>‘s

Ordinal number are assigned to all parameters of macro,a table named parameter name table (PNTAB) is used for this purpose.

Parameter name are entered in PNTAB in same order in which they appear in the prototype statement.

Page 71: Chapter 5 Macro and Macro Processors

Cont.

The information (<formal parameter

name>,<value>) in APT has been split into two tables

PNTAB- which contains formal parameter names

APTAB- which contains formal parameter values

PNTAB is used while processing a macro definition while

APTAB is used during macro expansion.

Page 72: Chapter 5 Macro and Macro Processors

Cont.

Similar analysis leads to splitting of EVT into

EVNTAB and EVTAB and SST into SSNTAB and

SSTAB.

EV name are entered in EVNTAB while processing EV

declarations.

SS name are entered in SSNTAB while processing an

SS reference or definition, whichever occur earlier.

Page 73: Chapter 5 Macro and Macro Processors

Cont.

replace parameter default table(PDT) by a keyword parameter default table (KPDTAB), this table have only kentries.

MNT has entries for all macros defined in a program, each entry contains three pointers MDTP,KPDTP and SSTP which are pointers to MDT,KPDTAB and SSNTAB for the macro respectively.

Page 74: Chapter 5 Macro and Macro Processors

Cont.

Macro preprocessor data structure can be summarized as follows: PNTAB and KPDTAB are constructed by processing the

prototype statement.

Entries are added to EVNTAB and SSNTAB as EV declarations and SS definitions/references are encountered.

MDT entries are constructed while processing modelstatements and preprocessor statements in macro body.

SSTAB entries, when the definition of sequencing symbol in encountered.

APTAB is constructed while processing a macro.

EVTAB is constructed at the start of expansion of macro.

Page 75: Chapter 5 Macro and Macro Processors

Tables of the macro preprocessorTable Fields in each entry

Macro name Table(MNT) Macro name,

Number of positional parameter(#PP),

Number of keyword parameter(#KP),

Number of expansion time variables(#EV),

MDT pointer (MDTP).

KPDTAB pointer (KPDTP).

SSTAB pointer (SSTP)

Parameter Name Table(PNTAB) Parameter name

EV Name Table (EVNTAB) EV name

SS Name Table (SSNTAB) SS name

Keyword Parameter Default

Table(KPDTAB)

Parameter name, default value

Macro Definition Table(MDT) Label, Opcode, Operands

Actual Parameter Table(APTAB) Value

EV Table (EVTAB) Value

SS Table (SSTAB) MDT entry #

Page 76: Chapter 5 Macro and Macro Processors

Cont.

MACRO

CLEARMEM &X, &N, &REG=AREG

LCL &M

&M SET 0

MOVEM &REG, =‘0’

.MORE MOVEM &REG, &X+&M

&M SET &M+1

AIF (&M NE N) .MORE

MEND

Page 77: Chapter 5 Macro and Macro Processors

PNTAB EVNTAB

SSNTAB

X

N

REG

M

MORE

MNT CLEARMEM 2 1

#PP #KP #EV MDTP KPDTP SSTP

KPDTAB 10 REG AREG SSTAB 5 27

MDT

MOVER (P, 3) =‘ 0 ’

MOVEM (P,3),(P,1)+(E,1)

MEND

25 LCL (E,1)

26 (E,1) SET 0

27

28

29 (E,1) SET (E,1)+1

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

31

MACRO

CLEARMEM &X, &N, &REG=AREG

LCL &M

&M SET 0

MOVEM &REG, =‘0’

.MORE MOVEM &REG, &X+&M

&M SET &M+1

AIF (&M NE N) .MORE

MEND

CLEARMEM 1CLEARMEM 25 10 5

EVTAB 0

Page 78: Chapter 5 Macro and Macro Processors

RECALL

Page 79: Chapter 5 Macro and Macro Processors

Tables of Macro Preprocessor

1. MACRO NAME TABLE

FIELDS:- a) Macro name

b) No. of Positional Parameters (#PP)

c) No. of Keyword Parameters (#KP)

d) No. of Expansion Time Variables (#EV)

e) MDT Pointer (MDTP)

f) KPDTAB Pointer (KPDTP)

g) SSTAB Pointer (SSTP)

2. PARAMETER NAME TABLE

FIELDS:- a) Parameter Name

Page 80: Chapter 5 Macro and Macro Processors

Tables of Macro Preprocessor3. EV NAME TABLE (EVNTAB)

FIELDS:- a) EV name

4. SS NAME TABLE FIELDS :- a)

SS Name

5. Keyword Parameter Default Table (KPDTAB)

FIELDS:- a) Parameter Name

b) Default Value

6. Macro Definition Table (MDT)

FIELDS :- a) Label

b) Opcode

c) Operands

Page 81: Chapter 5 Macro and Macro Processors

Tables of Macro Preprocessor

7. Actual Parameter Table (APT)

FIELDS :- a) Value

7. EV Table (EVTAB)

FIELDS:- a) value

9. SS Table (SSTAB)

FIELDS :- a) MDT Entry #

Page 82: Chapter 5 Macro and Macro Processors

EXAMPLEMACRO

TEST &X, &N, R=AREG

LCL &M

SET 0&M

.PQ MOVEM &R, &X+&M

&M SET &M + 1

AIF (&M NE N) .PQ

MEND

MACRO CALL

TEST S,10

REPRESENTS PARAMETER NAME

REPRESENTS EXPAN. VAR. NAME

REPRESENTS SEQUENCING

SYMBOL NAME

REPRESENTS ACTUAL

PARAMETER NAME

Page 83: Chapter 5 Macro and Macro Processors

R

N

X

PNTAB

M

EVNTAB

R AREG

KPDTAB

PQ

SSNTAB

S

10

AREGAPTAB

0

EVTAB

27

SSTAB

Page 84: Chapter 5 Macro and Macro Processors

Name #PP #KP #EV MDTP KPDTP SSTP

TEST 2 1 1 25 10 5

MNT

25 LCL (E,1)

26 (E,1) SET 0

27 MOVEM (P,3),(P,1)+(E,1)

28 (E,1) SET (E,1) +1

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

30 MEND

MDT

Page 85: Chapter 5 Macro and Macro Processors

MACRO

CLEAR &X, &REG=AREG

LCL &M

&M SET 1

.MORE MOVEM &REG, =‘0’

AIF (&X NE &M) .MORE

MEND

MACRO

SUM &R,&A,&PI=3.14

AIF (&R GT 0) .END

MOVER AREG, &R

MOVEM AREG, &A

.END MEND

Example

Page 86: Chapter 5 Macro and Macro Processors

PNTAB

MNT

MACRO

CLEAR &X, &REG=AREG

LCL &M

&M SET 1

.MORE MOVEM &REG, =‘0’

AIF (&X NE &M) .MORE

MEND

MACRO

SUM &R,&A,&PI=3.14

AIF (&R GT 0) .END

MOVER AREG, &R

MOVEM AREG, &A

.END MEND

1 X

2 REG

KPDTAB

1 REG AREG

EVNTAB

1 M 1 MORE

SSNTAB

1 3

SSTAB

1 R

2 A

3 PI

1 0

EVTAB

#PP #KP #EV MDTP KPDTP SSTP

CLEAR

SUM

1 1 1 1 1 1

2 1 0 2 2

1 LCL (E,1)2 (E,1) SET 13 MOVEM (P,2),=‘0’4 AIF (P,1) NE (E,1) (S,1)5 MEND6 AIF (P,1) GT 0 7 MOVER AREG, (P,1)8 MOVER AREG, (P,2)9 MEND10

2 PI 3.14 2 END 2 9

MDT

6

(S,2)

Page 87: Chapter 5 Macro and Macro Processors

Macro expansion

We use the following data structure to perform macro expansion:

APTAB – Actual parameter table

EVTAB – EV table

MEC – Macro expansion counter

APTAB_ptr – APTAB pointer

EVTAB_ptr – EVTAB pointer

Number of entries in APTAB equals to the sum of values in the #PP and #KP fields of the MNT entry of macro.

Page 88: Chapter 5 Macro and Macro Processors

Design of macro assembler

Macro preprocessor followed by conventional

assembler is an expensive way of handling macro since

the number of passes over the source program is large

and many function get duplicated.

Example:

A source statement to detect macro calls require us to

process the mnemonic field. Similar function is required in

first pass of the assembler. Similar functions of the

preprocessor and assembler can be merged if macros are

handled by a macro assembler which perform macro

expansion and program assembly simultaneously.

Page 89: Chapter 5 Macro and Macro Processors

Cont.

Macro expansion perform in single pass is not true,

as certain kinds of forward references in macros

cannot be handled in a single pass.

This problem leads to the classical two pass

organization for macro expansion.

First pass collects information about the symbols

defined in a program.

second pass perform macro expansion.

Page 90: Chapter 5 Macro and Macro Processors

Cont.

Pass structure of a macro-assembler

First merge the function of macro preprocessor with the function of conventional assembler, then the functions can be structured into passes of the macro assembler.

Pass-I Marco definition processing

SYMTAB construction

Pass-II Macro expansion

Memory allocation and LC processing

Processing of literals

Intermediate code generation.

Pass-III Target code generation.

Page 91: Chapter 5 Macro and Macro Processors

Cont.

The pass structure can be simplified if attributes of actual parameter are not to be supported.

Pass-I

Macro definition processing

Macro expansion

Memory allocation, LC Processing and SYMTAB Construction

Processing of Literals

Intermediate code generation.

Pass-II

Target code generation.

Page 92: Chapter 5 Macro and Macro Processors

Examples Questions

Construct all data structure for

the MACRO MACRO

&X, &Y, &REG=BREG

.EXIT(&Y EQ 0)

&REG, &X

&REG, &Y

BECE6

AIF

MOVER

MUL

.EXIT MEND

Generate the statement for these macro calls.

BECE6

BECE6

6, 8, REG=AREG

3,0

Page 93: Chapter 5 Macro and Macro Processors

PNTAB EVNTAB

SSNTAB

X

Y

REG

-

EXIT

MNT BECE6 2 1 0 35 20 6

#PP #KP #EV MDTP KPDTP SSTP

KPDTAB 20 REG BREG SSTAB 6 35

MDT AIF (P,2) EQ 0 .(S,1)

MOVER (P,3), (P,1)

MUL (P,3), (P,2)

MEND

35

36

37

38 (S,1)

39

40

41

Page 94: Chapter 5 Macro and Macro Processors

Generate the statement for these macro calls.

BECE6

BECE6

6, 8, REG=AREG

3,0

For first one BECE6

+ MOVER

+ MUL

6, 8, REG=AREG

AREG, 6

AREG, 8

Page 95: Chapter 5 Macro and Macro Processors

Linkers and Loaders

Page 96: Chapter 5 Macro and Macro Processors

Translation Hierarchy

Compiler:

Translates high-level language program into assembly language.

Assembler

Converts assembly language programs into object files.

Object files contain a combination of machine instructions, data,

and information needed to place instructions properly in memory.

Page 97: Chapter 5 Macro and Macro Processors
Page 98: Chapter 5 Macro and Macro Processors

Introduction

Translation:- A Program is translated into target program.

Linking:- The code of target program is combined with

codes of those programs and library routines that it calls.

Relocation:- A Program may have been coded or translated

with the idea of executing it in a specific area of memory.

However the OS may have used that memory area for

another purpose, so, it may allocated a different memory

area for program’s execution. Relocation is action of

changing memory address used in the code of program so

that it can execute correctly in allocated area.

Loading:- The Program is loaded in specific memory area

for execution

Page 99: Chapter 5 Macro and Macro Processors

Flow of Execution

Translator

Linker

Loader

Program

execution

.Obj

.exe

SOURCE

PROGRAM

Page 100: Chapter 5 Macro and Macro Processors

Process for producingan executable file

Page 101: Chapter 5 Macro and Macro Processors

Linker

Tool that merges the object files produced by separate

compilation or assembly and creates an executable file.

• Three tasks:-

❑Searches the program to find library routines used by program,

e.g. printf(),sqrt(),strcat() and various other.

❑Determines the memory locations that code from each module

will occupy and relocates its instructions by adjusting absolute

references.

Relocation, which modifies the object program so that it can be loaded at anaddress different from the location originally specified.

❑It combines two or more separate object programs and supplies theinformation needed to allow references between them .

Page 102: Chapter 5 Macro and Macro Processors

Translated, Linked and load time address

Translation Time Address(Translated Origin):- Address Assigned by Translator

Using START and ORIGIN (Translated ORIGIN)

Execution Start Address is translated start address.

Linked Address (Linked Origin):- Address Assigned by Linker while producing binary program

Same set of translated address may have been used in many objectmodules that are combined to form binary program (object modules oflibrary routines have same translated origin)

Load time address (Load Origin):- Address Assigned by loader while loading program in memory for

execution

OS may require that program should execute fro specific area ofmemory, which may require change in its origin.

Page 103: Chapter 5 Macro and Macro Processors

ORIGIN 500

READ A

LOOP

.

.

.

BC LT LOOP

STOP

A DS 1

B DS 1

END

Address Code

500) + 09 0 540

501) + .…………

.

.

.

538) + 06 1 501

539) + 00 0 000

540)

541)

Page 104: Chapter 5 Macro and Macro Processors

Relocation and Linking Concepts

Let AA be the set of absolute addresses used in the program (for

data or instruction).

Address Specific Program (P)

Address Sensitive instruction:- uses address ai included in set AA

Address Constant:- data word that contain address ai included in

set AA.

If P is loaded into memory area whose start address matches

with translated origin (or translated address), then every

operand of instruction use the same address.

However, if P is loaded in some other memory area, therefore P

would not be execute correctly.

So, address in each address sensitive instruction of P should be

“corrected”. And it achieved though program relocation.

Page 105: Chapter 5 Macro and Macro Processors

Cont…

Program relocation:-

Action of modifying the address used in the address sensitiveinstructions of a program such that the program can executecorrectly from designated area of memory.

If linked origin ≠ translated origin, relocation must perform by

linker.

If load origin ≠ linked origin, relocation must perform by loader.

In general, linker always perform relocation, whereas some

loaders do not.

Load origin = linked origin, such loaders called absolute loaders.

Loaders that perform relocation called relocating loaders.

Page 106: Chapter 5 Macro and Macro Processors

Correcting address used in address sensitive instruction:

Translated origin of program is 500.

Translation time address of A is 540.

The instruction READ A used address 540 , so it is a address sensitive

instruction.

IF linked origin is 900, then A used address 940.

ORIGIN 500

READ A

LOOP

.

.

.

BC LT LOOP

STOP

A DS 1

B DS 1

END

Address Code

500) + 09 0 540

501) + .…………

.

.

.

538) + 06 1 501

539) + 00 0 000

540)

541)

Page 107: Chapter 5 Macro and Macro Processors

Performing relocation

Let translated origin and linked origin of program p is

t_origin_p and l_origin_p respectively.

Let its translation time address be t_symb and linked address

l_symb.

The relocation factor of P is defined as follows:

relocation_factor_p=l_origin_p –t_origin_p; ……………..(1)

Consider statement which uses symbol as operand.T_symb=t_origin_p+ d_symb;

Where d_symb is offset of symb in p. After Program P has been relocated to the linked origin i.e. l_origin_p,

L_symb=l_origin_p +d_symb;

Using (1), l_symb=t_origin_p + relocation_factor_p +d_symb;

=t_origin_p +d_symb + relocation_factor_p;

=t_symb +relocation Factor_p……….(2)

Page 108: Chapter 5 Macro and Macro Processors

Performing relocation (Cont….)

Let IRR_p be the set of instructions in program p that require

relocation.

By Following Eq.(2), relocation of program can be performed by computing the

relocation factor for p and adding it to the translation time address used in every

instruction i included in IRR_p.

ORIGIN 500

READ A

LOOP

.

.

.

BC LT LOOP

STOP

A DS 1

B DS 1

END

Address Code

500) + 09 0 540

501) + .…………

.

.

.

538) + 06 1 501

539) + 00 0 000

540 )

541)

Relocation factor =900-500

=400

Find IRRp…..500 and 538

Instruction at 500, used address

540,relocate

=400+540

=940

Instruction at 538, used address

501,relocate

=400+501

=901

Page 109: Chapter 5 Macro and Macro Processors

Linking

A Program unit is any program or routine that is to be linked

with another program or routine.

Let an application consists a set of program units SP={Pi}

Consider Program unit Pi that requires another Program unit Pj

during its execution.(Either uses address some of the instructions or

address of some of the data).

To form binary program by combining Pi and Pj ,linked addresses

of relevant instructions or data located in Pj have to be supplied to

Pi ’s instruction.

It achieved by

1) Public definition:-A Symbol defined in program unit that may be

referenced in other program units.

2) External Reference:-A reference to a symbol that is not defined

in the program unit containing the reference.

Page 110: Chapter 5 Macro and Macro Processors

ORIGIN 500

ENTRY TOTAL

EXTERN MAX, ALPHA

READ A

LOOP

.

MOVER AREG, ALPHA

BC ANY, MAX

.

BC LT LOOP

STOP

A DS 1

TOTAl DS 1

END

Page 111: Chapter 5 Macro and Macro Processors

❑ EXTERN and ENTRY Statements:-❑ Entry Statements is a program unit lists the public definitions of the program unit.

❑ Extern Statements lists symbols to which external references are made in theprogram unit.

❑ Resolving external references:❑ If encounter Referenced symbol, Puts zeroes in the address field.

❑ Before program can be executed, the correct linked address should kept.

❑Linking:-❑Linking is the action of putting the correct linked address in those instructions of a program that contain external references.

❑External reference in instruction is said to be resolved when the correct

linked address has been put in instruction.

Page 112: Chapter 5 Macro and Macro Processors

Program Unit P

ORIGIN 500

ENTRY TOTAL

EXTERN MAX, ALPHA

READ A 500)

LOOP 501)

.

MOVER AREG, ALPHA 518)

BC ANY, MAX 519)

.

BC LT LOOP 538)

STOP 539)

A DS 1 540)

TOTAL DS 1 541)

END

START 200

ENTRY ALPHA

..

ALPHA DS 1 231)

END

Program Unit Q

Resolving External References:

Let linked origin pg P be 900 and its

size is 42.

Hence linked origin of Q is 942.

And Linked address of ALPHA

become 973.

Page 113: Chapter 5 Macro and Macro Processors

Constructing binary program

A Binary Program is machine language program comprising a set

of program, units SP such that for every Pi in SP

1. Pi has been relocated to the memory area whose staring address

matches its linked origin, and

2. Each external reference in Pi has been resolved.

To form a binary program from set of object modules, the

programmer invokes the linker by using command

linker <link_origin>, <object modules names> [,execution start

address]

Where <link_origin> specifies the memory address to be given

to first word of binary program.

Execution start address is pair(Program unit name and offset of

program unit). The linker converts in into linker start address and

keep it with binary program for use in execution.

Page 114: Chapter 5 Macro and Macro Processors

Object Module

The Object Module of program unit contains all the information

that would be needed to relocate and link the program unit with

other program units.

Header:- Translated origin, size and executable start address of

Program unit.

Program:- Machine language program corresponding to P.

Relocation Table (RELOCTAB):- This table describes instructions

that require relocation, i.i. IRRp. Contain single field: Translated address : Translated address of address sensitive instruction.

Linking Table (LINKTAB): Contain information concerning public

definitions and external references. Contains three fields:

Symbol:-Symbolic name

Type: Values PD and EXT

Translated Address

Page 115: Chapter 5 Macro and Macro Processors

Program Unit P

ORIGIN 500

ENTRY TOTAL

EXTERN MAX, ALPHA

READ A 500)

LOOP 501)

.

MOVER AREG, ALPHA 518)

BC ANY, MAX 519)

.

BC LT LOOP 538)

STOP 539)

A DS 1 540)

B DS 1 541)

END

Header:- Translated origin=500, size=42,

execution start address=500

Program:-Machine Language program.

The Relocation Table

The Linking Table:

Object Module

Translated Address

500

538

Symbol Type TranslatedAddress

ALPHA EXT 518

MAX EXT 519

TOTAL PD 541

Page 116: Chapter 5 Macro and Macro Processors

Self relocating Programs:

3-way classification of relocation

A non-relocating Program:-

A Relocated Program

A self Relocating Program

Can be loaded in any area of memory for execution. At the start

of execution perform its own relocation.

RELOCATAB

Code to perform relocation of address sensitive instruction called

relocating logic.

Page 117: Chapter 5 Macro and Macro Processors

Recall

Program relocation:-

Action of modifying the address used in the address sensitiveinstructions of a program such that the program can executecorrectly from designated area of memory.

If linked origin ≠ translated origin, relocation must perform by

linker.

If load origin ≠ linked origin, relocation must perform by loader.

In general, linker always perform relocation, whereas some

loaders do not.

Load origin = linked origin, such loaders called absolute loaders.

Loaders that perform relocation called relocating loaders.

Page 118: Chapter 5 Macro and Macro Processors

Recall

Page 119: Chapter 5 Macro and Macro Processors

30

0

30

1

. . .

50

0

50

1

. . .

ORIGIN 500

READ A

LOOP

.

.

.

BC LT LOOP

STOP

A DS 1

B DS 1

END

Memory

Translation Time Address/ Translation Origin

Page 120: Chapter 5 Macro and Macro Processors

ORIGIN 500

READ A

LOOP

.

.

.

BC LT LOOP

STOP

A DS 1

B DS 1

END

Address Code

500) + 09 0 540

501) + .…………

.

.

.

538) + 06 1 501

539) + 00 0 000

540)

541)

Page 121: Chapter 5 Macro and Macro Processors

30

0

30

1

. . .

50

0

50

1

. . .

ORIGIN 500

READ A

LOOP

.

.

.

BC LT LOOP

STOP

A DS 1

B DS 1

END

Memory

Translation Time Address/ Translated Origin

Page 122: Chapter 5 Macro and Macro Processors

30

0

30

1

. . .

50

0

50

1

. . .

ORIGIN 500

READ A

LOOP

.

.

.

BC LT LOOP

STOP

A DS 1

B DS 1

END

Memory

Translation Time Address/ Translated Origin

Page 123: Chapter 5 Macro and Macro Processors

30

0

30

1

. . .

50

0

50

1

. . .

90

0

ORIGIN 500

READ A

LOOP

.

.

.

BC LT LOOP

STOP

A DS 1

B DS 1

END

Memory

Translation Time Address/ Translated OriginLinking Time Address/ Linked Origin Loading Time Address/ Load Origin

900

Page 124: Chapter 5 Macro and Macro Processors

ORIGIN 500

READ A

LOOP

.

.

.

BC LT LOOP

STOP

A DS 1

B DS 1

END

Address Code

900) + 09 0 940

901) + .…………

.

.

.

938) + 06 1 901

939) + 00 0 000

940)

941)

Page 125: Chapter 5 Macro and Macro Processors

What is Linker ?

➢ Combines multiple relocatable object files

➢ Produces fully linked executable – directly loadable in

memory

➢ How?

✓ Symbol resolution – associating one symbol definition with

each symbol reference

✓ Relocation – relocating different sections of input

relocatable files

Page 126: Chapter 5 Macro and Macro Processors

Linker The Basics..

➢ Compiler in Action…

✓ gcc foo.c bar.c –o a.out

run assembler (as)

foo.c bar.c

foo.s bar.s

foo.o bar.o

a.out

Run compiler

linker

a.out = fully linked executable

Page 127: Chapter 5 Macro and Macro Processors

It achieved by

1) Public definition:-

2) External Reference

❑ EXTERN and ENTRY Statements:-

❑ Entry Statements is a program unit lists the public

definitions of the program unit.

❑ Extern Statements lists symbols to which external

references are made in the program unit.

❑ Resolving external references:❑ If encounter Referenced symbol, Puts zeroes in the address field.

❑ Before program can be executed, the correct linked address should kept.

❑ Linking:-

❑ Linking is the action of putting the correct linked address in

those instructions of a program that contain external

references.

❑ External reference in instruction is said to be resolved when

the correct linked address has been put in instruction.

Page 128: Chapter 5 Macro and Macro Processors

Program Unit P

ORIGIN 500

ENTRY TOTAL

EXTERN MAX, ALPHA

READ A

LOOP

.

MOVER AREG, ALPHA

BC ANY, MAX

.

BC LT LOOP

STOP

A DS 1

TOTAL DS 1 541)

END

START 200

ENTRY ALPHA

..

ALPHA DS 1 231)

END

Program Unit Q

Resolving External References:

Let linked origin P be 900 and its size

is 42.

Hence linked origin of Q is 942.

And Linked address of ALPHA

become 973.

Page 129: Chapter 5 Macro and Macro Processors

Constructing binary program

A Binary Program is machine language program

comprising a set of program

To form a binary program from set of object modules, the

programmer invokes the linker by using command

linker <link_origin>, <object modules names> [,execution start

address]

Where <link_origin> specifies the memory address to be

given to first word of binary program.

Execution start address is pair(Program unit name and

offset of program unit). The linker converts in into linker

start address and keep it with binary program for use in

execution.

Page 130: Chapter 5 Macro and Macro Processors

➢ Collection of concatenated object files – stored on disk in a particular format – archive

➢ An input to Linker

✓ Referenced object files copied to executable

libm.a

printf.o & fopen.o

linker(ld)

foo.obar.o

libc.a

a.out

fully linked executable object file

Page 131: Chapter 5 Macro and Macro Processors

Object Module The Object Module of program unit contains all the

information that would be needed to relocate and link the

program unit with other program units.

Header:- Translated origin, size and executable start

address of Program unit.

Program:- Machine language program corresponding to P.

Relocation Table (RELOCTAB):- This table describes

instructions that require relocation, i.i. IRRp. Contain single

field:

Translated address : Translated address of address sensitive

instruction.

Linking Table (LINKTAB): Contain information concerning

public definitions and external references. Contains three

fields:

Symbol:-Symbolic name

Type: Values PD and EXT

Translated Address

Page 132: Chapter 5 Macro and Macro Processors

Program Unit P

ORIGIN 500

ENTRY TOTAL

EXTERN MAX, ALPHA

READ A 500)

LOOP 501)

.

MOVER AREG, ALPHA 518)

BC ANY, MAX 519)

.

BC LT LOOP 538)

STOP 539)

A DS 1 540)

Total DS 1 541)

END

Header:- Translated origin=500, size=42,

execution start address=500

Program:-Machine Language program.

The Relocation Table

The Linking Table:

Object Module

Translated

Address

500

538

Symbol Type Translated

Address

ALPHA EXT 518

MAX EXT 519

TOTAL PD 541

Page 133: Chapter 5 Macro and Macro Processors

Self relocating Programs:

3-way classification of relocation

A non-relocating Program:-

A Relocated Program

A self Relocating Program

Can be loaded in any area of memory for execution. At the start

of execution perform its own relocation.

RELOCATAB

Code to perform relocation of address sensitive instruction called

relocating logic.

Page 134: Chapter 5 Macro and Macro Processors

Overlay Structured Programs

➢ Keep in memory only those instructions and data that

are needed at any given time.

➢ Needed when process is larger than amount of memory

allocated to it.

➢ An Overlay is part of program that has same load origin

as some other parts of program..

➢ A Program containing overlays called an overlay

structured program.➢ 1) A permanently resident part called root.

➢ 2) A set of overlays that would be loaded in memory when needed.

Page 135: Chapter 5 Macro and Macro Processors

init

read

Functions_a

Functions_b

Functions_c

print

0

10K

15K

35K

50K

60K

65K

init

read

0

10K

15K

20K

print

Functions_a

40K Functions_b

35K Functions_c

30K

Page 136: Chapter 5 Macro and Macro Processors

100k

A(20k)

B(20k)

C(30k)

D(10k)

E(20k)

(A) Subroutine calls between procedures

Page 137: Chapter 5 Macro and Macro Processors

(B) Overlay structure:-

B(20k) D(10k)

C(30k) E(20k)

70k

A(20k)

Page 138: Chapter 5 Macro and Macro Processors

(C) Possible storage assignment of each procedure

A

BD

EC

0

40k

60k

80k

20k

Page 139: Chapter 5 Macro and Macro Processors
Page 140: Chapter 5 Macro and Macro Processors

Loaders

Loader is a program which accepts object program/binary

program, prepares these program for execution

Page 141: Chapter 5 Macro and Macro Processors

Execution of a program includes following steps:-

Translation of a program

Linking of a program

Relocation of a program

Loading of a program

Page 142: Chapter 5 Macro and Macro Processors

LoaderLinkerTranslator

Binary program

Objectmodules

Binary program

Data

Result

Memory

Execution of a program includes following steps:-

Page 143: Chapter 5 Macro and Macro Processors

Loader must perform four functions:-

•Allocation:- Allocate space in memory for programs.

•Linking:- Resolve symbolic references between object programs.

•Relocation:- Adjust all address dependent locations to

correspond to the allocated space.

•Loading:- Physically places machine instruction in memory.

Page 144: Chapter 5 Macro and Macro Processors

RECALL

Page 145: Chapter 5 Macro and Macro Processors

Computer System

Page 146: Chapter 5 Macro and Macro Processors

Introduction

Loading◼ Brings the object program into memory for execution

Relocation◼ Modify the object program so that it can be loaded at an

address different from the location originally specified

Linking◼ Combine two or more separate object programs and

supplies the information needed to allow references between them

Absolute loader

Loader

Linking loader

Linker

Page 147: Chapter 5 Macro and Macro Processors

Basic Loader Functions

The most fundamental functions of a loader:

◼ Bringing an object program into memory and starting itsexecution

an Assemble-and-Go Loader

an Absolute Loader

A Simple Bootstrap Loader

A Relocating/Relative Loader

Direct Linking Loader

Page 148: Chapter 5 Macro and Macro Processors

Assemble-and-Go Loader

Characteristic

◼ The object code is produced directly in memory for immediate execution after assembly

Advantage◼ Useful for program development and testing

Disadvantage

◼ Whenever the assembly program is to be executed, it has to be assembled again

◼ Wastage of Memory

Page 149: Chapter 5 Macro and Macro Processors

C&go translator Program loaded In memory

assembler

Source program

COMPILE AND GO LOADER SCHEME.

Page 150: Chapter 5 Macro and Macro Processors

an Absolute Loader

In almost the same way as in compile and go scheme except that data is punched on cards

Absolute loader only performs loading function◼ Does not need to perform linking and program relocation.

◼ All functions are accomplished in a single pass.

Absolute Program ◼ Advantage

Simple and efficient

◼ Disadvantages The need for programmer to specify the actual address at which it will be loaded into

memory

Difficult to use subroutine libraries efficiently

Header records:-load origin, length, starting address

Sequence of binary image records: containing program code

Page 151: Chapter 5 Macro and Macro Processors

an Absolute Loader (Cont.)

In a single pass

◼ Check the Header record for program name, starting address, and length

◼ Bring the object program contained in the Text record to the indicated address

◼ No need to perform program linking and relocation

◼ Start the execution by jumping to the address specified in the End record

Page 152: Chapter 5 Macro and Macro Processors

MAIN

SQRT

Absolute

loader

MAIN

SQRT

100

237

400

475

Page 153: Chapter 5 Macro and Macro Processors

A Simple Bootstrap Loader

Bootstrap Loader

◼ When a computer is first turned on or restarted, a special type

of absolute loader, called a bootstrap loader is executed

In PC, BIOS acts as a bootstrap loader

◼ This bootstrap loads the first program to be run by the

computer -- usually an operating system

Page 154: Chapter 5 Macro and Macro Processors

Machine-Dependent Loader Features

Drawback of absolute loaders

◼ Programmer needs to specify the actual address at which it will be loaded into memory.

◼ Difficult to run several programs concurrently, sharing memory between them.

◼ Difficult to use subroutine libraries.

Solution: a more complex loader that provides

◼ Program relocation

◼ Program linking

Page 155: Chapter 5 Macro and Macro Processors

Relocation/Relocating Loaders

Loaders that allow for program relocation are called relocating

loaders or relative loaders.

BINARY SYMBOLIC SUBROUTINE(BSS)loader such as used in the

IBM 7094,IBM 1130,GE 635.

The BSS loader allows many procedure segments but only one data

(common)segment.

Two methods for specifying relocation as part of the objectprogram◼ Modification records

Suitable for a small number of relocations required

◼ When relative or immediate addressing modes are extensively used

◼ Relocation bits

Suitable for a large number of relocations required

◼ When only direct addressing mode can be used in a machine with fixed instruction format(e.g., the standard SIC machine)

Page 156: Chapter 5 Macro and Macro Processors

Relocation by Modification Record

A Modification record is used to describe each part of the object code that must be changed when the program is relocated.

Page 157: Chapter 5 Macro and Macro Processors

Relocation by Relocation Bit

If a machine primarily uses direct addressing and has a fixed instruction format◼ There are many addresses needed to be modified

◼ It is often more efficient to specify relocation using relocation bit

Relocation bit

◼ Each instruction is associated with one relocation bit

Indicate the corresponding word should be modified or not.

◼ These relocation bits in a Text record is gathered into bit masks

Relocation bit

◼ 0: no modification is needed

◼ 1: modification is needed

Page 158: Chapter 5 Macro and Macro Processors

MAIN

SQRT

Absolute

loader

MAIN

SQRT

100

237

400

Page 159: Chapter 5 Macro and Macro Processors

DIRECT-LINKING LOADERS

A direct-linking loader is a relocatable loader.

It has advantage of allowing programmer multiple procedure

segments and multiple data segments.

Complete freedom in referencing data or instructions contained in

other segments, provides flexible intersegment referencing.

Page 160: Chapter 5 Macro and Macro Processors

oThe assembler should give the following information to the

loader:

1. The length of the object code segment.

2. A list of external symbols (could be used by other

segments).

3. List of External symbols(The segment is using).

4. Information about address constants.

5. Machine code translation of the source program.

Page 161: Chapter 5 Macro and Macro Processors

oThe list of symbols not defined in the current segment but

used in the current segment are stored in a data structure

called USE table.

oThe lists of symbols defined in the current segment and

referred by the other segments are stored in a data structure

called DEFINITION table.

oTo place the object code in the memory there are two

situations:

1. Address of the object code could be absolute.

2. The address of object code can be relative.

Page 162: Chapter 5 Macro and Macro Processors

o The assembler generates following types of cards:

o ESD

o TXT

o RLD

o END

1. ESD -

External symbol dictionary contains information about all

symbols that are defined in the program but referenced

somewhere. It contains

· Reference no

· Symbol Name

· TYPE

· Relative Location

· Length

Page 163: Chapter 5 Macro and Macro Processors

o TYPE:

SD - Segment Definition.

LD - Local Definition.

ER - External Reference.

2. TXT –

Text card contains actual object code.(translated

source code).

3. RLD –

Relocation and linkage directory contains information

about locations in the program whose content depend

on the address at which program is placed.

Page 164: Chapter 5 Macro and Macro Processors

o The RLD cards contains information:

· Location of the constant that need relocation

· By what it has to be changed

· The operation to be performed

oThe Format of RLD

· Reference No

· Symbol

· Flag

· Length

· Relative Location

4. END –

Indicates the end of the program and specifies starting

address for execution

Page 165: Chapter 5 Macro and Macro Processors

DISADVANTAGE :

oAllocate, relocate, link, and load all the subroutines

each time in order to execute a program.

oFurthermore, even though loader program smaller

than assembler, it absorb considerable amount of

memory.