01SB072010004900011
01SB072010004900011
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
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=,®=AREG
• MOVER ®,&M
• ADD ®,&I
• MOVEM ®,&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.