-
April 3rd, 2014, 03:32 AM
#1
[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.
-
April 3rd, 2014, 05:48 AM
#2
Re: Wombat 1
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 3rd, 2014, 07:36 AM
#3
Re: Wombat 1
Originally Posted by rockx
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.
-
April 4th, 2014, 05:57 AM
#4
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.
-
April 21st, 2014, 07:38 PM
#5
Re: Wombat 1
--------------------------------------------------
Please pardon me for having bad English.
-
April 22nd, 2014, 09:00 AM
#6
Re: Wombat 1
Originally Posted by rockx
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 24th, 2014, 12:49 AM
#7
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.
-
April 24th, 2014, 05:53 AM
#8
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 02:22 AM.
Reason: Re think after seeing input file in post #10
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 24th, 2014, 06:39 AM
#9
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 01:43 AM.
Reason: see post #8
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 24th, 2014, 04:51 PM
#10
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.
-
April 24th, 2014, 05:56 PM
#11
Re: Wombat 1
Originally Posted by rockx
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?
-
April 25th, 2014, 02:21 AM
#12
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 25th, 2014, 03:22 AM
#13
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 25th, 2014, 09:07 PM
#14
Re: Wombat 1
Originally Posted by 2kaud
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.
-
April 25th, 2014, 09:19 PM
#15
Re: Wombat 1
Sorry, I didnt answer earlier. the WOMBAT code was absolutely correct.
--------------------------------------------------
Please pardon me for having bad English.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|