C program of Macro Processor Opcode | C codechamp

C program of Macro processor : A macro processor is a program that reads a file (or files) and scans them for certain keywords. When a keyword is found, it is replaced by some text. The keyword or text combination is called a macro. A macro processor enables you to define and to use macros in your assembly programs. When you define a macro, you provide text (usually assembly code) that you want to associate with a macro name. Then, when you want to include the macro text in your assembly program, you provide the name of the macro. The assembler replaces the macro name with the text specified in the macro definition.

Before going further, we must know what are the functions used in the program and what is their use. Let see the functions used in Macro processor C program :

MACROPROCESSOR MAIN FUNCTION

If (EXPANDING=FALSE)

Read each line and call GETLINE() and PROCESSLINE() until END keyword encounters.

PROCESSLINE ( ) FUNCTION

If OPCODE is a macroname then call EXPAND ( ) function.

Else if OPCODE is MACRO ,then call DEFINE ( ) function.

Else write line to expanded file as such.

DEFINE( )

Enter Macro name into CODECHAMP structure.Enter macro prototype into DEFINETAB structure. Set LEVEL=1 and substitute parameters with positional notations and enter to DEFINETAB structure.
If OPCODE=MACRO then LEVEL++;
If OPCODE=MEND then LEVEL–;
Continue this until LEVEL=0
Store beginning and end of definition as pointers within CODECHAMP structure.

EXPAND ( ) FUNTCION

IF (EXPANDING = TRUE)
Set up arguments from macro invocation in ARGUMENTTAB.
Write macro statement to expanded file as a comment line.
Call GETLINE() and PROCESSLINE() till macro definition ends.
At end, set EXPANDING=FALSE.

GETLINE ( ) FUNCTION

If EXPANDING is TRUE, read from DEFINETAB (data structure where macro body is stored) and substitute arguments for positional notations.
If EXPANDING is FALSE , read next line from input file.

NOTE : This program accepts an input file “input.txt” and generates the expanded output file “output.txt”. Steps to take care before running program :

1. Input file contains two parts : Macro and second Main program.

Separate the Macro and Main program with one blank line .

Also keep in mind leaving a blank empty line below the main program “END” statement. Else the code will go in infinite loop.

Well, I am software programmer and love to code. My hobbies is to do Hacking, Coding, Blogging, Web Designing and playing online games.
Feel free to contact me at shiviskingg@gmail.com or lokesh@hackingloops.com