c++ - 2 files with same data being computed the same way producing different output -
firstly, apologise vague question @ wits end. have made compiler converts assembly code binary.
i doing nandtotetris course making assembler.
i have 2 files called "test.asm" , "maxl.asm" both have same information in them:
// file part of www.nand2tetris.org // , book "the elements of computing systems" // nisan , schocken, mit press. // file name: projects/06/max/maxl.asm // symbol-less version of 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 when call program, .hack extended file generated (eg. test.hack)
when call program, maxl.hack , test.hack should have same output, don't!
basically if create .asm file myself, .hack that's created perfect, if use of courses .asm files, doesn't work, if whats inside them identical
any point in right direction incredibly appreciated.
here code context:
#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 = 0; < 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 terminal , pass file //outfile << templine << std::endl; } outfile.close(); //binary conversion, feed conversion 'line' variable //output } here (correct) input using test.asm in test.hack
0000000000000000 1111111100010000 0000000000000001 1111010011010000 0000000000001010 1110001100000001 0000000000000001 1111110000010000 0000000000001100 1110101010000111 0000000000000000 1111110000010000 0000000000000010 1110001100001000 0000000000001110 1110101010000111 now (incorrect) maxl.hack code
0000000000000000 1111111100010000 0000000000000001 1111111100010000 0000000000001010 1110001000000000 0000000000000001 0000000000010000 0000000000001100 1110101010000000 0000000000000000 0000000000010000 0000000000000010 1111111100001000 0000000000001110 1110101010000000 if change maxl.asm's name, it's still produces incorrect info, , if change 'test.asm's' name, still produces correct info
Comments
Post a Comment