-----------------------------------------------------------------------------
			   ASEMBLER DESIGN FOR A HYPOTHETICAL MACHINE
-----------------------------------------------------------------------------

PROBLEM DEFINITION:
-------------------
To design and implement a two-pass assembler for a hypothetical
computer model.

MACHINE ARCHITECTURE:
---------------------
The model has only one accumulator.
The details of the instructions and their functions are cited below :

		'A' is the name of the variable
		'L' is the name of the variable
	  'ACC' stands for the accumulator.

-----------------------------------------------------------------------------
Mnemonic    Machine    No. of      Length of      Example     Meaning
Code        Code       Operands    instruction
-----------------------------------------------------------------------------
ADD         01         1           2              ADD A      ACC:=ACC+A
SUB         02         1           2              SUB A      ACC:=ACC-A
MULT        03         1           2              MULT A     ACC:=ACC*A
JMP         04         1           2              JMP L      GOTO L
JNEG        05         1           2              JNEG L     If ACC<0 GOTO L
JPOS        06         1           2              JPOS L     If ACC>0 GOTO L
JZ          07         1           2              JZ L       If ACC=0 GOTO L
LOAD        08         1           2              LOAD A     ACC:=A
STORE       09         1           2              STORE A    A:=ACC
READ        10         1           2              READ A     A:=Input Data
WRITE       11         1           2              WRITE A    Output A
STOP        12         0           1              STOP       Stop Execution
CALL        13         1           2              CALL NEXT  Calls a procedure
RET         14         0           1              STOP       Stop Execution
-----------------------------------------------------------------------------

PSEUDO OPCODES (Assembler Directives) used are:
--------------
1> ORG  2> ENDP 3> END  4> DB  5> CONST

DATA STRUCTURES USED:
---------------------
1: Machine Opcode Table (MOT)

2: Pseudo Opcode Table (POT)

3: Symbol Table (ST)

4: Literal Table (LT)

5: Procedure Table (LT)

ASSUMPTIONS:
------------
 1. Input is case insensitive.

 2. A two segment approach has been taken i.e. data and code segments are
	seperate . ORG statement,if used should be the first statement of the a
	assembly language program, and this will be added to the code_seg_lc.
	If not specified the origin is taken as 0. By default the data_seg_lc
	is 0.

 3. Only one instruction per line is allowed.

 4. Comment starts with a semicolon and everything beyond it on that line
	is ignored.

 5. Data segment starts with ".DATA"  and ends with "DATA ENDS".

 6. Data must be defined only before the code segment.

 7. Code segment starts with ".CODE " and ends with "STOP".

 8. Main program ends with "END" and everything after "END" is ignored.

 9. Format of the instruction is:
		 [Label:]
			Mnemonic Opcode  [Operands]  [Comment]
	The Label is optional. If present it should be immediatetly followed by
	a colon. There should not be any (assembly language) statement after
	the label.

10. Data declaration should be as follows:
			  <variable>  db     <value>
			  <varaible>  const  <value>
	The first letter of the data-name should be an alphabet.

11. Only Integer Literals are allowed. eg: @10 means integer 10.



ALGORITHM:
----------
 1. Prepare the MOT, POT.

 2. Perform Pass1 scan of the assembler.

 3. If no errors are encountered in the Pass1 scan  proceed for Pass2 scan.
	Else report errors to the user and exit.
 4. The final Object Code is output at the end of the Pass2 scan.


PASS 1: perform_pass_1(I/P file,INTER file,SYMBTAB file)
-------
 1. Intitialize the ORG flag to FALSE to indicate it has not ocurred.
 2. Initialize location counter (loc_cnt) to -1.
 3. Until the Input file is fully read,
	   1] Read the current line.
	   2] Separate out the tokens in a line.
	   4] If 'END' set the end_program flag and check the error flag.
		  If reset(no errors) then move onto Pass2.
	   5] If 'STOP'search MOT for it's opcode write it into the intermediate
		  file and break (if no procs are present)inorder to check whether
		  PROCEDURES/END follow it or not.
	   6] If 'ORG' check whether it is first line of program.
			If yes,set the org_occured flag
			check whether org_parameter is a valid integer operand or not.
			If yes,  code_seg_lc = code_seg_lc + org_parameter
	   7] If '.DATA' set data_segment flag.
		  Call data_definitions() for analysis of data.
			  Until end of Input file,
			  (1) Read valid line from input file and separate tokens.
			  (2) If '.CODE' indicate that datasegment has not ended by flag.
				  Rewind the line ( using fseek ) to read .CODE again.
			  (3) If 'DATA ENDS' set data_ends_flag to ENCOUNTERED and break
				  off to check whether next valid statement is '.CODE'
			  (4) Separate out the variable from the line and check whether
				  it is a keyword ( check_for_keywords(variable name)).
			  (5) Separate out the size of operand (db/const)
			  (6) Search POT to check whether size of operand is present.
			  (7) Insert into st_file the symbol,corresponding line count and
				  assign it attributes 'v'/'c' accordingly.
	   8] If '.CODE' set code_segment flag to indicate entry into code area.
		  Check whether data_ends_flag is encountered.
	   9] If code_segment is ENTERED then
			  (1) Filter out the instruction into an array - [instruct].
			  (2) If it is followed by a colon ':' then it is a Label.
				  If label is not present in the ST then insert it and
				  return code_seg_lc = code_seg_lc + 1
				  If present return code_seg_lc = code_seg_lc + 1.
			  (3) Filter out the operand into an array - [para].
			  (4) Search [instruct] in the MOT
					 If it is JMP type of instruction then treat [para] as
					 a symbol (label) and search ST.
					 If not present insert it with an undetermined code_seg_lc
					 to indicate that the label is a FORWARD label.
					 Else
						write the corresponding code_seg_lc into the
						Inter_file	because it is a BACKWARD label.
 4. If no errors are detected at the end of Input file perform Pass2
	Else report appropriate errors at corresponding line numbers and exit.


PASS 2: perform_pass_2(I/P file,OBJECT file)
-------
 1. When we are going in for the second pass it is implied that the program
	is error free.
 2. So, Read a line from the input file until EOF .
 3. Ignore all lines till the .code statements occur
 4. When code_definition STARTS separate out the instruction and operands
	into [instruct] and [para].
 5. Search MOT for [instruct] position and write corresponding opcode into
	Object file.
 6. Search ST for [para] position of operand and write corresponding
	code_seg_lc value into Object file.
 7. Take similar actions for the literals.
 8. If "proc" statement is encountered it implies that a definition of
	a procedure is being made so first check the pt_table for entry of
	the proc name , if entry is not found it is case wherein procedure
	is defined but not defined so skip all the lines till you encounter
	a "ret" statement so that we do not write unnecessary data into the
	object file
 8. Return back to main and proceed for the display of the files that
	have been created.