0

私はMIPS命令セットシミュレータを実装している最中です。しかし、コードをコンパイルしようとすると、「範囲外」のエラーが発生し続け、非常にイライラするようになります。私はあらゆる種類のバリエーションを試しましたが、何も役に立たないようです。
これが私のMIPSsim.cppファイルです

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>

using namespace std;
const int BITS = 32;

string inst_decode(string line);
void execute(string line, string instruction, int &registers[]);
void print(int cycle, int inst_address, int regs[]/*, int data[]*/);
int convert_5(string binary);
int convert_32(string binary);
void add(string line, int &registers[]);
void sub(string line, int &registers[]);
void mult(string line, int &registers[]);


string inst_decode(string line){
    string instruction; 
    if(line.compare(0,1,"0") == 0){
        if (line.compare(1,5,"00010") == 0){
            cout << "JUMP" << endl;
            instruction = "JUMP";
        }else if (line.compare(1,5,"00100") == 0){
        cout << "BEQ" << endl;
        instruction = "BEQ";
    }else if (line.compare(1,5,"00001") == 0){
        cout << "BLTZ" << endl;
        instruction = "BLTZ";
    }else if (line.compare(1,5,"00111") == 0){
        cout << "BGTZ" << endl;
        instruction = "BGTZ";
    }else if (line.compare(1,5,"01011") == 0){
        cout << "SW" << endl;
        instruction = "SW";
    }else if (line.compare(1,5,"00011") == 0){
        cout << "LW" << endl;
        instruction = "LW";
    }else if (line.compare(1,5,"00010") == 0){
        cout << "SRL" << endl;
        instruction = "SRL";
    }else if ((line.compare(1,5,"11100") == 0)&&(line.compare(26,6,"000010") == 0)){
        cout << "MUL" << endl;
        instruction = "MUL";
    }else if (line.compare(1,5,"00000") == 0){
        if (line.compare(26,6,"100000") == 0){
            cout << "ADD" << endl;
            instruction = "ADD";
        }else if (line.compare(26,6,"100010") == 0){
            cout << "SUB" << endl;
            instruction = "SUB";
        }else if (line.compare(26,6,"100100") == 0){
            cout << "AND" << endl;
            instruction = "AND";
        }else if (line.compare(26,6,"100101") == 0){
            cout << "OR" << endl;
            instruction = "OR";
        }else if (line.compare(26,6,"101010") == 0){
            cout << "SLT" << endl;
            instruction = "SLT";
        }else if (line.compare(26,6,"000000") == 0){
            cout << "SLL" << endl;
            instruction = "SLL";
        }else if (line.compare(6,26,"00000000000000000000000000") == 0){
            cout << "NOP" << endl;
            instruction = "NOP";
        }
    }
    }else{
        if (line.compare(26,6,"100100") == 0){
            cout << "ADDI" << endl;
            instruction = "ADDI";
        }else if (line.compare(26,6,"100010") == 0){
            cout << "ANDI" << endl;
            instruction = "ANDI";
        }
    }
    return instruction;
}

void execute(string line, string instruction, int& registers[]){
    if(instruction == "ADD"){
        add(line, registers);
    }else if(instruction == "ADDI"){
        addi(line, registers);
    }else if(instruction == "SUB"){
        sub(line, registers);
    }else if(instruction == "MUL"){
        mult(line, registers);
    }
}

void print(int cycle, int inst_address, int regs[]/*, int data[][]*/){
    cout << "---------------------------\n";
    cout << "Cycle: " << cycle << "\t" << inst_address << "\t" << "\n";
    cout << "\nRegisters\n";
    cout << "R00:\t";
    for(int i=0; i<=15; i++){
        cout << regs[i] << "\t";
    }
    cout << "\nR16:\t";
    for(int i=16; i<32; i++){
            cout << regs[i] << "\t";
    }
    cout << "\n\nData\n";
        //cout << "\n148:\t";
        //for(int i=0; i<8; i++){
        //  cout << data[i] << "\t";
        //}
        //cout << "\n180:\t";
        //for(int i=9; i<17; i++){
            //  cout << data[i] << "\t";
        //}
        //cout << "\n212:\t";
        //for(int i=18; i<26; i++){
        //  cout << data[i] << "\t";
    //}
    cout << "\n";
}


int main(){
    int registers[32] = {0};
        //  int data[2][32];
    string inst;
    //int cycle = 1;
    //int PC = 64;
    string binary_inst[64];

    ifstream inFile;
    inFile.open("sample.txt");
    if (!inFile) {
        cout << "Unable to open file sample.txt" << endl;
        return 0;
    }
    string x;
    while(!inFile.eof()){
        for(int i=0; i<64; i++){
            inFile >> x;
            binary_inst[i] = x;
            inst = inst_decode(binary_inst[i]);
            execute(binary_inst[i], inst, registers);
            //print(cycle, PC, registers);
        }
    } 
    //  print(cycle, PC, registers);

    return 0;
}



int convert_5(string binary) {
    long power = 32;
    long sum = 0;
    assert(binary.size() == 5);

    for ( int i=0; i<BITS; ++i) {
        if ( i==0 && binary[i]!='0') {
            sum = -32;
        }
        else {
            sum += (binary[i]-'0')*power;
        }
        power /= 2;
    }
    return sum;
}

int convert_32(string binary) {
    long power = 4294967296;
    long sum = 0;
    assert(binary.size() == 32);

    for ( int i=0; i<BITS; ++i) {
        if ( i==0 && binary[i]!='0') {
            sum = -4294967296;
        }
        else {
            sum += (binary[i]-'0')*power;
        }
        power /= 2;
    }
    return sum;
}



void add(string line, int& registers[]){
    int dest = convert_5(line.substr(17,5));
    cout << "Dest: " << dest << endl;

    int target = convert_5(line.substr(12,5));
    cout << "Target: " << target << endl;

    int source = convert_5(line.substr(7,5));
    cout << "Source: " << source << endl;

    registers[dest] = registers[source] + registers[target];
    cout << "Addition: " << registers[dest] << endl;
}

void sub(string line, int& registers[]){
    int dest = convert_5(line.substr(17,5));
    cout << "Dest: " << dest << endl;

    int target = convert_5(line.substr(12,5));
    cout << "Target: " << target << endl;

    int source = convert_5(line.substr(7,5));
    cout << "Source: " << source << endl;

    registers[dest] = registers[source] - registers[target];
    cout << "Subtraction: " << registers[dest] << endl;
}

void mult(string line, int& registers[]){
    int dest = convert_5(line.substr(17,5));
    cout << "Dest: " << dest << endl;

    int target = convert_5(line.substr(12,5));
    cout << "Target: " << target << endl;

    int source = convert_5(line.substr(7,5));
    cout << "Source: " << source << endl;

    registers[dest] = registers[source] * registers[target];
    cout << "Multiplication: " << registers[dest] << endl;
}

これが私が受け取り続けるエラーメッセージです:

lin114-08:10% make all
g++ -Wall -o MIPSsim MIPSsim.cpp
MIPSsim.cpp:10: error: declaration of ‘registers’ as array of references
MIPSsim.cpp:14: error: declaration of ‘registers’ as array of references
MIPSsim.cpp:15: error: declaration of ‘registers’ as array of references
MIPSsim.cpp:16: error: declaration of ‘registers’ as array of references
MIPSsim.cpp:85: error: declaration of ‘registers’ as array of references
MIPSsim.cpp: In function ‘void execute(...)’:
MIPSsim.cpp:86: error: ‘instruction’ was not declared in this scope
MIPSsim.cpp:87: error: ‘line’ was not declared in this scope
MIPSsim.cpp:87: error: ‘registers’ was not declared in this scope
MIPSsim.cpp:89: error: ‘line’ was not declared in this scope
MIPSsim.cpp:89: error: ‘registers’ was not declared in this scope
MIPSsim.cpp:89: error: ‘addi’ was not declared in this scope
MIPSsim.cpp:91: error: ‘line’ was not declared in this scope
MIPSsim.cpp:91: error: ‘registers’ was not declared in this scope
MIPSsim.cpp:93: error: ‘line’ was not declared in this scope
MIPSsim.cpp:93: error: ‘registers’ was not declared in this scope
MIPSsim.cpp: In function ‘int main()’:
MIPSsim.cpp:146: warning: cannot pass objects of non-POD type ‘struct std::string’     through ‘...’; call will abort at runtime
MIPSsim.cpp:146: warning: cannot pass objects of non-POD type ‘struct std::string’     through ‘...’; call will abort at runtime
MIPSsim.cpp: At global scope:
MIPSsim.cpp:193: error: declaration of ‘registers’ as array of references
MIPSsim.cpp: In function ‘void add(...)’:
MIPSsim.cpp:194: error: ‘line’ was not declared in this scope
MIPSsim.cpp:203: error: ‘registers’ was not declared in this scope
MIPSsim.cpp: At global scope:
MIPSsim.cpp:207: error: declaration of ‘registers’ as array of references
MIPSsim.cpp: In function ‘void sub(...)’:
MIPSsim.cpp:208: error: ‘line’ was not declared in this scope
MIPSsim.cpp:217: error: ‘registers’ was not declared in this scope
MIPSsim.cpp: At global scope:
MIPSsim.cpp:221: error: declaration of ‘registers’ as array of references
MIPSsim.cpp: In function ‘void mult(...)’:
MIPSsim.cpp:222: error: ‘line’ was not declared in this scope
MIPSsim.cpp:231: error: ‘registers’ was not declared in this scope
make: *** [MIPSsim] Error 1

どんな助けでも大歓迎です

4

4 に答える 4

6

コンパイラが与える最初のエラーを常に修正してください:int &registers[]動作しません-int * registersまたはを使用してint registers[]ください。

本当に配列への参照が必要な場合は、サイズを指定する必要があります。

void execute(string line, string instruction, int (&registers) [10]);

これらのアプローチはすべて、関数に渡す実際の配列を変更できるようにします。コピーはありません。

サイドノート:

146行目で、aをvararg関数(または)MIPSsim.cppに渡そうとしています-それは機能しません。代わりに合格します。std::stringsprintfprintfstring.c_str()

于 2011-03-30T15:18:14.437 に答える
0

他に問題があるかどうかはわかりませんが、関数は参照の配列ではなく、配列として受け取ることができるとexecute思います。addsubmultregisters

void add(string line, int registers[]);

于 2011-03-30T15:18:26.367 に答える
0

C ++では、引数として配列を渡すことはできません。あなたがしなければならないことは、パラメータを宣言することです:

int (&registers)[32];

ただし、コードを実行することで変更される可能性のあるマシンの状態は、レジスタだけではありません。はるかに優れた解決策は、すべてのマシン状態(おそらくメモリを除く)で構造体を定義することです。

struct HWState
{
    int registers[32];
    unsigned cc;
    ??? pc;
    //  ...
};

次に、それを参照して渡します。問題はないはずです。(実際には、Cスタイルの配列は、静的初期化を使用する静的変数にのみ使用し、カウントが一定で大きすぎない場合はメンバーとして使用する必要があります。)

于 2011-03-30T16:06:52.773 に答える
0

すべて同じ引数を取る関数がたくさんある場合、それは通常、それらがメンバー関数である必要があることを示しています。ここに、があり、addこれらはすべてレジスタのセットで動作します。そしてもちろん、ほとんどの将来の指示もそうなるでしょう。submult

したがって、class CPU各レジスタ(つまりint r[32];float f[32];、およびメソッド)のデータメンバーを使用してを定義しCPU::add(std::string line)ます。add内ではr[4]、最初の関数の引数レジスタなどが必要かどうかを参照できます。

于 2011-03-30T17:23:20.897 に答える