Documente Academic
Documente Profesional
Documente Cultură
Module II
Macros
A macro is a facility for extending a programming
language.
It consists of
Syntax
MACRO and MEND are the macro header and macro end
statements.
The prototype statement indicates that INCR has 3
parameters called
MEM_VAL,INCR_VAL and REG.
Statements with the operation codes MOVER, ADD and
MOVEM are model statements.
No preprocessor statements are used in this macro.
MACRO
EXPANSION
A macro call leads to macro expansion
During macro expansion, the macro call
statement is replaced by a sequence of
assembly statements
Each expanded statement is marked with a
‘+’ preceding its label field
E.g.
Let’s consider a macro
MACRO
INCR &MEM_VAL, &INCR_VAL, ®
MOVER ®, &MEM_VAL
ADD ®, &INCR_VAL
MOVEM ®, &MEM_VAL
MEND
Consider the macro call
INCR A, B, AREG;
Now values of formal parameters are
Syntax
&<parameter name>[<parameter kind>[<default
value>]]
e.g.
INCR &MEM_VAL=, &INCR_VAL=, ®=BREG
different macro calls
INCR MEM_VAL=A,INCR_VAL=B
INCR MEM_VAL=A,INCR_VAL=B, REG=AREG
MACRO
INCR &MEM_VAL=, &INCR_VAL=,
®=BREG
MOVER ®, &MEM_VAL
ADD ®, &INCR_VAL
MOVEM ®, &MEM_VAL
MEND
Macros with mixed parameter list
MACRO
MAJOR &W1,&W2,&W3,&W4,&W5,&W6
EVAL &W1,&W2,&W3
STORE &W6
EVAL &W4,&W5,&W6
MEND
-------------------
-------------------
-------------------
+LOAD A
+LOAD D
+EVAL D,E,F
+SUB E
+ADD F
CONT…………….
A NESTED
EXAMPLE MACRO
2: CALL
MACRO
COMPUTE &FIRST,&SECOND
MOVEM BREG,TMP
INCR &FIRST,&SECOND,REG=BREG
MOVER BREG TMP
MEND
+MOVER 2
BREG,X 3
COMPUTE X,Y
+INCR X,Y +ADD
4
BREG,Y
5
+MOVEM
BREG X
ADVANCED MACRO
FACILITIES
It aimed to support semantic expansion.
It can be grouped into
. <ordinary string>
Similar to Go statement.
Local EV’s
Global EV’s
MACRO
CONSTANTS
LCL &A
&A SET 1
DB &A
&A SET &A+1
DB &A
MEND
SYNTAX
<attribute name>’<formal parameter spec>
The type ,length and size attributes have the names T, L and
S
E.g. MACRO
DCL_CONST &A
AIF (L’&A EQ 1) .NEXT
------
.NEXT - - - - - -
------
MEND
Conditional Expansion
MACRO
EVAL &X, &Y, &Z
AIF (&Y EQ &X) .ONLY
MOVER AREG, &X
SUB AREG,&Y
ADD AREG,&Z
AGO .OVER
.ONLY MOVER AREG,&Z
.OVER MEND
Procedure:-
There are many similar statements during
the expansion of a macro
IRP<formal
parameter>,<argument-list>
Program Target
Program
With macro program
Without
definitions and
macros
calls
N
MORE
REG M
SSNTAB
EVNTAB
PNTAB
5 SSTAB
28
KPDTAB REG AREG
10
MNT
25 LCL (E,1) AREA
26 (E,1) SET 0 10
APTAB
MOVER (P,3),=‘0’
27 AREG
28 (S,1) MOVEM (P,3),(P,1)+(E,1)
(E,1) SET (E,1)+1
29
31 MEND
MDT
PROCESSING
OF
MACRO DEFINITION
ALGORITHM
1. SSNTAB_ptr := 1;
PNTAB_ptr := 1;
2. Process the macro prototype
statement and form the MNT entry.
(a) name := macro name;
(b) For each positional parameter
(i) Enter parameter name in PNTAB
[PNTAB_ptr].
(ii) PNTAB_ptr := PNTAB_ptr+1;
(iii) #PP := #PP + 1;
(c) KPDTP := KPDTAB_ptr;
(d) For each keyword parameter
(i) Enter parameter name and default
value, in
KPDTAB [KPDTAB_ptr].
(ii) Enter parameter name in
PNTAB [PNTAB-ptr].
(iii) KPDTAB_ptr := KPDTAB_ptr + 1;
(iv) PNTAB_ptr := PNTAB_ptr + 1;
(v) #KP := #KP + 1;
(e) MDTP := MDT_ptr;
(f) #EV := 0;
(g) SSTP := SSTAB_ptr;
3. While not a MEND statement
(a) if an LCL statement then
(i) Enter the expansion time variable name in
EVNTAB.
(ii) #EV := #EV + 1;
Source Results
program
Object Binary
modules programs
Data flow
Control flow
Translated, linked & load
Translated (translated time)address:-
time Address
Address assigned by the translator
Linked address:-
Address assigned by the linker
Load time(load) address:-
Address assigned by the loader
Translated origin:-
Address of the origin assumed by
the translator. This is the address
specified in
the ORIGIN statement.
Linked origin:-
Address of the origin assigned by
the linker while producing a binary
program.
Load origin:-
Address of the origin assigned by
the loader while loading the
program for execution.
Address sensitive program is a
program that assumes its instructions
and data to occupy memory words
with specific addresses.
Address sensitive instruction : An
instruction which uses an absolute
address.
It is the process of
modifying the address used
in the address sensitive
instructions of a program
such that the program can
execute correctly from the
designated area of memory.
When relocation is needed..?
If Linked origin = translator origin ,
relocation must be performed by linker.
If Load origin = linked origin, relocation
must be performed by the loader.
A linker always performs relocation,
whereas some loaders do not.
Loaders do not perform relocation are
called absolute loaders.
Performing Relocation
Program P
Here t_originp = 500.
If l_originp = 900
Relocation factor = 900-500=400
Let IRRp designate the set of Instructions
Requiring Relocation in program p. IRRp
contains instructions with translated addresses
500 and 538. The instruction with trans. Address
500 contains the address 540 in the operand
field. This address is changed to 540+400 = 940.
Let symb be 540
tsymb = t_originp +dsymb (500+40=540)
lsymb = l_originp +dsymb (900+40=940)
Program Q
Let program unit P be linked with the program unit
Q.
P contains an external reference to symbol ALPHA
which is a public definition in Q with translation
address 231. Let the link origin of P be 900 and its
size is 42 words. The link origin of Q is therefore
942, and the link time address of ALPHA is 973.
Linking is performed by putting the link time address
of ALPHA in the instruction of P using ALPHA. Ie,
by putting the address 973 in the instruction with the
translation time address 518 in P.
Binary Programs
To form a binary program from a set of object
modules, the programmer invokes the linke
using the command:
Object module
Header
Header contains translated origin, size &
execution start address of P.
Program
This component contains the machine language program
corresponding to p.
Program P
Object module of Eg. program
contains
EXT 519
PD 541
P 900
TOTAL 941
Q 942
ALPHA 973
….
2.Relocatable pgms
3.Self-relocating pgms
Non Relocatable programs
It contains:
1.A table of information concerning the address
sensitive instructions exists as a part of the
program.
2.Code to perform the relocation of address
sensitive
instructions also exists as a part of the
program. This
is called the relocating logic.
The start address of the relocating logic is specified as
the execution start address of the program.
The relocating logic gains control when the program is
loaded in memory for execution.
It uses the load address and the information concerning
the address sensitive instructions to perform its own
relocation.
Execution control is now transferred to the relocated
program.
A self-relocating program can execute in any area of the
memory.
Relocating
Program Program Logic
+ +
Data Data
Non-relocatable pgm Program
+
Relocation Data
Relocatable pgm
Infmn
Relocation
Self-relocating
Infmn pgm
LOADER
S
A loader is a program that takes an input
as an object program, prepares these
programs for execution by the computer
and initiates execution.
Functions of a loader
1. Allocation : It allocates space in the
memory for the programs.
2. Linking : It resolves symbolic
references between objects
3. Relocation : It adjusts all address
dependent locations,such as address
constant etc, to correspond to the
allocated space.
4. Loading : It physically places the
machine instructions or data into the
memory.
Source Translat Object
Progra or Program
m 1 Object
programs
ready for
Loader execution
+
Source Object
Translat Program
Progra or
m 2 Loader
General Loader
Scheme
Type of loaders
◦Assemble-and-go loader
(Compile-and-go loader)
◦Absolute loader (Bootstrap
loader)
◦Relocating loader (Relative
loader)
◦Direct linking loader
Compile-and-go-loader
One method of performing the loader
functions is to have the assembler run in
one part of memory and place the
assembled machine instructions and data,
as they are assembled, directly into their
assigned memory locations . When the
assembly is completed, the assembler
causes a transfer to the starting instruction
of the program. This is a simple solution,
involving no extra procedures. Such a
loading scheme is commonly called
"compile-and-go" or "assemble and-go." It
is relatively easy to implement. The
assembler simply places the code into core,
and the "loader" consists of one instruction
Disadvantages
A portion of memory is wasted.
Advantage:-
Simplicity
Disadvantage:-
Line Does not display the text
Editors
in the manner it would appear
if printed
Views the entire text as a stream
of characters.
Advantage:-
Essentialfeatures of word
processors are commands for
moving sections of text from one
place to another, merging of text,
and searching and replacement
of words.
Word Processors
Many word processors support a
spell check option. e.g. Wordstar,
MS Word
Incorporates an awareness of the structure
of a document.
Debug Monitors