未在此范围内声明

发布于 2024-10-28 06:34:13 字数 9555 浏览 3 评论 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

任何帮助将不胜感激

I'm in the process of implementing a MIPS instruction set simulator. But when I try compiling my code, I keep getting "out of scope" errors and it's getting to be very frustrating. I've tried all kinds of variations, but nothing seems to be helping.
Here's my MIPSsim.cpp file

#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 ®isters[]);
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 ®isters[]);
void sub(string line, int ®isters[]);
void mult(string line, int ®isters[]);


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;
}

Here are the error messages I keep getting:

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

Any help would be greatly appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

或十年 2024-11-04 06:34:13

始终修复编译器给您的第一个错误:int ®isters[] 不起作用 - 使用 int * registersint 寄存器[]

如果您确实想要引用数组,则需要指定大小:

void execute(string line, string instruction, int (®isters) [10]);

所有这些方法都将允许您修改传递给函数的实际数组 - 没有复制。

旁注:

MIPSsim.cpp 第 146 行中,您尝试将 std::string 传递给 vararg 函数(例如 sprintf 或 < code>printf) - 那是行不通的。改为传递 string.c_str()

Always fix the first error the compiler gives you: int ®isters[] won't work - use int * registers or int registers[].

If you really want a reference to an array, you need to specify the size:

void execute(string line, string instruction, int (®isters) [10]);

All of these approaches will let you modify the actual array you pass to the function - there's no copying.

Side note:

In MIPSsim.cpp line 146 you're trying to pass a std::string to a vararg function (e.g. sprintf or printf) - That won't work. Pass string.c_str() instead.

生生漫 2024-11-04 06:34:13

我不能说是否还有其他问题,但我认为您的 executeaddsubmult 函数可以将 registers 作为数组,而不是引用数组。

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

I can't say if there are other problems but I think your execute, add, sub, mult functions can just take registers as an array, not an array of references.

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

望笑 2024-11-04 06:34:13

在 C++ 中不能将数组作为参数传递。你需要做的是
声明参数:

int (®isters)[32];

但是寄存器并不是唯一可能的机器状态
通过执行一些代码来改变。更好的解决方案是
定义一个包含所有机器状态的结构(可能除了内存):

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

然后通过引用传递它,你应该不会有任何问题。 (在
实际上,C 风格的数组只能用于静态变量,
静态初始化,并作为成员,当计数恒定且
不太大。)

You can't pass an array as an argument in C++. What you'd have to do is
declare the parameters:

int (®isters)[32];

But registers aren't the only machine state which is likely to be
changed by executing some code. A much better solution would be to
define a struct with all of the machine state (except maybe the memory):

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

Then pass that by reference, and you shouldn't have any problem. (In
practice, C style arrays should only be used for static variables, with
static initialization, and as members, when the count is constant and
not too big.)

静若繁花 2024-11-04 06:34:13

当你有很多函数都采用相同的参数时,这通常表明它们应该是成员函数。在这里,您有 addsubmult,它们都在您的寄存器组上运行。当然,大多数未来的说明也会如此。

因此,定义一个类CPU,其中包含每个寄存器的数据成员(即int r[32];float f[32];)和一个方法CPU:: add(std::string line)。在add中,如果需要第一个函数参数寄存器等,可以直接引用r[4]

When you have a lot of functions that all take the same argument, that's usually a sign that they should be member functions. Here, you have add, sub and mult which all operate on your set of registers. And of course, most future instructions will too.

Therefore, define a class CPU with data members for each register (i.e. int r[32];float f[32];, and a method CPU::add(std::string line). Within add, you can just refer to r[4] if you need the first function argument register etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文