Macro Processors Basic Functions Machine-Independent Features Design Options Implementation Examples.

Slides:



Advertisements
Similar presentations
COP 3402 Systems Programming
Advertisements

Intermediate Code Generation
Macro simple idea of textual substitution useful when you need a group of instructions or directives frequently.
The Assembly Language Level
Chapter 3 Loaders and Linkers
Chapter 3 Loaders and Linkers
Macro Processor.
The assembler is the system program that translate source code written in assembly language to object code( Machine Language) and other information for.
Machine-Independent Loader Features
Macro Processors (MP) Macro: Macro Processors (MP): Examples:
1 Machine-Independent Features Automatic Library Search automatically incorporate routines from a subprogram library Loading Options.
Chapter 6: Machine dependent Assembler Features
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chih-Hung Wang Chapter 2: Assembler (Part-1) 參考書目 Leland L. Beck, System Software: An Introduction to Systems Programming (3rd), Addison-Wesley, 1997.
Chapter 3 Assembly Language: Part 1. Machine language program (in hex notation) from Chapter 2.
An introduction to systems programming
Assemblers Dr. Monther Aldwairi 10/21/20071Dr. Monther Aldwairi.
1 Chapter 4 Macro Processors Source Code (with macro) Macro Processor Expanded Code Compiler or Assembler obj.
Chapter 4 Macro Processors
Assembler (Basic Functions)
Chapter 15: Operator Overloading
1 Chapter 4 Macro Processors Professor Gwan-Hwan Hwang Dept. Computer Science and Information Engineering National Taiwan Normal University 9/17/2009.
Dr Masri Ayob TK 2633: Microprocessor & Interfacing Lecture 7: Assembly Language.
A Simple Two-Pass Assembler
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Assembler Design Options
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
Assemblers.
Chapter 1 Computer architecture Languages: machine, assembly, high
An introduction to systems programming
Machine Independent Macro Processor Features Concatenation of Macro Parameters Generation of Unique Labels Conditional Macro Expansion Keyword Macro.
4. Macro Processors1 Chapter IV: Macro Processors Overview: r To study the design and implementation of macro processors. r A macro represents a commonly.
CPS4200 System Programming 2007 Spring 1 Systems Programming Chapter 2 Assembler II.
Objective At the conclusion of this chapter you will be able to:
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
M4 Macro-processing Language Geoffrey Sewell. What will be shown? What’s a macro processor? History of M4 Uses Autotools Syntax Hopefully, you all learn.
Ass. Prof. Dr Masri Ayob TK 6123 Lecture 13: Assembly Language Level (Level 4)
Execution of an instruction
Computer Science 210 Computer Organization More on Assembler.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
2 : Assembler 1 Chapter II: Assembler Chapter goal: r Introduce the fundamental functions that any assembler must perform. m Assign machine address m Translate.
More on MIPS programs n SPIM does not support everything supported by a general MIPS assembler. For example, –.end doesn’t work Use j $ra –.macro doesn’t.
Machine Independent Assembler Features
Macro Processors.
Loader and Linker.
Assembler Design Options One-Pass and Multi-Pass Assemblers.
 A macro represents a commonly used group of statements in the source programming language.  The macro processor replaces each macro instruction with.
COMPILERS CLASS IV Er. Vikram Dhiman M.tech NIT jalandhar.
Linking Loader untuk SIC/XE Machine. Lebih Lanjut mengenai Absolute Loader Shortcoming of an absolute loader –Programmer needs to specify the actual address.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
1 Chapter 4 Macro Processors. 2 Introduction A macro instruction (abbreviated to macro) is simply a notational convenience for the programmer. A macro.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 11–Macro-Processors.
CPS4200 System Programming Spring 1 Systems Programming Chapter 1 Background I.
Macro Processor Design Options Recursive Macro Expansion General-Purpose Macro Processors Macro Processing within Language Translators.
Programming what is C++
Addressing Modes in Microprocessors
Machine Independent Assembler Features
Machine Independent Assembler Features
Assembler Design Options
MACRO Processors CSCI/CMPE 3334 David Egle.
Chapter 4 Macro Processors
Loaders and Linkers: Features
Machine Independent Features
Fundamentals of Programming
Algorithm for One-pass Macro Processor
A Simple Two-Pass Assembler
Assemblers CSCI/CMPE 3334 David Egle.
Chapter 1 Computer architecture Languages: machine, assembly, high
UNIT – IV MACRO PROCESSORS
CHAP 4 MACRO PROCESSORS.
Presentation transcript:

Macro Processors Basic Functions Machine-Independent Features Design Options Implementation Examples

A macro instruction (macro) –It is simply a notational convenience for the programmer to write a shorthand version of a program. –It represents a commonly used group of statements in the source program. –It is replaced by the macro processor with the corresponding group of source language statements. This operation is called “expanding the macro” For example: –Suppose it is necessary to save the contents of all registers before calling a subroutine. –This requires a sequence of instructions. –We can define and use a macro, SAVEREGS, to represent this sequence of instructions. Macro Instructions

A macro processor –Its functions essentially involve the substitution of one group of characters or lines for another. –Normally, it performs no analysis of the text it handles. –It doesn’t concern the meaning of the involved statements during macro expansion. Therefore, the design of a macro processor generally is machine independent. Macro processors are used in –assembly language –high-level programming languages, e.g., C or C++ –OS command languages –general purpose Macro Processors

Basic Functions Macro Definition Macro Invocation Macro Expansion One-Pass Algorithm Data Structure

Two new assembler directives are used in macro definition: –MACRO: identify the beginning of a macro definition –MEND: identify the end of a macro definition Prototype (pattern) for the macro: –Each parameter begins with ‘&’ label op operands name MACRO parameters : body : MEND Body: the statements that will be generated as the expansion of the macro. Macro Definition

Macro definition Macro body contains no label Example of Macro Definition

Macro definition Example of Macro Definition Macro body contains no label

A macro invocation statement (a macro call) gives the name of the macro instruction being invoked and the arguments in expanding the macro. The processes of macro invocation and subroutine call are quite different. –Statements of the macro body are expanded each time the macro is invoked. –Statements of the subroutine appear only one, regardless of how many times the subroutine is called. Macro Invocation

Macro invocations Example of Macro Invocation

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

Comment lines within the macro body have been deleted, but comments on individual statements have been retained. Macro invocation statement itself has been included as a comment line. The label on the macro invocation statement CLOOP has been retained as a label on the first statement generated in the macro expansion. This allows the programmer to use a macro instruction in exactly the same way as an assembler language mnemonic. Macro Expansion

Example of Macro Expansion

.

Problem of the label in the body of macro: –There will be duplicate labels, which will be treated as errors by the assembler, if the same macro is expanded multiple times at different places in the program. Solutions: –Simply not to use labels in the body of macro. Explicitly use PC-relative addressing instead. For example, in RDBUFF and WRBUFF macros, JEQ * +11 JLT *-14 It is inconvenient and error-prone. –Other better solution? No Label in the Body of Macro

Two-pass macro processor –Pass 1: Process macro definition –Pass 2: Expand all macro invocation statements Problem –This kind of macro processor cannot allow recursive macro definition, that is, the body of a macro contains definitions of other macros (because all macros would have to be defined during the first pass before any macro invocations were expanded). Two-Pass Macro Processor

MACROS (for SIC) –contains the definitions of RDBUFF and WRBUFF written in SIC instructions. MACROX (for SIC/XE) –contains the definitions of RDBUFF and WRBUFF written in SIC/XE instructions. A program that is to be run on SIC system could invoke MACROS whereas a program to be run on SIC/XE can invoke MACROX. Defining MACROS or MACROX does not define RDBUFF and WRBUFF. These definitions are processed only when an invocation of MACROS or MACROX is expanded. Example of Recursive Macro Definition The same macro name

Example of Recursive Macro Definition

A one-pass macro processor that alternate between macro definition and macro expansion in a recursive way is able to handle recursive macro definition. Because of the one-pass structure, the definition of a macro must appear in the source program before any statements that invoke that macro. One-Pass Macro Processor

DEFTAB (definition table) –Stores the macro definition including macro prototype macro body –Comment lines are omitted. –References to the macro instruction parameters are converted to a positional notation for efficiency in substituting arguments. NAMTAB –Stores macro names –Serves an index to DEFTAB pointers to the beginning and the end of the macro definition ARGTAB –Stores the arguments of macro invocation according to their positions in the argument list –As the macro is expanded, arguments from ARGTAB are substituted for the corresponding parameters in the macro body. Data Structures

MAIN procedure –iterations of GETLINE PROCESSLINE PROCESSLINE procedure –DEFINE –EXPAND –output source line DEFINE procedure –make appropriate entries in DEFTAB and NAMTAB EXPAND procedure –set up the argument values in ARGTAB –expand a macro invocation statement (like in MAIN procedure) iterations of –GETLINE –PROCESSLINE GETLINE procedure –get the next line to be processed from input file DEFTAB Algorithm

In DEFINE procedure –When a macro definition is being entered into DEFTAB, the normal approach is to continue until an MEND directive is reached. –This would not work for recursive macro definition because the first MEND encountered in the inner macro will terminate the whole macro definition process. –To solve this problem, a counter LEVEL is used to keep track of the level of macro definitions. Increase LEVEL by 1 each time a MACRO directive is read. Decrease LEVEL by 1 each time a MEND directive is read. A MEND can terminate the whole macro definition process only when LEVEL reaches 0. This process is very much like matching left and right parentheses when scanning an arithmetic expression. Handling Recursive Macro Definition

Algorithm