3. C Program for Swapping:- #include<stdio.h> void exchange(int i, int j) { int temp; temp = i; i = j; j = temp; printf("AFTER - %d %d", i, j); } main() { int i=1, j=3; printf("BEFORE - %d %d", 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("BEFORE - %d %d", i, j); swap(i,j); printf("AFTER - %d %d", i, j); }
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.
7.
8.
9.
10.
11.
12.
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 ------ -----
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.
18.
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. 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 KPDTAB SSNTAB APTAB EVTAB SSTAB R N X M AREG R PQ AREG 10 S 0 27
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. 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. 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. 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. 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 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. 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. (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. 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;