MACRO
First Macro Example:- #include <stdio.h> #define SquareOf(x) x*x void main() { int xin=3; printf(&quot;\nxin=%i&quot;,xin); printf(&quot;\nSquareOf(xin)=%i&quot;, SquareOf(xin) ); printf(&quot;\nSquareOf(xin+4)=%i&quot;, SquareOf(xin+4) ); printf(&quot;\nSquareOf(xin+xin)=%i&quot;, SquareOf(xin+xin) ); }
C Program for Swapping:- #include<stdio.h> void exchange(int i, int j) { int temp; temp = i; i = j; j = temp; printf(&quot;AFTER - %d %d\n&quot;, i, j); } main() { int i=1, j=3; printf(&quot;BEFORE - %d %d\n&quot;, i, j); exchange(i, j); }
Macro for Swapping:- #include<stdio.h> #define swap(i,j) { int temp; temp=i; i=j; j=temp; } void main() { int i=1, j=3; printf(&quot;BEFORE - %d %d\n&quot;, i, j); swap(i,j); printf(&quot;AFTER - %d %d\n&quot;, i, j); }
Macro:- A macro is a unit of specification for program generation through expansion. A macro consists of a name, a set of formal parameters and a body of code. The use of macro name with a set of actual parameters is replaced by code generated from its body.
Macro definition & Call:- A macro definition is enclosed between the name of a  macro header  and a  macro end  statement . It consists of :- A macro prototype statement One or more model statement  Macro processor statement. Macro prototype statement Syntax: <macro name> [<formal parameters>]
Macro Call:- A macro is called by writing the macro name in the mnemonic field of an assembly statement. Syntax:- <macro name> [<actual parameter list>] Example:- MACRO TEST   &X, &Y, &Z AIF  (&Y EQ &X)  .ONLY MOVER AREG, &Y AGO .LAST .ONLY MOVER  AREG, &Z .LAST MEND Macro Call:- TEST  S,10 Macro header & End stmt
ADVANCED MACRO FACILITIES Facilities for change of flow of control Expansion time variables Attributes of parameters
For change of flow of control:- Two features are provided for this purpose Expansion time Sequencing Symbol Expansion time Statements ( AIF,AGO,ANOP) Sequencing Symbol( SS) SS is defined by putting it in the label field of a stmt in the macro body. Statements (syntax) AIF->  AIF ( <expression>) <Sequencing Symbol (SS)> Expression includes relational expression ( LT, NE etc.). If condition is true then control is transferred to SS. AGO-> AGO <SS>    unconditional transfer ANOP-> <SS> ANOP    used to define SS
Example MACRO TEST &X, &Y, &Z AIF  (&Y EQ &X)  .ONLY MOVER AREG, &Y AGO .LAST .ONLY MOVER  AREG, &Z .LAST MEND REPRESENTING   SS If condition is true ( Y= X) then control will be transferred to  .ONLY
Expansion time variables EV’s are variables which can be only used during the expansion of macro calls. It may be of two types:- Local EV (LCL) can be used only in one particular macro. Global EV (GBL) works like global variables. Syntax:- LCL <EV specification>, ……… GBL <EV specification>, …….. Values of EV can be changed using SET (preprocessor stmt). <EV specification> SET <SET expression>
Example of EV MACRO TESTEV LCL  &A &A SET 1 LCL &A declares local variable A
Attributes of parameters An attribute is written using the syntax:- <attribute name>  ’  <formal parameter specification> T– type L – length S – size Example MACRO ME &A AIF  (L’&A EQ 1) .NEXT ------ -----
ALGORITHM OF MACRO PROCESSOR
EXAMPLE MACRO TEST &X, &N, &R=AREG LCL  &M &M SET  0 .PQ MOVEM &R, &X+&M &M SET  &M + 1 AIF (&M NE N) .PQ MEND
EXAMPLE MACRO TEST & X , & N ,  R =AREG LCL  & M   &M SET  0 .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
Tables of Macro Preprocessor 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
Tables of Macro Preprocessor 3.  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
Tables of Macro Preprocessor 7.  Actual Parameter Table (APT) FIELDS :- a) Value 8.  EV Table (EVTAB) FIELDS:- a) value 9.  SS Table (SSTAB) FIELDS :- a) MDT Entry #
EXAMPLE MACRO TEST & X , & N ,  R =AREG LCL  & M   &M SET  0 .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
PNTAB EVNTAB KPDTAB SSNTAB APTAB EVTAB SSTAB R N X M AREG R PQ AREG 10 S 0 27
MNT MDT SSTP KPDTP MDTP #EV #KP #PP Name 5 10 25 1 1 2 TEST 30 29 28 27 26 25 MEND AIF  (E,1) NE (P,2) (S,1) (E,1)  SET  (E,1) +1 MOVEM (P,3),(P,1)+(E,1) (E,1)  SET  0 LCL (E,1)
Steps for MNT Entry TEST & X , & N ,  R= AREG STRUCTURE OF MNT For each positional parameter (i) Enter  Parameter name in  PNTAB [PNTAB_ptr] (ii)PNTAB_ptr := PNTAB_Ptr + 1; (iii)# PP := # PP + 1 : PNTAB x #pp=1 N #pp=1+1=2 SSTP KPDTP MDTP #EV #KP #PP Name
Steps for MNT Entry TEST & X , & N ,  R= AREG STRUCTURE OF MNT 10 PNTAB For each keyword parameter (i)Enter  parameter name and default value  ( if any), in KPDTAB   (ii) Enter parameter name  in PNTAB [ PNTAB_Ptr}. (iii) KPDTAB_Ptr := KPDTAB _Ptr + 1 ; (iv) PNTAB_Ptr := PNTAB_Ptr  + 1; (v)# KP := # KP + 1 ; KPDTAB R R,  AREG # KP := 1 N x AREG R SSTP KPDTP MDTP #EV #KP #PP Name
MDT 5 SSTAB MNT 30 29 28 27 26 25 MEND AIF  (E,1) NE (P,2) (S,1) (E,1)  SET  (E,1) +1 MOVEM (P,3),(P,1)+(E,1) (E,1)  SET  0 LCL (E,1) 27 SSTP KPDTP MDTP #EV #KP #PP Name 5 10 25 1 1 2 TEST
LCL  & M EVNTAB M If an LCL statement then (i) Enter expansion time variable name in EVNTAB  (ii)# EV := # EV + 1 ; M
.PQ MOVEM &R, &X+&M SSNTAB a)  If  SS is present in SSNTAB then get index No. Else 1 Enter SS in SSNTAB, then get index No. and  2. Enter MDT_Ptr in SSTAB b) For a parameter , generate the specification ( p,#n). c) For an expansion variable,generate the  specification ( E, # m). . PQ SSTAB 5 27 .PQ MOVEM &R, &X+&M MOVEM  (P,3) , (P,1)+(E,1) Enter this line into MDT Steps for Model Stmt
(Processing of a macro definition) 1.  SSNTAB_Ptr :=1; SSTAB_PTR:=1;MDT_ptr:=1; PNTAB_ptr :=1;KPDTAB_PTR:=1; 2.  Process the macro prototype statement and from 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  ( if any), in  KPDTAB [KPDTAB_ptr + 1]; (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 expansion time variable name in EVNTAB  (ii) # EV := # EV + 1 ; (b) If a model statement then (i) If a label field contains a sequencing symbol then If symbol is present in SSNTAB then q := entry number in SSNTAB;   else Enter symbol in SSNTAB [ SSNTAB_Ptr]. q:= SSNTAB_Ptr; SSNTAB_ptr := SSNTAB_Ptr + 1; SSTAB [ SSTP + q – 1] := MDT_ Ptr; (ii) For a parameter , generate the specification ( p,#n). (iii) For an expansion variable,generate the  specification ( E, # m). (iv) Record the IC in MDT [ MDt_ptr]; (v) MDT_Ptr := MDT_Ptr + 1 :
(c ) If a preprocessor statement then (i) If a SET statement Search each expansion time variable  name used in the statement in EVNTAB  and generate (E,#m) (ii) If an AIF  or Ago statement then If sequencing symbol used in the  statement is present is SSNTAB then q = entry number in SSNTAB else Enter symbol in SSNTAB [ SSNTAB _ Ptr] q :=  SSNTABPtr ; SSNTAB_ Ptr := SSNTAB_ Ptr + 1 : Replace the symbol by ( S, SSTP + q – 1 ) (iii) Record the  IC in MDT [MDT_Ptr]. (iv) MDT_Ptr := MDT_ Ptr + 1 :
4. (MEND Statement ) If SSNTAB_Ptr = 1 ( i,e, SSNTAB is empty ) then SSTP := 0; Else SSTAB_Ptr := SSTAB_ Ptr + SSNYAB_Ptr – 1 : If # KP = 0 then KPDTP =0;

Macro

  • 1.
  • 2.
    First Macro Example:-#include <stdio.h> #define SquareOf(x) x*x void main() { int xin=3; printf(&quot;\nxin=%i&quot;,xin); printf(&quot;\nSquareOf(xin)=%i&quot;, SquareOf(xin) ); printf(&quot;\nSquareOf(xin+4)=%i&quot;, SquareOf(xin+4) ); printf(&quot;\nSquareOf(xin+xin)=%i&quot;, SquareOf(xin+xin) ); }
  • 3.
    C Program forSwapping:- #include<stdio.h> void exchange(int i, int j) { int temp; temp = i; i = j; j = temp; printf(&quot;AFTER - %d %d\n&quot;, i, j); } main() { int i=1, j=3; printf(&quot;BEFORE - %d %d\n&quot;, i, j); exchange(i, j); }
  • 4.
    Macro for Swapping:-#include<stdio.h> #define swap(i,j) { int temp; temp=i; i=j; j=temp; } void main() { int i=1, j=3; printf(&quot;BEFORE - %d %d\n&quot;, i, j); swap(i,j); printf(&quot;AFTER - %d %d\n&quot;, i, j); }
  • 5.
    Macro:- A macrois a unit of specification for program generation through expansion. A macro consists of a name, a set of formal parameters and a body of code. The use of macro name with a set of actual parameters is replaced by code generated from its body.
  • 6.
    Macro definition &Call:- A macro definition is enclosed between the name of a macro header and a macro end statement . It consists of :- A macro prototype statement One or more model statement Macro processor statement. Macro prototype statement Syntax: <macro name> [<formal parameters>]
  • 7.
    Macro Call:- Amacro is called by writing the macro name in the mnemonic field of an assembly statement. Syntax:- <macro name> [<actual parameter list>] Example:- MACRO TEST &X, &Y, &Z AIF (&Y EQ &X) .ONLY MOVER AREG, &Y AGO .LAST .ONLY MOVER AREG, &Z .LAST MEND Macro Call:- TEST S,10 Macro header & End stmt
  • 8.
    ADVANCED MACRO FACILITIESFacilities for change of flow of control Expansion time variables Attributes of parameters
  • 9.
    For change offlow of control:- Two features are provided for this purpose Expansion time Sequencing Symbol Expansion time Statements ( AIF,AGO,ANOP) Sequencing Symbol( SS) SS is defined by putting it in the label field of a stmt in the macro body. Statements (syntax) AIF-> AIF ( <expression>) <Sequencing Symbol (SS)> Expression includes relational expression ( LT, NE etc.). If condition is true then control is transferred to SS. AGO-> AGO <SS>  unconditional transfer ANOP-> <SS> ANOP  used to define SS
  • 10.
    Example MACRO TEST&X, &Y, &Z AIF (&Y EQ &X) .ONLY MOVER AREG, &Y AGO .LAST .ONLY MOVER AREG, &Z .LAST MEND REPRESENTING SS If condition is true ( Y= X) then control will be transferred to .ONLY
  • 11.
    Expansion time variablesEV’s are variables which can be only used during the expansion of macro calls. It may be of two types:- Local EV (LCL) can be used only in one particular macro. Global EV (GBL) works like global variables. Syntax:- LCL <EV specification>, ……… GBL <EV specification>, …….. Values of EV can be changed using SET (preprocessor stmt). <EV specification> SET <SET expression>
  • 12.
    Example of EVMACRO TESTEV LCL &A &A SET 1 LCL &A declares local variable A
  • 13.
    Attributes of parametersAn attribute is written using the syntax:- <attribute name> ’ <formal parameter specification> T– type L – length S – size Example MACRO ME &A AIF (L’&A EQ 1) .NEXT ------ -----
  • 14.
  • 15.
    EXAMPLE MACRO TEST&X, &N, &R=AREG LCL &M &M SET 0 .PQ MOVEM &R, &X+&M &M SET &M + 1 AIF (&M NE N) .PQ MEND
  • 16.
    EXAMPLE MACRO TEST& X , & N , R =AREG LCL & M &M SET 0 .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
  • 17.
    Tables of MacroPreprocessor 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
  • 18.
    Tables of MacroPreprocessor 3. 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
  • 19.
    Tables of MacroPreprocessor 7. Actual Parameter Table (APT) FIELDS :- a) Value 8. EV Table (EVTAB) FIELDS:- a) value 9. SS Table (SSTAB) FIELDS :- a) MDT Entry #
  • 20.
    EXAMPLE MACRO TEST& X , & N , R =AREG LCL & M &M SET 0 .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
  • 21.
    PNTAB EVNTAB KPDTABSSNTAB APTAB EVTAB SSTAB R N X M AREG R PQ AREG 10 S 0 27
  • 22.
    MNT MDT SSTPKPDTP MDTP #EV #KP #PP Name 5 10 25 1 1 2 TEST 30 29 28 27 26 25 MEND AIF (E,1) NE (P,2) (S,1) (E,1) SET (E,1) +1 MOVEM (P,3),(P,1)+(E,1) (E,1) SET 0 LCL (E,1)
  • 23.
    Steps for MNTEntry TEST & X , & N , R= AREG STRUCTURE OF MNT For each positional parameter (i) Enter Parameter name in PNTAB [PNTAB_ptr] (ii)PNTAB_ptr := PNTAB_Ptr + 1; (iii)# PP := # PP + 1 : PNTAB x #pp=1 N #pp=1+1=2 SSTP KPDTP MDTP #EV #KP #PP Name
  • 24.
    Steps for MNTEntry TEST & X , & N , R= AREG STRUCTURE OF MNT 10 PNTAB For each keyword parameter (i)Enter parameter name and default value ( if any), in KPDTAB (ii) Enter parameter name in PNTAB [ PNTAB_Ptr}. (iii) KPDTAB_Ptr := KPDTAB _Ptr + 1 ; (iv) PNTAB_Ptr := PNTAB_Ptr + 1; (v)# KP := # KP + 1 ; KPDTAB R R, AREG # KP := 1 N x AREG R SSTP KPDTP MDTP #EV #KP #PP Name
  • 25.
    MDT 5 SSTABMNT 30 29 28 27 26 25 MEND AIF (E,1) NE (P,2) (S,1) (E,1) SET (E,1) +1 MOVEM (P,3),(P,1)+(E,1) (E,1) SET 0 LCL (E,1) 27 SSTP KPDTP MDTP #EV #KP #PP Name 5 10 25 1 1 2 TEST
  • 26.
    LCL &M EVNTAB M If an LCL statement then (i) Enter expansion time variable name in EVNTAB (ii)# EV := # EV + 1 ; M
  • 27.
    .PQ MOVEM &R,&X+&M SSNTAB a) If SS is present in SSNTAB then get index No. Else 1 Enter SS in SSNTAB, then get index No. and 2. Enter MDT_Ptr in SSTAB b) For a parameter , generate the specification ( p,#n). c) For an expansion variable,generate the specification ( E, # m). . PQ SSTAB 5 27 .PQ MOVEM &R, &X+&M MOVEM (P,3) , (P,1)+(E,1) Enter this line into MDT Steps for Model Stmt
  • 28.
    (Processing of amacro definition) 1. SSNTAB_Ptr :=1; SSTAB_PTR:=1;MDT_ptr:=1; PNTAB_ptr :=1;KPDTAB_PTR:=1; 2. Process the macro prototype statement and from 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 ( if any), in KPDTAB [KPDTAB_ptr + 1]; (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 ;
  • 29.
    3. While nota MEND statement (a) If an LCL statement then (i) Enter expansion time variable name in EVNTAB (ii) # EV := # EV + 1 ; (b) If a model statement then (i) If a label field contains a sequencing symbol then If symbol is present in SSNTAB then q := entry number in SSNTAB; else Enter symbol in SSNTAB [ SSNTAB_Ptr]. q:= SSNTAB_Ptr; SSNTAB_ptr := SSNTAB_Ptr + 1; SSTAB [ SSTP + q – 1] := MDT_ Ptr; (ii) For a parameter , generate the specification ( p,#n). (iii) For an expansion variable,generate the specification ( E, # m). (iv) Record the IC in MDT [ MDt_ptr]; (v) MDT_Ptr := MDT_Ptr + 1 :
  • 30.
    (c ) Ifa preprocessor statement then (i) If a SET statement Search each expansion time variable name used in the statement in EVNTAB and generate (E,#m) (ii) If an AIF or Ago statement then If sequencing symbol used in the statement is present is SSNTAB then q = entry number in SSNTAB else Enter symbol in SSNTAB [ SSNTAB _ Ptr] q := SSNTABPtr ; SSNTAB_ Ptr := SSNTAB_ Ptr + 1 : Replace the symbol by ( S, SSTP + q – 1 ) (iii) Record the IC in MDT [MDT_Ptr]. (iv) MDT_Ptr := MDT_ Ptr + 1 :
  • 31.
    4. (MEND Statement) If SSNTAB_Ptr = 1 ( i,e, SSNTAB is empty ) then SSTP := 0; Else SSTAB_Ptr := SSTAB_ Ptr + SSNYAB_Ptr – 1 : If # KP = 0 then KPDTP =0;