Cp0675 03 may-2012-rm04

971 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
971
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
62
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Cp0675 03 may-2012-rm04

  1. 1. Macro Processors Chapter 4 System SoftwareAn introduction to systems programming Leland L. Beck 1
  2. 2. Introductionq Concept » A macro instruction is a notational convenience for the programmer » It allows the programmer to write shorthand version of a program (module programming) » The macro processor replaces each macro invocation with the corresponding sequence of statements (expanding) 2
  3. 3. Macro Processorq Recognize macro definitionsq Save the macro definitionq Recognize macro callsq Expand macro calls Source Macro Expanded Compiler or obj Code(with macro) Processor Code Assembler 3
  4. 4. Macro Definitionq copy codeq parameter substitutionq conditional macro expansionq macro instruction defining macros 4
  5. 5. Copy code -- ExampleSource Expanded sourceSTRG MACRO . STA DATA1 . STB DATA2 . { STX DATA3 STA DATA1 MEND STB DATA2 . STX DATA3STRG . { . STA DATA1STRG STB DATA2 . STX DATA3 . . 5
  6. 6. Macro vs. Subroutineq Macro » the statement of expansion are generated each time the macro are invokedq Subroutine » the statement in a subroutine appears only once 6
  7. 7. Parameter Substitution -- ExampleSource Expanded souceSTRG MACRO &a1, &a2, &a3 . STA &a1 . STB &a2 . { STX &a3 STA DATA1 MEND STB DATA2 . STX DATA3STRG DATA1, DATA2, DATA3 . { . STA DATA4STRG DATA4, DATA5, DATA6 STB DATA5 . STX DATA6 . . 7
  8. 8. Parameter Substitutionq Dummy arguments » Positional argument STRG DATA1, DATA2, DATA3 GENER ,,DIRECT,,,,,,3 » Keyword argument STRG &a3=DATA1, &a2=DATA2, &a1=DATA3 GENER TYPE=DIRECT, CHANNEL=3q Example: Fig. 4.1, Fig. 4.2 » Labels are avoided in macro definition 8
  9. 9. One-Pass Macro Processorq Prerequisite » every macro must be defined before it is calledq Sub-procedures » macro definition: DEFINE » macro invocation: EXPAND NAMTAB MACRO DEFINE DEFTAB PROCESSLINE CALL EXPAND ARGTAB 9
  10. 10. Data Structures -- Global Variablesq DEFTABq NAMTABq ARGTAB EXPANDING 11
  11. 11. Nested Macros Definitionq Macro definition within macros » process macro definition during expansion timeq Example 4.3 12
  12. 12. Figure 4.3 (b) 13
  13. 13. One-Pass Macro Processor That Allows Nested Macro Definitionq Sub-procedures » macro definition: DEFINE » macro invocation: EXPANDq EXPAND may invoke DEFINE when encounter macro definition NAMTAB MACRO DEFINE DEFTAB PROCESSLINE CALL ARGTAB EXPAND Expanding MACRO Definition 14
  14. 14. 1-Pass Macro Processor D E F IN E M ACR O PR O C ESSO R G E T L IN EE X PA N D IN G = FA L SE P R O C E S S L IN EG E T L IN E EXPANDP R O C E S S L IN E E X PA N D IN G = T R U E G E T L IN E G E T L IN E P R O C E S S L IN E E X P A N D IN G FA L SE TRUE R EAD FRO M R EAD FR O M D EFTAB IN P U T 17
  15. 15. Comparison of Macro Processors Designq Single pass » every macro must be defined before it is called » one-pass processor can alternate between macro definition and macro expansion » nested macro definitions may be allowed but nested calls are notq Two pass algorithm » Pass1: Recognize macro definitions » Pass2: Recognize macro calls » nested macro definitions are not allowed 18
  16. 16. Concatenation of Macro Parametersq Pre-concatenation » LDA X&ID1q Post-concatenation » LDA X&ID→1q Example: Figure 4.6 19
  17. 17. Generation of Unique Labelsq Example » JEQ *-3 » inconvenient, error-prone, difficult to readq Example Figure 4.7 – $LOOP TD =X’&INDEV’ » 1st call: – $AALOOP TD =X’F1’ » 2nd call: – $ABLOOP TD =X’F1’ 20
  18. 18. RDBUFF F1, BUFFER, LENGTH
  19. 19. Conditional Macro Expansionq Macro-time conditional statements » Example: Figure 4.8 » IF-ELSE-ENDIFq Macro-time variables » any symbol that begins with the character & and that is not a macro parameter » macro-time variables are initialized to 0 » macro-time variables can be changed with their values using SET – &EORCK SET 1 23
  20. 20. RDBUFF F3, BUF, RECL, 04, 2048RDBUFF 0E, BUFFER, LENGTH, , 80
  21. 21. RDBUFF F1, BUFF, RLENG, 04
  22. 22. Conditional Macro Expansion (Cont.)q Macro-time looping statement » Example: Figure 4.9 » WHILE-ENDWq Macro processor function » %NITEMS: THE NUMBER OF MEMBERS IN AN ARGUMENT LIST 27
  23. 23. Nested Macro Invocationsq Macro invocations within macros » process macro invocation during expansion timeq Recursive macro expansion » Example: Figure 4.11 » Problems: – ARGTAB – EXPANDING » Solution – Recursive call – While loop with stack 28
  24. 24. ARGTAB DEFTAB MACRO Definition DEFINE NAMTAB GETLINEPROCESSLINE Macro Invocation EXPAND ARGTAB 29
  25. 25. 1-Pass Macro Processor D E F IN E M ACR O PR O C ESSO R G E T L IN EE X PA N D IN G = FA L SE P R O C E S S L IN EG E T L IN E EXPANDP R O C E S S L IN E E X PA N D IN G = T R U E G E T L IN E G E T L IN E P R O C E S S L IN E E X P A N D IN G FA L SE TRUE R EAD FRO M R EAD FR O M D EFTAB IN P U T 30
  26. 26. Allowing Nested Macro Invocation D E F IN E (f) M ACR O PR O C ESSO R G E T L IN E (f) P R O C E S S L IN E (f) G E T L IN E (0 ) P R O C E S S L IN E (0 ) EXPAND G E T L IN E (1 ) G E T L IN E (f) P R O C E S S L IN E (1 ) f FA L SE TRUE R EAD FRO M R EAD FR O M D EFTAB IN P U T 31
  27. 27. Macro-Assemblerq Advantage » reduce 1 pass » share same data structureq Disadvantage » more complex 32
  28. 28. Pass 1 READ Search Pass 2 R Type? (Pseudo-Op Table) Search NAMTAB MACRO Process (Macro Name Table) Define pseudo-ops Search R R (Machine Op Table) MACRO Process Expand machine instruction R R
  29. 29. General Purpose Macro Processorq ELENA » Software: Practice and Experience, Vol. 14, pp. 519-531, Jun. 1984q Macro definition » header: – a sequence of keywords and parameter markers (%) – at least one of the first two tokens in a macro header must be a keyword, not a parameter marker » body: – the character & identifies a local label – macro time instruction (.SET, .IF .JUMP, .E) – macro time variables or labels (.) 34
  30. 30. ELENA (cont.)q Macro invocation » There is no single token that constitutes the macro “name” » Constructing an index of all macro headers according to the keywords in the first two tokens of the header » Example – DEFINITION: q ADD %1 TO %2 q ADD %1 TO THE FIRST ELEMENT OF %2 – INVOCATION: DISPLAY %1 q DISPLAY TABLE %1 TABLE 35

×