C++ Programs
       
  - Assembler Compiler    
  - Eratosthenes Program    
       
       
       
  Assemble Compiler    
       
 

//assembler.cpp

//Lab.5-7. Assembler

// G.Amila Goonawardena

/*contain all global definations used by assemble functions*/

#include <afxwin.h>

#include "assembler.h"

/*expects filename which is to be assembles and returns error strings*/

CString assemble (CString filename)

{

ofstream out ;

ifstream in ;

typeMOT MOT[15] ;

typePOT POT[15] ;

symbol symTab[15] ;

symbol hashedSymTab[150] ;

int k = getMOT (MOT) ; //get mot instruction from file into array

int h = getPOT (POT) ; //get pot instruction from file into array

char lineno[4], errorcount[4] ;

CString error="" ;

CString first, second, third ;

CString ins, op ;

int lenght, type, repetition, temp, symcount=0 ;

int linecounter=0, flag=0 , errorcounter=0, loc=0;

char line[LINESIZE+1] ;

out.open("output.txt"); //outputs into sample output.txt

in.open(filename, ios::nocreate) ;//open incoming file

out << setw(5) << "LOC" << setw(8) << "OBJECT" //header in output file

<< setw(20) << "SOURCE STATEMENT\n\n" ;

while (in.good()) //read one line at a time from input file

{

linecounter++ ; //track line number

in.getline(line, LINESIZE) ; //getline from input file

breakline (line, first, second, third) ;//break line into symbol, instrutcion and operands

flag=compareMOT (second, MOT, k); //compare with MOT instructions

if (flag!=-1) //mot instruction found

{

MOT[flag].retreiveInfo(ins, op, lenght, type) ;

out << setw(6) << setfill ('0') << setiosflags (ios::right)

<< hex << loc << "\t" << ins << "\t" << line << endl ; //output according to mot instruction

if (first!="") //check for symbol

{

symTab[symcount].saveInfo(first, loc, lenght) ;

symcount++ ;

}

loc+=lenght ;

}

else

{

flag=comparePOT (second, POT, h) ;//else check for pot instruction

if (flag!=-1) //pot instruction found

{

if (flag==999) //check for DC instruction

ins="DC";

else if (flag==1000) //check for DS instruction

ins="DS";

else

POT[flag].retreiveInfo(ins) ;

if (flag==999 || flag==1000) //if DC or DS instrution output accordingly

{

repetition=DSDCroutine(third, loc, lenght) ;

out << setw(6) << setfill ('0') << setiosflags (ios::right)

<< hex << loc << "\t" << ins << "\t" << line << endl ;

if (first!="")

{

symTab[symcount].saveInfo(first, loc, lenght) ;

symcount++ ;//check for symbol

}

loc+=lenght ;

for (int loopi=0; loopi<repetition-1 ;loopi++)//check for repetition factor

{

temp=DSDCroutine(third, loc, lenght) ;

out << setw(6) << setfill ('0') << setiosflags (ios::right)

<< hex << loc << "\t" << ins << "\t" << line << endl ;

if (first!="")

{

symTab[symcount].saveInfo(first, loc, lenght) ;

symcount++ ;

}

loc+=lenght ;

}

}

else //if pot instruction other than DC or DS output accordingly

{

out << setw(6) << setfill ('0') << setiosflags (ios::right)

<< hex << loc << "\t" << ins << "\t" << line << endl ;

if (first!="") //check for symbol

{

symTab[symcount].saveInfo(first, loc, 0) ;

symcount++ ;

}

}

}

else //instruction unrecognized .. error condition

{

out << "ERROR (LINE:" << linecounter << ")" << "\t" << line<< endl ;

errorcounter++ ;

_itoa(linecounter, lineno, 10) ;

if(linecounter<10)

error+="0" ;

error+=lineno ; //add line number to error string

}

}

}

hashSymTable(symTab,hashedSymTab, symcount) ; //hash and print the symboltable

_itoa(errorcounter, errorcount, 10) ;

if (errorcounter>0&&errorcounter<10) //insert number of error in first two position of the error string

{

error.Insert(0,"0") ;

error.Insert(1,errorcount) ;

}

else if(errorcounter>9)

error.Insert(0,errorcount);

out << error << endl ;

out.close() ;

in.close() ;

return error; //return error string

/*error string has on first two position -> no of error found

each subsequent two digits is the line numbers on which error found*/

}

/*fills the typeMOT array from file mot.in*/

int getMOT (typeMOT MOT[])

{

ifstream in ;

in.open("mot.in") ;

char i[6], o[6];

int l, t, k=0 ;

while (in>>ws && in.good())

{

in >> i >> o >> l >> t ;

MOT[k].saveInfo(i,o,l,t) ;

k++ ;

}

in.close() ;

return k ;

}

/*fills the typePOT array from file pot.in*/

int getPOT (typePOT POT[])

{

ifstream in ;

in.open("pot.in") ;

char i[7] ;

int h=0 ;

while (in>>ws && in.good())

{

in >> i ;

POT[h].saveInfo(i) ;

h++ ;

}

in.close() ;

return h ;

}

/*break input line into three segments for symbol, instruction and operands(a,b,c)*/

/*line is substringed when encountered white spaces*/

void breakline (char line[], CString &a, CString &b, CString &c)

{

a=b=c="";

int counter=0 ;

char *token ;

if (line[0]==' ')

counter=1 ;

token = strtok(line, " ") ;

while (token!=NULL)

{

if (counter==0)

a=token ;

else if(counter==1)

b=token ;

else

c=token ;

token = strtok(NULL, " ") ;

counter++ ;

}

a.Remove(' ') ;

a.MakeUpper() ;

b.Remove(' ') ;

b.MakeUpper() ;

c.Remove(' ') ;

c.MakeUpper() ; //change to uppercase ..... :-)

}

//compares instruction to MOT. k= number of instruction in typeMOT array

int compareMOT (CString ins, typeMOT MOT[], int k)

{

CString a,b ;

int c,d ;

int compare ;

for (int i=0; i<k; i++)

{

MOT[i].retreiveInfo(a,b,c,d) ;

compare=strcmp(ins, a) ;

if (compare==0)

return i ;//return what index instruction found in typeMOT array

}

return -1 ;//not found, return -1

}

//compares instruction to POT. h= number of instruction in typePOT array

int comparePOT (CString ins, typePOT POT[], int h)

{

CString a ;

int compare ;

for (int i=0; i<h; i++)

{

POT[i].retreiveInfo(a) ;

compare=strcmp(ins,a);

if (compare==0)

{

compare=strcmp(ins,"DS");

if (compare==0)

return 1000 ;

else

{

compare=strcmp(ins,"DC");

if (compare==0)

return 999 ;

else

return i ; //return index of where the instruction found in POT array

}

}

}

return -1 ; // not found, return -1

}

/*increment loc according to the content of the DC or DS intruction*/

int DSDCroutine (CString code, int &loc, int &len)

{

int alpha, i=0 ;

CString a;

alpha = IsCharAlpha (code[0]) ;

if (alpha==0)

{

a=code[0] ;

alpha=atoi(a) ;

i=1 ;

}

switch (code[i])

{

case 'F' : case 'f' : //full word

while (loc%4!=0) //loc should be evenly divisible by 4

loc++ ;

len=4 ;

break ;

case 'H' : case 'h' : //half word

while (loc%2!=0) //loc should be evenly divisible by 2

loc++ ;

len=2 ;

break ;

default :

len=1 ;

break ;

}

return alpha ;

}

/*hash index rows of passed symtable into newtable calls to print it*/

void hashSymTable(symbol table[], symbol newtable[], int index)

{

CString sym ;

char ch ;

int loc, lenght ;

int x, flag=1 ;

for (int i=0; i<index ;i++)

{

if (table[i].info()==1)

{

table[i].retreiveInfo(sym,loc,lenght) ;

ch=sym[0] ;

x=(ch-65)*5 ;

flag=1 ;

while (flag==1)

{

if (newtable[x].info()==0)

{

newtable[x].saveInfo(sym,loc,lenght) ;

flag=0 ;

}

else

x++ ;

}

}

}

printSymTable(newtable, 150) ;

}

/*prints index rows of a symtable type object into an output file*/

void printSymTable(symbol table[], int index)

{

ofstream out ;

out.open("symbolTable.txt" ) ;

out << "Symbol" << setw(6) << "LOC" << setw(10) << "Lenght\n" ;

int loc, lenght ;

CString sym ;

for (int i=0 ; i<index ; i++)

{

if (table[i].info()==1)

{

table[i].retreiveInfo(sym, loc, lenght) ;

out << sym << "\t" << hex << setw(6) << setfill('0')

<< loc << "\t" << lenght << endl ;

}

}

out.close() ;

}

 
     
Eratosthenes Program
  //G.Amila Goonawardena CSC228 Mar 26 2001
//A simple and efficient method to determine all prime numbers not exeecing a given positive integer.

#include<iostream.h> //standard Header file
#include<math.h> //for sqrt function
#include"ourtools.h" //for vprn function

#define max_size 1000 //definition of size of array

int sieve(int n, int primes[]); //declare the function

void main()
{
int intarray[max_size] ={0}; //initialize the array to zeros
int numbers;

numbers=sieve(max_size,intarray);//call to the function

vprn<<numbers<<" prime numbers between 0 and less than or equal to "<<max_size<<" range.\n";
//Message for the how many primes in that range
}

int sieve(int n, int primes[]) //function header
{
int i,j,k; //Declare the index variables
int limit; //Declare a variable for squreroot of n value
int count=0; //Declare a variable for # of prime # in array

limit = int(sqrt(n)); //find the value of squreroot of n value


for(i =2; i<limit+2; i++) //run the for loop from 2 to limit+2
{ //we start from 2 because we need to skip 0, 1 values
//we know 0 is not a prime and every integer divisable by 1
if(primes[i]==0) //when the array value is 0
{
for(j= 2*i; j<n ; j = j+i)//Check the multiple values of i until end of array
primes[j] = 1; //if it is change array value 0 to 1
}

}

for(k=1; k<=n; k++) //Go through the array by skipping 1st value 0
{
if(primes[k]==0) //If the array value is o
{
vprn<<setw(6)<<k; //Print it location
count++; //Increase the count value by 1
if (count%10==0) //for make the 10 numbers for 1 row
vprn<<endl; //skip a line

}
}
vprn<<endl<<endl; //Skip to new line
return count; //return the count value
}

 

 

Copyright © 2003 Amila Goonawardena . All rights reserved.
Revised: 05/18/04.