I have a code of mine which is supposed to be an assembler for a mips processor for a project in college.
Now my problem is when executing the program everything goes right until the program is exiting the main it throws this error:
stack around the variable
binaryinstruction
was corrupted .
The error is thrown when exiting the main and I can't find a valid solution to this anywhere.
Below a sample of my code:
#include <iostream>
#include <fstream>
#include <string>
#include <bitset>
using namespace std;
//function to convert string to binary
bool tobool(char x)
{
if (x == '0')
return 0;
else
return 1;
}
//function to decode the register name into an address using call by refrence to modify the instruction boolean encoding array
void register_encode(string Register, bool &address2, bool &address3, bool &address4, bool &address5, bool &address6)
{
if (Register == "$zero")
{
address2 = 0; address3 = 0; address4 = 0; address5 = 0; address6 = 0;
}
else if (Register == "$t3")
{
address2 = 0; address3 = 1; address4 = 0; address5 = 1; address6 = 1;
}
else if (Register == "$s2")
{
address2 = 1; address3 = 0; address4 = 0; address5 = 1; address6 = 0;
}
}
//function to count the number of occurence of commas (,) inside the string
int count_commas(string s) {
int count = 0;
for (int i = 0; i < s.size(); i++)
if (s[i] == ',') count++;
return count;
}
void main()
{
int numberofinstruction = 0;
string testingstring;
string line[64];
ifstream myfile;
myfile.open("test.txt");
// taking input from file into an array "line" which holds every single line inside the array in a single element inside the array
while (getline(myfile, testingstring))
{
line[numberofinstruction] = testingstring;
numberofinstruction++;
}
myfile.close();
//transform to binary
bool binaryinstruction[31];
string firstterm, secondterm, thirdterm, fourthterm;
for (int counter = 0; counter<numberofinstruction; counter++)
{
switch (count_commas(line[counter])) {
case 1:
firstterm = line[counter].substr(0, line[counter].find(" "));
secondterm = line[counter].substr(line[counter].find(" ") + 1, line[counter].find(",") - line[counter].find(" ") - 1);
thirdterm = line[counter].substr(line[counter].find(",") + 1, line[counter].size() - line[counter].find(","));
if (firstterm == "lw")
{ //LW encoding
binaryinstruction[0] = 1;
binaryinstruction[1] = 0;
binaryinstruction[2] = 0;
binaryinstruction[3] = 0;
binaryinstruction[4] = 1;
binaryinstruction[5] = 1;
register_encode(secondterm, binaryinstruction[11], binaryinstruction[12], binaryinstruction[13], binaryinstruction[14], binaryinstruction[15]);
string offset = thirdterm.substr(0, thirdterm.find('('));
offset = bitset<16>(stoi(offset)).to_string();
for (int x = 0; x <= 15; x++)
{
binaryinstruction[16 + x] = tobool(offset[x]);
}
string lwregister = thirdterm.substr(thirdterm.find('$'), thirdterm.size() - thirdterm.find('(') - 2);
register_encode(lwregister, binaryinstruction[6], binaryinstruction[7], binaryinstruction[8], binaryinstruction[9], binaryinstruction[10]);
}
else
{ //SW encoding
binaryinstruction[0] = 1;
binaryinstruction[1] = 0;
binaryinstruction[2] = 1;
binaryinstruction[3] = 0;
binaryinstruction[4] = 1;
binaryinstruction[5] = 1;
//begin
register_encode(secondterm, binaryinstruction[11], binaryinstruction[12], binaryinstruction[13], binaryinstruction[14], binaryinstruction[15]);
string offset = thirdterm.substr(0, thirdterm.find('('));
offset = bitset<16>(stoi(offset)).to_string();
for (int x = 0; x <= 15; x++)
{
binaryinstruction[16 + x] = tobool(offset[x]);
}
string lwregister = thirdterm.substr(thirdterm.find('$'), thirdterm.size() - thirdterm.find('(') - 2);
register_encode(lwregister, binaryinstruction[6], binaryinstruction[7], binaryinstruction[8], binaryinstruction[9], binaryinstruction[10]);
//end
}
break;
}
}
//testing
ofstream myfile2("testout.txt");
for (int f = 0; f <= 31; f = f + 8)
{
myfile2 << binaryinstruction[f] << binaryinstruction[f + 1] << binaryinstruction[f + 2] << binaryinstruction[f + 3] << binaryinstruction[f + 4] << binaryinstruction[f + 5] << binaryinstruction[f + 6] << binaryinstruction[f + 7] << endl;
}
//end testing
}
In the test file I have 1 line which is lw $t3,4($s2)
, the output to be written within the testout file is as expected to be:
10001110
01001011
00000000
00000100