Macro Processor Macro Definition and call Macro Expansion

  • Slides: 58
Download presentation
� Macro Processor: � Macro Definition and call, � Macro Expansion, � Nested Macro

� Macro Processor: � Macro Definition and call, � Macro Expansion, � Nested Macro Calls and definition, � Advanced Macro Facilities, � Design of Macro Processor Sytem Programming

Introduction �A macro is a facility for extending a programming language. � A macro

Introduction �A macro is a facility for extending a programming language. � A macro represents a commonly used group of statements in the source programming language � Each time this name Occurs in a program, the sequence of codes is substituted at that point. ◦ It allows the programmer to write shorthand version of a program. 2

Example | | | DATA DC 5 ADD 1, DATA ADD 2, DATA ADD

Example | | | DATA DC 5 ADD 1, DATA ADD 2, DATA ADD 3, DATA | | | ADD 1, DATA ADD 2, DATA ADD 3, DATA | |

Macro � 3 main step of macro 1. Deifine the macro name 2. Write

Macro � 3 main step of macro 1. Deifine the macro name 2. Write its definition 3. Use the macro name from with in the program anywhere its definition � Macro Definition ◦ Two new assembler directives �MACRO �MEND

Macro Instruction with parameter Syntax Start of definition……………. . MACRO Macro name with parameter………………

Macro Instruction with parameter Syntax Start of definition……………. . MACRO Macro name with parameter……………… Sequence of instruction…………. . { End of definition………………. . . MEND Example: MACRO INCR &ARG ADD AREG, &ARG MEND 5

Types of Macro Expansion � 1. Lexical Expansion � 2. Semiantic Expansion 1. Lexical

Types of Macro Expansion � 1. Lexical Expansion � 2. Semiantic Expansion 1. Lexical Expansion: Character string is replaced by another character string in the generation of program. All formal parameter is replaced by actual parameter. 2. Semiantic Expansion : Instruction as per requirement of specific usage are generated.

� MACRO: is the first line of the definition & identifies the following line

� MACRO: is the first line of the definition & identifies the following line as the macro instruction name. � The definition is terminated by with MEND statement � Macro call : macro processor replaces each macro call with macro instruction.

Example Source program | MACRO INCR ADD 1, DATA ADD 2, DATA ADD 3,

Example Source program | MACRO INCR ADD 1, DATA ADD 2, DATA ADD 3, DATA MEND | INCR | | | DATA DC 5 Expanded Source ADD 1, DATA ADD 2, DATA

Difference between macro and subroutine: � Difference between macro and subroutine: 1. a macro

Difference between macro and subroutine: � Difference between macro and subroutine: 1. a macro call is an instruction to replace the macro name with its body, whereas subroutine call is an instruction to transfer the program’s control to the subroutine’s definition with all paraneters, if required. 2. A macro call results in macro expansion, whereas subroutine call results in execution. 3. Macro expansion increases the size of the program but subroutine execution doesn’t affect the size of the program 4. Macro expansion doesn’t affect the execution speed of the program much in comparison to subroutines affect the execution speed of the program

3/3/2021 16

3/3/2021 16

3/3/2021 17

3/3/2021 17

3/3/2021 18

3/3/2021 18

3/3/2021 19

3/3/2021 19

3/3/2021 20

3/3/2021 20

Example: MACRO INCR &X, &Y, &REG=AREG MOVER &REG, &X ADD &REG, &Y MOVEM &REG,

Example: MACRO INCR &X, &Y, &REG=AREG MOVER &REG, &X ADD &REG, &Y MOVEM &REG, &X MEND MACRO DECR &X, &Y, &REG=BREG MOVER &REG, &X SUB &REG, &Y MOVEM &REG, &X MEND

START 100 READ N 1 READ N 2 INCR N 1, N 2, REG=CREG

START 100 READ N 1 READ N 2 INCR N 1, N 2, REG=CREG DECR N 1, N 2 STOP N 1 DS N 2 DS END 1 1

3/3/2021 33

3/3/2021 33

3/3/2021 34

3/3/2021 34

3/3/2021 36

3/3/2021 36

3/3/2021 37

3/3/2021 37

Advanced Macro Facilities � Advanced Macro Facilities are basically used to enhance the semantic

Advanced Macro Facilities � Advanced Macro Facilities are basically used to enhance the semantic macro Expansion

� No operation is carried out by an ANOP instruction.

� No operation is carried out by an ANOP instruction.

� No operation is carried out by an ANOP instruction.

� No operation is carried out by an ANOP instruction.

� The length of A is 1.

� The length of A is 1.

� Used to create number of similar statement.

� Used to create number of similar statement.

�Semantic � Semantic Expansion: expansion is the generation of instruction as per requirements of

�Semantic � Semantic Expansion: expansion is the generation of instruction as per requirements of a specific usage

MACRO ). THRICE. ONCE . TWICE . THRICE . FINAL MEND VARY AIF AIF

MACRO ). THRICE. ONCE . TWICE . THRICE . FINAL MEND VARY AIF AIF AGO MOVER ADD ADD AGO &COUNT , &ARG 1 (&COUNT. EQ. 1). ONCE (&COUNT. EQ. 2). TWICE (&COUNT. EQ. 3. FINAL AREG, X AREG, &ARG 1. FINAL MOVER AREG, X AREG, &ARG 1. FINAL

Expansion time Variable � These variables are declared as local variables: LCL <&var>[, <&var>……]

Expansion time Variable � These variables are declared as local variables: LCL <&var>[, <&var>……] EV can be manipulated using SET statement <EV> SET <Expr>

MACRO CLEAR MOVEM MOVEM MEND &ARG AREG=‘ 0’ AREG, &ARG+1 AREG, &ARG+2 AREG, &ARG+3

MACRO CLEAR MOVEM MOVEM MEND &ARG AREG=‘ 0’ AREG, &ARG+1 AREG, &ARG+2 AREG, &ARG+3

&M. MORE &M MACRO CLEAR &ARG, &N LCL &M SET 0 MOVER AREG, =‘

&M. MORE &M MACRO CLEAR &ARG, &N LCL &M SET 0 MOVER AREG, =‘ 0’ MOVEM AREG, &ARG+&M SET &M+1 AIF(&M. NE. &N). MORE MEND

MOVER MOVEM AREG, =‘ 0’ AREG, A……………. M=0 AREG, A+1 ………. . M=1 AREG,

MOVER MOVEM AREG, =‘ 0’ AREG, A……………. M=0 AREG, A+1 ………. . M=1 AREG, A+2 ………. . M=2 AREG, A+3 ………. . M=3