Macro Processors.

Slides:



Advertisements
Similar presentations
COP 3402 Systems Programming
Advertisements

1 Chapter 8: Code Generation. 2 Generating Instructions from Three-address Code Example: D = (A*B)+C =* A B T1 =+ T1 C T2 = T2 D.
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Macro simple idea of textual substitution useful when you need a group of instructions or directives frequently.
EECC250 - Shaaban #1 Lec # 6 Winter Stack-Related Instructions PEA Push Effective Address Calculates an effective address and pushes it.
The Assembly Language Level
Macro Processor.
Macro Processors (MP) Macro: Macro Processors (MP): Examples:
Chapter 10 Introduction to Arrays
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 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Chapter 4 Macro Processors
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
1 Chapter 4 Macro Processors Professor Gwan-Hwan Hwang Dept. Computer Science and Information Engineering National Taiwan Normal University 9/17/2009.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
Assembler When a source program is a assembly language and the target program is a numerical machine language then the translator is called as a assembler.
Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.
A Simple Two-Pass Assembler
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
 2008 Pearson Education, Inc. All rights reserved Function Call Stack and Activation Records Data structure: collection of related data items.
Chapter 6: User-Defined Functions
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
1 Homework / Exam Finish up K&R Chapters 3 & 4 Starting K&R Chapter 5 Next Class HW4 due next class Go over HW3 solutions.
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
EEC4133 Computer Organization & Architecture Chapter 6: Languages and the Machine by Muhazam Mustapha, May 2014.
Assembly Language Macros Most assemblers include support for macros. The term macro refers to a word that stands for an entire group of instructions. Macro.
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.
Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 7 – Subroutines These are lecture notes to accompany the book SPARC Architecture,
1 Assemblers System Programming by Leland L. Beck Chapter 2.
ICS312 Set 14 MACROS. Macros The program structure is similar to that for procedures. As for procedure names, macro names represent a group of instructions.
Lecture 7 A closer look at procedures Dr. Dimitrios S. Nikolopoulos CSL/UIUC.
Python Functions.
Assembly Language for x86 Processors 7th Edition Chapter 13: High-Level Language Interface (c) Pearson Education, All rights reserved. You may modify.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
by Richard P. Paul, 2nd edition, 2000.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Macro Processors Basic Functions Machine-Independent Features Design Options Implementation Examples.
 A macro represents a commonly used group of statements in the source programming language.  The macro processor replaces each macro instruction with.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
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.
Macro Processor Design Options Recursive Macro Expansion General-Purpose Macro Processors Macro Processing within Language Translators.
Assembly language programming
Format of Assembly language
Introduction to C++ Systems Programming.
CS 326 Programming Languages, Concepts and Implementation
UNIT - V STORED PROCEDURE.
Chapter 5 Conclusion CIS 61.
Computer Architecture and Organization Miles Murdocca and Vincent Heuring Chapter 4 – The Instruction Set Architecture.
Chapter 7 Subroutines Dr. A.P. Preethy
Assembler Design Options
MACRO Processors CSCI/CMPE 3334 David Egle.
Instructions - Type and Format
Fundamentals of Programming
by Richard P. Paul, 2nd edition, 2000.
A Simple Two-Pass Assembler
System Programming by Leland L. Beck Chapter 2
UNIT – IV MACRO PROCESSORS
CHAP 4 MACRO PROCESSORS.
Corresponds with Chapter 5
Procedures & Macros Introduction Syntax Difference.
Procedures and Macros.
Presentation transcript:

Macro Processors

Introduction Concept A macro instruction is a notational convenience for the programmer It allows the programmer to write shorthand version of a program (module programming) Macro is a Single line abbreviation for set of instructions . The macro processor replaces each macro invocation with the corresponding sequence of statements i.e called as Macro expantion.

Functions of Macro Processor Recognize macro definitions Save the macro definition Recognize macro calls Expand macro calls Source Code (with macro) Macro Processor Expanded Code(without macro) Compiler or Assembler obj

Macro Definition copy code parameter substitution conditional macro expansion macro instruction defining macros

Structure of Simple Macro -- Example Source MACRO XYZ …..; macro Header .. .. } ;Macro Body …. MEND ; Macro ends MEND XYZ --- . ---- XYZ ;Macro Call

Structure of parameterized Macro -- Example Source MACRO XYZ para1, para2…..; macro Header .. .. } ;Macro Body …. MEND ; Macro ends MEND XYZ --- . ---- XYZ 1,2,….. ;Macro Call

Example of macro expansion

Classification of macros Simple macro Nested Macro Macro call within a macro definition Macro definitions inside the Macro definitions of another Macro Conditional Macro Recursive Macro

Example 1: Simple Macro ( Find the Expanded code) Line no. Instructions Operands 1 Macro XYZ 2 Load A 3 Add B 4 Mend 5 PQR 6 X 7 Y 8 9 ABC 10 P 11 Q 12

Example 1: Simple Macro ( Find the Expanded code Line no. Instructions Operands 13 Mult C 14 Div D 15 XYZ macro call 16 Add 17 ;macro call 18 ABC 19 Store M 20 PQR ; macro call 21 End

MNT( Macro Name Table) Name of the Macro Number of Parameters Starting Row index in MDT Ending Row index in MDT XYZ 1 2 PQR 3 4 ABC 5 6

MDT( Macro Definition Table) Line No. Instructions( Macro Body) 1 Load A 2 Add B 3 Load X 4 Add Y 5 Load P 6 Add q

MDT( Modified Macro Definition Table) Line No. Instructions( Macro Body) 1 Load A 2 Add B 3 MEND 4 Load X 5 Add Y 6 7 Load P 8 Add q 9

MNT( Modified Macro Name Table) Name of the Macro Number of Parameters Starting Row index in MDT XYZ 1 PQR 4 ABC 7

Expanded Code( out put of Macro processor) Line No. Expanded Code 1 Mult c 2 Div d 3 Load a 4 Add b 5 Add c 6 7 8 Load p 9 Add q 10 Store m 11 Load x 12 Add y 13 End

Example 1: Parameterized Macro( Find the Expanded code) Line no. Instructions Operands 1 Macro XYZ &a,&b 2 Load &a 3 Add &b 4 Mend 5 ABC 6 P 7 q 8 9 XYZ &a, &b ,&c 10 X 11 12 Sub &c

Example 1: Parameterized Macro( Find the Expanded code) Line no. Instructions Operands 13 Mult y 14 Store &b 15 Mend 16 Load M 17 ABC 18 XYZ 3,4 19 5,6,7 End

MDT( Macro Definition Table) Line No. Instructions( Macro Body) 1 Load #1 2 Add #2 3 MEND 4 5 6 7 Load x 8 Add #1 9 Sub #3 10 Mult y 11 Store #2 12

MNT(Macro Name Table) Name of the Macro Number of Parameters Starting Row index in MDT XYZ 2 1 PQR 4 3 7

Formal Vs Positional Parameter List for Macro XYZ (with 3 parameters) Formal Parameter Positional Parameter &a #1 &b #2 &c #3

Actual Vs Positional Parameter List for Macro XYZ (with 3 parameters) Formal Parameter Positional Parameter 5 #1 6 #2 7 #3

Expanded Code for parameterized Macro ( out put of Macro processor) Line No. Expanded Code 1 Load m 2 Load p 3 Add q 4 Load 3 5 Add 4 6 Load x 7 Add 5 8 Sub 6 9 Mult y 10 Store 7 11 End

Activities of single pass macro processors Read the source program statements from input file till EOF. If Source statement is a normal ALP statement then dump it as it is to output file.( Definition mode flag (d) and expansion mode flag(e) are set to 0), Reading is from input file and writing it into output file  If a statements corresponds to macro definition , then, Dump the macro body along with MEND ,starting from next available row index in MDT after replacement of positional parameters. Reflect the corresponding changes into MNT. Here d is set( d=1) and e is reset(e=0). Reading is from input file and writing it into MDT. Formal Vs Positional parameter list is used . d is reset( d=0) on MEND.  

If a statements corresponds to macro call Consult MNT to get index in MDT and read statements one by one from MDT till MEND , replace it with Actual Parameter and write it into output file. Here e is set( e=1) and d is reset(d=0). Reading is from MDT and writing it into Output file. Actual Vs Positional parameter list is used . e is reset( e=0) when MEND is encountered in MDT .

Summary of Activities of single pass macro processors Mode activity d=0, e=0 d=1, e=0 d=0, e=1 d=1, e=1 Read from INPUT FILE MDT ? Write into OUTPUT FILE

Data structures used by macroprocessor Pass 2: Intermediate file Output file MNT MDT MDT pointer MNT pointer LC Actual Vs Positional para. List Pass 1: Input source file Intermediate file MNT MDT MDT pointer MNT pointer LC Formal Vs Positional para. List

Find the output of the following program Line no. Label Instructions Operands 1 Read a 2 b 3 Macro Add2 &x,&y,&z 4 Load &x 5 Add &y 6 Store &z 7 Mend 8 Macro mult1 &l ,&x, &y, &z 9 =‘0’ 10 11 & l: 12 add

Find the output of the following program Line no. Instructions Operands 13 Store &z 14 Load &y 15 Sub =‘1’ 16 17 Jpos &l 18 Mend 19 Add2 P,Q,R 20 Mult1 E,F,G,H 21 Endp

Expanded Code ( out put of Macro processor) Line No. Expanded Code 1 Read a 2 Read b 3 Load P 4 Add Q 5 Store R 6 Load =‘0’ 7 Store H 8 E : Load H 9 add F 10 Store H 11 Load G Line No. Expanded Code 12 Sub =‘1’ 13 Store G 14 Jpos E 15 Endp

Default value Parameters in macro Macro XYZ &a, &b=10, &c Laod &a add &b Store &c MEND .. XYZ 3, , 5 XYZ 6,7,8 Endp Here &b=10 is Default Parameter

keyword parameters in macro Macro TEST &a=, &b=, &op=ADD, &lab= &lab : Laod &a &op &b Store &a MEND .. TEST b=5, lab=BACK, a=4 …….. Endp OUTPUT : BACK : load 4 Add 5 store 4 Here &a= is a keyword parameter

Macro Overloading Macro FUNCT &arg1, &arg2,&arg3 load &arg1 store &arg1 MEND … FUNCT a, add,b …. FUNCT a,sub, b Endp

Output Expansion due to first macro call Load a add b Store a Expansion due to second macro call sub b

Concatenation of macro parameter Macro SUM &a, &b load X+&a Add X+&b Mend .. SUM A1,A2 … Endp Expanded code : Load XA1 Add XA2

Error Handling by Macroprocessor Error 1 : Call to a macro which is not defined . Error 2 : parameter mismatch in call and definition Error 3 : Multiple definition of same macro. Error 4 : Unexpected End of File.

Nested Macro Macro within a macro. Macro call within a macro definition Early expansion method. Late Expansion method. Macro definition within a macro definition

Example 1: Nested Macro( Find the Expanded code) Line no. Instructions Operands 1 Load A 2 Store B 3 Macro Add1 &arg 4 5 Add =‘1’ 6 7 Mend 8 Macro Add5 &a1,&a2,&a3 9 C 10 D 11 Add1 5 ; nested call 12 &a1 ; nested call

Example 1: Nested Macro( Find the Expanded code) Line no. Instructions Operands 13 Load &a2 14 &a3 15 Mend 16 Add5 Data1, data2,data3 17 endp

MNT( Macro Name Table) Name of the Macro Number of Parameters Starting Row index in MDT Add1 1 Add5 3 5 Formal Vs Positional parameter list for macro Add1 &arg #1

Formal Vs Positional Parameter List for Macro Add5 (with 3 parameters) Formal Parameter Positional Parameter &a1 #1 &a2 #2 &a3 #3

MDT( Macro Definition Table) Early expansion method Line No. Instructions( Macro Body) 1 Load #1 2 Add =‘1’ 3 Store #1 4 MEND 5 Load c 6 Store d 7 Load 5 8 9 Store 5 10 Load #1 11

MDT( Macro Definition Table) Early expansion method Line No. Instructions( Macro Body) 12 Store #1 13 Load #2 14 Load #3 15 MEND

Actual Vs Positional Parameter List for Macro Add1(with 1 parameters) Actual Parameter Positional Parameter 5 #1

Actual Vs Positional Parameter List for Macro Add5(with 3 parameters) Actual Parameter Positional Parameter Data1 #1 Data2 #2 Data3 #3

Expanded Code ( out put of Nested Macro processor by Early expansion method ) Line No. Expanded Code 1 load A 2 Store B 3 Load C 4 Store D 5 Load 5 6 Load =‘1’ 7 Store 5 8 Load Data1 9 Add =‘1’ 10 Store Data1 11 Load Data2 Line No. Expanded Code 12 Load Data3 13 Endp

MDT( Macro Definition Table) Late expansion method Line No. Instructions( Macro Body) 1 Load #1 2 Add =‘1’ 3 Store #1 4 MEND 5 Load c 6 Store d 7 Add1 5 8 Add1 #1 9 Load #2 10 Load #3 11

Expanded Code ( out put of Nested Macro processor by Late expansion method ) Line No. Expanded Code 1 load A 2 Store B 3 Load C 4 Store D 5 Load 5 6 Load =‘1’ 7 Store 5 8 Load Data1 9 10 Store Data1 11 Load Data2 Line No. Expanded Code 12 Load Data3 13 Endp

Example 2: Nested Macro( Find the Expanded code) Line no. Instructions Operands 1 Macro XYZ &a 2 Load 3 Mend 4 Macro PQR &a,&b 5 &b 6 7 PQR add,6 8 XYZ,7 9 Endp

Nested macro definition Line no. Instructions Operands 1 Macro Outer &macname, &op 2 Macro &macname &x, &y, &z 3 Load &x 4 &op &y 5 Store &z 6 Mend 7 …….. Outer Adder ,add

Nested macro definition Line no. Instructions Operands Outer Substractor, sub ……. …… Adder a,b,c Substractor m,n,o …. Endp

Expanded Code Line No. Expanded Code 1 Load a 2 Add b 3 Store c Load m Sub n Store o

Ex 2: Nested macro definition Line no. Instructions Operands 1 Macro A &f1 2 Load 3 Macro B &f2 4 Store 5 Mend B 6 Mend A 7 A a1 8 B b1 9 Endp

MDT( Macro Definition Table) Line No. Instructions( Macro Body) 1 Load (#1,1) 2 Store (#2,1) 3 MEND 4 Mend

Expanded Code Line No. Expanded Code 1 Load a1 2 Store b1 3 Endp

Algorithm for Nested macro processor REFER TO DOC DOCUMENT.

Conditional Macro Expansion Expansion based on condition. 2 macro pseudo ops for conditional macro expansion are AIF AGO Syntax of flow of control statements: AIF(< condition>) Conditional Label Ex : AIF( &arg1 = &arg2). FINI AGO Label Ex: AGO .OVER

Ex : Conditional macro Expansion (Find expanded code) Line no. Instructions Operands 1 Macro VARY &arg0, &count, &arg1, &arg2 , &arg3 2 &arg0: add &arg1 3 AIF (&count = 1). FINI 4 add &arg2 5 AIF (&count = 2). FINI 6 &arg3 7 .FINI AGO .Over 8 .Over Mend …………..

Ex : Conditional macro Expansion (Find expanded code) Line no. Instructions Operands …… 12 VARY Loop1,3,data1,data2,data3 ….. 15 Loop2,2,data1,data2 ………………… 18 Loop3,1,data1 Endp

Expanded Code due to call at Line no 12 Loop1 Add data1 2 Add data2 3 Add data3

Expanded Code due to call at Line no 15 Loop2 Add data1 2 Add data2

Expanded Code due to call at Line no 18 Loop3 Add data1

Ex 2 : Conditional macro Expansion (Find expanded code) Line no. Instructions Operands 1 Macro FIRST &arg1, &arg2 , &arg3 2 AIF (&arg1= &arg2). ONLY 3 Load &arg1 4 Add &arg2 5 .ONLY AGO .OVER 6 .Over Mul &arg3 7 Mend 8 Macro SECOND &a1, &a2,&a3,&a4,&a5 9 FIRST &a1, &a2,&a3

Ex 2 : Conditional macro Expansion (Find expanded code) Line no. Instructions Operands 10 Div &a4 11 Store &a5 12 Mend ……. ………………………………. ….. 15 SECOND A,B,C,D,E ………………………….. ………………………. 18 A,A,B,C,D Endp

Expanded Code due to call at Line no 15 Load A 2 Add B 3 Mul C 4 Div D 5 Store E

Expanded Code due to call at Line no 18 Mul B 2 Div C 3 Store D

Recursive Macro Recursive Macro is nothing but call to a macro say “XYZ” inside the definition of “XYZ” itself.

MACRO Vs FUNCTIONS Macro Functions (or Subroutines) Definition Macros are single-line abbreviations of a small sequence of instructions. Functions are blocks of code that are to be executed repeatedly by the program Lines of Code Macros are used when number of repeating instructions is very small (3 to 5 instructions) Functions can be used for any length of code that is to be executed repeatedly. Use of CALL mnemonic Only the macro name is used to call the macro CALL mnemonic is used along with the function name (as its operand), to call the function

Time of processing Macros are processed at compilation time Functions are processed during execution time Processing the call Macro calls are replaced by corresponding sequence of instructions; these instructions are not actually executed. When a function is called, its corresponding instructions are actually executed. Housekeeping operations No housekeeping operations (pushing and popping on the stack) are needed. The return address is pushed onto stack, when a function is called and is popped back when the function returns Need of a Preprocessor Macro calls are not recognized by the assembler. So, a separate Macro Processor is needed to expand them Functions (or Subroutines) are executed by the Assembler itself (as Procedures) Return value Macros cannot return a value Functions can return a value Size of the program Macro calls are expanded inline. So, they increase the size of the program Functions do not increase the size of the program Speed of execution Since there are no housekeeping operations involved, programs using macros execute slightly faster than the ones using functions Housekeeping operations make programs using functions execute at lesser speed.

Chapter Summary  Macros are single-line abbreviations for a small sequence of commands. Macros appear functionally similar to functions (or subroutines). But there are many fundamental differences between them. Macros are expanded inline i.e. the macro call is replaced by corresponding sequence of instructions; these instructions are not actually executed. Functions are used to repeatedly execute a long enough sequence of instructions. Also, processing of macros does not require any housekeeping operations, whereas executing functions needs return address (and sometimes current state of the program) to be pushed onto stack.

Functions of Macro Processor are: Recognize the macro definition Store the macro definition Recognize the macro call Expand the macro call Features of macro facility include: Parameterized Macros – Passing arguments to a macro, either by position or by Keywords Nested Macros – Using one macro from within another macro Nested Macro Calls: Calling a macro from within another macro Nested Macro Definition: Defining one macro inside definition of another macro.

AIF is used as a conditional jump; AGO is used for unconditional jump. Conditionally Macro Expansion – Using AIF and AGO statements to conditionally decide which part of code to insert into the macro body. AIF is used as a conditional jump; AGO is used for unconditional jump.  Recursive Macros – Calling a macro from within itself. But to limit the depth of recursion, proper conditions must be given to terminate the recursion. A Simple Macro Processor takes two passes to completely process and expand the macros. In Pass 1, All macros used in the program get listed in MNT and their definitions get stored in MDT. ALA is used to convert formal parameters to positional parameters. In Pass 2, macro calls in the program get expanded by using MNT and MDT. ALA is used to convert positional parameters to actual parameters.

Thank You!!!