SlideShare a Scribd company logo
1 of 5
Download to read offline
System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 1
HANDOUT#04
Aim:
Implementation of simple macro
Theory:
A macro is a unit of specification for program generation through expansion.
Macro Definition
A macro definition is enclosed between a macro header statement and a macro end
statement. Macro definitions are typically located at the start of a program. Macro
definition consists of:
1. Macro prototype statements
2. One or more model statements
3. Macro preprocessor statements
1. Macro prototype statements.
The macro prototype statement declares the name of the macro and the names and
kinds of its parameters. The macro prototype statement has the following syntax:
<macro name> [<formal parameter specification>[ …..]]
where <macro name> appears in the mnemonic field of assembly Statement and
<formal parameter specification> is of the form
&<parameter name> [<parameter kind>]
2. Model statements.
A model statement is a statement from which assembly language statements may
be generated during macro expansion.
3. Macro preprocessor statements.
A preprocessor statement is used to perform auxiliary functions during macro
expansion.
System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 2
Macro call
A macro is called by writing the macro name in the mnemonic field of an
assembly statement. The macro call has syntax
<macro name> [<actual parameter specification>[,….]]
where actual parameter resembles an operand specification in assembly statement.
Example
The definition of macro INCR is given below
MACRO
INCR &MEM_VAL, &INCR_VAL, &REG
MOVER &REG, &MEM_VAL
ADD &REG, &INCR-VAL
MOVEM &REG, &MEM_VAL
MEND
Macro Expansion
A macro leads to macro expansion. During macro expansion, the use of a macro
name with a set of actual parameters i.e. the macro call statement is replaced by a
sequence of assembly code. To differentiate between the original statement of a
program and the statements resulting from macro expansion, each expanded
statement is marked with a ‘+’ preceding its label field.
There are two kinds of expansions
1. Lexical expansion.
2. Semantic expansion
1. Lexical expansion.
Lexical expansion implies replacement of a character string by another character
string during program generation. It replaces the occurrences of formal parameter
by corresponding actual parameters.
Example
Consider the following macro definition
MACRO
INCR &MEM_VAL, &INCR_VAL, &REG
MOVER &REG, &MEM_VAL
ADD &REG, &INCR-VAL
MOVEM &REG, &MEM_VAL
System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 3
MEND
Consider macro call statement on INCR
INCR A, B, AREG
The values of formal parameters are:
Formal parameter value
MEM_VAL A
INCR_VAL B
REG AREG
Lexical expansion of the model statement leads to the code
+ MOVER AREG, A
+ ADD AREG, B
+ MOVEM AREG, A
2. Semantic expansion
Semantic expansion implies generation of instructions tailored to the requirements
of a specific usage e.g. generation of type specific instruction for manipulation of
byte and word operands. It is characterized by the fact that different use of macro
can lead to codes which differ in the number, sequence and opcodes of
instructions.
Example
Consider the macro definition
MACRO
CLEAR &X, &N
LCL &M
&M SET 0
MOVER AREG, =’0’
.MORE MOVEM AREG, &X+&M
&M SET &M+1
AIF (&M NE N) .MORE
MEND
Consider the macro call CLEAR B, 3
System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 4
This macro call leads to generation of statements:
+ MOVER AREG, =’0’
+ MOVEM AREG, B
+ MOVEM AREG, B+1
+ MOVEM AREG, B+2
Input:
#include<stdio.h>
#define xyz 5
#define asd 10
#define pf printf
#define sf scanf
void main(void)
{
pf("Enter a no:");
sf("%d",&a);
b=asd+xyz;
pf("xyz=%d",b);
}
Output:
The old program
#include<stdio.h>
#define xyz 5
#define asd 10
#define pf printf
#define sf scanf
void main(void)
{
System Programming Sunita M. Dol, CSE Dept
Walchand Institute of Technology, Solapur Page 5
pf("Enter a no:");
sf("%d",&a);
b=asd+xyz;
pf("xyz=%d",b);
}
Macro Expansion
xyz 5
asd 10
pf printf
sf scanf
The new program
#include<stdio.h>
void main(void)
{
printf("Enter a no:");
scanf("%d",&a);
b=10+5;
printf("xyz=%d",b);
}
Conclusion:
A macro is a unit of specification for program generation through expansion. A
simple macro is implemented in C-language

More Related Content

What's hot (20)

Handout#03
Handout#03Handout#03
Handout#03
 
Handout#02
Handout#02Handout#02
Handout#02
 
Handout#09
Handout#09Handout#09
Handout#09
 
Assignment4
Assignment4Assignment4
Assignment4
 
Handout#01
Handout#01Handout#01
Handout#01
 
Assignment2
Assignment2Assignment2
Assignment2
 
Handout#12
Handout#12Handout#12
Handout#12
 
Assignment5
Assignment5Assignment5
Assignment5
 
Oops
OopsOops
Oops
 
Introduction of C++ By Pawan Thakur
Introduction of C++ By Pawan ThakurIntroduction of C++ By Pawan Thakur
Introduction of C++ By Pawan Thakur
 
The smartpath information systems c plus plus
The smartpath information systems  c plus plusThe smartpath information systems  c plus plus
The smartpath information systems c plus plus
 
Top C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerTop C Language Interview Questions and Answer
Top C Language Interview Questions and Answer
 
C programming notes
C programming notesC programming notes
C programming notes
 
C notes
C notesC notes
C notes
 
Chapter2
Chapter2Chapter2
Chapter2
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Bcsl 031 solve assignment
 
Literals,variables,datatype in C#
Literals,variables,datatype in C#Literals,variables,datatype in C#
Literals,variables,datatype in C#
 
Chap02
Chap02Chap02
Chap02
 
Fortran 90 Basics
Fortran 90 BasicsFortran 90 Basics
Fortran 90 Basics
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)
 

Similar to Handout#04

System software - macro expansion,nested macro calls
System software - macro expansion,nested macro callsSystem software - macro expansion,nested macro calls
System software - macro expansion,nested macro callsSARASWATHI S
 
Task Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdfTask Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdfacsmadurai
 
Preprocessor directives in c language
Preprocessor directives in c languagePreprocessor directives in c language
Preprocessor directives in c languagetanmaymodi4
 
Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguageTanmay Modi
 
33443223 system-software-unit-iv
33443223 system-software-unit-iv33443223 system-software-unit-iv
33443223 system-software-unit-ivShaniya Fathimuthu
 
Inline functions & macros
Inline functions & macrosInline functions & macros
Inline functions & macrosAnand Kumar
 
MIGHTY MACROS AND POWERFUL PARAMETERS: MAXIMIZING EFFICIENCY AND FLEXIBILITY ...
MIGHTY MACROS AND POWERFUL PARAMETERS: MAXIMIZING EFFICIENCY AND FLEXIBILITY ...MIGHTY MACROS AND POWERFUL PARAMETERS: MAXIMIZING EFFICIENCY AND FLEXIBILITY ...
MIGHTY MACROS AND POWERFUL PARAMETERS: MAXIMIZING EFFICIENCY AND FLEXIBILITY ...VLSICS Design
 
Computer fundamentals
Computer fundamentalsComputer fundamentals
Computer fundamentalssameer sheikh
 
N_Asm Assembly macros (sol)
N_Asm Assembly macros (sol)N_Asm Assembly macros (sol)
N_Asm Assembly macros (sol)Selomon birhane
 
Sas macros part 4.1
Sas macros part 4.1Sas macros part 4.1
Sas macros part 4.1venkatam
 
Actionscript Standards
Actionscript StandardsActionscript Standards
Actionscript StandardsKapil Mohan
 

Similar to Handout#04 (20)

Unit 2
Unit 2Unit 2
Unit 2
 
Module 5.pdf
Module 5.pdfModule 5.pdf
Module 5.pdf
 
handout6.pdf
handout6.pdfhandout6.pdf
handout6.pdf
 
System software - macro expansion,nested macro calls
System software - macro expansion,nested macro callsSystem software - macro expansion,nested macro calls
System software - macro expansion,nested macro calls
 
Task Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdfTask Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdf
 
Unit 4 sp macro
Unit 4 sp macroUnit 4 sp macro
Unit 4 sp macro
 
Ss4
Ss4Ss4
Ss4
 
Preprocessor directives in c language
Preprocessor directives in c languagePreprocessor directives in c language
Preprocessor directives in c language
 
Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguage
 
33443223 system-software-unit-iv
33443223 system-software-unit-iv33443223 system-software-unit-iv
33443223 system-software-unit-iv
 
Macro-processor
Macro-processorMacro-processor
Macro-processor
 
Inline functions & macros
Inline functions & macrosInline functions & macros
Inline functions & macros
 
MIGHTY MACROS AND POWERFUL PARAMETERS: MAXIMIZING EFFICIENCY AND FLEXIBILITY ...
MIGHTY MACROS AND POWERFUL PARAMETERS: MAXIMIZING EFFICIENCY AND FLEXIBILITY ...MIGHTY MACROS AND POWERFUL PARAMETERS: MAXIMIZING EFFICIENCY AND FLEXIBILITY ...
MIGHTY MACROS AND POWERFUL PARAMETERS: MAXIMIZING EFFICIENCY AND FLEXIBILITY ...
 
Computer fundamentals
Computer fundamentalsComputer fundamentals
Computer fundamentals
 
Pre processor directives in c
Pre processor directives in cPre processor directives in c
Pre processor directives in c
 
N_Asm Assembly macros (sol)
N_Asm Assembly macros (sol)N_Asm Assembly macros (sol)
N_Asm Assembly macros (sol)
 
Preprocessors
PreprocessorsPreprocessors
Preprocessors
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Sas macros part 4.1
Sas macros part 4.1Sas macros part 4.1
Sas macros part 4.1
 
Actionscript Standards
Actionscript StandardsActionscript Standards
Actionscript Standards
 

More from Sunita Milind Dol (16)

9.Joins.pdf
9.Joins.pdf9.Joins.pdf
9.Joins.pdf
 
8.Views.pdf
8.Views.pdf8.Views.pdf
8.Views.pdf
 
7. Nested Subqueries.pdf
7. Nested Subqueries.pdf7. Nested Subqueries.pdf
7. Nested Subqueries.pdf
 
6. Aggregate Functions.pdf
6. Aggregate Functions.pdf6. Aggregate Functions.pdf
6. Aggregate Functions.pdf
 
5. Basic Structure of SQL Queries.pdf
5. Basic Structure of SQL Queries.pdf5. Basic Structure of SQL Queries.pdf
5. Basic Structure of SQL Queries.pdf
 
4. DML.pdf
4. DML.pdf4. DML.pdf
4. DML.pdf
 
3. DDL.pdf
3. DDL.pdf3. DDL.pdf
3. DDL.pdf
 
2. SQL Introduction.pdf
2. SQL Introduction.pdf2. SQL Introduction.pdf
2. SQL Introduction.pdf
 
1. University Example.pdf
1. University Example.pdf1. University Example.pdf
1. University Example.pdf
 
Assignment12
Assignment12Assignment12
Assignment12
 
Assignment10
Assignment10Assignment10
Assignment10
 
Assignment9
Assignment9Assignment9
Assignment9
 
Assignment8
Assignment8Assignment8
Assignment8
 
Assignment7
Assignment7Assignment7
Assignment7
 
Assignment6
Assignment6Assignment6
Assignment6
 
Assignment3
Assignment3Assignment3
Assignment3
 

Recently uploaded

College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 

Recently uploaded (20)

College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 

Handout#04

  • 1. System Programming Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 1 HANDOUT#04 Aim: Implementation of simple macro Theory: A macro is a unit of specification for program generation through expansion. Macro Definition A macro definition is enclosed between a macro header statement and a macro end statement. Macro definitions are typically located at the start of a program. Macro definition consists of: 1. Macro prototype statements 2. One or more model statements 3. Macro preprocessor statements 1. Macro prototype statements. The macro prototype statement declares the name of the macro and the names and kinds of its parameters. The macro prototype statement has the following syntax: <macro name> [<formal parameter specification>[ …..]] where <macro name> appears in the mnemonic field of assembly Statement and <formal parameter specification> is of the form &<parameter name> [<parameter kind>] 2. Model statements. A model statement is a statement from which assembly language statements may be generated during macro expansion. 3. Macro preprocessor statements. A preprocessor statement is used to perform auxiliary functions during macro expansion.
  • 2. System Programming Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 2 Macro call A macro is called by writing the macro name in the mnemonic field of an assembly statement. The macro call has syntax <macro name> [<actual parameter specification>[,….]] where actual parameter resembles an operand specification in assembly statement. Example The definition of macro INCR is given below MACRO INCR &MEM_VAL, &INCR_VAL, &REG MOVER &REG, &MEM_VAL ADD &REG, &INCR-VAL MOVEM &REG, &MEM_VAL MEND Macro Expansion A macro leads to macro expansion. During macro expansion, the use of a macro name with a set of actual parameters i.e. the macro call statement is replaced by a sequence of assembly code. To differentiate between the original statement of a program and the statements resulting from macro expansion, each expanded statement is marked with a ‘+’ preceding its label field. There are two kinds of expansions 1. Lexical expansion. 2. Semantic expansion 1. Lexical expansion. Lexical expansion implies replacement of a character string by another character string during program generation. It replaces the occurrences of formal parameter by corresponding actual parameters. Example Consider the following macro definition MACRO INCR &MEM_VAL, &INCR_VAL, &REG MOVER &REG, &MEM_VAL ADD &REG, &INCR-VAL MOVEM &REG, &MEM_VAL
  • 3. System Programming Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 3 MEND Consider macro call statement on INCR INCR A, B, AREG The values of formal parameters are: Formal parameter value MEM_VAL A INCR_VAL B REG AREG Lexical expansion of the model statement leads to the code + MOVER AREG, A + ADD AREG, B + MOVEM AREG, A 2. Semantic expansion Semantic expansion implies generation of instructions tailored to the requirements of a specific usage e.g. generation of type specific instruction for manipulation of byte and word operands. It is characterized by the fact that different use of macro can lead to codes which differ in the number, sequence and opcodes of instructions. Example Consider the macro definition MACRO CLEAR &X, &N LCL &M &M SET 0 MOVER AREG, =’0’ .MORE MOVEM AREG, &X+&M &M SET &M+1 AIF (&M NE N) .MORE MEND Consider the macro call CLEAR B, 3
  • 4. System Programming Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 4 This macro call leads to generation of statements: + MOVER AREG, =’0’ + MOVEM AREG, B + MOVEM AREG, B+1 + MOVEM AREG, B+2 Input: #include<stdio.h> #define xyz 5 #define asd 10 #define pf printf #define sf scanf void main(void) { pf("Enter a no:"); sf("%d",&a); b=asd+xyz; pf("xyz=%d",b); } Output: The old program #include<stdio.h> #define xyz 5 #define asd 10 #define pf printf #define sf scanf void main(void) {
  • 5. System Programming Sunita M. Dol, CSE Dept Walchand Institute of Technology, Solapur Page 5 pf("Enter a no:"); sf("%d",&a); b=asd+xyz; pf("xyz=%d",b); } Macro Expansion xyz 5 asd 10 pf printf sf scanf The new program #include<stdio.h> void main(void) { printf("Enter a no:"); scanf("%d",&a); b=10+5; printf("xyz=%d",b); } Conclusion: A macro is a unit of specification for program generation through expansion. A simple macro is implemented in C-language