Macro and Macro Processor 1242022 Nikita chaudhari Introduction

  • Slides: 62
Download presentation
Macro and Macro Processor 1/24/2022 Nikita chaudhari

Macro and Macro Processor 1/24/2022 Nikita chaudhari

Introduction • A macro instruction (macro) is a notational convenience for the programmer •

Introduction • A macro instruction (macro) is a notational convenience for the programmer • It allows the programmer to write shorthand version of a program (module programming) • The macro processor replaces each macro instruction with the corresponding group of source language statements (expanding) • Normally, it performs no analysis of the text it handles. • It does not concern the meaning of the involved statements during macro expansion. • The design of a macro processor generally is machine independent! 1/24/2022 Nikita chaudhari

1/24/2022 Nikita chaudhari

1/24/2022 Nikita chaudhari

 • The language processor replaces a macro call statement by a sequence of

• The language processor replaces a macro call statement by a sequence of assembly language statements that are generated by using the body of code in the macro definition. This process is called macro expansion. 1/24/2022 Nikita chaudhari

Macro Definition A macro definition in program defines either a new operation or a

Macro Definition A macro definition in program defines either a new operation or a new method of declaring data A macro is a unit of specification for program generation through expansion. • A macro consist of a name, a set of formal parameters and a body of code. The use of a macro name with a set of actual parameters is replaced by some code generated from its body. This is called macro expansion • Two kinds of expansion can be readily identified 1. lexical expansion 2. Semantic expansion 1/24/2022 Nikita chaudhari

Lexical Substitution • It implies replacement of a character string by another character string

Lexical Substitution • It implies replacement of a character string by another character string during program generation. • Lexical substitution is typically employed to replace occurrences of formal parameters by corresponding actual parameters. 1/24/2022 Nikita chaudhari

Semantic expansion • It implies generation of statements that are tailored to the requirement

Semantic expansion • It implies generation of statements that are tailored to the requirement of a specific macro call. 1/24/2022 Nikita chaudhari

Macro expansion • Lexical expansion 1. Implies replacement of character string by another character

Macro expansion • Lexical expansion 1. Implies replacement of character string by another character string during program generation. 2. Typically employed to replace occurrences of formal parameters by corresponding actual parameters. 1/24/2022 • Semantic expansion 1. Implies generation of instructions tailored to the requirements of a specific usage. Eg> generation of specific instruction for manipulation of byte and word operands. 2. Is characterized by fact that different uses of a macro can lead to codes which differ in number, sequences and opcode of instructions. Nikita chaudhari

Differences between Macro and Subroutine • After macro processing, the expanded file can be

Differences between Macro and Subroutine • After macro processing, the expanded file can be used as input to the assembler. • The statements generated from the macro expansions will be assembled exactly as though they had been written directly by the programmer. • The differences between macro invocation and subroutine call – The statements that form the body of the macro are generated each time a macro is expanded. – Statements in a subroutine appear only once, regardless of how many times the subroutine is called. 1/24/2022 Nikita chaudhari

Basic macro processor functions • Two new assembler directives are used in macro definition

Basic macro processor functions • Two new assembler directives are used in macro definition MACRO: identify the beginning of a macro definition MEND: identify the end of a macro definition • Prototype for the macro Each parameter begins with ‘&’ name MACRO parameters : body : MEND • Body: the statements that will be generated as the expansion of the macro 1/24/2022 Nikita chaudhari

Macro definition can have 3 kinds of statements 1. Macro prototype : declares the

Macro definition can have 3 kinds of statements 1. Macro prototype : declares the name of the macro and the names and kinds of its formal parameters. 2. A model statement is a statement from which an assembly language statement may be generated during macro expansion 3. A macro preprocessor statement is used to perform auxiliary function during macro expansion. 1/24/2022 Nikita chaudhari

Macro prototype syntax: <macro name> [<formal parameter specification>[, ……. ]] ü <macro name> appears

Macro prototype syntax: <macro name> [<formal parameter specification>[, ……. ]] ü <macro name> appears in mnemonic field of an assembly language ü formal parameter appears in operand field formal parameter is of form &<parameter name> [<parameter kind> ] ü parameter can be either positional or keyword parameter. ü If parameter kind is omitted then by default it is assumed that it is positional type 1/24/2022 Nikita chaudhari

MACRO macro header statement INCR_M &MEM_VAL, &INCR_VAL, &REG prototype statement MOVER &REG , &MEM_VAL

MACRO macro header statement INCR_M &MEM_VAL, &INCR_VAL, &REG prototype statement MOVER &REG , &MEM_VAL ADD &REG, INCR_VAL MOVEM &REG , &MEM_VAL MEND macro end statement 1/24/2022 Nikita chaudhari

 • Macro call syntax: <macro name> [<actual parameter specification>[, …. . ]] ü

• Macro call syntax: <macro name> [<actual parameter specification>[, …. . ]] ü macro name : appears in mnemonic field ü Actual parameter: appears in operand fields ü <actual parameter specification> resembles < operand_specifications> 1/24/2022 Nikita chaudhari

Macro Expansion 1/24/2022 Nikita chaudhari

Macro Expansion 1/24/2022 Nikita chaudhari

Macro call 1/24/2022 Nikita chaudhari

Macro call 1/24/2022 Nikita chaudhari

 • Each macro invocation statement will be expanded into the statements that form

• Each macro invocation statement will be expanded into the statements that form the body of the macro. • Arguments from the macro invocation are substituted for the parameters in the macro prototype. • The arguments and parameters are associated with one another according to their positions. • The first argument in the macro invocation corresponds to the first parameter in the macro prototype, etc 1/24/2022 Nikita chaudhari

Macro expansion • Performed by two kinds of language processors 1. Macro assembler :

Macro expansion • Performed by two kinds of language processors 1. Macro assembler : performs expansion of each macro call in a program into a sequence of assembly statements - assembles the resulting assembly program. 2. macro preprocessor: performs expansion of macro calls in a program. It does not process assembly statements. - produces assembly program 1/24/2022 Nikita chaudhari

 • We assume that the macro preprocessor would help the programmer in distinguishing

• We assume that the macro preprocessor would help the programmer in distinguishing between original statements of a program and the statements generated during macro expansion by printing the symbol ‘+’ before the label field of each generated statement. 1/24/2022 Nikita chaudhari

Macro expansion • Each macro invocation statement will be expanded into the statements that

Macro expansion • Each macro invocation statement will be expanded into the statements that form the body of the macro. • Arguments from the macro invocation are substituted for the parameters in the macro prototype (according to their positions). – In the definition of macro: parameter – In the macro invocation: argument • Comment lines within the macro body will be deleted. • Macro invocation statement itself has been included as a comment line. • The label on the macro invocation statement has been retained as a label on the first statement generated in the macro expansion. ØWe can use a macro. Nikita instruction in exactly the same way 1/24/2022 chaudhari as an assembler language mnemonic

Macro expansion • 2 key notations used 1. Flow of control during expansion 2.

Macro expansion • 2 key notations used 1. Flow of control during expansion 2. Lexical substitution 1/24/2022 Nikita chaudhari

1. Flow of control during expansion • Determine the order in which model statement

1. Flow of control during expansion • Determine the order in which model statement in the macro’s definition would be visited for expansion of a macro call. • Default flow of control during expansion is sequential • In absence of preprocessor statements, model statements are visited sequentially starting prototype statement to MEND • Preprocessor statements can alter the flow of control. • Some model statements are either never visited during expansion or are repeatedly visited. • The former results in conditional expansion -> some model statement are not expanded for some of macro calls and latter in expansion time loop -> some model statements may be expanded more that once. 1/24/2022 Nikita chaudhari

Macro expansion Algorithm 1. MEC : = statement no. of 1 st model statement

Macro expansion Algorithm 1. MEC : = statement no. of 1 st model statement following the prototype statement in the definition of the called macro. 2. While (MEC!=MEND) a) if model statement then i) expand the statement through lexical substitution ii) MEC: = MEC+1; b) else (i. e the statement is a preprocessor statement) MEC: = value specified in the preprocessor statement; 3. Exit from macro expansion 1/24/2022 Nikita chaudhari

2. Lexical substitution • A model statement consist of 3 types of strings. 1.

2. Lexical substitution • A model statement consist of 3 types of strings. 1. an ordinary string , any string other than a string of type 2 or type 3 2. The name of a formal parameter which is preceded by the character ‘&’ 3. The name of preprocessor variable , which is also preceded by the character’&’ Ordinary string - which stands for itself - string in a model statement is retained in its original form. • A name of a formal parameter or preprocessor variables appearing in the model statement is replaced by the ‘value’. • The value of a formal parameter string used in macro call, where the rules of correspondence depends on what kind a parameter is • The value of the formal parameter is corresponding actual parameter string. 1/24/2022 Nikita chaudhari

Position parameter • Syntax &<parameter spec. > eg> & SAMPLE where SAMPLE name of

Position parameter • Syntax &<parameter spec. > eg> & SAMPLE where SAMPLE name of parameter <parameter kind> is omitted Position parameter is simply < ordinary string> In call on macro using positional parameters, the <actual parameter specifications> is an ordinary string Rules of positional association : Used to determine the value of positional formal parameters. 1. Find the ordinal position of XYZ in the list of formal parameters in the macro prototype statement. 2. Find the actual parameter specifications occupying the same ordinal position in the list of actual parameters in the macro call statement list. If it is an ordinary string ABC, the value of formal parameter XYZ would be ABC 1/24/2022 Nikita chaudhari

Example • Consider the call INCR A, B, AREG On the macro INCR following

Example • Consider the call INCR A, B, AREG On the macro INCR following rules of positional association, value of the formal parameters are : formal parameter value MEM_VAL A INCR_VAL B REG AREG lexical substitution(expansion) of the model statements + MOVER AREG, A + ADD AREG, B + MOVEM AREG, A 1/24/2022 Nikita chaudhari

KEYWORD Parameters For keyword parameters , the specification <parameter kind> is the string ‘=‘

KEYWORD Parameters For keyword parameters , the specification <parameter kind> is the string ‘=‘ <actual parameter specification> is written as <formal parameter name > =< ordinary string> The value of formal parameter XYZ is determined by rules of keyword association as follows: 1. Find the actual parameter specification which has the formal XYZ= <ordinary string> 2. Let <ordinary string> in the specification be the string ABC. Then the value of the formal parameter XYZ is ABC. XYZ= ABC very useful in situations where long list of parameter have to be used. It is immaterial in the list of actual parameter. 1/24/2022 Nikita chaudhari

A Macro Definition Using Keyword Parameters MACRO INCR_M MOVER ADD MOVEM MEND 1/24/2022 &MEM_VAL=

A Macro Definition Using Keyword Parameters MACRO INCR_M MOVER ADD MOVEM MEND 1/24/2022 &MEM_VAL= , &INCR_VAL=, &REG= &REG , &MEM_VAL &REG , &INCR_VAL= &REG , &MEM_VAL Nikita chaudhari

Example Macro INCR_M rewritten as macro INCR_M using keyword parameters INCR_M MEM_VAL=A, INCR_VAL=B, REG=AREG

Example Macro INCR_M rewritten as macro INCR_M using keyword parameters INCR_M MEM_VAL=A, INCR_VAL=B, REG=AREG ………………. INCR_M MEM_VAL=B, INCR_VAL=AREG, REG=A Are now equivalent. MACRO INCR_M &MEM_VAL=, &INCR_VAL=, &REG= MOVER &REG , &MEM_VAL ADD &REG, INCR_VAL MOVEM &REG , &MEM_VAL MEND 1/24/2022 Nikita chaudhari

Default specification of parameters • Default specification useful in situation where a parameter has

Default specification of parameters • Default specification useful in situation where a parameter has the same value in most call on macro, this value can be specified as its default value in the macro definition itself. • Default is a standard assumption in the absence of an explicit specification by the programmer. This way programmer can specify a value of the parameter only when it differs from its default value specified in macro definition. • When desired value is different from the default value , explicitly mention in macro call. • Overrides the default value of parameter for the duration of the call. &<parameter name>[<parameter kind>[default value>]] 1/24/2022 Nikita chaudhari

Example • AREG is used for arithmetic operation • Macro INCR_M contain &REG= AREG

Example • AREG is used for arithmetic operation • Macro INCR_M contain &REG= AREG INCR_D MEM_VAL=A, INCR_VAL=B INCR_D INCR_VAL=B , MEM_VAL=A INCR_D INCR_VAL=A , MEM_VAL=B , REG=BREG ------REDEFINED DEFAULT VALUE MACRO INCR_M MOVER ADD MOVEM MEND 1/24/2022 &MEM_VAL=, &INCR_VAL=, &REG= AREG &REG , &MEM_VAL &REG, INCR_VAL &REG , &MEM_VAL Nikita chaudhari

Mixed parameter list • Macro may be defined to use both positional and keyword

Mixed parameter list • Macro may be defined to use both positional and keyword parameters. • In such a case all positional parameters must precede all keyword parameters. • Eg> SUMUP A, B, G=20, H=X • A, B are positional parameters while G and H are keyword parameters. • Correspondence between actual and formal parameters is established by applying the rules governing the positional & keyword parameters separately. 1/24/2022 Nikita chaudhari

Other use of parameters • Up till now in all examples formal parameters used

Other use of parameters • Up till now in all examples formal parameters used in operand field. But use of parameters are not restricted to these fields. • Formal parameters can also appears in label and opcode fields of model statement 1/24/2022 Nikita chaudhari

Example MACRO CALC &X, &Y , &OP=MULT, &LAB MOVER AREG, &X &OP AREG, &Y

Example MACRO CALC &X, &Y , &OP=MULT, &LAB MOVER AREG, &X &OP AREG, &Y MOVEM AREG, &X MEND Expansion of the call CALC A, B, LAB=LOOP lead to the following code: +LOOP MOVER AREG, A + MULT AREG, B + MOVEM AREG, A 1/24/2022 Nikita chaudhari

NESTED MACRO CALL • A model statement in a macro may constitute a call

NESTED MACRO CALL • A model statement in a macro may constitute a call on another macro. Such a calls are known as Nested macro call • The macro containing the nested call as the outer macro • Called macro as the inner macro • Expansion of the nested macro call follows the last in first out (LIFO) rule • Thus, in a structure of nested macro call , expansion of the latest macro call(i. e. the innermost call in the structure) is complete first. 1/24/2022 Nikita chaudhari

Example nested macro call Nested macro call MACRO COMPUTE MOVEM INCR_D MOVER MEND 1/24/2022

Example nested macro call Nested macro call MACRO COMPUTE MOVEM INCR_D MOVER MEND 1/24/2022 &FIRST, &SECOND BREG, TMP &FIRST, &SECOND, REG=BREG, TMP Nikita chaudhari

Expanded code for a nested macro COMPUTE X, Y + MOVEM BREG, TMP [1]

Expanded code for a nested macro COMPUTE X, Y + MOVEM BREG, TMP [1] + INCR_D X, Y + MOVER BREG, TMP[5] 1/24/2022 Nikita chaudhari + MOVER BREG, X[2] + ADD BREG, Y[3] +MOVEM BREG, X[4]

+ + + 1/24/2022 MOVEM MOVER ADD MOVEM MOVER BREG , TMP BREG ,

+ + + 1/24/2022 MOVEM MOVER ADD MOVEM MOVER BREG , TMP BREG , X BREG, Y BREG, X BREG, TMP Nikita chaudhari

Advanced Macro Facilities Advanced macro facility are aimed at supporting semantic expansion. These facilities

Advanced Macro Facilities Advanced macro facility are aimed at supporting semantic expansion. These facilities can be grouped into 1. Facilities for alteration of flow of control during expansion. 2. Expansion time variables 3. Attributes of parameter 1/24/2022 Nikita chaudhari

Alteration of flow of control during expansion §Two features are provided to facilitate alteration

Alteration of flow of control during expansion §Two features are provided to facilitate alteration of flow of control during expansion 1. Expansion time sequencing symbols. 2. Expansion time statements AIF, AGO and ANOP A sequencing symbol(SS) has the syntax <ordinary string> §As SS is defined by putting it in the label filed of a statement in the macro body. §It is used as an operand in an AIF or AGO statement to designed the destination of an expansion time control transfer. §It never appears in the expanded form of a model statement 1/24/2022 Nikita chaudhari

Sequencing symbol syntax <ordinary string> • It appears in label field of a statement

Sequencing symbol syntax <ordinary string> • It appears in label field of a statement constitutes its definition • Its purpose is merely to identify a statement for expansion time control transfers, so it does not appear in the expanded code when that statement is expanded 1/24/2022 Nikita chaudhari

An AIF statement has the syntax • AIF (<expression>)<sequence symbol> • Where <expression> is

An AIF statement has the syntax • AIF (<expression>)<sequence symbol> • Where <expression> is relational expression involving ordinary strings , formal parameters and their attributes & expansion time variables. • If relational expression evaluates to true , expansion time control is transferred to the statement containing <sequence symbol> in its label field. • Otherwise the expansion time control flow is not altered. 1/24/2022 Nikita chaudhari

AGO statement has the syntax AGO <sequencing symbol> Unconditionally transfers expansion time control to

AGO statement has the syntax AGO <sequencing symbol> Unconditionally transfers expansion time control to the statement containing <sequencing symbol> in its label field. 1/24/2022 Nikita chaudhari

ANOP statement is written as <sequencing symbol> ANOP • Simply has the effect of

ANOP statement is written as <sequencing symbol> ANOP • Simply has the effect of defining the sequencing symbol. • It is inserted prior to a statement in the macro definition to which expansion time control flow is to be transferred if that statement has another symbol in its label field. • The ANOP does not appear in the expanded code of a macro call. 1/24/2022 Nikita chaudhari

Attributes of formal parameters • Attributes is written by using the syntax <attribute name>’<formal

Attributes of formal parameters • Attributes is written by using the syntax <attribute name>’<formal parameter spec> • Represents information about the value of the formal parameter i. e. about the corresponding actual parameter • The type, length and size attributes have the names T, L, S • Attributes can be used in preprocessor statement 1/24/2022 Nikita chaudhari

Attributes of formal parameters and <eg> for AIF . NEXT 1/24/2022 MACRO DCL_CONST &A

Attributes of formal parameters and <eg> for AIF . NEXT 1/24/2022 MACRO DCL_CONST &A AIF (L’&A EQ 1). NEXT ----------MEND Nikita chaudhari

Expansion time variable(EV) - Variables only used during the expansion of macro call -

Expansion time variable(EV) - Variables only used during the expansion of macro call - Accordingly , its value can be used only within a macro definition-in a preprocessor statement that assigns a value to an expression variable in a model statement, and in the every expression of and AIF statement. - A macro definition must contain the declaration of every expansion time variable that it uses. - Two types of expansion time variables exist 1. A local EV is created for use only during a particular macro call. 2. A global EV exists across all macro call situated in a program & used in any macro which has declaration for it. 1/24/2022 Nikita chaudhari

EV - Local & global EV are created using declaration syntax: LCL <EV specification>[,

EV - Local & global EV are created using declaration syntax: LCL <EV specification>[, <EV specification>. . ] GBL <EV specification>[, <EV specification>. . ] Where <EV specification> syntax : &<EV name>= ordinary string • Values of EV’s can be manipulated through the preprocessor statement SET. • SET syntax < EV specifications> SET <SET- expression> Ø < EV specifications> appears in label field Ø SET in mnemonic field. Ø Set statement assigns the value of SET-expression to EV specified in <EV specification> Ø The value of an EV can be used in any filed of model statement & in the expression of an AIF statement 1/24/2022 Nikita chaudhari

Example MACRO CONSTANTS LCL &A &A SET 1 DB &A &A SET &A+1 DB

Example MACRO CONSTANTS LCL &A &A SET 1 DB &A &A SET &A+1 DB &A MEND Call on CONSTANT macro: The local EV A is created. The 1 st SET assigns the value ‘ 1’ to it. The 1 st DB declares a byte constant ‘ 1’ The second SET assigns the value ‘ 2’ to A and 2 nd DB statement declares a constant ‘ 2’ 1/24/2022 Nikita chaudhari

Conditional expansion • Expansion time loop • Comparison with execution time loops 1/24/2022 Nikita

Conditional expansion • Expansion time loop • Comparison with execution time loops 1/24/2022 Nikita chaudhari

Conditional expansion • Means model statement is expanded only under specific conditions. • Achieved

Conditional expansion • Means model statement is expanded only under specific conditions. • Achieved by altering the flow of control during macro expansion by using AIF and AGO statements. • Conditional expansion is used to generate efficient code for a macro call by adapting to the actual parameters used in it. • Such code would not contain any redundancies and it would access and manipulate operands and efficient during execution of the program 1/24/2022 Nikita chaudhari

Conditional expansion of statements in macro definition A macro named EVAL is to be

Conditional expansion of statements in macro definition A macro named EVAL is to be defined such that the call EVAL A, B, C • Would generate efficient code to evaluate the expression A-B+C using AREG • When 1 st two parameters of a call are identical, EVAL should generate a single MOVER instruction to load the 3 rd parameter into AREG MACRO EVAL &X, &Y, &Z AIF (&Y EQ &X). ONLYMOVER AREG , &Y SUB AREG, &Y ADD AREG, &Z AGO. OVER. ONLYMOVER AREG , &Z. OVER MEND 1/24/2022 Nikita chaudhari

Conditional Expantion Conditional Macro Expansion IF ( Boolean expression ). . . ELSE. .

Conditional Expantion Conditional Macro Expansion IF ( Boolean expression ). . . ELSE. . . ENDIF -----WHILE ( Boolean expression ). . . ENDW 1/24/2022 Nikita chaudhari

EXPANSION TIME LOOP • An operation may be realized through repeated application of an

EXPANSION TIME LOOP • An operation may be realized through repeated application of an action. • A macro that stores zero in a 3 -bytes area of memory MACRO CLEAR &A MOVER AREG, =‘ 0’ MOVEM AREG, &A+1 MOVEM AREG, &A+2 MEND 1/24/2022 Nikita chaudhari

EXPANSION TIME LOOP Drawbacks • Writing of macro definition as shown in example is

EXPANSION TIME LOOP Drawbacks • Writing of macro definition as shown in example is cumbersome. • It lacks flexibility because different macros would have to be defined for clearing memory area of different size Drawbacks overcome by 1. Using expansion time loop which visits one or more model statements repeatedly during macro expansion. Expansion time loop are written using expansion time variables and the expansion time control transfer statements AIF and AGO 1/24/2022 Nikita chaudhari

A macro for clearing memory area of any size is written as follows MACRO

A macro for clearing memory area of any size is written as follows 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 expansion of the macro call CLEAR B, 3 The LCL statement declares M to be a local expansion time variable. SET initializes M=0 at the start of expansion call 1/24/2022 Nikita chaudhari

 • Thus macro call lead to generation of statements + MOVER AREG, =‘

• Thus macro call lead to generation of statements + MOVER AREG, =‘ 0’ + MOVEM AREG, B+1 + MOVEM AREG, B+2 1/24/2022 Nikita chaudhari

Other facility for expansion time loops • Many assembler provides this • An ELSE

Other facility for expansion time loops • Many assembler provides this • An ELSE clause in AIF being an obvious example. • Assembler M 68000 & Intel 8088 processor provides explicit constructs for writing expansion time loop. • Two such a facilities are 1. REPT 2. IRP 1/24/2022 Nikita chaudhari

REPT statement • REPT syntax REPT<expression> • Where <expression> should evaluate to a numerical

REPT statement • REPT syntax REPT<expression> • Where <expression> should evaluate to a numerical value during macro expansion. • The macro processor visits the statement between the REPT and first ENDM following it for expansion as many times as the value of <expression> 1/24/2022 Nikita chaudhari

Macro CONST 10 generates code to declare 10 constants with the value 1, 2….

Macro CONST 10 generates code to declare 10 constants with the value 1, 2…. 10 &M &M 1/24/2022 MACRO CONST 10 LCL SET REPT DC SETA ENDM MEND &M 1 10 ‘&M’ &M+1 Nikita chaudhari

IRP statement IRP <formal parameter>, <argument list> • formal parameter mentioned in the IRP

IRP statement IRP <formal parameter>, <argument list> • formal parameter mentioned in the IRP statement takes successive value from argument list • For each value the statements between the IRP and first ENDM following it are expanded once. MACRO CONSTS IRP DC ENDM MEND 1/24/2022 &M, &N, &Z &Z, &M, 7, &N ‘&Z’ Nikita chaudhari

Semantic expansion 1/24/2022 Nikita chaudhari

Semantic expansion 1/24/2022 Nikita chaudhari