以相同的方式计算具有相同数据的2个文件以产生不同的输出

时间:2015-05-31 07:25:42

标签: c++ file compiler-construction compiler-errors

首先,我为这个含糊不清的问题道歉,但我最终会陷入困境。我创建了一个编译器而不是将汇编代码转换为二进制代码。

我正在做制作汇编程序的NandToTetr​​is课程。

我有两个名为" test.asm"和" MaxL.asm"两者都有相同的信息:

// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/06/max/MaxL.asm

// Symbol-less version of the Max.asm program.

@0
D=AMD
@1
D=D-M
@10
D;JGT
@1
D=M
@12
0;JMP
@0
D=M
@2
M=D
@14
0;JMP

当我调用我的程序时,会生成.hack扩展文件(例如test.hack)

当我调用我的程序时,MaxL.hack和test.hack应该具有相同的输出,但是他们不会!

基本上如果我自己创建.asm文件,那么创建的.hack是完美的,但是如果我使用任何一个.asm文件,它就不起作用,即使它们里面有什么是相同的

正确方向的任何一点都会令人难以置信。

这是我的上下文代码:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int tempVarInst;
int tempVarDest;
int tempVarJmp = 0;


bool replace(std::string& str, const std::string& from, const std::string& to) {
    size_t start_pos = str.find(from);
    if(start_pos == std::string::npos)
        return false;
    str.replace(start_pos, from.length(), to);
    return true;
}

bool isComment(string line){
    string comment = "/";
    if((line.find(comment)) != string::npos){
        return true;
    }else{
        return false;
    }
}

string ConvertToBinary(unsigned int val)
{

    string tempStore;

   unsigned int mask = 1 << (sizeof(int) * 4 - 1);

   for(int i = 0; i < sizeof(int) * 4; i++)
   {
      if( (val & mask) == 0 )
         tempStore+='0';
      else
         tempStore+='1';


      mask  >>= 1;
   }
   return tempStore;
}

bool isMemLoc(string line){
    string symbol = "@";
    if(line.find(symbol) != string::npos ){
        return true;
    }else{
        return false;
    }

}


int destConstants(string line){

    if(line== "A")
        return 2;
    if(line== "M")
        return 8;
    if(line== "D")
        return 16;
    if(line== "AM")
        return 40;
    if(line== "AD")
        return 48;
    if(line== "MD")
        return 24;
    if(line== "AMD")
        return 56;  
}

int isInst(string line){

    if(line=="0")
        return 60032;
    if(line=="1")
        return 61376;
    if(line=="-1")
        return 61056;
    if(line=="D")
        return 58112;
    if(line=="!D")
        return 58176;
    if(line=="M")
        return 64512;
    if(line=="A")
        return 60416;
    if(line=="!M")
        return 64576;
    if(line=="!A")
        return 60480;
    if(line=="-D")
        return 58304;
    if(line=="-M")
        return 64704;
    if(line=="-A")
        return 60608;
    if(line=="D+1")
        return 59328;
    if(line== "M+1")
        return 64960;
    if(line== "A+1")
        return 60864;
    if(line== "D-1")
        return 58240;
    if(line== "M-1")
        return 64640;
    if(line== "A-1")
        return 60544;
    if(line== "D+M")
        return 61568;
    if(line== "D+A")
        return 57472;
    if(line== "D-M")
        return 62656;
    if(line== "D-A")
        return 58560;
    if(line== "M-D")
        return 61888;
    if(line== "A-D")
        return 57792;
    if(line== "D&M")
        return 61440;
    if(line== "D&A")
        return 57344;
    if(line== "D|M")
        return 62784;
    if(line== "D|A")
        return 58688;   
}


int jmpConstants(string line){

    if(line== "JGT")
        return 1;
    if(line== "JEQ")
        return 2;
    if(line== "JGE")
        return 3;
    if(line== "JLT")
        return 4;
    if(line== "JNE")
        return 5;
    if(line== "JLE")
        return 6;
    if(line== "JMP")
        return 7;
}

bool isJumpInstruction(string line){

    string symbol = ";";
    if(line.find(symbol) != string::npos ){
        return true;
    }else{
        return false;
    }

}

int main( int argc, const char* argv[] )
{
    string outLine = "testa output";
    string file1 = argv[1];
    replace(file1, "asm", "hack");

    //input
    //WHILE READ LINE()
    ifstream infile(argv[1]);
    string tempLine;

    ofstream outfile(file1.c_str());
    tempVarJmp=0;
    tempVarDest=0;
    tempVarInst=0;
    while (getline(infile, tempLine)){
        //Blank check
        cout << "The tempLine is: " << tempLine << endl;
        if(tempLine.length()<=1){
            continue;
        }
        //Comment Check
        else if(isComment(tempLine))
            continue;
        //@ Check
        else if(isMemLoc(tempLine)){
            tempLine.erase(0,1);
            int number = (atoi(tempLine.c_str()));
            cout << ConvertToBinary(number) << endl;
            outfile << ConvertToBinary(number) << std::endl;
            continue;
            }

            //tempLine=tempLine(isInst).c_str+tempLine(destConstants)+tempLine(jmpConstants);
            //outfile << ConvertToBinary(tempLine) << endl;

        else if(isJumpInstruction(tempLine)){

                cout << "Jump Instruction" << endl;
                tempVarJmp = jmpConstants(tempLine.substr(tempLine.find(';')+1));
                cout << "TempVarInst: " << tempLine.substr(tempLine.find(';')+1) << endl;
                cout << "tempVarJmp: " << tempVarJmp << endl;
                tempVarDest = isInst(tempLine.substr(0,tempLine.find(';')));
                cout << "tempvarDest = " << tempVarDest << endl;

                int finalVal=tempVarDest+tempVarJmp;
                outfile << ConvertToBinary(finalVal) << std::endl;
                cout << ConvertToBinary(finalVal) << endl;
            }else{

                cout << "Right TempLineInst is: " << tempLine.substr(tempLine.find('=')+1,tempLine.length()) << endl;
                cout << "Right TempLineDest is: " << tempLine.substr(0,tempLine.find('=')) << endl;

                tempVarInst = isInst(tempLine.substr(tempLine.find('=')+1,tempLine.length()));
                tempVarDest = destConstants(tempLine.substr(0,tempLine.find('=')));
                tempVarJmp = 0;
                if(tempLine.substr(1,2) == ";J"){
                    tempVarJmp = jmpConstants(tempLine.substr(2,4));
                    cout << "TempLineDest is: " << tempLine.substr(2,4) << endl;

                }
                cout << "tempvarInst = " << tempVarInst << endl;
                cout << "tempvarDest = " << tempVarDest << endl;
                cout << "tempvarJmp = " << tempVarJmp << endl;
                int totalSum = tempVarInst+tempVarDest+tempVarJmp;
                cout << "totalSum: " << ConvertToBinary(totalSum) << endl;

                outfile << ConvertToBinary(totalSum) << std::endl;
            }


        //print to terminal and pass to file
        //outfile << tempLine << std::endl;
    }

    outfile.close();
    //BINARY CONVERSION, feed conversion into 'line' variable
    //output
}

以下是test.hack中使用test.asm的(正确)输入

0000000000000000
1111111100010000
0000000000000001
1111010011010000
0000000000001010
1110001100000001
0000000000000001
1111110000010000
0000000000001100
1110101010000111
0000000000000000
1111110000010000
0000000000000010
1110001100001000
0000000000001110
1110101010000111

现在(不正确)MaxL.hack代码

0000000000000000
1111111100010000
0000000000000001
1111111100010000
0000000000001010
1110001000000000
0000000000000001
0000000000010000
0000000000001100
1110101010000000
0000000000000000
0000000000010000
0000000000000010
1111111100001000
0000000000001110
1110101010000000

如果您更改MaxL.asm的名称,它仍然会生成错误信息,如果您更改了test.asm&#39; s&#39;名称,它仍然产生正确的信息

0 个答案:

没有答案