0% found this document useful (0 votes)
6 views129 pages

01SB072010004900011

The document explains the concept of macros in programming, detailing how they allow for the definition of reusable code sequences with optional parameters. It covers macro definitions, calling conventions, types of parameters, nested macros, and the workings of a macro processor, including its two-pass system. Additionally, it discusses loader functions and types, including compile-and-go and general loaders, highlighting their advantages and disadvantages.

Uploaded by

Umera Rawoot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views129 pages

01SB072010004900011

The document explains the concept of macros in programming, detailing how they allow for the definition of reusable code sequences with optional parameters. It covers macro definitions, calling conventions, types of parameters, nested macros, and the workings of a macro processor, including its two-pass system. Additionally, it discusses loader functions and types, including compile-and-go and general loaders, highlighting their advantages and disadvantages.

Uploaded by

Umera Rawoot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 129

Unit 2

Macro
• Macro: Macro allows a sequence of a source language code to be
defined once and then referred many times whenever it gets
called.
• Many languages provide built-in facilities for writing macros.
E.g. Ada, C and C++
• Syntax for Macro :
• MACRO
Name of macro [Set of parameters]
Body of macro
MEND

[Parameters in macro are optional ]


• A MACRO allows to attach name to this repeated sequence
as follows .
• A macro consist of
– i) Name of the macro
– ii) Set of parameters
– iii) Body of macro
– iv) MEND

• Parameters for an macro is optional.


• MACRO
macro name [parameters]
//macro body
MEND
MACRO DEFINITION ITHOUT
PARAMETER
• MACRO MYMACRO
• ADD AREG,X // MACRO BODY
• ADD BREG,X
• MEND
MACRO DEFINITION VITH PARAMETER
• MACRO MYMACRO &A……Formal Parameter
• ADD AREG,&A // MACRO BODY
• ADD BREG,&A
• MEND
• Macro definition: Macro definition is set of instruction
enclosed between a macro header and macro end statement. E.g
– MACRO
– CAL1 &A …………………Formal parmeter
– ADD AREG,&A
– SUB BREG &A
– MEND
• Here Macro CAL1 is defined
• Macro Calling: A macro is called by writing the macro name in
the mnemonic field of an assembly statement.
• E.g. CAL1 B………..actual parameter
Macro definition and Call
• Example of Macro Definition
– MACRO
– INCR &MEM_VAL, &INCR_VAL, &REG
– MOVER &REG, &MEM_VAL
– ADD &REG, &INCR_VAL
– MOVEM &REG, &MEM_VAL
– MEND
Example of Macro Call
• INCR A, B, AREG
• Macro Expansion: Macro calls leads to macro expansion.
• During macro expansion, the macro call is replaced by a
sequence of assembly statements in macro definition.
Macro Expansion/Types of macro
parameter
• The rule for determining the value of formal parameter
depend on kind of parameter.
1. Positional Parameter
2. Keyword Parameter
3. Default Specification of Parameter
4. Macros with Mixed Parameter
5. Other uses of Parameter
Positional Parameter
• Positional Parameter
– A positional formal parameter is written as
&<parameter name>.
– The <actual parameter specification> in a macro call is
simply an <ordinary string>.
– The value of a positional formal parameter XYZ is
determined by the rule of positional association as :
1. Find the ordinal position of XYZ in the list of formal
parameters.
2. Find the actual parameter specification occupying the same
ordinal position in the list of actual parameters in macro
call statement.
Keyword Parameter:
• Keyword Parameter:
• For keyword parameter, formal parameter is written as
&<parameter name>=.
• The <actual parameter specification> in a macro call is written
as <formal parameter name> = <ordinary string>.
• The value of a positional formal parameter XYZ is
determined by the rule of positional association as :
• Find the actual parameter specification which has the form
XYZ = <ordinary string>.
• Let <ordinary string> in the specification be the string ABC.
• Then the value of formal parameter XYZ = ABC.
• Default Specification of Parameter
– A default is a standard specification in the absence of an
explicit specification by the programmer.
– The syntax of formal parameter specification is
&<parameter name>[<parameter kind>[<default value>]]
• Macros with mixed parameter list
– A macro may be defined to use both positional and keyword
parameter.
• In such a case, all positional parameter must precede all
keywords parameter.
• Other uses of parameters:
• Formal parameter can also appear in the label and opcode
fields of model statement.
Nested Macro Calls
• A model statement in a macro may constitute a call on
another macro. Such calls are known as nested macro calls.
• Macro containing the nested call is referred to as the outer
macro and the called macro as the inner macro.
• Expansion of nested macro calls follows the last-in-first-
out (LIFO) rule
Nested Macro Definition
• A macro can be called inside of the body of a macro. i.e.
macro call within macro definition
• This concept can be used for defining a group of similar
macros.
• Inner macro comes into existence after a call to outer
macro.
• Inner macro can be called after it has come into existence.
• E.g.
• MACRO
• ADD1 &ARG
• LOAD AREG,ARG
• ADD AREG, =F ’1’
• STORE AREG,&AREG
• MEND
• MACRO
• ADDS &ARG1,&ARG2,&ARG3
• ADD1,&ARG1
• ADD1,&ARG2
• ADD1,&ARG3
• MEND
• START
• ADDS X,Y,Z
• X DC 2
• Y DC 2
• Z DS 2
• END
• Expanded Macro
• MACRO
• ADD1 &ARG
• LOAD AREG,ARG
• ADD AREG, =F ’1’
• STORE AREG,&AREG
• MEND
• MACRO
• ADDS &ARG1,&ARG2,&ARG3
• ADD1,&ARG1
• ADD1,&ARG2
• ADD1,&ARG3
• MEND
• START
• ADDS X,Y,Z----------------------------
• X DC 2
• Y DC 2
• Z DS 2
• END
Advance Macro Facilities.
• Advanced macro facilities permit conditional reordering of
the sequence of macro expansion.
• Flow of control during macro expansion can be altered
using:
• 1. conditional branch pseudo opcode- AIF

• 2. Unconditional branch pseudo opcode –AGO.


• 3.Expansion time variable-EV’s
• A sequencing symbol (SS) has the syntax
.<ordinary string>
e.g .ONCE
• SS is the label field of a statement in macro body.
• It is used as an operand in AIF or AGO and it is destination
of an expansion time control transfer.
Advance Macro Facilities
Expansion time variables (EV’s):
– EV’s are variables which can only be used during the
expansion of macro calls.
– A local EV is created for use only during a particular macro call.
– A global EV exists across all macro calls situated in a program
and can be used in macro.
LCL <& vname>
GBL <&vname>
o Value is set for EV’s
o set <vname> <exp>
Working of Macro Processor
• Macro processor takes a source program containing macro
definitions and macro calls and translates into an assembly
language program without any macro definitions or calls.
• This program can now be handed over to assembler to
obtain the target language.
• Macro processor has to perform the four basic tasks:
1. Recognize macro definition: - A macro definition is
identified by MACRO and MEND pseudo opcodes.
2. Save the macro definition: - A macro definition is saved
as it is required during macro expansion.
3. Recognize macro call: - Macro call is appears as a
operation mnemonic.
4. Expand macro calls and substitute arguments: -During
macro expansion the macro processor must substitute formal
parameters with actual parameters.
• Macro processor has two passes: • Pass 1 • Pass 2
Pass1 Macro Processor
• It has following data structures:
1. MNT- Macro Name Table :stores name of macro
2. MDT- Macro Definition Table: stores body of macro def.
3. ALA- Argument list array: substitute index for formal
parameters before storing macro definition.
4. MNTC(MNT Counter)-used to indicate next available entry
in MNT.
5. MDTC(MDT Counter)-used to indicate next available entry
in MDT.
Pass 2 Macro Processor
• Input: copy of input from pass-I(intermediate File)
• Output: expanded source(target file) given to assembler)
• Function of Pass 2 Macro Processor
a) Locate the macro name in MNT.
b)Establish correspondence between formal parameters
and actual parameters.
c)Obtain information from MNT regarding position of the
maro definition in MDT.
d) Expand the macro call by picking up model statements
from MDT.
Databases for Macro Pass-II
1. MDT created by Pass-I
2. MNT created by Pass-I
3. MNTP-Macro name table pointer: points next entry in
MNT
4. MDTP-Macro Defintion table pointer :used to indicate next
line of text used during macro expansion.
5. ALA:Argument list array: substitute index for actual
parameters before storing macro definition.
Structure of data structure

Index Macro MDT Index Index card


Name

MDT
MNT

iNDEX Arguments

ALA
Show the contents of MDT, MNT and
ALA & output of Pass-II
ALA AFTER PASS-I
INTERMEDIATE CODE AFTER PASS-I
ALA (PASS-II)
Expanded form after pass-II
Output of pass-II macro assembler
Generate MNT,MDT,ALA ,IC after pass-I
and Expanded code after Pass-II
• MACRO
• &LAB INCR &ARG1,&ARG2.&ARG3
• &LAB ADD AREG,&ARG1
• ADD AREG,&ARG1
• ADD AREG,&ARG1
• MEND
• START
• LOOP INCR A,B,C
• LABEL INCR DATA1,DATA2,DATA3
• A DC 2
• B DC 2
• C DS 2
• DATA1 DC 3
• DATA2 DS 2
• DATA3 DC 4
• END
• MACRO
• INCR &M,&I=,&REG=AREG
• MOVER &REG,&M
• ADD &REG,&I
• MOVEM &REG,&M
• MEND
• MACRO
• CALC &X,&Y=B,&OP=MULT
• MOVER AREG,&X
• &OP AREG,&Y
• MOVEM AREG,&M
• MEND
• START 100
• READ N1
• CALC A,OP=ADD
• ADD AREG,21
• LDA,CREG,100
• SUB CREG,A
• INCR B,REG=BREG,I=A
• A DS1
• B DS 1
• END
Handling of nested macro calls
Methods
1. Several level of expansion
2. Recursive expansion
3. Use of stack during expansion
Several level of expansion: -
• The expanded code itself contains macro calls.
• The macro expansion algorithm can be applied to the first
level expanded code to expand these macro calls and so on
until we obtain a code from which does not contain any
macro calls.
Recursive Expansion:
• -To handle nested macro calls the macro expansion
function should be able to work recursively.
• - A recursion is handled through a stack where local
variables are stored onto the stack before making a
recursive call.
Use of stack during expansion:
• -Nested macro calls can be handled with the help of explicit
stack.
• - Macro calls are handled in LIFO manner.
• - Stack can be used to accommodate the expansion time
data structure.
• - Expansion time data structure include:
– 1. MEC- Macro expansion counter
– 2. Actual parameter table.
Expanded code
• MACRO
• XYZ &A,&B,&C
• READ &A
• ADD &B,=5
• PRINT &C
• MEND
• MACRO
• MIT &Z
• MACRO
• &Z &V
• SUB &V,=6
• MOVER AREG,B
• ADD &V,=5
• MEND
• ADD CREG,=5
• MEND
• START 200
• MIT HELLO
• MULT BREG,=4
• HELLO BREG
• PRINT C
• C DS 1
• XYZ L,M,N
• END
• Loader
Introduction
• Loader takes object code as input and prepares it for
execution and initiate execution.
• Object code is output of assemblers
• A loader is a system software program that performs the
loading function.
• Loading is the process of placing the program into
memory for execution.
• Loader is responsible for initiating the execution of the
process.
Functions of Loader
• Allocation : the space for program is allocated in the main
memory, by calculating the size of the program.
• Linking - which combines two or more separate object
programs and supplies the necessary information.
• Relocation – modifies the object program so that it can be
loaded at an address different from the location originally
specified.
• Loading – brings the object program into memory for
execution
Types of Loader Schemes
1. Compile and go loader
2. General Loader Scheme
3. Absolute Loader
4. Subroutine Linkage
5. Relocating loaders
6. Direct- Linking loader
COMPILE-AND-GO LOADER
• In compile and go loader is a link editor/program loader
in which the assembler itself places the assembled
instruction directly into the designated memory locations
for execution.
• The instruction are read line by line, its machine code is
obtained and it is directly put in the main memory at some
known address.
• After completion of assembly process, it assigns the
starting address of the program to the location counter. •
• Examples of compile-and-go systems are WATFOR, PL/C,
and Dartmouth BASIC.
• It is also called an assemble-and-go or a load-and-go
system.
• because The assembler is first executed and when it is
finished, causes a branch straight to the first instruction of
the program.
• There is no stop between the compilation, link editing,
loading, and execution of the program
• Advantages :-
• They are simple and easier to implement.
• No additional routines are required to load the compiled
code into the memory.
• Disadvantages :-
• There is wastage in memory space due to the presence of
the assembler.
• There is no production of .obj file, the source code is
directly converted to executable form. Hence even though
there is no modification in the source program it needs to
be assembled and executed each time.(time consuming)
General Loader
• In general loader output of assembler is saved in a file.
• This output can be loaded and executed whenever the user
wants to run the program.
• The output of assembler is an intermediate file, the
disadvantages of compile and go scheme can be avoided.
1. Retranslation of program every time it is run is avoided.
2. The assembler need not reside in the main memory at
time of execution. Only the loader need to be in the
memory but it is much smaller compare to a compile and
go loader.
3. It is possible to write a modular program under general
loader scheme. (subbroutine)
4. In this type of loader , the loader accepts object files and
places machine instructions and data in the memory for
execution.
Absolute Loader
• It is simplest type of loader fits under general loader scheme as it
handles different programs written in different languages.
• In this relocated object files are created, loader accepts that files and
places them at specified memory location.
• It is called absolute because no relocation information is needed rather
it is obtained from assembler/programmer.
• Starting address of every module is known to programmer .starting
address is placed into object file and loader simply loads executable
form of m/c instructions from that starting address.
• Assembler/programmer have knowledge of memory mgmt.
• Resolution of externel reference or linking of different subroutine are
the issue which are handled by programmer.
• The four loading functions are accomplished in an absolute loading
scheme.
1. . Allocation- by programmer
2. Linking- by programmer
3. Relocation-by assembler
4. . Loading-by loader
• Advantages:
1. No relocation info. is required so the size of the object
module is comparatively small.
2. This scheme is easy to implement.
3. This scheme makes more memory available for loading
since the assembler is not in memory at the time of
loading.
4. No modification of address sensitive entities is required
at the time of loading.
5. This scheme supports multiple object modules to reside in
memory.
• Disadvantages:
1. Since linking is handled by the programmer, programmer
has to remember the address of each module and use that
absolute address explicitly for linking.
2. The programmer has to be careful not to assign
overlapping locations to modules to be linked.
3. Lot of memory lying between modules will be wasted.
4. If changes are made to one module that increases its size
then it can overlap the start of another module. This
manual shifting can become very complex and tedious.
Absolute loader Compile and go loader
• Multiple programs are • Multiple programs are not
possible possible
• This is efficient scheme • This is very less efficient
scheme
• Programmer must have • Memory knowledge is not
knowledge of memory required by programmer
• Complex than compile & go • Very simple scheme
• Translator is not in main • Translator is in main memory.
memory
• Object code is generated • No object code generation
Subroutine Linkage
• A program consisting of main program and a set of functions
could reside in several files.
• These program units are assembled separately.
• Suppose, A main program A wishes to call subroutine B and if
the subroutine B resides in another file then assembler will not
know the address of B and declare it as an undefined symbol.
• To realize such interactions A and B must contain public
definition and external references.
• There are two types of statements are present subroutine
linkage:
1. EXTRN statements
2. ENTRY statements
• EXTRN statements - The EXTERN statement lists the
symbols to which external references are made in the
current program unit. These symbols are defined in other
program units.
• ENTRY statements - The ENTRY statement lists the public
definition of a program unit i.e. it lists those symbols
defined in the program unit which may be referenced in
other program units.
Relocating loaders
• To avoid possible assembling of all subroutines when
single subroutine is changed and to perform the tasks of
allocation and linking for the programmer the general class
of relocating loader was introduced.
• The output of relocating loader is the program and
information about all other programs it references.
• BSS is an example of relocating loader.
• The BSS loader allows many code segments but only one data
segment.
• The output of assembler using BSS loader is:
1. Object Program
2. Reference about other programs to be accessed.
3. Information about address sensitive entities.
• Here all memory addressing is performed using displacement(offset).
• Starting memory address is stored in the segment register and actual
address is given by: contents of segment register + address of the operand
in the instruction. • 500 + 30 = 530 Displacement(offset)
Functions of BSS Loader
• The BSS loader allows many code segments and one common
data segment.
• The assembler assembles each code segment independently
and passes on to the loader the following.
1. Transfer vector that consists of address containing names of
subroutines referenced by the source program.
2. Relocation information, i.e. locations in the program that must
be changed if it is loaded in an arbitrary location in the main
memory.
3. Length of source program and length of transfer vector. –
• The loader loads the transfer vector and the object code into
memory, then the loader loads each subroutine identified in
transfer vector.
• The transfer vector is used to solve the problem of linking and
the program length information is used to solve the problem of
allocation.
Direct- Linking loader
• 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
• The assembler should give the following information to the
loader:
• 1)The length of the object code segment.
• 2)The list of all the symbols which are not defined in the other
segment but can be used in the current segment.
• 3)The list of all the symbols which are defined in the current
segment but can be referred by the other segments
• There are 4 Sections of Object Program : (data structure)
1. External Symbol Dictionary (ESD).
2. Instruction and Data Cards, called “TEXT” of Program
(TXT).
3. Relocation and Linkage Directory cards (RLD).
4. END card (END)
ESD
• The ESD cards contain the information necessary to build
the external symbol dictionary or symbol table.
• External symbols are symbols that can be referred beyond
the subroutine level.
• There are 3 types of external symbols:
1. Segment Definition (SD).- name of segments
2. Local Definition (LD).- Entry Symbols
3. External Reference (ER)- Extern Symbols
• Each SD and ER symbol is given a unique number e.g. 0, 1
by the assembler.
TXT
• Text portion of object module contains the relocatable
machine language instructions and data that were
produced during translation.
RLD
• It contains one entry for each address that must be
changed when the module is loaded into main memory.
• The relocation directory contains the following
information:
1. The address of each operand that needs to be changed
due to relocation.
2. By what it has to be changed.
3. The operation to be performed.

• END : This indicates the end of the module.


GEST
• The GEST contains every external symbol and its
corresponding assigned memory address.
• If we assume that PG1 is loaded at location 204 and PG2 is
loaded at location 268
• A direct linking loader requires two passes to complete the
linking process.
I. Pass I assigns addresses to all external symbols.
II. Pass II performs actual loading, relocation and linking.
• In pass I a global external symbol table is prepared. - It
contains every external symbol and the corresponding
absolute address value.
Overlay Structure
• It is used in a system which does not support virtual
memory.
• In static loading/linking, it is necessary that all the
subroutine needed for execution are loaded into the main
memory at the same time.
• If the total amount of memory required by all these
subroutines exceeds the amount available it will create
problem, so we can get rid of this problem with the help of
overlays.
• Usually the subroutines of a program are needed at
different times.
• By finding which subroutines call other subroutines it is
possible to produce an overlay structure that identifies
mutually exclusive subroutines.
• From diagram a) program consisting of 5 subprograms
(A,B,C,D and E) that require 100k bytes of memory.
Linker
Linking
• Linking intakes the object codes generated by the
assembler and combines them to generate the executable
module.
• Establishing the linking between all the modules or all the
functions of the program in order to continue the program
execution is called linking.
• The linking combines all object modules of a program to
generate executable modules it also links the library
function in the object module to built-in libraries of the
high-level programming language.
Object module
• An assembler produces an object module. The object
module is a sequence of object records.
• It contains all information necessary to relocate and link
different object modules. It has four components:
1. Header
2. Binary image(machine instructions)
3. Relocation table
4. Linking table containing external references and public
definitions.
• The binary image contains machine language program of
the module.
• Relocation table contains the list of instructions requiring
relocation.
• The operands in such instruction are address sensitive.
• Linking table contains information about external
references and public declaration
Self Relocating Program
• Program relocatability refers to ability to load and execute
a given program into an arbitrary place in memory.
• A self relocating program is a program which can perform
the relocation itself. It contains the following:
1. A table of information about address sensitive
instructions in the program.
2. Relocating logic that can perform the relocation of the
address sensitive instructions.
• So there is no need of a linker in self relocating programs.
Dynamic link library
• DLL is microsoft implementation of shared library in
windows.
• The file formats of DLL are the same as windows EXE files.
• A DLL can contain:
1. Code
2. Data
3. Resources
Sharing of Code
• With dynamic linking, shared code is placed into a single
separate file.
• The programs that call this file are connected to it at run
time, with operating system performing linking.
• While the DLL code may be shared, the data is private until
shared data is explicitly requested by the library.
• Each process using DLL has its own copy of all the DLLs
data.
Sharing of Data
• Sharing of DLL’s data section allows inter-process
communication through this shared memory.
• Import Libraries: Linking to dynamic libraries is usually
handled by linking to an import library.
• The created executable file contains an import address in
import address table(IAT).
• DLL function calls are referenced through this address.
• Each referenced DLL function contains its own entry in the
IAT.
• At run time the IAT is filled with appropriate addresses
that point directly to a function in the separately loaded
DLL
• The import libraries for DLL have .lib extension. For
example, kernel32.dll the primary dynamic library for
windows base functions such as file creation and memory
management is linked via kernel32.lib.
• Explicit run time linking:
– It is possible to link an executable program with DLL by resolving
addresses of the imported functions at the compile time.
• DLL files may be explicitly loaded and linked at run time. •
• Windows provides a set of standard API functions for it:
1. LoadLibrary: It loads a DLL file.
2. GetProcAddress: It looks up the exported symbol by name
and returns its address in IAT.
3. FreeLibrary: It unloads the DLL
Call back function
• A callback function is a function which we write but is
called by some other program or module, such as windows
or DLLs.
• For example, a DLL may control several clients when a
certain event occurs from the DLL ,the callback function in
the client is called.
• A callback is a function in a application that a DLL can call
at suitable times.
• It is possible for a DLL to call a function in an application
provided that it has a pointer to such functions.
Implementing callback function
• Implementation of callback function involves writing a
DLL that will make use of the callback.
• This can be broken into five parts:

1. Defining a callback function.


2. Declaring a type for a callback function
3. Writing the code that uses the callback
4. Implementing the function in the client
5. Call to DLL
Assignment
1. Write a short note on ESD, TXT and RLD .
2. What is direct linking loader explain with its advantages
and disadvantages.
3. What is macro and explain the working of macro
processor.
4. Define following terms: 1. DLL 2. call back function 3. Self
relocating program
5. What is linker? Give comparison between static linking
and dynamic linking.

You might also like