Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
195 views
58 pages
Macros Notes
Macros notes
Uploaded by
Muskaan Kalra
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save Macros notes For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
195 views
58 pages
Macros Notes
Macros notes
Uploaded by
Muskaan Kalra
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save Macros notes For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 58
Search
Fullscreen
Maeros And Maero Processor 31 MACRO AND MACRO PROCESSOR © Macro instruction or macros are single line abbreviation for group of instructions. Macros are special code fragments that are defined once in the program and are used repetitively by calling them from various places within the program. © Thus, while implementing macros a programmer defines a single instruction to represent a block of code. For every occurrence of this one line macro instruction the macro processing assembler substitutes the entire block. i.e. whenever this macro instruction occurs in a program macro processor replaces that instruction with the code it represents. e The assembly language programmer often finds it necessary to repeat some block of code many times in a program. For example, a programmer wants to increment the value in a memory word by a constant for ten times at different parts of a program. Incrementing memory word by a constant involves following operations : 1. Move the value from the memory word into a machine register 2. Increment the value in machine register 3. Move the new value into the memory word. Now, rather than repeating the instruction sequence MOVE-ADD-MOVE for ten times in a program, it would be more convenient to define a macro named INCREMENT representing these three statement and to use this macro name in place of these statement.3.2 MACROS AND MACRO PROCESSOR Now, whenever the macro name INCREMENT will be encountered, it would be replaced by instruction sequence MOVE-ADD-MOVE. Thus, one macro instruction would be expanded into three assembly language instruction. In this way, the macros help in generating concise and compact code and save a lot memory space. Macro instructions are considered as an extension of the basic assembler language. A macro consists of a name, a set of formal parameters and a body of code. Thus a macro is a unit of specification for program generation through expansion. Macroprocessor Macro processor is a software that replaces each macro instruction with the corresponding group of source language statement that a macro represents. The function of macro processor is to substitute one group of statements for another at the time of compiling or assembling the program. Macro processor is viewed as an extension of basic assembler algorithm. The macro processor accepts an assembly language program containing macro definitions and calls and translates it into an assembly program which does not contain any macro definition or calls. The meaning of macro definition and call is explained in section 3.2. Fig. 3.1 shows the operation performed by macro processor . The output of macro processor is given to the assembler to obtain machine language form of the program. The design and capabilities of a macro processor may be influenced by the form of the statement of programming language. The design of macro processor is not directly related to the architecturciof the computer on which it is to run because macro processor is not usually concerned with the meaning of statements and their translation into machine language.MACROS AND MACRO PROCESSOR 3,3) Program Nee with macro lachine and call Source Program Target Program Program without macros Fig. 3.1 Operation by macro processor 3.2 MACRO DEFINITION, CALL AND EXPANSION Macro Definition e Macro definition represents the group of statements e A macro definition begins with an assembler directive called MACRO (see fig. 3.2) e After MACRO keyword, second line of macro definition is the macro prototype statement that represents name of macro and kinds of its parameters. ¢ The macro prototype statement has syntax :
[
] Here
appears in the mnemonic opcode field of assembly statement. The
is known as macro instruction argument. These arguments always ‘begin with ampersand (&) sign. This ampersand (&) symbol is macro language symbol rather than an assembly language symbol. Thus the syntax is : ‘macro name> &
3.4 MACROS AND MACRO PROCESSOR Following the macro name line, macro definition contains the sequence or group of instructions that are to be replaced by macro call. These statements form the body of the macro. The macro definition ends with the MEND (‘macro end’) keyword (see figure 3.2) Once the macro is defined, a programmer can use a macro name at various places in the program to represent the corresponding instruction sequence. Macro definitions are typically located at the start of a program Macro call A macro call is a statement that invokes the macro. MACRO tart of macro defi MEND End of macro definition ] << —_ macro name sequence of statements to be abbreviated Fig. 3.2. Format of macro definition A macro is called by writing the macro name in the mnemonic field of an assembly statement. The syntax for macro call is :
[
] Here, an actual parameter resembles an operand specification in an assembly language statement. The use of macro call is shown in figure 3.3 Macro expansion The process of replacing the macro call with the sequence of statements that a macro definition represents is known as macro expansion. During macro expansion, the actual parameters specified in macro call areMACROS AND MACRO PROCESSOR 3.5 substituted in place of formal or dummy arguments in macro definition. This type of macro expansion is known as lexical expansion. ¢ The body of macro definition usually consists of 3 types of strings : 1. An ordinary string 2. The formal parameter which is preceded by ampersand (&) 3. A preprocessor variable which is also preceded by amperes and (8) During lexical expansion strings of type 1 are retained without substitution, Strings of type 2 i.e. of formal parameters are replaced by the values of actual parameters. Strings of type 3 are also replaced by the values of preprocessor variables. ¢ The expansion of macro call is shown in figure 3.3 * The macro definition itself does not appear in the expanded source code. This definition is saved by the macro processor Example of Macro Let us consider an assembly language program where a group of statement is repeated number of times : ADD 1,DATA Add contents of DATA to register 1 ADD 2, DATA Add contents of DATA to register 2 ADD 3,DATA Add contents of DATA to register 3 ADD 1,DATA Add contents of DATA to register I ADD 2, DATA Add contents of DATA to register 2 ADD 3,DATA Add contents of DATA to register 3 DATA DC ‘3.6 In the above program three statements are repeated twice. Rather than repeating this sequence throughout the program we can create a macro definition and use it in our The use of macro definition and macro call for the above program is shown in figure program. 3.3 1, DATA 2, DATA 3, DATA j { i | DATA DC s macro definition J macro cait ] macro call MACROS AND MACRO PROCESSOR ADD ADD ADD ADD ADD ADD DATA DC 1, DATA 2, DATA 3, DATA 1, DATA 2, DATA 3, DATA In figure 3.3, INCR is the macro call. Whenever macro processes encounters INCR i.e. macro call, it replace it with the sequence of instruction present in the body of macro i.e. the following instructions ADD ADD ADD This process of replacing macro call by group of statements that a macro Fig. 3.3 Use of macros. 1, DATA 2, DATA 3, DATA represents is called expansion.MACROS AND MACRO PROCESSOR 3.7 3.3 MACROS AND SUBROUTINES A macro is similar to subroutines in the following manner ; 1. Both can be used to organize the program in a better way by separating out the frequently used fragment into a different block. 2. The main program can call macro or subroutine as and when needed. 3. Both of them can have an associated list of parameters or arguments. A macro is different from subroutine in the following manner : In case of a subroutine, during execution of the program, at each call to the subroutine, control branches to it and returns to the calling point at the end of executing the subroutine. This involves saving a lot of context information (such as values of arguments passed, local variables, CPU registers, return address etc.) into the stack. The context is restored when control returns after executing the subroutine. Macro processors work by simple code substitution. Moreover the substitution takes place at the time of compiling or assemblying the program. Each call to the macro is replaced by the body of the macro directly with parameters substituted by the arguments passed. Thus, once the expansion has taken place, there is no need to do context switching during execution. This saves a lot of time, if the macro is called quite often. To decide whether a code fragment is to be implemented as a subroutine or as a macro, the following points are noted : 1. Size The size of code fragment is an important factor In the case of a macro, each call is replaced by the code fragment. Thus, if the size of macro is significantly high, the resulting compiled or assembled Program becomes too big and this may result in slower execution of the program. On the other hand, if size of code fragment is small, it is better to implement it as macro because substitution of it will not increase the code size significantly. At the same time, the saving in context switching will result in faster execution of the program. 2. Number of Parameters Ifa subroutine contains a large number of parameters or arguments then it3.8 MACROS AND MACRO PROCESSOR 5 will require a stack to save these parameters at the time of calling a . Subroutine. This may require large stack area and may slow down execution. 3. Debugging ¢ Debugging may be easier with a subroutine since the execution may be suspended at the entry to the subroutine. * But, in case of macros, code substitution will change the source line number . and also setting break points at each of the substituted code fragments become cumbersome. 4. Recursion ¢ It cannot be implemented with a macro because the recursive calls are to be executed at the run-time and this is not possible at compile time 5. Parameter Passing * Macros support call-by-name strategy only. Since in the macro expansion, each reference to a parameter within the body is substituted by the argument passed from the corresponding macro call. > On the other hand, subroutines in most of programming languages support call-by-value and/or call-by-reference strategies, Comparison between macros and subroutine. S.No. | Macros Subroutines 1, The macro is an open subroutine that replaces given text when a macro definition being called for further processing. Subroutine is a program or source code that contains set of instruction units that are entitled to do a specific job. The macro calls are made during assembly process. The subroutine calls are made during run time state of a program. The macros are known only as open subroutines. The subroutines are also known as procedures, functions and module. When macro is called the control is not transferred, instead the macro definition replaces that call of macro in the program i.e. simple code substitution is performed. When subroutine is called the control is transferred to the definition of the function and after end of that called function, it returns back to main program.MACROS AND MACRO PROCESSOR 39 5. | Context switching is not performed | Context switching is performed as when a macro is called. Thus it does | the context (i.e. value of CPU not require any stack since there is | registers, local variables etc.) is to no call and returns in macro. be restored. Thus it requires stack to maintain call and return of function. 6. | The overhead in case of macros are | The overhead are more in comparatively less as compared to | subroutines due to number of calls subroutines. and returns, 7. | Debugging in case of macros is | Debugging is easier with subroutine. complex and difficult. 8. | Macros support only call by name | Subroutines support both call-by- strategy. value as well as call-by reference strategy. 9. | The macro represents commonly | The functions provide only modular used group of statements and used | approach where the problem can be only for replacing them. subdivided. 10. | Macros do not emphasize on called | The functions are _ basically and returned functions. emphasized on called and returned function. 11. | main() main () { { function () ; } } function() macro( ) { { print (“function”); } print (“macro”); }3.10 3.4 FEATURES OF MACRO FACILITY 3.4.1 Macro Instruction arguments MACROS AND MACRO PROCESSOR © Macros provide a facility for specifying arguments or parameters in the macro calls. « For all the parameters passed in macro call, there should be dummy arguments in macro definition. « As discussed earlier, the various arguments or parameters to be used with macros are specified in macro prototype i.e. name line. These arguments are preceded by the ampersand (&) sign. e Let us consider an assembly language program that performs same operations with two different operands : DATAI and DATA2. ‘These two operands are treated as argument or parameters. ADD ADD ADD DATAL DC DATA2 DC The macro definition for the operation on them (see fig. 3.4) 1,DATAL 2,DATA1 3, DATAI 1, DATA2 2, DATA2 3, DATA2 “= 10° above program contains a dummy argument or parameter (see figure 3.4) The two different operands : DATAI and DATA2 are passed as actual arguments in two separate macro call in order to perform theMACROS AND MACRO PROCESSOR 3.11 ~ ~~ Source Expanded Source MACRO INCR &Arg Macro INCR has ADD 1, &Arg only one argument ADD 2, &Arg ADD 3, & Arg MEND : INCR DATAL Use DATAI as ADD 1,DATAI : operand ADD 2,DATAL : ADD 3,DATAL INCR DATA? Use DATA2 as ADD 1,DATA2 : operand ADD 2,DATA2 : ADD 3,DATA2 DATAL : DATAL DCS’ DATA2 DC s DATA2 DC ‘10° DC 40° Fig. 3.4 Use of arguments in macro definition and macro call e As shown in figure 3.4, two separate macro calls : INCR DATAL INCR DATA2 are used in order to operate on two different operands. Each of these calls contains one argument i.e. actual argument and there is corresponding one dummy argument in macro definition also. This dummy argument accepts the value specified in the macro call. Specifying more than one argument in macro call © It is also possible to supply more than one argument in a macro call. When number of arguments are specified in macro call, each argument must correspond to a definition ie, dummy argument on the macro name line of3.12 MACROS AND MACRO PROCESSOR macro definition. When a macro call is processed, the arguments given in macro calls are replaced for the respective dummy argument in the macro definition. For example, consider the following problem : LOOP1 ADD 1, DATAL ADD 2, DATA2 ADD 3, DATA3 LOOP2 ADD 1, DATA3 ADD 2, DATA2 ADD 3, DATAI DATA1 Dc 3 DATA2 pc 10° DATA3 DC 15° In the above program the number of operands used is more as compared to previous example. Moreover the order of appearance of operands in two sequences is also different. Sequence 1 adds DATA to register 1 but sequence 2 adds DATA3 to register 1. In addition to this there is one additional argument called label argument ie. LOOP1 and LOOP2. The macro definition and its corresponding expanded sourge code for the above example is shown in figure 3.5 o Source oe Expanded Source ~ MACRO &LAB INCR & Argl, & Arg2, &Arg3 &LAB ADD 1, & Argi ADD 2, & Arg? ADD 3, & Arg3MACROS AND MACRO PROCESSOR 3.13 MEND os LOOP1 INCR DATAI,DATA2,DATA3 |LOOP1 ADD 1,DATAL : ADD 2, DATA2 : ADD 3, DATA3 LOOP2 INCR DATA3,DATA2,DATA1 |LOOP2 ADD 1, DATA3 : ADD 2, DATA2 : ADD 3, DATAL DATAL DC ‘5 DATAL DC ‘5? DATA2 DC ‘10° DATA2 DC ‘10’ DATA3 DC ‘15° DATA3 DC ‘15’ Fig. 3.5 Use of multiple arguments ¢ The macro shown in figure 3.5 contains 4 arguments. The argument shown as &LAB is the label argument. This label argument is usually specified in the label field of statement. Here three dummy arguments are used in macro definition namely Argl, Arg2 and Arg3 to represent three operands DATAI, DATA2 and DATA3. Types of parameters or arguments There are three kinds of parameters used in macros : 1. Positional arguments or parameters 2. Keyword arguments or parameters 3. Default arguments or parameters 1. Positional arguments © Positional arguments are the parameters that are matched with dummy arguments in macro prototype according to their position i.e. according to the order in which they appear in the macro name line. ¢ A positional formal parameters is written in macro definition in the form : &
3.14 MACROS AND MACRO PROCESSOR © The actual parameter in a macro call is simply an ordinary string (see figure 3.6) © While using positional parameters, the programmer must be careful to specify the arguments in the proper order. ¢ Fig. 3.6, shows the use of positional parameters. The dummy arguments in macro definition and actual parameters in macro call are matched in the order they appear i.e. according to their positions. dummy agruments &Argt, &Arg2, &Arg3, LABEL © —» macro prototype 1, 8Argt 2, &Arg2 3, &Arg3 DATA2, DATA3, DATA1, LOOP2 =—* macro call Senn arene nee eee Actual arguments Formal/dummy parameter | __ Actual parameter Argl DATA2 Arg? DATA3 Arg3 DATAL LABEL. LOOP2 Fig. 3.6 Positional parameter Thus DATA2 replace first argument, DATA3 replaces second, DATAI replaces third and LOOP? replaces fourth argument. © Ifan argument is to be omitted, the macro call must contain a null argument in order to maintain the correct argument positions. Such a null argument can be represented by using two consecutive commas.MACROS AND MACRO PROCESSOR 3.15 For example, consider a macro call : INCR DATA2, DATA3, DATAI, LOOP2 If we do not want to supply second and third argument ie. DATA3 and DATAI then we can omit it by specifying consecutive commas to provide null argument. In this case macro call statement might look like : . INCR — DATA2,,, LOOP2 Thus three consecutive commas represent two null arguments. 2. Keyword arguments In keyword parameters, each argument value is written with a keyword that names the corresponding parameter ie. It allows reference to dummy arguments by name as well as by position. In keyword parameters, the dummy argument in macro prototype (i.e. macro name line) is specified as :
For example, &Argl = and &Arg2 = The actual parameter in macro call is specified as :
=
For example, Arg] = DATAI, Arg2 = DATA2. In keyword parameter, actual arguments (in macro call) are not matched with dummy argument (in macro definition) in the sequence in which they are written. Arguments may appear in any order. Any arguments that are not supplied in macro call are presumed blank by the macro processor. The keyword arguments offer certain advantages over positional arguments : 1. The statement containing keyword argument is easier to read. 2. Itis less error prone. Fig. 3.7 shows the usage of keyword arguments. Here, value is not supplied for argument 3 i.e. Arg3 so it will automatically accept null value3.16 MACROS AND MACRO PROCESSOR MACRO INCR &Argi =, &Arg? =, &Arg3 =, &LABEL = &LABEL ADD 1, &Argl ADD 2, &Arg2 ADD 3, &Arg3 MEND INCR = Argl = DATA2, Arg2 = DATA3, LABEL = LOOP2 Formal argument Actual argument &Argl DATA2 &Arg2 DATA3 &Arg3 NULL Label LOOP2 Fig. 3.7 Keyword argument 3. Default Parameters Macros also provide the facility to specify default values for parameters. e The parameters have default values in macro definition are called default parameter. e These default parameters get their default value if its name does not appear in macro call i.e. if the value is not explicitly specified in macro call. e Default parameter has following syntax : &
=
© Default parameter is useful in situations where a parameter has the same value in most calls. © However a programmer can override the default value by explicitly specifying a value for a parameter in macro call. In this way the default value of parameter is not used and the new value supplied by macro call is used.MACROS AND MACRO PROCESSOR 3.17 * Fig. 3.8 shows the usage of default parameters. This macro use four parameters. Here, the default values are supplied for two parameters in macro definition. These values are provided for Arg! and Arg3 (i.e argument 1 and argument 3). The first macro call : INCR Arg2 = DATA2, Label = LOOP1 supplies values for two parameters and the remaining two parameters get their default value specified in macro definition MACRO INCR = &Argi = DATAI, &Arg2 =, &Arg3 = DATA, &Labe! &Label ADD 1, &Argl ADD 2, &Arg? ADD 3, &Arg3 MEND INCR Arg? = DATA2, Label = LOOP INCR —Argi = DATA2, Arg? = DATA2, Arg3 = DATA3, Label * =LoopP2 Fig. 3.8 Default Parameters The second macro call : INCR Arg] = DATA2, Arg2 = DATA2, Arg3 = DATA3, Label = LOOP2 supplies values for all four parameters. Now the default values of argument 1 and 3 (i.e. Arg and Arg3) are overridden by new values supplied in the macro call.3.18 MACROS AND MACRO PROCESSOR Comparison between positional parameters and keyword parameters. Positional Parameters Keyword Parameter 1. Positional parameters are the parameters in which arguments in macro call are matched with the argument in macro definition according to their position i.e. in the order they appear. 1. Keyword parameters are the parameters in which arguments in macro call are matched with dummy arguments by the names and well as the position. 2. The actual argument in macro call is simply an ordinary string for example : INCR DATAI, DATA2, DATA3 The actual argument in macro call is specified by using name of the argument and its supplied value. For example : INCR Argl = DATAI, Arg2 = DATA2, Arg3 = DATA3 3. Proper ordering of actual argument in macro call is mandatory 3. The actual arguments in macro call can be specified in any order 4. Statement containing positional argument is not easier to read and is error prone. 4. Statement containing keyword argument is easier to read and is less error prone. 3.4.2 Conditional Macro Expansion © Conditional macro expansion means macro calls produce different sequence of statements depending upon the arguments supplied by the macro calls. © Different set of arguments satisfy different conditions and produces different set of instruction for various calls. ¢ Inconditional macro expansion we can alter the flow of control during macro expansion. © Thus conditional macro expansion is defined as conditional selection of the machine instruction that appear in expansions of a macro call. * Inorder to implement condition expansion we make use of 1. Expansion time sequencing symbols. 2. Expansion time statements like AIF, AGO and ANOP. 1, Expansion time sequencing symbol ¢ A sequencing symbol (SS) is usually a label that is used in macro definition .MACROS AND MACRO PROCESSOR 3.19 Such symbol is used as an operand in an AIF or AGO statement (discussed shortly) to specify the place where the control will be transferred during the expansion of macro. i.e. it is a label where jump will be made during expansion. © Sequencing symbol (SS) is usually specified in the label field of a statement in the macro body. © The syntax for specifying sequencing symbol is :
¢ Such a sequencing symbol never appears in the expanded form of source code. 2. Expansion Time Statements These statements are directives to macro processor and control the sequence in which the macro processor expands the statements in macro instructions. The various expansion time statements are : (a) AIF ¢ AIF is a conditional branch statement. It is similar to ‘If statement’ of C language. © It performs arithmetic test and branches only if the tested condition is true. e AnAIF statement has the following syntax : AIF(
)
Here
is a relational expression that may include ordinary strings, formal parameters etc. e If the relational expression is true, the control is then transferred to the statement containing
in its label field. (b) AGO © AGO is unconditional branch statement. It is similar to ‘GOTO statement’ of C language. It specifies the sequencing symbol (label) appearing on some other statement in the macro instruction definition. ¢ The AGO statement has the following syntax : ieee AGO
~ .3.20 MACROS AND MACRO PROCESSOR e Thus, an AGO statement unconditionally i.e. (without checking some condition) transfers the control to the statement condition sequencing symbol in the label field. (©) ANOP ¢ ANOP statement is used to define the sequencing symbol. © ANOP statement has the following syntax : ‘
ANOP Examples of Conditional macro expansion Example I. This example shows a macro named EVAL that uses three parameters. This macro generates the code to evaluate A-B+C in register AREG for the macro call : EVAL ABC. n the first two parameters of a call are same then EVAL generates a single OVER instruction to load the 3 parameter into AREG. MACRO EVAL &X, &Y, &Z AIF (&Y EQ &X), .ONLY If first and second operand is same then goto statement containing the label ONLY MOVER AREG , &X SUB AREG, &Y ADD AREG, &Z AGO -OVER ONLY MOVER AREG, &Z .OVER MEND In the above code, if first and second parameter (operand) is same then the control is transferred to the statement containing label ONLY. In this case only third parameter will be moved to register A. As we know that a number subtracted from itself is always zero i.e. X-X = 0 so there will be no fun in moving first operand into AREG and then subtracting the same operand from AREG.MACROS AND MACRO PROCESSOR: 3.21 If first and second operand is not same then the MOVE-SUB-ADD sequence is generated and control is transferred to the statement. OVER MEND that terminates the expansion. Example 2 This example shows the use of macro VARY that uses five arguments out of which one is label argument. This macro generates the code to add three different values to three registers AREG, BREG. and CREG depending upon the number of arguments/ parameters given in macro call. If only one argument is given then that single operand is added to register A (AREG). If two arguments are passed in macro call then 1* operand is added to AREG and second to BREG. MACRO &ARGO VARY &COUNT, &ARG1, &ARG2, SARG3 &ARGO ADD AREG, &ARGI AIF (&COUNT EQ 1) .FINI If count =1 then terminate expansion ADD BREG, &ARG2 AIF (&COUNT EQ 2) .FINI If count = 2 then terminate expansion ADD CREG, &ARG3 |-FINI MEND ‘Xpanded source LOOP1 VARY 3, DATA1, DATA2, DATA3: LOOP1 ADD AREG, DATA1 ¥ ADD BREG DATA2 : ADD CREG, DATA3 [LOOP2 VARY 2, DATA3, DATA2 ,OOP2 ADD AREG, DATA3 3 ADD BREG, DATA2 ILOOP3) = VARY 1,DATA1 ‘LOOP3 ADD AREG, DATA13.22 MACROS AND MACRO PROCESSOR 3.4.3 Expansion Time Variables ¢ Expansion time variables (EV’s) are variables which can only be used during the expansion of macro calls. ¢ EV can be local or global ¢ A local EV is created for use only during a particular macro call. « A global EV exists across all macro calls situated in a program and can be used in any macro which has a declaration for it. * Local EV can be created using LCL statement and global EV can be created using GBL statement e > The syntax for LCL and GBL statements are : - LCL
GBL
¢
has syntax : &
Here EV name can be any string. e Values of EV’s can be manipulate using a preprocessor statement called SET e The syntax for SET statement is : SEV specification> SET
e Here,
appears in the label field and SET in the mnemonic field. Example of expansion time variables This example uses a macro named CONSTANTS. &A MACRO CONSTANTS LCL &A SET 1 DB &AMACROS AND MACRO PROCESSOR 3.23 &A SET &At1 DB &A MEND When this macro is called a local EV A is created. The first SET statement assigns the value ‘1’ to it. The first DB statement thus declares a byte constant “1” The second SET statement assigns the value ‘2’ to A and the second DB statement declares a constant ‘2’. 3.4.4 Expansion time loops © When a certain set of statements are repeated number of times during macro expansion, it forms expansion time loop. ¢ Example of expansion time loops : MACRO CLEAR &A MOVER AREG, = ‘0” MOVEM AREG, &A MOVEM AREG, &A+1 MOVEM AREG, &A+2 MEND When the above written macro is called as CLEAR B, the MOVER statement puts the value ‘0° in AREG. The three MOVEM statements store this value in 3 consecutive bytes with the addresses B, B+1 and B+2. 3.4.5 Macro calls within macro ¢ Amacro call to one macro can be used as an instruction in the definition of another macro i.e. a body of a macro can contain a macro call of another macro as one of its instructions. © Consider the following program. Here, one macro called ADD1 is defined first (This macro adds 1 to an operand) Then there is another macro called ADDS. This macro contains the calls of macro-ADDI as instructions in its definition.3.24 MACROS AND MACRO PROCESSOR MACRO ADDI &Arg Macro } LOAD AREG, &Arg ADDI | ADD AREG, = ‘1’ STORE AREG, &Arg MEND MACRO ADDS GArgl, &Arg?, &Arg 3 Macro | ADDI &Argl = Call to macro -ADD1 ADDS | ADDI &Arg2 > Call to macro -ADD1 ADDI &Arg3 > Call to macro-ADD1 MEND Figure 3.9 shows how macro calls within another macro are handled and how the expanded source code is obtained. Expanded Source Source : Expanded Source Level 2 ‘LEVEL 1 MACRO ADDI &Arg LOAD AREG, &Arg ADD AREG, ="1° STORE —AREG, &Arg MEND MACRO ADDS —&Argl, &Arg2, &Arg3 ADDI &Argl ADDI &Arg? ADDI —- &Arg3 MEND Expansion of ADDS | Expansion of ADDIMACROS AND MACRO PROCESSOR 3.25 ADDS DATAI, DATA2, (ADD|_ DATAI OAD AREG, DATA] DATA3. ADD AREG, = ‘1’ STORE AREG, DATAI ADDI DATA2 OAD AREG, DATA2 ADD STORE AREG, DATA2 ADDI DATA3 LOAD — AREG, DATA3 ADD AREG, = ‘1° STORE AREG, DATA3 DATAI DC #St DATA2 DC “10° DATA3 DC S15? Fig. 3.9 Implementation of macro call within a macro As shown in figure 3.9, macro call within a macro involve several levels. When a macro—ADDS is called with 3 parameters, its expansion creates three new calls to macro-ADD1 (Level 1) These three instructions of ADDS are further expanded (Level 2). 3.4.6 Concatenation of Macro parameters © Macro processors also allows the actual parameters (specified in macro call) to be concatenated (i.e. attached) with other character strings. * For example, suppose that a program contains one series of variables named by the symbols XA1, XA2, XA3 ..., another series named by XB1, XB2, XB3....If similar processing is to be performed on each series of variables then we need not to repeat certain strings portions : X and numbers 1, 2, 3.... The only thing that needs to be changed is variable A, B, C.....In this way we just need to specify only variable portion A, B, ...in macro call. These variables are to be attached with symbol X and number 1, 2, 3... ¢ The dummy argument in macro body will be represented by : &
3.26 MACROS AND MACRO PROCESSOR ¢ In order to perform concatenation some macro processors use special symbol ~. This symbol is known as concatenation operator. ¢ Figure 3.10 shows an example of a macro named SUM that make use of a concatenation operator > SUM A LDA XAl : ADD XA2 : ADD XA3 A STA XA SUM ALPHA LDA XALPHAIL ADD XALPHA2 ADD XALPHA3 STA XALPHA Fig. 3.10 Concatenation of macro parameters As shown in figure 3.10, the parameter to macro -SUM is named-&Arg, Within the body of macro the parameter &Arg is concatenated after the character string X and before the character string 1, 2, 3... ¢ The concatenation operator does not appear in the macro expansion because the macro processor deletes all the occurrences of the concatenation operator immediately after performing parameter substitution (see figure 3.10) 3.5 DESIGN OF €@ MACRO PROCESSOR The main function of macro processor is to recognize and process macro definitions and macro call.MACROS AND MACRO PROCESSOR 3.27 There are four basic tasks that any macro instruction processor must perform : 3.5. 1. Recognize macro definitions. A macro processor must recognize macro definitions identified by the MACRO and MEND keywords. 2. Save the definition. The processor must store the macro instruction definitions. These definitions are later used for expanding the macro calls. 3. Recognize the macro calls. The macro processor must recognize the macro calls that appear as operation mnemonics. 4, Expand calls and substitute arguments. The macro processor must substitute dummy arguments (present in macro definitions) with the actual arguments specified in the macro call. 1. Two Pass Macro Processor Two pass macro processor make two systematic scans over the source program The first pass searches for macro definitions and the second pass searches macro calls and perform substitution of arguments. These two passes are required because the macro processor cannot expand a macro call before having found and saved the corresponding macro definition. Thus two passes are required : one to handle definitions and other to handle calls. The first pass examines every operation code and saves all macro definitions in a database called Macro Definition Table (MDT) and saves a copy of source program minus macro definition on secondary storage device (such as magnetic tape or disk) for use in the second pass. The first pass also prepares another database called Macro Name Table (MNT) that contains the names of all the macros present in the program. In addition to containing names, MNT also contains pointer to the entry in the MDT where the macro definition starts. The second pass examines every operation mnemonics and replaces each macro name (i.e. call) with appropriate text from the macro definitions. Databases used by Pass 1 of macro processor The pass I of two pass macro processor uses following databases : 1. The source program that contains macros. It acts as input to macro processor.3.28 MACROS AND MACRO PROCESSOR . The output macro source program copy. It acts as input to pass-2 » The Macro Definition Table (MDT). It is used to store the body of the macro definitions. . The Macro Name Table (MNT). It is used to store the names of macros defined in source program. . The Macro Definition Table Counter (MDTC). It is used to indicate the next entry available in the MDT. . The Macro Name Table Counter (MNTC). It is used to indicate the next entry of macro available in the MNT. . The Argument List Array (ALA). It is used to substitute index markers for dummy arguments before storing a macro definition. Databases used by Pass-2 of Macro Processor The pass-2 of a two pass macro processor uses following databases : i 2, ay 4. >: The copy of the input macro source program. . The output expanded source program to be used as input to the assembler. The Macro Definition Table (MDT), created by Pass 1 - The Macro Name Table (MNT), created by Pass 1 . The Macro Definition Table Pointer (MDTP). It is used to indicate the next line of text to be used during macro expansion. . The Argument List Array (ALA). Ii is used to substitute macro call arguments for the index markers in he stored macro definition. Macro Definition Table (MDT) MDT is a table of text lines. It is used to store the definitions of the various macros used in the source program Each entry in MDT can of maximum 80 bytes. Every line of macro definition except the keyword MACRO is stored in MDT. The keyword MEND is kept in MDT in order to indicate the end of the definition.MACROS AND MACRO PROCESSOR 3.29 MACRO DEFINITION TABLE (MDT) _ INDEX SOURCE CODE LINES (80 Bytes per entry) &LAB INCR SArgl, &Arg?, &Arg3 #0 ADD 1,41 | ADD 2, #2 ADD 3, #3 MEND Fig. 3.11 Macro Definition Table (MDT) The macro name line is also stored in MDT so as to facilitate keyword argument replacement. For example, considering the macro INCR of figure 3.5 the MDT will look like (see figure 3.11). Here MACRO keyword is not saved in MDT, the macro name line and all other lines till MEND are saved. “Macro Name Table (MNT) MNT is used to store the macro names and the MDT index number of all the macros used in the source program. Thus each entry in MNT consists of a character string specifying the macro name and index (pointer) to the entry in the MDT that corresponds to the beginning of the macro definition. The macro name part of entry can be maximum of 8 bytes and index value can be of 4 bytes,3.30 MACROS AND MACRO PROCESSOR ° For example, the MNT entry for the macro INCR will be (see figure 3.12) : ‘MACRO NAME TABLE (MNT) INDEX NAME MDT INDEX (8 bytes) (4 bytes) 4 “INCRbbbb” 20 Fig. 3.12 Macro Name Table (MNT) © The example shows the macro name INCR and its MDT index value 20 which means that the definition of macro INCR starts at index (line no.) 20 in MDT. Here bbbb represents blank bytes. Argument List Array (ALA) « ALA is used during both pass 1 and pass 2 of macro processor ¢ During Pass 1, the dummy arguments in the macro definition are replaced with positional indicators when the definition is stored in MDT. This is done to simply the task of argument replacement during macro expansion. e As shown in Fig. 3.11, when macro definition is stored in MDT, the arguments are replaced by positional indicators. Here Argl, Arg2 and Arg3 have been placed by #1, #2, and #3. # symbol is reserved for the use of the macro processor and is not available to programmer. Also label argument LAB has been replaced by #0. e During pass-2, ALA is again utilized in order to substitute arguments given in macro call in place of positional indicators used in macro definition. © Considering the example of fig. 3.5, when the following macro call will be encountered : LOOP! INCR DATAI1, DATA2, DATA3MACROS AND MACRO PROCESSOR 3.31 The macro processor would prepare an argument list array (ALA) as shown in fig. 3.13 ARGUMENT LIST ARRAY (ALA) INDEX ARGUMENT (8 bytes per entry) 0 “LOOP 16bb” 1 “DATAIbbb” 2 “DATA2bbb” a “DATA3bbb” Fig. 3.13 Argument List Array e Ifthe arguments in macro call are different then different ALA is prepared for different calls. ¢ For example, If a macro call is : INCR Arg] = DATA3, Arg? = DATA2, Arg3 = DATAIL e The macro processor will find Arg], Arg2 and Arg3 occupy argument position 1, 2 and 3 on the macro name line. It will then prepare ALA as shown in figure 3.14 « The arguments not represented in call are considered blank and extra argument supplied‘in call are ignored. ARGUMENT LIST ARRAY (ALA) INDEX "ARGUMENT 0 “bbbbbbbb” 1 “DATA3bbb” 2 “DATA2bbb” 3 DATAIbbb” Fig. 3.14 ALA332 MACROS AND MACRO PROCESSOR 3.5.1.1 Pass-1 of Macro Processor * The main purpose of pass-1 is to process and save the macro definitions. Flowchart for Pass-1 of Macro processor Pass 1 abies MOTC «1 MNTC 1 fan ie |b seeds eee ne eee ane sge eee source line MACRO SNS} write copy pseudo-op of source code line} 2 Yes END No pseudo-op > Read next source code fine T Yes Enter macro name Goot and current value PASS 2 of MDTC in MNT entry number MNTC. ¥ Readnext |, source line MNTC + MNTC +1 ‘Substitute index Prepare argument notation for list array arguments y Enter macro name * line into MOT line into Enter line into MDT WOT OTC 4 I MDTC — MDTC +1 + Yes“ MEND No pseudo-op 2MACROS AND MACRO PROCESSOR 3.33 For this pass-1 algorithm tests each input line. If the line contains MACRO pseudo-op, then the entire macro definition that it follows is saved in the next available locations in the Macro Definition Table (MDT). While saving the macro definition in MDT, a pointer called Macro Definition Table Pointer (MDTP) is incremented appropriately. The macro name line (i.e. the first line of macro definition) is then entered into the Macro Name Table (MNT) along with the pointer to the first location of the MDT entry of the definition. When the END assembler directive is encountered, all of the macro definitions have been processed so the control is transferred to pass 2 so as to Process macro calls. 3.5.1.2 Pass-2 of macro processor The main function of pass-2 is to recognize the macro calls present in source program and to perform the macro expansion The algorithm for pass-2 tests the operation mnemonics of each input line to see if it is a name in the MNT. When a call is found, the call processor sets a pointer Macro Definition Table Pointer (MDTP), to the corresponding macro definition stored in the MDT. The initial value of MDTP is obtained from the MNT. This value is present as “MDT index” field of MNT, corresponding to each macro name. The macro expander prepares the Argument List Array (ALA). This ALA consists of two columns : dummy argument indices (i.e. 0, 1, 2, 3, ..) and argument (i.e. specifying all the arguments present in macro call) (see fig. 3.13) As the various instructions lines are read from the MDT, the macro processor replaces all dummy argument indices present in macro definition by their actual values specified in macro call. This information is obtained from ALA. While reading instructions from MDT whenever macro processor encounters MEND in MDT it ends the macro expansion. When pass-2 encounters END assembler directive it passes the expanded source code to assembler for further processing.3.34 MACROS AND MACRO PROCESSOR Flow chart for Pass-2 of macro processor Pass 2 tL Read next source pa Code tine (copied by pass 1) J ‘Search MNT for match with operation code Macro name found 2 No) write ito expanded ‘source code file Yes + MOTP « MDT index END \ no from MNT entry Psousoop Sot up argument Yes list array ‘Supply expanded source file to assembler processing MDTP = MDTP+1 }*-———— Get line from MOT. ‘Substitute arguments from macro call ‘Write expanded source code 3.6 GENERAL PURPOSE MACRO PROCESSOR General purpose macro processors are the macro processor that can be used with a variety of different languages.MACROS AND MACRO PROCESSOR 35 ¢ They are not dependent on any particular programming language. © The general purpose macro processors provide some advantages over language specific macro processors. These advantages are : 1. The programmer does not need to learn about macro facilities for different languages. As a result much of the time and expense involved in training is eliminated. 2. Although the cost involved in producing a general purpose macro processor is greater than those for developing a language specific processor. But this expense does not need to be repeated for each language. As a result there is substantial overall saving in software development cost. 3. General purpose macro processors also incurs less maintenance cost. © Inspite of the advantages of general purpose macro processor there are relatively few general purpose macro processor. This is because of the large number of details that must be dealt with in a real programming language. ‘The general purpose macro processor, must provide some way for a user to define specific set of rules to be followed for wide variety of language. General purpose macro processor must incorporate ways to deal with construct, rules, syntax of different programming languages. Some of these issues are : 1. In a typical programming language, there are several situations in which normal macro parameter substitution should not occur. For example, comments should be ignored by a macro processor. However, each programming language has its own method for identifying comment. It will be difficult for macro processor to deal with all these methods. For example: (a) Language such as Pascal and C use special characters to mark the start and end of a comment such as double backslash (\\) or forward siash and asterik /*.. ....*/. (b) Language as FORTRAN uses a special symbol to flag an entire line as a comment. (c) In most assembler languages, any characters on a line following the end of the instruction operand field are automatically taken as comments. (di) Sometimes comments are recognized partly by their position in the source line.3.36 MACROS AND MACRO PROCESSOR 2. Different languages use different methods for grouping terms, expressions or statements together. A general purpose macro processor may need to take these grouping methods into account in scanning the source statements. For example : (a) Some languages such as Pascal use keywords such as begin and end for grouping statements. (b) Some languages use special character like curly braces {and }. 3. Different languages have different rules for the formation of tokens (identifiers, constants, operators and keywords) and constants. Moreover various languages also put different restrictions on the length of identifiers. For example. (a) Some languages use multiple character operators such as oF gin FORTRAN and := in pascal. Problem may arise if these are treated as two separate character rather than as a single operator. (b) Languages like COBOL and FORTRAN use special statement formatting rules. Also the macro processor must be concerned with whether or not blank spaces are significant. 4. As different programming languages have different syntax for statements, the problem may arise in specifying the syntax used for macro definitions and macro call statements. 3.7 MACRO PROCESSOR WITH LANGUAGE TRANSLATOR OR ASSEMBLER ¢ The macro processors that have been discussed to far are called preprocessors as they process macro definitions and expand macro calls before passing the source code to assembler i.e. preprocessor produces expanded form of source program and then it is used as input to assembler or compiler However, a macro processor can also be combined with language translator or assembler. * Macro processor can be combined with pass 1 of assembler. This will eliminate the overhead of intermediate: files. This integration of macro processor and assembler can be further improved by combining similar functions. For example, the Macro Name Table (MNT) can be combined with Mnemonics Table or POT. In this case, a flag in each entry indicates whether or not it is a Macro nameMACROS AND MACRO PROCESSOR 3.37 There are two different ways in which a macro processor can be combined with language translators. These are : 1. Line by Line macro processor 2. Integrated macroprocessor 1. Line by line macro processor It is the simplest technique to combine macro processor with assembler. In this approach, the macro processor reads the source program statements, performs all its function of saving macro definition, recognizing macro calls and expanding the macro calls and then passes these source program statements to language translator line by line i.e one line at a time. Thus, the output lines are passed to language translator as they are generated (one at a time), instead of being written to an expanded source file. The macro processor acts as a sort of input routine for the assembler or compiler. Inspite of this coordination the macro processor and language translator work independently. The main form of communication between the two is the passing of source statements from one to the other. Advantages of line by line macro processor 1. It does not make an extra pass over the source program i.e. it does not first write the expanded source program into a file and then read ‘it for assembler. As a result it is more efficient than macro processor. . Some of the data structures used by the macro processor and the language translator can be combined. For example MNT and OPTAB can be combined together. . There are certain common utility subroutines and functions (such as scanning input lines of source code, searching tables, and converting numeric values to internal representations), that can be used by both the macro processor and language translator. . Line by line macro processor also make it easier to give diagnostic messages related to the source statements that contain errors. 2. Integrated Macro Processor In integrated macro processor, there is more closer cooperation between the macro processor and the language translator. This is because of the fact that3.38 MACROS AND MACRO PROCESSOR an integrated macro processor can use any information about source program that has been extracted by the language translator. « Thus a macro processor can use the result of different operations that have been performed by the assembler such as scanning for symbols, constant etc. « The macro processor can use these results without being involved into the various details like rules for token formation, multiple character operators, continuation lines etc. « However, the degree of integration between a macro processor and an assembler varies from one system to another. Disadvantages of macro procssor with language translator ‘The various disadvantages of combining macro processor functions with that of a Janguage translator are : 1. They are designed and written to work with particular type of an assembler or compiler and not just with a particular programming language. 2. Such a software is usually expensive to develop because it involves the cost of creating both language translator as well as macro processor. 3. Combining macro processor with language translator increases its size. As a result, large size may be a problem if this software is to run on a computer with limited memory. SOLVED EXAMINATION QUESTIONS Q.1. What is a macro? Or « Q.1. What do we need macros ? Or Ans. Macro instruction or macros are single line abbreviations for group of instructions. Macros are special code fragments that are defined once in the program and are used repetitively by calling them from various places within the program. Thus, while implementing macros a programmer defines a single instruction to represent a block of code. For every occurrence of this one line macro instruction the macro processing assembler substitutes the entire block. i.e. whenever this macroMACROS AND MACRO PROCESSOR 3.39 instruction occurs in a program macro processor replaces that instruction with the code it represents, The assembly language programmer often finds it necessary to repeat some block of code many times in a program. For example, a programmer wants to increment the value in a memory word bya constant for ten times at different parts of a program. Incrementing memory word by a constant involves following operations : 1, Move the value from the memory word into a machine register 2. Increment the value in machine register 3. Move the new value into the memory word. Now, rather than repeating the instruction sequence MOVE-ADD-MOVE for ten times in a program, it would be more convenient to define a macro named INCREMENT representing these three statement and to use this macro name in place of these statement. Now, whenever the macro name INCREMENT will be encountered, it would be replaced by instruction sequence MOVE-ADD-MOVE. Thus, one macro instruction would be expanded into three assembly language instruction. In this way, the macros help in generating concise and compact code and save a lot memory space. Q.2.Define macro and macro processor. Ans. A macro is a unit of specification for program generation through expansion. Macros are single line abbreviations for group of instructions. Macros are special code fragments that are defined once in the program and are used repetitively by calling them from various places within the program. A macro consists of a name, a set of formal parameters and a body of code. Macro processor is software that replaces each macro instruction with the corresponding group of source language statement that a macro represents. The function of macro processor is to substitute one group of statements for another at the time of compiling or assembling the program. Macro processor is viewed as an extension of basic assembler algorithm. The macro processor accepts an assembly language program containing macro definitions and calls and translates it into an assembly program which does not contain any macro definition or calls.3.40 MACROS AND MACRO PROCESSOR Machine language ‘Source Program Target Program Program without macros Q.3. What is macro definition and macro call ? Or Q.3. Describe macro definition and macro call. Ans. Macro definition represents the group of statements A macro definition begins with an assembler directive called MACRO. After MACRO keyword, second line of macro definition is the macro prototype statement that represents name of macro and kinds of its parameters. The macro prototype statement has syntax :
[
] Here
appears in the mnemonic opcode field of assembly statement. The
is known as macro instruction argument. These arguments always begin with ampersand (&) sign. Txis ampersand (&) symbol is macro language symbol rather than an assembly language symbol. Thus the syntax is :
&
Following the macro name line, macro definition contains the sequence or group of instructions that are to be replaced by macro call. These statements form the body of the macro. The macro definition ends with the MEND (‘macro end’) keyword . Once the macro is defined, a programmer can use a macro name at various places in the program to represent the corresponding instruction sequence. Macro definitions are typically located at the start of a program.MACROS AND MACRO PROCESSOR 3.41 MACRO art mace defi tin ad — macro name sequence of statements to be abbreviated MEND A End of macro definition Format of macro definition Macro call A macro call is a statement that invokes the macro. A macro is called by writing the macro name in the mnemonic field of an assembly statement. The syntax for macro call is :
[
] Here, an actual parameter resembles an operand specification in an assembly language statement. ADD 1, DATA macro, sop aioe | eetiton am bam weno incr J] macro cat ‘oo 4,DATA , aa ao oan J cro cn INCR ADD 1, DATA moo bara ADD 3, DATA ou oc cater Use of macros3.42 MACROS AND MACRO PROCESSOR Q.4. What is macro expansion ? Ans. The process of replacing the macro call with the sequence of statements that a macro definition represents is known as macro expansion. During macro expansion, the actual parameters specified in macro call are substituted in place of formal or dummy arguments in macro definition. This type of macro expansion is known as lexical expansion. The body of macro definition usually consists of 3 types of strings : 1. An ordinary string 2. The formal parameter which is preceded by ampersand (&) 3. A preprocessor variable which is also preceded by ampersand (&) During lexical expansion strings of type 1 are retained without substitution. Strings of type 2 i.e. of formal parameters are replaced by the values of actual parameters. Strings of type 3 are also replaced by the values of preprocessor variables. The expansion of macro call is shown in figure below The macro definition itself does not appear in the expanded source code. This definition is saved by the macro processor 1,DATA macro ADD 2,DATA | definition 3, DATA |] macro ca ADD 1, DATA ADD 2, DATA ADD 3, DATA 1 ) J macro ca ADD 1, DATA ADD 2, DATA ADD 3, DATA DATA DC ‘sMACROS AND MACRO PROCESSOR 3.43 In the above figure, INCR is the macro call. Whenever macro processes encounters INCR i.e. macro call, it replace it with the sequence of instruction present in the body of macro i.e, the following instructions ADD 1, DATA ADD 2, DATA ADD 3, DATA This process of replacing macro call by group of statements that a macro represents is called expansion. Q.5. What is the difference between positional and keyword arguments ? Ans. Positional Parameters Keyword Parameter 1. Positional parameters the parameters in which arguments in macro call are matched with the argument in macro definition according to their position i.e. in the order they appear. are 1. Keyword parameters are the parameters in which arguments in macro call are matched with dummy arguments by the names and well as the position. 2. The actual argument in macro call is simply an ordinary string for example : INCR DATAI, DATA2, DATA3 The actual argument in macro call is specified by using name of the argument and its supplied value. For example : INCR Argl = DATAI, Arg2 = DATA2. Arg3 = DATA3 3. Proper ordering of actual argument in macro call is mandatory 3. The actual arguments in macro call can be specified in any order 4. Statement containing positional argument is not easier to read and is error prone. 4. Statement containing keyword argument is easier to read and is less error prone. Q.6. What are the two ways of specifying arguments to a macro call? Ans. The two ways of specifying arguments are: 1, Positional arguments 2. keyword arguments3.44 MACROS AND MACRO PROCESSOR Positional arguments Positional arguments are the parameters that are matched with dummy arguments in macro prototype according to their position ie. according to the order in which they appear in the macro name line. A positional formal parameters is written in macro definition in the form : &
The actual parameter in a macro call is simply an ordinary string. While using positional parameters, the programmer must be careful to specify the arguments in the proper order. Figure below shows the use of positional parameters. The dummy arguments in macro definition and actual parameters in macro call are matched in the order they appear i.e. according to their positions. dummy agruments Ganon aso ei &Argt, &Arg2, &Arg3, KLABEL © —> macro prototype 1, &Argi 2, &Arg2 3, &Arg3 &LABEL DATA2, DATA3, DATA1, LOOP2 = —> macro call jee eet Actual arguments 2 “Actual parameter DATA2 Arg? DATA3 a DATAl LABEL Loop2 Positional parameterMACROS AND MACRO PROCESSOR 3.45 Thus DATA2 replace first argument, DATA3 replaces second, DATA\ replaces third and LOOP? replaces fourth argument. Keyword arguments In keyword parameters, each argument value is written with a keyword that names the corresponding parameter i.e. It allows reference to dummy arguments by name as well as by position. In keyword parameters, the dummy argument in macro prototype (i.e. macro name line) is specified as :
For example, &Argl = and &Arg2 = The actual parameter in macro call is specified as :
=
For example, Arg] = DATA, Arg2 = DATA2. In keyword parameter, actual arguments (in macro call) are not matched with dummy argument (in macro definition) in the sequence in which they are written. Arguments may appear in any order. The figure below shows the usage of keyword arguments. Here, value is not supplied for argument 3 i.e. Arg3 so it will automatically accept null value MACRO INCR —- &Argi =, & Arg? =, &Arg3 =, &LABEL = &LABEL = ADD 1, &Argl ADD 2, &Arg? ADD 3, &Arg3 MEND INCR Argl = DATA2, Arg? = DATA3, LABEL = LOOP2 nt Actual argument DATA2 DATA3 NULL. Loop23.46 MACROS AND MACRO PROCESSOR Q.7. What is the advantage of using keyword parameter? Ans. In keyword parameter, actual arguments (in macro call) are not matched with dummy argument (in macro definition) in the sequence in which they are written. Arguments may appear in any order. The keyword arguments offer certain advantages over positional arguments : 1, The statement containing keyword argument is easier to read. 2. It is less error prone Q.8. Explain the concept of macro processor. Or Q.8. Explain the task performed by macro processor. Ans. The main function of macro processor is to recognize and process macro definitions and macro call. There are four basic tasks that any macro instruction processor must perform : 1. Recognize macro definitions. A macro processor must recognize macro definitions identified by the MACRO and MEND keywords. 2. Save the definition. The processor must store the macro instruction definitions. These definitions are later used for expanding the macro calls. 3. Recognize the macro calls. The macro processor must recognize the macro calls that appear as operation mnemonics. 4. Expand calls and substitute arguments. The macro processor must substitute dummy arguments (present in macro definitions) with the actual arguments specified in the macro call. Q.9. Explain the following with the help of an example. (a) Conditional assembly —_(b) Expansion time variables Ans. Conditional macro expansion means macro calls produce different sequence of statements depending upon the arguments supplied by the macro calls. Different set of arguments satisfy different conditions and produces different set of instruction for various calls. In conditional macro expansion we can alter the flow of control during macro expansion. Thus conditional macro expansion is defined as conditional selection of the machine The conditional statement is : AIF AIF is a conditional branch statement. It is similar to ‘If statement’ of C language.MACROS AND MACRO PROCESSOR 3.47 It performs arithmetic test and branches only if the tested condition is true, An AIF statement has the following syntax : AIF(
)
Here
is a relational expression that may include ordinary strings, formal parameters etc. If the relational expression is true, the control is then transferred to the statement containing
in its label field. Example of Conditional macro expansion This example shows a macro named EVAL that uses three parameters. This macro generates the code to evaluate A-B+C in register AREG for the macro call : EVAL A,B,C. When the first two parameters of a call are same then EVAL generates a single MOVER instruction to load the 3" parameter into AREG. MACRO, EVAL &X, BY, &Z AIF (&Y EQ &X), ONLY If first and second operand is same then goto statement containing the label ONLY MOVER AREG, &X suB AREG, &Y ADD AREG, &Z AGO LOVER ONLY MOVER AREG, &Z OVER MEND In the above code, if first and second parameter (operand) is same then the control is transferred to the statement containing label ONLY. In this case only third parameter will be moved to register A. As we know that a number subtracted from itself is always zero i.e. X-X = 0 so there will be no fun in moving first operand into AREG and then subtracting the same operand from AREG. If first and second operand is not same then the MOVE-SUB-ADD sequence is generated and control is transferred to the statement. OVER MEND that terminates the expansion. Expansion Time Variables Expansion time variables (EV’s) are variables which can only be used during the expansion of macro calls.3.48 MACROS AND MACRO PROCESSOR EV can be local or global. A local EV is created for use only during a particular macro call. A global EV exists across all macro calls situated in a program and can be used in any macro which has a declaration for it. Local EV can be created using LCL statement and global EV can be created using GBL statement The syntax for LCL and GBL statements are : LCL
GBL
has syntax : &
Here EV name can be any string. Values of EV’s can be manipulate using a preprocessor statement called SET The syntax for SET statement is :
SET
Here,
appears in the label field and SET in the mnemonic field. Example of expansion time variables This example uses a macro named CONSTANTS. MACRO. CONSTANTS LCL SA &A SET 1 DB&A &A SET &A+ When this macro is called a local EV A is created. The first SET statement assigns the value ‘1’ to it. The first DB statement thus declares a byte constant “1”. The second SET statement assigns the value ‘2’ to A and the second DB statement declares a constant ‘2’.MACROS AND MACRO PROCESSOR 3.49 Q.10. Explain the work of nested macro calls with the help of suitable code fragments. Or Q.10. How does a macro call within a macro expand ? Describe with an example. Or Q.10. Elaborate the concept of nested macros ? Ans. A macro call to one macro can be used as an instruction in the definition of another macro i.e. a body of a macro can contain a macro call of another macro as one of its instructions. Consider the following program. Here, one macro called ADD1 is defined first (This macro adds 1 to an operand) Then there is another macro called ADDS. This macro contains the calls of macro-ADD1 as instructions in its definition. MACRO ADDL S&Arg Macro LOAD AREG, &Arg ADDI ADD AREG, = ‘1’ STORE AREG, &Arg MEND MACRO ADDS SArgl, &Arg2, &Arg 3 Macro | ADDI &Argl > Call to macro -ADD1 ADDS: ADDI &Arg? > Call to macro -ADD1 ADDI &Arg3 > Call to macro -ADD1 MEND The figure below shows how macro calls within another macro are handled and how the expanded source code is obtained. Expanded Source Expanded Source Level 2 Leven1 — roe Source MACRO ADDI = &Arg3.50 MACROS AND MACRO PROCESSOR LOAD AREG, &Arg ADD AREG, ="1’ STORE AREG, &Arg MEND MACRO ADDS &Argl, KArg2, &Arg3 ADDI &Arg) ADDI = &Arg2 ADDI &Arg3 MEND Expansion of ADDS | Expansion of ADDI ADDS DATAI!, DATA2, ADD1 DATAI LOAD AREG, DATA DATA3 ADD AREG,= ‘I!’ STORE AREG, DATAI ADDI DATA2 (LOAD AREG, DATA2 ADD AREG, = ‘1’ LSTORE AREG, DATA2 ADDI DATA3 LOAD AREG, DATA3 ADD AREG, = ‘1’ . STORE AREG, DATA3 DATAI DC ‘S’ DATA2 DC ‘10° DATA3 DC ‘15° Implementation of macro call within a macro ‘As shown in figure, macro call within a macro involves several levels. When a macro-ADDS is called with 3 parameters, its expansion creates three new calls to macro-ADD1 (Level 1). These three instructions of ADDS are further expanded (Level 2).MACROS AND MACRO PROCESSOR 3.51 Q.11. Draw the flowchart of pass I and pass II of macro processor and describe their functions. Or Q.11. Write an algorithm for a two-pass macro-processor in which all macro definitions are processed in first pass and all macro invocations are expanded in the second pass. Ans. Two pass macro processor make two systematic scans over the source program The first pass searches for macro definitions and the second pass searches macro calls and perform substitution of arguments. These two passes are required because the macro processor cannot expand a macro call before having found and saved the corresponding macro definition. Thus two passes are required : one to handle definitions and other to handle calls. The first pass examines every operation code and saves all macro definitions in a database called Macro Definition Table (MDT) and saves a copy of source program minus macro definition on secondary storage device (such as magnetic tape or disk) for use in the second pass. The first pass also prepares another database called Macro Name Table (MNT) that contains the names of all the macros present in the program. In addition to containing names, MNT also contains pointer to the entry in the MDT where the macro definition starts. ‘The second pass examines every operation mnemonics and replaces each macro name (ie. call) with appropriate text from the macro definitions. Databases used by Pass 1 of macro processor The pass I of two pass macro processor uses following databases : 1. The source program that contains macros. It acts as input to macro processor. 2. The output macro source program copy. It acts as input to pass-2 3. The Macro Definition Table (MDT). It is used to store the body of the macro definitions. 4, The Macro Name Table (MNT). It is used to store the names of macros defined in source program. 5. The Macro Definition Table Counter (MDTC). It is used to indicate the next entry available in the MDT.352 MACROS AND MACRO PROCESSOR 6. The Macro Name Table Counter (MNTC). It is used to indicate the next entry of macro available in the MNT. 7. The Argument List Array (ALA). It is used to substitute index markers for dummy arguments before storing a macro definition. Databases used by Pass-2 of Macro Processor The pass-2 of a two pass macro processor uses following databases : 1. The copy of the input macro source program. 2. The output expanded source program to be used as input to the assembler. 3. The Macro Definition Table (MDT), created by Pass 1 4. The Macro Name Table (MNT), created by Pass 1 5. The Macro Definition Table Pointer (MDTP). It is used to indicate the next line of text to be used during macro expansion. 6. The Argument List Array (ALA). It is used to substitute macro call arguments for the index markers in he stored macro definition Yass-1 of Macro Processor © The main purpose of pass-I is to process and save the macro definitions. Forthis pass-1 algorithm tests each input line. If the line contains MACRO pseudo-op, then the entire macro definition that it follows is saved in the next available locations in the Macro Definition Table (MDT). «While saving the macro definition in MDT, a pointer called Macro Definition Table Pointer (MDTP) is incremented appropriately. «” The macro name line (i.e. the first line of macro definition)'is then entered into the Macro Name Table (MNT) along with the pointer to the first location _ of the MDT entry of the definition. When the END assembler directive is encountered, all of the macro definitions have been processed so the control is transferred to pass 2 so as to process macro calls. Pass-2 of macro processor The main function of pass-2 is to recognize the macro calls present in source program and to perform the macro expansion. © The algorithm for pass-2 tests the operation mnemonics of each input line to see if it is a name in the MNT.MACROS AND MACRO PROCESSOR 3.53 © When a call is found, the call processor sets a pointer Macro Definition Table Pointer (MDTP), to the corresponding macro definition stored in the MDT. ¢ The initial value of MDTP is obtained from the MNT. This value is present as “MDT index” field of MNT, corresponding to each macro name. Flowchart for Pass-1 of Macro processor Pass woTo +1 wnto<1 Road nea seurcu tne WACRO Wie coy oeudo-op > ot sures cote ine " END \_ no ona Read next "i 2 = cunso ode to ves Ede pare ene ae sare vas ea oruore m MNT nity number MNT t Read not I =e WNTO = MNT 1 ‘Substitute index Aaekmaoen stu nc ae T ‘arguments: Trier mare nae q Tent OT a eae NOT WOT = MOTO+1 T WOTG + MOTC™ 1 ‘Yes MEND No pseudo-op v3.54 MACROS AND MACRO PROCESSOR, « The macro expander prepares the Argument List Array (ALA). This ALA consists of two columns : dummy argument indices (ie. 0, 1, 2, 3, ..) and argument (i.e. specifying all the arguments present in macro call) (see fig. 3.13) As the various instructions lines are read from the MDT, the macro processor replaces all dummy argument indices present in macro definition by their actual values specified in thacro call. This information is obtained from ALA. Flow chart for Pass-2 of macro processor Pass 2 J Read next source Code fine (copied by pase 1) sae Ss ‘Search MNT for match wih operation code No] write into expanded! ‘source code fle Yos MDTP = MDT index from MNT entry = ‘Set up argument ist array MOTP
You might also like
Macroprocessor New
PDF
No ratings yet
Macroprocessor New
61 pages
Macro Processor - Class
PDF
No ratings yet
Macro Processor - Class
28 pages
System Software: Dr. Manish Khare Macro Processors
PDF
No ratings yet
System Software: Dr. Manish Khare Macro Processors
33 pages
MACRO and ASM-DIRECTIVES (Unit4spmacro-170906062957 PDF
PDF
No ratings yet
MACRO and ASM-DIRECTIVES (Unit4spmacro-170906062957 PDF
62 pages
Systems Programming Notes
PDF
No ratings yet
Systems Programming Notes
62 pages
Macro Module 3
PDF
No ratings yet
Macro Module 3
25 pages
Module 3
PDF
No ratings yet
Module 3
36 pages
Unit Iv
PDF
No ratings yet
Unit Iv
78 pages
Chapter 4 Macro Processors: - Basic Macro Processor Functions
PDF
No ratings yet
Chapter 4 Macro Processors: - Basic Macro Processor Functions
30 pages
Macro Processors: Basic Function Machine-Independent Features Design Options Implementation Examples
PDF
No ratings yet
Macro Processors: Basic Function Machine-Independent Features Design Options Implementation Examples
18 pages
Ch6 Macro Processors
PDF
No ratings yet
Ch6 Macro Processors
4 pages
System-Software Unit IV
PDF
100% (8)
System-Software Unit IV
9 pages
SP 11
PDF
No ratings yet
SP 11
50 pages
Macro Processors: Unit - Iv
PDF
No ratings yet
Macro Processors: Unit - Iv
87 pages
Macro Processor
PDF
100% (2)
Macro Processor
44 pages
Module 3
PDF
No ratings yet
Module 3
24 pages
Unit IV System Software
PDF
No ratings yet
Unit IV System Software
10 pages
UNIT IV Macro Processor
PDF
No ratings yet
UNIT IV Macro Processor
14 pages
Macro Processor Introduction Basic
PDF
No ratings yet
Macro Processor Introduction Basic
9 pages
Chapter - 3
PDF
100% (1)
Chapter - 3
46 pages
Unit-Ii Sab
PDF
No ratings yet
Unit-Ii Sab
73 pages
Basic Macro Processor Functions 1
PDF
50% (4)
Basic Macro Processor Functions 1
10 pages
Macro and Macro Preprocessor
PDF
0% (1)
Macro and Macro Preprocessor
72 pages
System Software Module 2
PDF
No ratings yet
System Software Module 2
7 pages
Macro Part1
PDF
No ratings yet
Macro Part1
32 pages
SPCC - 3
PDF
No ratings yet
SPCC - 3
10 pages
Macro
PDF
No ratings yet
Macro
7 pages
Unit-IV-Macro Processor - Sri Eshwar
PDF
100% (2)
Unit-IV-Macro Processor - Sri Eshwar
105 pages
Chapter 4
PDF
No ratings yet
Chapter 4
41 pages
Macro Macro Processor PDF
PDF
No ratings yet
Macro Macro Processor PDF
53 pages
UNIT3 CH-1 Macros
PDF
No ratings yet
UNIT3 CH-1 Macros
23 pages
Macro Processor
PDF
No ratings yet
Macro Processor
53 pages
Module 5
PDF
No ratings yet
Module 5
65 pages
Macro Preprocessor Part 1
PDF
No ratings yet
Macro Preprocessor Part 1
33 pages
MacroPPT Module3
PDF
No ratings yet
MacroPPT Module3
72 pages
Macros and Macro Processor
PDF
No ratings yet
Macros and Macro Processor
43 pages
9 Macros
PDF
No ratings yet
9 Macros
17 pages
Unit 2 Notes and PPTs PDF
PDF
No ratings yet
Unit 2 Notes and PPTs PDF
153 pages
Notes of System Programming
PDF
No ratings yet
Notes of System Programming
36 pages
SP - Unit 2 - Macro Peocessor - MRD
PDF
No ratings yet
SP - Unit 2 - Macro Peocessor - MRD
36 pages
Module 5
PDF
No ratings yet
Module 5
26 pages
SP 11
PDF
No ratings yet
SP 11
50 pages
Macro Preprocessor
PDF
No ratings yet
Macro Preprocessor
75 pages
Macro and Macroprocessors
PDF
No ratings yet
Macro and Macroprocessors
92 pages
Macroprocessors Donovan
PDF
No ratings yet
Macroprocessors Donovan
25 pages
Macro and Macro Processors
PDF
No ratings yet
Macro and Macro Processors
4 pages
Unit - 4 Macros and Macro Processors
PDF
No ratings yet
Unit - 4 Macros and Macro Processors
64 pages
Unit 3 Macros2
PDF
No ratings yet
Unit 3 Macros2
26 pages
ch4 Macro
PDF
No ratings yet
ch4 Macro
53 pages
3.macro Processor2016
PDF
No ratings yet
3.macro Processor2016
51 pages
System Software Module
PDF
No ratings yet
System Software Module
37 pages
SP Unit2 RNP
PDF
No ratings yet
SP Unit2 RNP
103 pages
SS Macro
PDF
No ratings yet
SS Macro
87 pages
Macro
PDF
No ratings yet
Macro
73 pages
23458study Materials For BCA 3rd Semester System Programming3
PDF
No ratings yet
23458study Materials For BCA 3rd Semester System Programming3
7 pages