Member Avatar for simonsayz27
simonsayz27

This is starting to drive me nuts. I'm writing a program that simulates a two-pass algorithm which translates assembly language code in C++. I started with the first pass which reads the labels and address for each line. My whole program is in a mess and need a lot of help. Its supposed to read in a block of assembly code and output a symbol table in this form:

Address------- Machine code ------- Operands------- Instructions
000500 ------- 33FC --------------------- 4F------- MOVE #79, $002000

The code to translate from is:


--------- ORG $500
--------- MOVE #79, $00002000
--------- TRAP #1 ;READLN (Y), INPUT INTO DO
LOOP1 -- MOVE DO, $00020004 ;STORE DO IN LOCATION Y
--------- MOVE $00002000, DO ;FETCH VARIABLE I FOR THE AD
---------ADD $00002004, DO ;ADD Y+1
---------MOVE DO, $00002002 ; STORE THE SUM IN X
LOOP2 -- ADDI #18, $00002002 ;ADD 18 TO X
---------TRAP #2 ; PRINT X
----------- TRAP #0 ; STOP
----------- END

Here is where I am so far. I can't find any tutorials online for the SMC 68000 so if you know of any or can help me out in any way I would REALLY appreciate it.

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <vector>

using namespace std;

struct symbol
{
	string label;					// Holds Label Name
	int address;					// Holds Label Address
	int machineCode;
	int operand;
	string instructions;
};

void Parse(vector<symbol> &table);
void secondPass(vector<symbol> &table);
void findMachineCode(vector<symbol> &table, string line);
bool check(string word);
int find(string word, vector <symbol> table);

void main()
{
	vector<symbol> table;			// Symbol Table
	Parse(table);			// Parse Document and Set Symbol Table
	secondPass(table);
}

// First Pass of Document *** Set Symbol Table
void Parse(vector<symbol> &table)
{
	ifstream inFile;				// Input File
	string line;					// Command Line
	int count = 0;					// Instruction Number
	inFile.open("inData");			// Open File

	inFile >> line;			

	while (!inFile.eof())			// Until No More Commands Available
	{
		// If Not Known, Instruction is a Label
		if (!check(line))
		{
			table.resize(table.size() + 1);
			table[table.size() - 1].label = line;		
			table[table.size() - 1].address = count;	
		}
		else
		{
			count++;				// Increase Location
		}
		//inFile >> "\n";
		inFile >> line;		// Get Next Line
	}
	inFile.close();					// Close File
}

void secondPass(vector<symbol> &table)
{
	string line;
	ifstream inFile;				
	inFile.open("inData");			// Open File
	getline(inFile, line);
	findMachineCode(table, line);
}

void findMachineCode(vector<symbol> &table, string line)
{
	
}

// Check to See if Command is Known *** If Not, Command is a Label
bool check(string word)
{
	if (word == "MOVE" || word == "TRAP" || word == "ADD" || word == "ADDI" || word == "ORG")
		return true;					// If Know Command Then Return 1
	else
		return false;					// Else Command is a Label
}



// Search Symbol Table For Label Addresses
int find(string word, vector <symbol> table)
{
	int lcv = 0;					// Start At Begining
	while (lcv < table.size())		// Search For Label
	{
		if (word == table[lcv].label)
			return table[lcv].address;	// Return Label Address
		else
			lcv++;
	}
	//	Debug
	cout << "Address of Label " << word << " Not Found" <<endl;
	return -127;					// Return False Address If Label Not Found
}