// ready for printing
/****************************************************************************/
/****************************************************************************/
/**																		   **/
/**																		   **/
/**						 	FILE  :-  MAC_ALG.TXT					       **/
/**							FOR MACRO-ASSEMBLER.						   **/
/**						IT CONSISTS OF THE FOLLOWING.					   **/
/**																		   **/
/**  					1.> PROBLEM DEFINITION							   **/
/**  					2.> ASSUMPTIONS	     							   **/
/**  					3.> ALGORITHM									   **/
/**  					4.> DATA STRUCTURES USED						   **/
/**  					5.> FUTURE EXPANSIONS							   **/
/**  					6.> ADDITIONAL FEATURES							   **/
/**																		   **/
/****************************************************************************/
/****************************************************************************/


			 ͻ
			 	     	  PROBLEM DEFINITION		 		 
			 ͼ

1.> Design a macro assembler for hypothetical single register machine.

	a.> Use suitable data-structures for MNT and MDT.
	b.> Input   :-  .asm program with macro definitions
	c.> Output  :-  AA.YY program with all macros expanded and which is
					ready for assembly.
	d.> Errors  :-  Errors as an when they occur.
	e.> Future Expansion scope.





			 ͻ
			 	     			ASSUMPTIONS			 		 
			 ͼ

1.> Nested call to a macro inside a macro should be such that it has been
	preveously defined i.e its definition has to occur before a call is made
	to it.

2.> Actual No. of parameters have to match with the no. of parameters the
	macro is expecting else the program will jump out with error message.

3.> Define all the macro  at the start of the program ie all macro definitions
	should be done before the .CODE statement

4.> If macros are having labels they should be passed only as parameters
	and so subsequent changes have to be made in your program.





			 ͻ
			 	     ALGORITHM FOR MACRO-ASSEMBLER	 		 
			 ͼ


1.> Open the required files in the required modes .

2.> Call the macro_process funtion.
	a.> initially reset the def_mode_indicator_flag and
		expansion_depth_counter
	b.> while not end of the input file do
		if in macro definition mode

			1.> request for a node and if it happens to be the first node in
				the macro def then set pointer_to_mdt in IT's mnt_entry to
				this node .
			2.> if we encounter MEND reset macro_def_mode flag
				else replace ith formal parameter ( if any ) in the line by
				#i and write the line to the MDT .

		end if

	c.> else  ( we are into the copy or the expansion mode )

			case : ( cases are in the form of if statements in the code )

			1.> MEND :  ( end of the current macro call )
					a.> pop actual parameter list from the actual parameter
						stack.
					b.> decrement the depth counter.
				end

			2.> MACRO :  ( new definition encountered )
					a.> SET the macro_def_mode indicator .
					b.> Obtain space in MDT for storing the macro def.
					c.> Enter the macro name along with its stating address
						in MDT, ( this will be done when we request for the
						first node ), into MNT.
				end

			3.> MACRO_NAME :  ( new call encountered )
					a.> encrement the depth counter by 1 .
					b.> Obtain space in MDT for storing the macro def.
					c.> Enter the macro name along with its stating address
						in MDT, ( this will be done when we request for the
						first node ), into MNT.
				end

			4.> OTHERS :
					a.> if ( expansion counter > 0 )
						replace #i by the ith actual parameter from topmost
						list lin the actual parameter stack .
					b.> Write the line to the output file.
				end

		endcase

3.> if expansion counter is > 0
			then read next line of the current macro from the MDT
	else
			read next line from the input file

end ( while do )

END ( algorithm macro_processor with nested calls )






			 ͻ
			 	     	 	DATA STRUCTURES USED			 
			 ͼ

1.> MACRO DEFINITION TABLE

typedef struct macro_def_table
							{
								char            mneumonic [10] ;
								int				is_macro_or_label ;
								char 			parameter_index [5][15] ;
								struct 			macro_def_table *next ;
							} *MDT_ENTRY;


2.> MACRO NAME TABLE

typedef struct
				{
						char	 name[MAX] ;
						int  	 no_of_parameters ;
						char 	 parameter[5][6] ;
						MDT_ENTRY		 pointer_to_mdt ;
				}MNT;
MNT mnt_entry[10] ;


3.> ARRAY IN WHICH ALL THE KEYWORDS ARE PRESENT

char	key_words [ NO_OF_KEYWORDS ] [6]
							=  {
								 { "ADD"  },
								 { "SUB"  },
								 { "MULT" },
								 { "JMP"  },
								 { "JNEG" },
								 { "JPOS" },
								 { "JZ"   },
								 { "LOAD" },
								 { "STORE"},
								 { "READ" },
								 { "WRITE"},
								 { "STOP" },
								 { "CALL" },
								 { "RET"  },
								 { "ORG"  },
								 { "CONST"},
								 { "DB"   },
								 { "ENDP" },
								 { "END"  },
						  };






			 ͻ
			 	     	 	FUTURE EXPANSIONS		 		 
			 ͼ

1.> Presently every thing is getting resolved in the first pass itself.
	However if want forward definitions of macros ie nested call to a
	macro when it has not been defined we need to go in for second pass

2.> Many other unresolved aspects can be resolved if we go in for second
	pass.






			 ͻ
			 	     	   ADDITIONAL FEATURES 				 
			 ͼ

1.> Macro assembler is smart enough to correct some of your mistakes
	automatically and proceeds with the assembly

2.> Eg if you happen to call to a macro which expects a label and suppose
	you pass label as " YY " , and again you want  to call this macro
	and you happen to call it with the same parameter i.e " YY ",
	now the macro-assembler will detect this and to prevent multiple
	labels in the output file with the same name it will pad " YY "
	with "a" ie new label will become " YYa " and so on ...




// ͻ
// 					  END OF THE FILE MAC_ALG.TXT						 
// ͼ
