47
1 4.2 Machine-Independent Macro Processor Features o Extensions to the basic macro processor functions n Concatenation of Macro Parameters n Generation of Unique Labels n Conditional Macro Expansion n Keyword Macro Parameters

4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

  • Upload
    others

  • View
    17

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

1

4.2 Machine-Independent Macro Processor Featureso Extensions to the basic macro processor

functionsn Concatenation of Macro Parameters

n Generation of Unique Labels

n Conditional Macro Expansion

n Keyword Macro Parameters

Page 2: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

2

4.2.1 Concatenation of Macro Parameterso Allow parameters to be concatenated with

other character stringso Suppose the parameter is named &ID, the

macro body may contain a statement:LDA X&ID1

n &ID is concatenated after the string “X” and before the string “1”.à LDA XA1 (&ID=A)à LDA XB1 (&ID=B)

Page 3: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

3

4.2.1 Concatenation of Macro Parameterso Problem: ambiguous situation

n E.g., X&ID1 may meano “X” + &ID + “1”o “X” + &ID1

n This problem occurs because the end of the parameter is not marked.

o Solution:n Use a special concatenation operator “->” to specify the

end of the parametern E.g. LDA X&ID→1

o Example: Figure 4.6

Beginning of the macro parameter is identified by &, the end of the

parameter is not marked.

Page 4: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

4

Concatenation of Macro Parameters (Fig. 4.6)

Page 5: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

5

4.2.2 Generation of Unique Labelso Labels in the macro body may have duplicate

labels problem n If the macro is invocated multiple times.n Use of relative addressing is very inconvenient,

error-prone, and difficult to read.o Example

n JEQ *-3n Inconvenient, error-prone, difficult to read

Page 6: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

6

4.2.2 Generation of Unique Labelso Generating unique labels within macro expansions

n Labels within the macro body begin with the character $n During macro invocation, $ will be replaced by $xx,

o xx is a two-character alphanumeric counter of the number of macro instructions expanded.

o Example: Figure 4.7n $LOOP TD =X’&INDEV’n 1st call:

o $AALOOP TD =X’F1’n 2nd call:

o $ABLOOP TD =X’F1’

Page 7: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

7

Generation of unique labels within macro expansion (fig. 4.7)

•Macro definition

Page 8: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

8

Generation of unique labels within macro expansion (fig. 4.7) (Cont.)RDBUFF F1, BUFFER, LENGTH •Macro expansion

Page 9: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

9

4.2.3 Conditional Macro Expansiono Arguments in macro invocation can be used

to:n Substitute the parameters in the macro body.

n Modify the sequence of statements in macro body for conditional macro expansion.o This capability adds greatly to the power and

flexibility of a macro language.

Page 10: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

10

4.2.3 Conditional Macro Expansiono Macro-time conditional statements

n Macro processor directives:o IF-ELSE-ENDIFo SET

n Example: Figure 4.8

o Macro-time variables (also called a set symbol)n Be used to store working values during the macro expansionn Any symbol that begins with the character & and is not a macro

parametern Be initialized to 0n Be changed with their values using SET

o &EORCK SET 1

Page 11: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

11

Boolean expression

Macro-time variable

IF-ELSE-ENDIF Structure

Page 12: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

12

Use of Macro-time Conditional Statements (Fig. 4.8) (Cont.)

RDBUFF F3, BUF, RECL, 04, 2048

Page 13: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

13

Use of Macro-time Conditional Statements (Fig. 4.8) (Cont.)

RDBUFF 0E, BUFFER, LENGTH, , 80

Page 14: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

14

Use of Macro-time Conditional Statements (Fig. 4.8) (Cont.)

RDBUFF F1, BUFF, RLENG, 04

Page 15: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

15

Conditional Macro Expansion (Cont.)o The testing of Boolean expression in IF statements

occurs at the time macros are expanded. n By the time the program is assembled, all such decisions

have been made.n There is only one sequence of source statements during

program execution.

o In contrast, the COMPR instruction tests data values during program execution. n The sequence of statements that are executed during

program execution may be different.

Page 16: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

16

Conditional Macro Expansion (Cont.)o Macro-time looping statement

n Macro processor directives:o WHILE-ENDW

n Example: Figure 4.9o Macro processor function

n %NITEMS: the number of members in an argument listo E.g. &EOR=(00,03,04)

=> %NITEMS(&EOR) is 3o Specify member in the list: &EOR[1]

Page 17: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

17

Use of Macro-time Looping Statements (Fig. 4.9)

Macro processor function

Macro-time looping statement

Page 18: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

18

Use of Macro-time Looping Statements (Fig. 4.9) (Cont.) A list of end-of-

record characters

Page 19: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

19

4.2.4 Keyword Macro Parameterso Positional parameters

n Parameters and arguments are associated according to their positions in the macro prototype and invocation.

n If an argument is to be omitted, a null argument (two consecutive commas) should be used to maintain the proper order in macro invocation:o E.g. RDBUFF 0E, BUFFER, LENGTH, , 80

n It is not suitable if a macro has a large number of parameters, and only a few of these are given values in a typical invocation.

Page 20: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

20

4.2.4 Keyword Macro Parameters (Cont.)o Keyword parameters

n Each argument value is written with a keyword that names the corresponding parameter.

n Arguments may appear in any order.o Null arguments no longer need to be used.

n E.g. o GENER TYPE=DIRECT, CHANNEL=3

n It is easier to read and much less error-prone than the positional method.

n E.g. Fig. 4.10

Page 21: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

21

Use of keyword parameters in macro instructions (Fig. 4.10)

Default values of parameters

Page 22: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

22

Use of keyword parameters in macro instructions (Fig. 4.10) (Cont.)

Page 23: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

23

Use of keyword parameters in macro instructions (Fig. 4.10) (Cont.)

Page 24: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

24

4.3 Macro Processors Design Optionso Recursive macro expansion

o General-purpose macro processors

o Macro processing within language translators

Page 25: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

25

4.3.1 Recursive Macro Expansiono Recursive macro expansion

n Macro invocations within macrosn Example: Figure 4.11

o Problems in previous macro processor design : n Values in ARGTAB were overwritten

o The procedure EXPAND would be called recursivelyo Thus the invocation arguments in the ARGTAB will be

overwritten.n Recursive call of the procedure EXPANDING

o The Boolean variable EXPANDING would be set to FALSE when the “inner” macro expansion is finished

o That is, the macro process would forget that it had been in the middle of expanding an “outer” macro.

Page 26: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

26

Example of nested macro invocation (Fig. 4.11)

RDCHAR is also a macro

Page 27: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

27

Example of nested macro invocation (Fig. 4.11) (Cont.)

Page 28: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

28

4.3.1 Recursive Macro Expansion (Cont.)o Solutions:

n Write the macro processor in a programming language that allows recursive callso Thus local variables will be retained.o Most high-level language have been supported recursive callso The compiler would be sure that previous values of any variables

declared within a procedure were saved when the procedure was called recursively

n Use a stack to take care of pushing and popping local variables and return addresses

Page 29: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

29

4.3.2 General-Purpose Macro processorso Three examples of actual macro processors:n A macro processor designed for use by assembler

language programmersn Used with a high-level programming languagen General-purpose macro processor

o Not tied to any particular languageo Can be used with a variety of different languages.

Source Code

(with macro)

Macro Processor

Expanded Code

Compiler /Assembler

Objectprogram

Page 30: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

30

General-Purpose Macro processors (Cont.)o General-purpose macro processors

n Advantageso Programmers do not need to learn many macro languages.o Overall saving in software development cost and software

maintenance effortn Difficulties:

o Large number of details must be dealt with in a real programming languagen Comment identifications ( //, /* */, …)n Grouping together terms, expressions, statements (begin_end,

{ }, …)n Tokens (keywords, operators)n …

Page 31: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

31

4.3.3 Macro processing within language translatorso Macro processors can be

n Preprocessorso Produce an expanded version of the source program, which is

then used as input to an assembler or compiler

n Line-by-line macro processoro Used as a sort of input routine for the assembler or compiler

n Read source programn Process macro definitions and expand macro invocationsn Pass output lines to the assembler or compiler

n Integrated macro processor

Page 32: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

32

4.3.3 Macro processing within language translatorso Preprocessors

o Combining macro processing functions with the language translator itself

Source Code

(with macro)

Macro Processor

Compiler /Assembler

Objectprogram

Source Code

(with macro)

Macro Processor

Expanded Code

Compiler /Assembler

Objectprogram

Page 33: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

33

Macro processing within language translators (Cont.)o Combining macro processing functions with

the language translator itselfn Line-by-line macro processor: n Integrated macro processor

n Advantages: share some data structures, functionsn Disadvantages: more complex

Page 34: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

34

Line-by-Line Macro Processoro Benefits

n It avoids making an extra pass over the source program.n Data structures required by the macro processor and the language

translator can be combinedo E.g., OPTAB and NAMTAB)

n Utility subroutines can be used by both macro processor and the language translator.o Scanning input lineso Searching tableso Data format conversion

n It is easier to give diagnostic messages related to the source statements.o i.e., the source statement error can be quickly identified without

need to backtrack the source

Page 35: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

35

Integrated Macro Processoro Integrate a macro processor with a language

translator (e.g., compiler)o Advantages

n An integrated macro processor can potentially make use of any information about the source program that is extracted by the language translator.

n An integrated macro processor can support macro instructions that depend upon the context in which they occur.

n Since the Macro Processor may recognize the meaning of source language

Page 36: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

36

Drawbacks of Line-by-line or Integrated Macro Processoro They must be specially designed and writtenn To work with a particular implementation of an

assembler or compiler.o The costs of macro processor development is

added to the costs of the language translatorn Which results in a more expensive software.

o The assembler or compiler will be considerably larger and more complex.

Page 37: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

37

4.4 Implementation Exampleso MASM Macro Processor

o ANSI C Macro Language

o The ELENA Macro Processor

Page 38: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

38

4.4.1 MASM Macro Processoro Macro processor in Microsoft MASM

assemblern Integrated with pass 1 of the assemblern Supports all of the main macro processor

functions discussed previouslyn Iteration statement:

o E.g. IRP S, <‘LEFT’,’DATA’,’RIGHT’>

Page 39: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

39

Examples of MASM macro and conditional statements (Fig. 4.12)

Page 40: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

40

Examples of MASM macro and conditional statements (Fig. 4.12) (Cont.)

Page 41: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

41

Examples of MASM iteration statements (Fig. 4.13)

Page 42: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

42

4.4.2 ANSI C Macro Languageo Definitions and invocations of macros are

handled by a preprocessor.n Simply makes string substitutions, without

considering the syntax of the Co Macro definition:n E.g. #define NULL 0n E.g. #define AB(X,Y) ( X > Y ? X – Y : Y – X )

o Macro invocationn E.g. AB(3,4)

Page 43: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

43

4.4.3 The ELENA Macro Processoro ELENA

n A research tool, not as a commercial software product.n Software: Practice and Experience, Vol. 14, pp. 519-531, Jun. 1984

o Macro definitions are composed of a header and a body.n header:

o a sequence of keywords and parameter markers (%)o at least one of the first two tokens in a macro header must be a keyword,

not a parameter markern body:

o the character & identifies a local labelo macro time instruction (.SET, .IF .JUMP, .E)o macro time variables or labels (.)

Page 44: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

44

Examples of ELENA macro definition and invocation (Fig. 4.14)

•Macro definition(header)•Macro definition(body)

•Macro invocation

•Macro expansion

Page 45: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

45

Examples of ELENA macro definition and invocation (Fig. 4.14) (Cont.)

•Macro definition(body)

•Macro invocation

•Macro expansion

Page 46: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

46

Examples of ELENA macro-time instructions (Fig. 4.15)

Page 47: 4.2 Machine-Independent Macro Processor Featuresosnet.cs.nchu.edu.tw/powpoint/SP93_2/Chapter 4-2.pdf · 10 4.2.3 Conditional Macro Expansion o Macro-time conditional statements n

47

The ELENA Macro Processor (Cont.)o Macro invocation

n There is no single token that constitutes the macro “name”n Constructing an index of all macro headers according to the keywords

in the first two tokens of the headern ELENA selects the header with the fewest parameters if there are two

or more matching headers with the same number of parameters, themost recently defined macro is selected.

n Examples:o Macro definition:

n ADD %1 TO %2n ADD %1 TO THE FIRST ELEMENT OF %2

o Macro invocation: n DISPLAY TABLE for DISPLAY %1 or %1 TABLEn A=B+1 for %1=%2+%3 or %1=%2+1