C++ 以相同方式计算具有相同数据的2个文件,产生不同的输出
首先,我为这个模糊的问题道歉,但我已经不知所措了。我制作了一个编译器,将汇编代码转换成二进制代码 我正在学习制作汇编程序的NandTotteris课程 我有两个名为“test.asm”和“MaxL.asm”的文件,它们都包含相同的信息:C++ 以相同方式计算具有相同数据的2个文件,产生不同的输出,c++,file,compiler-construction,compiler-errors,C++,File,Compiler Construction,Compiler Errors,首先,我为这个模糊的问题道歉,但我已经不知所措了。我制作了一个编译器,将汇编代码转换成二进制代码 我正在学习制作汇编程序的NandTotteris课程 我有两个名为“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
// 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是完美的,但是如果我使用任何一个courses.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
}
现在查看(不正确的)MaxL.hack代码
0000000000000000
1111111100010000
0000000000000001
1111111100010000
0000000000001010
1110001000000000
0000000000000001
0000000000010000
0000000000001100
1110101010000000
0000000000000000
0000000000010000
0000000000000010
1111111100001000
0000000000001110
1110101010000000
如果更改MaxL.asm的名称,它仍然会生成不正确的信息;如果更改“test.asm”的名称,它仍然会生成正确的信息您确定输入的文件相同吗?试着把其中的一个复制到一个新的名字上,看看goeshey Amit是怎样的,它们是一样的,我把输入从一个复制粘贴到另一个,它们仍然会产生不同的输出,输出应该是什么样子?不正确的输出有什么问题?我不相信两个输入文件是相同的。如果您只是重命名正确的文件或使用文件系统复制文件,而不是复制/粘贴可以切换编码或行尾,会发生什么情况?我将添加所有这些信息,现在已退休的忍者,谢谢!两个文件的行尾是否相同?您是否对这两个文件进行了二进制比较,没有发现任何差异?
0000000000000000
1111111100010000
0000000000000001
1111111100010000
0000000000001010
1110001000000000
0000000000000001
0000000000010000
0000000000001100
1110101010000000
0000000000000000
0000000000010000
0000000000000010
1111111100001000
0000000000001110
1110101010000000