3

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
chue x
  • 18,573
  • 7
  • 56
  • 70

3 Answers3

1

My problem was accessing the an index of the array which doesn't exist i forgot that when identifying an array in C++ You type the number of elements inside the array and not what the last index of the array would be. what got me confused was the place where the error is thrown, when debugging with break points , it would throw the exception while exiting the void main() and not while accessing the unidentified array element.

1

Unlike most other high-level languages, C++ does not necessarily perform array index checking. Because of this, indexing errors result in undefined behavior. In your case this resulted in a message at exit, but quite often this type of error goes completely unreported.

There are solutions to this though, notably the use of more modern array-type containers such as std::array and std::vector. For reasons of speed optimization, these also do not perform index checking in the default mode of operation, however there are ways that you can enable it. See my answer here.

Community
  • 1
  • 1
Brent Bradburn
  • 51,587
  • 17
  • 154
  • 173
0

This is not an answer but cannot be fitted into a comment

Here we go...

bool tobool(char x)
{
    if (x == '0')
        return 0;
    else
        return 1;

should be

bool tobool(char x)
{
   return x == '0'? false : true;
}

Then

 void register_encode(string Register, bool &address2, bool &address3, bool &address4, bool &address5, bool &address6)

{

should be

void register_encode(const std::string& Register, bool &address2, bool &address3, bool &address4, bool &address5, bool &address6)

{

Ditto with

count_commas

And I am sure that there is a method in std::string to do this

Also

void main()

should be

 int main()
Ed Heal
  • 59,252
  • 17
  • 87
  • 127