[RESOLVED] Wombat 1
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: [RESOLVED] Wombat 1

  1. #1
    Join Date
    May 2004
    Posts
    209

    [RESOLVED] Wombat 1

    I guess the above is one of the ancient methods of low level programming. Say for example if I have a bunch of Wombat 1 code (which i may not have the best of knowledge), how can i use C++ to interpret/translate the code.

    assuming the codes are in separate text files
    --------------------------------------------------
    Please pardon me for having bad English.

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,847

    Re: Wombat 1

    Have a look at http://cr4.globalspec.com/thread/80881/C-Interpreter

    To use c++ to interpret the code you will need to write an interpreter/parser. See http://en.wikipedia.org/wiki/Parsing#Computer_languages
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Jul 2013
    Posts
    375

    Re: Wombat 1

    Quote Originally Posted by rockx View Post
    I guess the above is one of the ancient methods of low level programming. Say for example if I have a bunch of Wombat 1 code (which i may not have the best of knowledge), how can i use C++ to interpret/translate the code.

    assuming the codes are in separate text files
    I've never heard about Wombat. It seems to be both a highlevel functional language and a CPU.

    I googled a little and came accross this,

    http://www.cs.colby.edu/djskrien/CPUSim/

    http://en.wikipedia.org/wiki/CPU_Sim

    CPUSim seems to be a CPU simulation package written in Java. That's nice but the good news is that it comes with an example CPU namely Wombat 1.

    So one approach would be to download CPUSim, get it to simulate Wombat 1, and then try to run the Wombat assembly files you have.

    I don't know what you want to accomplish but the next step could be to study the internals of CPUSim and try to understand what a simulation program might look like.

    Since Wombat 1 only has 12 instructions it shouldn't be too hard to write a simulator for it or a cross-assembler that translates the Wombat code to some other assembly language or even C.

  4. #4
    Join Date
    May 2004
    Posts
    209

    Re: Wombat 1

    well i have hada copy of the CPU Simulator. and I have coded a simple addition program and a program which returns the square of the number input.

    The requirement however, needs to read a well written Wombat 1 code from a *.txt file and perform actions.

    So what i came up with was, to read and store each line from the text file and save line by line in a vector or a List. So later discard everything from all the nodes except the 0's and 1's. Later seperate the operand and the opcodes. And than translate/interpret it. All tis to be done in C++
    --------------------------------------------------
    Please pardon me for having bad English.

  5. #5
    Join Date
    May 2004
    Posts
    209

    Re: Wombat 1

    Anyone?
    --------------------------------------------------
    Please pardon me for having bad English.

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,847

    Re: Wombat 1

    Quote Originally Posted by rockx View Post
    Anyone?
    For what? How far have you got with your coding as per your top level design as per your post #4?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    Join Date
    May 2004
    Posts
    209

    Re: Wombat 1

    This is what I have done so far. but something doesnt seem right to me


    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <sstream>
    
    using namespace std;
    
    int dlocation[100][2];//holds data location line number at 0 + value being held at location at 1
    
    int binarycon(string x)
    {
        //binarycon function converts string to its decimal equivalent
        //and returns integer value
          int sum=0;
    
          if(x.substr(0,1)=="1")
          {
            sum=sum+2048;
          }
          if(x.substr(1,1)=="1")
          {
            sum=sum+1024;
          }
          if(x.substr(2,1)=="1")
          {
            sum=sum+512;
          }
          if(x.substr(3,1)=="1")
          {
            sum=sum+256;
          }
          if(x.substr(4,1)=="1")
          {
            sum=sum+128;
          }
          if(x.substr(5,1)=="1")
          {
            sum=sum+64;
          }
          if(x.substr(6,1)=="1")
          {
            sum=sum+32;
          }
          if(x.substr(7,1)=="1")
          {
            sum=sum+16;
          }
          if(x.substr(8,1)=="1")
          {
            sum=sum+8;
          }
          if(x.substr(9,1)=="1")
          {
            sum=sum+4;
          }
          if(x.substr(10,1)=="1")
          {
            sum=sum+2;
          }
          if(x.substr(11,1)=="1")
          {
            sum=sum+1;
          }
    
    
      return sum;
    }
    
    int datalocate(int x)
    {
        //locates the appropriate data location number and returns integer
        for(int i=0;i<=100;i++)
        {
            if(dlocation[i][0]==x)
            {
                return i;
            }
        }
    }
    int main()
    {
         string marray[100][2];//holds machine code instructions split into opcode(0) + memory address(1)
         int m=0;
         int acc=0; //holds value in accumulator
         string line;
         string filename;
        
         cout << "\nEnter machine language program file path\n\t:";
         cin >> filename;
         ifstream myfile;
         myfile.open(filename.c_str());
    
         if (myfile.is_open())
         {
            /*loop reads machine instructions from file as string, splits it into (0)opcode and (1)address
            and assigns results to 2D array - marray*/
            while (!(myfile.eof()))
            {
              getline (myfile,line);
    
              marray[m][0]=line.substr(0,4);
              marray[m][1]=line.substr(4,12);
    
              if(marray[m][0]=="0000")
              {
                  dlocation[m][0]=m;//assigns line number
                  dlocation[m][1]=binarycon(marray[m][1]);//assigns default value of data location
              }
    
              m=m+2;//counter-to simulate wombat it increments in twos
    
             }
    
            myfile.close();
         }
         else{cout << "\n\t!!File not Found!!\n\n\n"; 
         system("PAUSE");
         return 0;
         }
    
        int iarray[100]; //holds input values
        
        
        cout << "\n\nEnter input file path\n\t: ";
        cin >> filename;
        ifstream infile;
        infile.open(filename.c_str());
    
         if (infile.is_open())
         {
            int n=0;
    
            while (!(infile.eof()))
            {
              int input;
              infile>> input;
              iarray[n]=input;
              n++;
              }
    
            infile.close();
         }
         else{cout << "\n\t!!File not Found!!\n\n\n"; system("PAUSE");return 0;}
    
         string status="loop";
         int x=0,c=0,d=0;
    
         while(status!="end"){
            //if statements compare opcodes and execute the appropriate operations
    
             if(marray[x][0]=="0000")
             {
                 status="end";
             }
             else if(marray[x][0]=="0001")
             {
                 c=binarycon(marray[x][1]);
                 acc=dlocation[datalocate(c)][1];
                 x=x+2;
             }
             else if(marray[x][0]=="0010")
             {
                 c=binarycon(marray[x][1]);
                 dlocation[datalocate(c)][1]=acc;
                 x=x+2;
             }
             else if(marray[x][0]=="0011")
             {
                 acc=iarray[d];
                 d++;
                 x=x+2;
             }
             else if(marray[x][0]=="0100")
             {
                 cout << "\n\tacc value is "<<acc;
                 x=x+2;
             }
             else if(marray[x][0]=="0101")
             {
                 c=binarycon(marray[x][1]);
                 acc=acc+dlocation[datalocate(c)][1];
                 x=x+2;
             }
             else if(marray[x][0]=="0110")
             {
                 c=binarycon(marray[x][1]);
                 acc=acc-dlocation[datalocate(c)][1];
                 x=x+2;
             }
             else if(marray[x][0]=="0111")
             {
                 c=binarycon(marray[x][1]);
                 acc=(acc*dlocation[datalocate(c)][1]);
                 x=x+2;
             }
             else if(marray[x][0]=="1000")
             {
                 c=binarycon(marray[x][1]);
                 acc=(acc/dlocation[datalocate(c)][1]);
                 x=x+2;
             }
             else if(marray[x][0]=="1001")
             {
                x=binarycon(marray[x][1]);
             }
             else if(marray[x][0]=="1010")
             {
                 if(acc==0)
                 {
                     x=binarycon(marray[x][1]);
                 }
                 else{x=x+2;}
             }
             else if(marray[x][0]=="1011")
             {
                 if(acc<0)
                 {
                     x=binarycon(marray[x][1]);
                 }
                 else{x=x+2;}
             }
    
         }
    
        //writing output file
        cout << "\n\nEnter output data file path\n\t: ";
        cin >> filename;
        ofstream out_file;
        out_file.open(filename.c_str());
        out_file << acc << endl;
    
        system("PAUSE");
        return 0;
    
    }
    --------------------------------------------------
    Please pardon me for having bad English.

  8. #8
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,847

    Re: Wombat 1

    In c++11 you can use stoi(x, NULL, 2). For c++03, consider
    Code:
    inline int binarycon(string x)
    {
    const int bin[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048};
    
    int l;
    
    int sum = 0;
    
    	if (((l = (int)x.length() - 1) > 11) || (l < 1))
    		return 9999;
    
    	for (int i = l; i >= 0; --i)
    		if (x[i] == '1')
    			sum += bin[l - i];
    
    	return sum;
    }
    Last edited by 2kaud; April 25th, 2014 at 03:22 AM. Reason: Re think after seeing input file in post #10
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,847

    Re: Wombat 1

    I've tidied up a bit your code. Are you getting the expected answer? As I haven't got any input files to test with or definitions of the opcodes, I can't really provide much more guidance other than to use the debugger to step through the code and make sure its doing what is expected from the program design.

    Good hunting!

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    int dlocation[100][2];//holds data location line number at 0 + value being held at location at 1
    
    inline int binarycon(string x)
    {
    	return stoi(x, NULL, 2);
    }
    
    int datalocate(int x)
    {
        //locates the appropriate data location number and returns integer
        for (int i = 0; i <= 100; i++)
            if (dlocation[i][0] == x)
                return i;
    
    	return 0;
    }
    
    int main()
    {
    string marray[100][2];       //holds machine code instructions split into opcode(0) + memory address(1)
    string line;
    string filename;
    
    int m = 0;
    int acc = 0;        //holds value in accumulator
        
    	cout << "\nEnter machine language program file path\n\t:";
    	cin >> filename;
    
    ifstream myfile(filename.c_str());
    
         if (!myfile.is_open())
         {
    		cout << "\n\t!!File not Found!!\n\n\n"; 
    		system("PAUSE");
    		return 1;
    	 }
    
    	/*loop reads machine instructions from file as string, splits it into (0)opcode and (1)address
    	and assigns results to 2D array - marray*/
    	while (myfile.good())
    	{
    		getline (myfile,line);
    
    		marray[m][0] = line.substr(0, 4);
    		marray[m][1] = line.substr(4, 12);
    
    		if (marray[m][0] == "0000")
    		{
    			dlocation[m][0] = m;                             //assigns line number
    			dlocation[m][1] = binarycon(marray[m][1]);       //assigns default value of data location
    		}
    
    		m += 2;         //counter-to simulate wombat it increments in twos
    	}
    
    	myfile.close();
    
    int iarray[100];    //holds input values
    
    	cout << "\n\nEnter input file path\n\t: ";
    	cin >> filename;
    
    ifstream infile(filename.c_str());
    
    	if (!infile.is_open())
    	{
    		cout << "\n\t!!File not Found!!\n\n\n";
    		system("PAUSE");
    		return 1;
    	}
    
    	for (int n = 0; infile.good(); n++)
    		infile >> iarray[n];
    
    	infile.close();
    
    int x = 0,
        c = 0,
        d = 0;
    
    bool pend = false;
    
    	while (!pend)
    	{
    		switch (binarycon(marray[x][0]))
    		{
    			case 0:
    				pend = true;
    				break;
    
    			case 1:
    				c = binarycon(marray[x][1]);
    				acc = dlocation[datalocate(c)][1];
    				x += 2;
    				break;
    
    			case 2:
    				c = binarycon(marray[x][1]);
    				dlocation[datalocate(c)][1] = acc;
    				x += 2;
    				break;
    
    			case 3:
    				acc = iarray[d++];
    				x += 2;
    				break;
    
    			case 4:
    				cout << "\n\tacc value is " << acc << endl;
    				x += 2;
    				break;
    
    			case 5:
    				c = binarycon(marray[x][1]);
    				acc = acc + dlocation[datalocate(c)][1];
    				x += 2;
    				break;
    
    			case 6:
    				c = binarycon(marray[x][1]);
    				acc = acc - dlocation[datalocate(c)][1];
    				x += 2;
    				break;
    
    			case 7:
    				c = binarycon(marray[x][1]);
    				acc = (acc * dlocation[datalocate(c)][1]);
    				x += 2;
    				break;
    
    			case 8:
    				c = binarycon(marray[x][1]);
    				acc = (acc / dlocation[datalocate(c)][1]);
    				x += 2;
    				break;
    
    			case 9:
    				x = binarycon(marray[x][1]);
    				break;
    
    			case 10:
    				x = (acc == 0) ? binarycon(marray[x][1]) : x + 2;
    				break;
    
    			case 11:
    				x = (acc < 0) ? binarycon(marray[x][1]) : x + 2;
    				break;
    
    			default:
    				cout << "Bad op\n";
    
    		}
    	}
    
    	//writing output file
    	cout << "\n\nEnter output data file path\n\t: ";
    	cin >> filename;
    
    ofstream out_file (filename.c_str());
    
    	out_file << acc << endl;
    
    	system("PAUSE");
    	return 0;
    }
    Last edited by 2kaud; April 25th, 2014 at 02:43 AM. Reason: see post #8
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  10. #10
    Join Date
    May 2004
    Posts
    209

    Re: Wombat 1

    you can use this text file to test the code.

    Code:
    0011000000000000 ; 3 0 	 Line 0 read in a number
    0010000000100110 ; 2 38  Line 2 store the number at location 46, the storage for 
    1010000000001010 ; 10 10 Line 4 jump to line  if the number is == 0 goto 10
    1011000000010000 ; 11 16 Line 6 if n < 0  goto line 16
    1001000000011010 ; 9 26  Line 8 if n > 0 go to line 26
    0001000000101000 ; 10 38 Line 10 load line 50 where constant 10 is stored
    0100000000000000 ; 4 0   Line 12 write to screen
    0000000000000000 ; 0 0   Line 14 stop 
    0111000000100110 ; 7 38  Line 16 n x n = n ^ 2
    0111000000100110 ; 7 38  Line 18 n x n = n ^ 3
    0111000000100110 ; 7 38  Line 20 n x n = n ^ 4
    0111000000100110 ; 7 38  Line 22 n x n = n ^ 5
    1001000000001100 ; 9 12  Line 24 jump to location 12
    0111000000100110 ; 7 38  Line 26 n x n = n ^ 2
    0110000000100110 ; 6 38  Line 28 accumulator - n = n ^ 2 - n
    0110000000100110 ; 6 38  Line 30 accumulator - n = n ^ 2 - 2n
    0101000000101010 ; 5 42  Line 32 accumulator + 2 = n ^ 2 - 2n + 2
    1001000000001100 ; 9 12  Line 34 jump to location 12
    0000000000000000 ; 0 0   Line 36
    0000000000000000 ; 0 0   Line 38 store tem result n
    0000000000001010 ; 0 10  Line 40 stores the constant term 10
    0000000000000010 ; 0 2   Line 42 stores the constant term 2
    anything after the semicolon ( is considered a comment so it should be discarded
    --------------------------------------------------
    Please pardon me for having bad English.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,478

    Re: Wombat 1

    Quote Originally Posted by rockx View Post
    you can use this text file to test the code.
    Since 2kaud was nice enough to rewrite the code for you, why don't you test the code?

  12. #12
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,847

    Re: Wombat 1

    Note that for line 10 of the wombat text, the description does not match the code. I think the code is right?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  13. #13
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,847

    Re: Wombat 1

    I've cleaned up the code some more now that I know what the input file looks like. What does the wombat program supposed to do? Is the wombat code correct for this?

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    const int MAXLINES = 200;
    
    int binarycon(string x)
    {
    const int bin[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048};
    
    int l,
        sum = 0;
    
    	if (((l = (int)x.length() - 1) > 11) || (l < 1))
    		return 9999;
    
    	for (int i = l; i >= 0; --i)
    		if (x[i] == '1')
    			sum += bin[l - i];
    
    	return sum;
    }
    
    int main()
    {
    int marray[MAXLINES][2] = {0};       //holds machine code instructions split into opcode(0) + memory address(1)
    
    string	line,
    	filename;
        
    	cout << "Enter machine language program file path: ";
    	cin >> filename;
    
    ifstream myfile(filename.c_str());
    
         if (!myfile.is_open())
         {
    		cout << "!!File not Found!!\n"; 
    		//system("PAUSE");
    		return 1;
         }
    
    	/*loop reads machine instructions from file as string, splits it into (0)opcode and (1)address
    	and assigns results to 2D array - marray*/
    	for (int m = 0; myfile.good() && m < MAXLINES; m +=2)
    	{
    		getline (myfile, line);
    
    		if (line.length() >= 16) {
    			marray[m][0] = binarycon(line.substr(0, 4));
    			marray[m][1] = binarycon(line.substr(4, 12));
    		}
    	}
    
    	myfile.close();
    
    int iarray[MAXLINES];    //holds input values
    
    	cout << "Enter input file path: ";
    	cin >> filename;
    
    ifstream infile(filename.c_str());
    
    	if (!infile.is_open())
    	{
    		cout << "!File not Found!!\n";
    		//system("PAUSE");
    		return 1;
    	}
    
    	for (int n = 0; infile.good() && n < MAXLINES; n++)
    		infile >> iarray[n];
    
    	infile.close();
    
    int	x = 0,
    	d = 0,
    	c = 0,
    	acc = 0;        //holds value in accumulator
    
    bool pend = false;
    
    	while (!pend)
    	{
    		if (x >= MAXLINES) {
    			cout << "Invalid line number: " << x << endl;
    			break;
    		}
    
    		switch (marray[x][0])
    		{
    			case 0:
    				pend = true;
    				break;
    
    			case 1:
    				if ((c = marray[x][1]) < MAXLINES)
    					acc = marray[c][1];
    
    				x += 2;
    				break;
    
    			case 2:
    				if ((c = marray[x][1]) < MAXLINES)
    					marray[c][1] = acc;
    
    				x += 2;
    				break;
    
    			case 3:
    				//acc = iarray[d++];
    				cout << "Enter a number: ";
    				cin >> acc;
    				x += 2;
    				break;
    
    			case 4:
    				cout << "Acc value is: " << acc << endl;
    				x += 2;
    				break;
    
    			case 5:
    				if ((c = marray[x][1]) < MAXLINES)
    					acc = acc + marray[c][1];
    
    				x += 2;
    				break;
    
    			case 6:
    				if ((c = marray[x][1]) < MAXLINES)
    					acc = acc - marray[c][1];
    
    				x += 2;
    				break;
    
    			case 7:
    				if ((c = marray[x][1]) < MAXLINES)
    					acc = acc * marray[c][1];
    
    				x += 2;
    				break;
    
    			case 8:
    				if ((c = marray[x][1]) < MAXLINES)
    					acc = acc / marray[c][1];
    
    				x += 2;
    				break;
    
    			case 9:
    				x = marray[x][1];
    				break;
    
    			case 10:
    				x = (acc == 0) ? marray[x][1] : x + 2;
    				break;
    
    			case 11:
    				x = (acc < 0) ? marray[x][1] : x + 2;
    				break;
    
    			default:
    				cout << "Bad op " << marray[x][0] << " in line " << x << endl;
    				pend = true;
    
    		}
    	}
    
    	//writing output file
    	cout << "\nEnter output data file path: ";
    	cin >> filename;
    
    ofstream out_file (filename.c_str());
    
    	if (!out_file.is_open())
    	{
    		cout << "!File not opened!!\n";
    		//system("PAUSE");
    		return 1;
    	}
    
    	out_file << acc << endl;
    	out_file.close();
    
    	//system("PAUSE");
    
    	return 0;
    }
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  14. #14
    Join Date
    May 2004
    Posts
    209

    Re: Wombat 1

    Quote Originally Posted by 2kaud View Post
    Note that for line 10 of the wombat text, the description does not match the code. I think the code is right?
    Yeah the comment should have read line 40. I did some line deletions so i reduced my program from 52 lines to 42 lines. At line 40, the constant 10 is stored.

    That particular code was for a an instruction set as follows:

    1. User enters an integer n
    2. If n < 0 then n^5 (n x n x n x n x n); if n = 0 then 10; if n > 0 then n^2 - 2n +2
    3. THere was no looping, it was just a single run program.
    --------------------------------------------------
    Please pardon me for having bad English.

  15. #15
    Join Date
    May 2004
    Posts
    209

    Re: Wombat 1

    Sorry, I didnt answer earlier. the WOMBAT code was absolutely correct.
    --------------------------------------------------
    Please pardon me for having bad English.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center