Upcoming SlideShare
×

# Macro

12,067 views

Published on

Subroutines of System Software Engg

Published in: Technology, Art & Photos
11 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• It's very useful ppt

Are you sure you want to  Yes  No
• macros : the door to begin

Are you sure you want to  Yes  No
Views
Total views
12,067
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
514
2
Likes
11
Embeds 0
No embeds

No notes for slide

### Macro

1. 1. MACRO
2. 2. First Macro Example:- #include <stdio.h> #define SquareOf(x) x*x void main() { int xin=3; printf(&quot; xin=%i&quot;,xin); printf(&quot; SquareOf(xin)=%i&quot;, SquareOf(xin) ); printf(&quot; SquareOf(xin+4)=%i&quot;, SquareOf(xin+4) ); printf(&quot; SquareOf(xin+xin)=%i&quot;, SquareOf(xin+xin) ); }
3. 3. 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 &quot;, i, j); } main() { int i=1, j=3; printf(&quot;BEFORE - %d %d &quot;, i, j); exchange(i, j); }
4. 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 &quot;, i, j); swap(i,j); printf(&quot;AFTER - %d %d &quot;, i, j); }
5. 5. 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.
6. 6. <ul><li>Macro definition & Call:- </li></ul><ul><li>A macro definition is enclosed between the name of a macro header and a macro end statement . </li></ul><ul><li>It consists of :- </li></ul><ul><li>A macro prototype statement </li></ul><ul><li>One or more model statement </li></ul><ul><li>Macro processor statement. </li></ul><ul><li>Macro prototype statement </li></ul><ul><li>Syntax: </li></ul><ul><li><macro name> [<formal parameters>] </li></ul>
7. 7. <ul><li>Macro Call:- </li></ul><ul><li>A macro is called by writing the macro name in the mnemonic field of an assembly statement. </li></ul><ul><li>Syntax:- </li></ul><ul><li><macro name> [<actual parameter list>] </li></ul><ul><li>Example:- </li></ul><ul><ul><ul><ul><ul><li>MACRO </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>TEST &X, &Y, &Z </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>AIF (&Y EQ &X) .ONLY </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>MOVER AREG, &Y </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>AGO .LAST </li></ul></ul></ul></ul></ul><ul><li>.ONLY MOVER AREG, &Z </li></ul><ul><li>.LAST MEND </li></ul><ul><li>Macro Call:- </li></ul><ul><li>TEST S,10 </li></ul>Macro header & End stmt
8. 8. ADVANCED MACRO FACILITIES <ul><li>Facilities for change of flow of control </li></ul><ul><li>Expansion time variables </li></ul><ul><li>Attributes of parameters </li></ul>
9. 9. <ul><li>For change of flow of control:- </li></ul><ul><li>Two features are provided for this purpose </li></ul><ul><li>Expansion time Sequencing Symbol </li></ul><ul><li>Expansion time Statements ( AIF,AGO,ANOP) </li></ul><ul><li>Sequencing Symbol( SS) </li></ul><ul><li>SS is defined by putting it in the label field of a stmt in the macro body. </li></ul><ul><li>Statements (syntax) </li></ul><ul><li>AIF-> AIF ( <expression>) <Sequencing Symbol (SS)> </li></ul><ul><li>Expression includes relational expression ( LT, NE etc.). If condition is true then control is transferred to SS. </li></ul><ul><li>AGO-> AGO <SS>  unconditional transfer </li></ul><ul><li>ANOP-> <SS> ANOP  used to define SS </li></ul>
10. 10. Example <ul><ul><ul><ul><ul><li>MACRO </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>TEST &X, &Y, &Z </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>AIF (&Y EQ &X) .ONLY </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>MOVER AREG, &Y </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>AGO .LAST </li></ul></ul></ul></ul></ul><ul><li>.ONLY MOVER AREG, &Z </li></ul><ul><li>.LAST MEND </li></ul>REPRESENTING SS If condition is true ( Y= X) then control will be transferred to .ONLY
11. 11. Expansion time variables <ul><li>EV’s are variables which can be only used during the expansion of macro calls. </li></ul><ul><li>It may be of two types:- </li></ul><ul><li>Local EV (LCL) can be used only in one particular macro. </li></ul><ul><li>Global EV (GBL) works like global variables. </li></ul><ul><li>Syntax:- </li></ul><ul><li>LCL <EV specification>, ……… </li></ul><ul><li>GBL <EV specification>, …….. </li></ul><ul><li>Values of EV can be changed using SET (preprocessor stmt). </li></ul><ul><li><EV specification> SET <SET expression> </li></ul>
12. 12. Example of EV <ul><ul><ul><ul><ul><li>MACRO </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>TESTEV </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>LCL &A </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>&A SET 1 </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>LCL &A declares local variable A </li></ul></ul></ul></ul></ul>
13. 13. 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 ------ -----
14. 14. ALGORITHM OF MACRO PROCESSOR
15. 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. 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. 17. Tables of Macro Preprocessor <ul><li>MACRO NAME TABLE </li></ul><ul><ul><li>FIELDS:- a) Macro name </li></ul></ul><ul><ul><li>b) No. of Positional Parameters (#PP) </li></ul></ul><ul><ul><li>c) No. of Keyword Parameters (#KP) </li></ul></ul><ul><ul><li>d) No. of Expansion Time Variables (#EV) </li></ul></ul><ul><ul><li>e) MDT Pointer (MDTP) </li></ul></ul><ul><ul><li>f) KPDTAB Pointer (KPDTP) </li></ul></ul><ul><ul><li>g) SSTAB Pointer (SSTP) </li></ul></ul><ul><ul><li>2. PARAMETER NAME TABLE </li></ul></ul><ul><ul><li>FIELDS:- a) Parameter Name </li></ul></ul>
18. 18. Tables of Macro Preprocessor <ul><li>3. EV NAME TABLE (EVNTAB) </li></ul><ul><ul><li>FIELDS:- a) EV name </li></ul></ul><ul><ul><li>4. SS NAME TABLE </li></ul></ul><ul><ul><li>FIELDS :- a) SS Name </li></ul></ul><ul><ul><li>5. Keyword Parameter Default Table (KPDTAB) </li></ul></ul><ul><ul><ul><li>FIELDS:- a) Parameter Name </li></ul></ul></ul><ul><ul><ul><li>b) Default Value </li></ul></ul></ul><ul><ul><li>6. Macro Definition Table (MDT) </li></ul></ul><ul><ul><li>FIELDS :- a) Label </li></ul></ul><ul><ul><li>b) Opcode </li></ul></ul><ul><ul><li>c) Operands </li></ul></ul>
19. 19. 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 #
20. 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. 21. PNTAB EVNTAB KPDTAB SSNTAB APTAB EVTAB SSTAB R N X M AREG R PQ AREG 10 S 0 27
22. 22. 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)
23. 23. 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
24. 24. 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
25. 25. 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
26. 26. LCL & M EVNTAB M If an LCL statement then (i) Enter expansion time variable name in EVNTAB (ii)# EV := # EV + 1 ; M
27. 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. 28. (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 ;
29. 29. 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 :
30. 30. (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 :
31. 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;