2011-03-30 260 views
1

我正在實施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 

任何幫助,將不勝感激

+2

你有沒有嘗試*修復*錯誤? 「移動代碼」不是編程如何完成的。 – Jon 2011-03-30 15:19:06

+1

第一個問題是語言中不存在的「引用數組」。 – 2011-03-30 15:21:46

+0

也許你應該爲所有這些函數聲明添加一個頭文件。 – tgmath 2011-03-30 15:40:18

回答

6

始終固定第一錯誤編譯給你:int &registers[]將不起作用 - 使用int * registersint registers[]

如果你真的想要一個數組的引用,你需要指定大小:

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

所有這些方法都可以讓你修改你傳遞給函數的實際陣列 - 有沒有複製。

旁註:

MIPSsim.cpp線146你想一個std::string傳遞給可變參數函數(例如sprintfprintf) - 這是行不通的。改爲通過string.c_str()

+0

順便說一句'RegisterSetSet {}'可能會有所幫助。您經常將寄存器集合處理爲單個對象,例如在這裏將設置傳遞給一個函數。 – MSalters 2011-03-30 17:13:37

0

如果有其他問題,但我覺得你的executeaddsubmult功能正好可以registers作爲一個數組,而不是引用數組我不能說。

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

+1

即使我想更改在main()中初始化的原始寄存器數組中的實際位置? – 2011-03-30 15:24:41

0

不能傳遞數組如在C++中的參數。什麼你必須做的就是 聲明參數:

int (&registers)[32]; 

但寄存器不是唯一的機器狀態,這很可能是 通過執行一些代碼改變。一個更好的解決辦法是 定義的所有機器狀態的結構(也許除了內存):

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

然後傳遞,通過引用,你不應該有任何問題。 (在 實踐中,C風格的數組應該只用於靜態變量,與 靜態初始化,併爲成員,當計數是恆定的, 不是太大。)

0

當你有很多的功能,所有的採取相同的論點,這通常表明他們應該是成員職能。在這裏,您有addsubmult,它們都在您的寄存器組上運行。當然,大多數未來的說明也是如此。

因此,定義與數據成員爲每個寄存器class CPU(即int r[32];float f[32];,和一種方法CPU::add(std::string line)。中添加,可以只參照r[4]如果需要的第一個函數參數寄存器等