//Get the register code char* findRegister(char key[]) { while(key[0] == ' ' || key[0] == '\t') key = ++key; int lastIndex = strlen(key) - 1; while(key[lastIndex] == ' ' || key[lastIndex] == '\t'){ key[lastIndex] = '\0'; lastIndex--; } struct hash *r; static char buffer[5]; HASH_FIND_STR(registers, key, r); if(r != NULL){ itoa(r->value, buffer, 2); if(strlen(buffer) < 5) strcpy(buffer, completeBinary(5, buffer)); buffer[5] = '\0'; return buffer; } return "NULL"; }
//Checks if the data is valid, and translate him. char* checkData(char *line) { char *aux; int value, valueSize; //Takes only the value aux = strchr(line, 'd'); aux = strchr(aux, aux[1]); //Clean all spaces; while(aux[0] == ' ') aux = ++aux; //Converts the string to int valueSize = strlen(aux); value = atoi(aux); //If the value is wrong if(valueSize == 0 || (value == 0 && aux[0] != '0' && valueSize > 0)) return "0"; //Value overflow if(value > getBinaryRange(31, '+') || value < getBinaryRange(31, '-')) return "1"; //Converts to binary itoa(value, aux, 2); strncpy(aux,completeBinary(32, aux), 32); aux[32] = '\0'; return aux; }
/** * @brief Converter::decimalToBinary * Convierte números (cadenas de strings) de una base a otra */ std::string Converter::decimalToBinary(const std::string &pDecimalNumber) { std::string str(pDecimalNumber); // Convert from std::string 2 Qstring QString qstrDecimalNumber(str.c_str()); // donde qstr es el QString //qDebug() << qstrDecimalNumber; // << qDebugendl; // número decimal bool ok = false; // convierte un string de decimal a binario QString binaryNumber = QString::number(qstrDecimalNumber.toLongLong(&ok, 10), 2); std::string stringBinary = binaryNumber.toStdString(); stringBinary.length() == 8 ? stringBinary : completeBinary(stringBinary); //cout << "Conversión entero " << completeBinary( stringBinary ) << endl; return completeBinary( stringBinary ); }
/** * @brief Converter::stringToBinary * @param pStringLetters * Función para convertir std::string a binario */ std::string Converter::stringToBinary(const std::string &pStringLetters) { std::string string; for (std::size_t i = 0; i < pStringLetters.size(); ++i) { std::bitset<8> array(pStringLetters.c_str()[i]); string += array.to_string(); } //cout << "Conversión : " << completeBinary( string ) << endl; return completeBinary( string ); //string; }
//Get the label code char* findLabel(char key[]) { while(key[0] == ' ' || key[0] == '\t') key = ++key; struct hash *lbl; static char buffer[16]; HASH_FIND_STR(labels, key, lbl); if(lbl != NULL){ itoa(lbl->value, buffer, 2); if(strlen(buffer) < 16) strcpy(buffer, completeBinary(16, buffer)); buffer[16] = '\0'; return buffer; } return "NULL"; }
/** * @brief Converter::fromDoubleString2BinaryString * @param pDoubleString * @return * Funcion para convertir un string double en string binario * Por ej: 7.3 -> 73 (01001001) y contador - 1 (00000001) que es cantidad * de espacios después del punto 10 ** (contador - 1) */ std::string Converter::fromDoubleString2BinaryString( std::string pDoubleString ) { int contador = 0; double doubleNumber = fromString2Double( pDoubleString ); for(int i = pDoubleString.find("."); i < pDoubleString.length() ; i++){ contador++; } int intDoubleNumber = doubleNumber * ( pow( 10, (contador - 1) ) ); //cout << contador - 1 << endl; //cout << pDoubleString.find(".") << endl; cout << doubleNumber << endl; cout << intDoubleNumber << endl; std::string entero = decimalToBinary( fromInt2String(intDoubleNumber) ); cout << entero << endl; std::string decimal = decimalToBinary( fromInt2String(contador - 1) ); cout << decimal << endl; cout << entero + decimal << endl; return completeBinary( entero + decimal ); //entero + decimal; }
//Writes the binary file. void writeOnFile() { FILE *file; file = fopen("binary.txt", "w"); char dRegBase[32], strAux[32]; struct command *com, *aux; com = (struct command*)malloc(sizeof(struct command)); //Puts the number of instruction and data in two first lines of the file itoa(numberOfInstructions, dRegBase, 2); strcpy(strAux, completeBinary(32, dRegBase)); fputs(strAux, file); fputc('\n', file); com = first; while(com != NULL){ fputs(com->line, file); com = com->next; if(com != NULL) fputc('\n', file); } fclose(file); }
//Translate the instruction to binary char* mountBinary(char instruction[],int nRegisters,int nConstants,int nLabels, char type, char *opcode, char *function, int currentAddress) { int i = 0, j = 0, nRegsAux = 0, nConstAux = 0, nLabAux = 0; static char binary[32]; char param[10], name[10]; int isRegister = 0, isLabel = 0, isConstant = 0; //Define the initial binary for(i = 0; i < 32; i++) binary[i] = '0'; i = 0; //Push the opcode concatBinary(0, 6, binary, opcode); //Takes the instruction name while(instruction[i] != ' '){ name[i] = instruction[i]; i++; } name[i] = '\0'; i = 0; //Formats the instruction instruction = strchr(instruction, ' '); instruction = ++instruction; while(instruction[i] != '\0'){ //Push the current instruction parameter if(instruction[i] != ','){ param[j] = instruction[i]; j++; } //If the char is comma or it is the instruction final if(instruction[i] == ',' || i == strlen(instruction) - 1){ param[j] = '\0'; //If the parameter is a register if(nRegisters != nRegsAux){ isRegister = 1; isLabel = 0; isConstant = 0; nRegsAux++; } //If the parameter is a constant else if(nConstants != nConstAux){ isRegister = 0; isLabel = 0; isConstant = 1; nConstAux++; } //If the parameter is a label else if(nLabels != nLabAux){ isRegister = 0; isLabel = 1; isConstant = 0; nLabAux++; } //Operation for register if(isRegister == 1){ //To load and store char copy[10]; //Load and Store if(nRegsAux == 2 && (strcmp(name, "lw") == 0 || strcmp(name, "lh") == 0 || strcmp(name, "lb") == 0 || strcmp(name, "sw") == 0 || strcmp(name, "sh") == 0 || strcmp(name, "sb") == 0) ) { strcpy(copy, param); strcpy(param, strchr(param, '$')); param[strlen(param) - 1] = '\0'; isRegister = 0; isLabel = 0; isConstant = 1; nConstAux++; } //Find the register code strcpy(param, findRegister(param)); //If is not exists if(strcmp(param, "NULL") == 0) return "1"; //Push on correct position if(type == 'R'){ if(nRegsAux == 1){ if(strcmp(name, "ext") == 0 || strcmp(name, "ins") == 0) concatBinary(11, 5, binary, param); else if(strcmp(name, "div") == 0 || strcmp(name, "divu") == 0 || strcmp(name, "madd") == 0 || strcmp(name, "maddu") == 0 || strcmp(name, "msub") == 0 || strcmp(name, "msubu") == 0 || strcmp(name, "mult") == 0 || strcmp(name, "multu") == 0 || strcmp(name, "mthi") == 0 || strcmp(name, "mtlo") == 0 || strcmp(name, "jr") == 0) concatBinary(6, 5, binary, param); else concatBinary(16, 5, binary, param); } else if(nRegsAux == 2){ if(strcmp(name, "wsbh") == 0 || strcmp(name, "seh") == 0 || strcmp(name, "seb") == 0 || strcmp(name, "div") == 0 || strcmp(name, "divu") == 0 || strcmp(name, "madd") == 0 || strcmp(name, "maddu") == 0 || strcmp(name, "msub") == 0 || strcmp(name, "msubu") == 0 || strcmp(name, "mult") == 0 || strcmp(name, "multu") == 0 || strcmp(name, "sll") == 0 || strcmp(name, "srl") == 0 || strcmp(name, "sra") == 0 || strcmp(name, "rotr") == 0 || strcmp(name, "sllv") == 0 || strcmp(name, "srav") == 0 || strcmp(name, "srlv") == 0 || strcmp(name, "rotrv") == 0) concatBinary(11, 5, binary, param); else concatBinary(6, 5, binary, param); } else if(nRegsAux == 3){ if(strcmp(name, "sllv") == 0 || strcmp(name, "srav") == 0 || strcmp(name, "srlv") == 0 || strcmp(name, "rotrv") == 0) concatBinary(6, 5, binary, param); else concatBinary(11, 5, binary, param); } } else if(type == 'I'){ if(nRegsAux == 1){ if(strcmp(name, "beq") == 0 || strcmp(name, "bne") == 0 || strcmp(name, "bgtz") == 0 || strcmp(name, "bltz") == 0) concatBinary(6, 5, binary, param); else concatBinary(11, 5, binary, param); } else if(nRegsAux == 2){ if(strcmp(name, "beq") == 0 || strcmp(name, "bne") == 0 || strcmp(name, "bgtz") == 0 || strcmp(name, "bltz") == 0) concatBinary(11, 5, binary, param); else concatBinary(6, 5, binary, param); } } //To load and store if(isConstant == 1){ strcpy(param, copy); strcpy(param, strchr(strrev(param), '(')); strcpy(param, strrev(strchr(param, param[1]))); while(param[0] == ' ' || param[0] == '\t') strcpy(param, strchr(param, param[1])); } } //Operation for label else if(isLabel == 1){ //Find the label code strcpy(param, findLabel(param)); //If is not valid if(strcmp(param, "NULL") == 0) return "2"; //Branchs instructions if(strcmp(name, "beq") == 0 || strcmp(name, "bgtz") == 0 || strcmp(name, "bne") == 0 || strcmp(name, "bltz") == 0) { int labelAddress = 0, i = 0; for(i; i < 16; i++){ if(param[i] == '1') labelAddress++; if(i != 15) labelAddress = labelAddress << 1; } itoa(labelAddress - currentAddress, param, 2); strcpy(param, completeBinary(16, param)); } //Push on correct position concatBinary(16, 16, binary, param); } //Operation for constant if(isConstant == 1){ //Convert to integer int constValue = atoi(param); //Verify if is valid if(constValue == 0 && param[0] != '0') return "3"; if(type != 'R'){ //Unsigned instructions if(strcmp(name, "addiu") == 0 || strcmp(name, "sltiu") == 0) { if(constValue < 0) return "7"; if(constValue > getBinaryRange(17, '+')) return "6"; } //Signed else{ if(constValue > getBinaryRange(16, '+') || constValue < getBinaryRange(16, '-')) return "6"; } //Converts to binary and push to instruction char constant[16]; itoa(constValue, constant, 2); strcpy(constant, completeBinary(16, constant)); concatBinary(16, 16, binary, constant); } else{ if(constValue > getBinaryRange(5, '+') || constValue < getBinaryRange(5, '-')) return "6"; //Shamt, ins and ext char constant[5]; int pos, size; if(strcmp(name, "ext") == 0 && nConstAux == 2) constValue--; itoa(constValue, constant, 2); strcpy(constant, completeBinary(5, constant)); if(strcmp(name, "ext") == 0 && nConstAux == 1) concatBinary(21, 5, binary, constant); else if(strcmp(name, "ext") == 0 && nConstAux == 2) concatBinary(16, 5, binary, constant); else if(strcmp(name, "ins") == 0 && nConstAux == 1) pos = constValue; else if(strcmp(name, "ins") == 0 && nConstAux == 2){ size = constValue; int msb = (pos + size) - 1; itoa(msb, constant, 2); strcpy(constant, completeBinary(5, constant)); concatBinary(16, 5, binary, constant); itoa(pos, constant, 2); strcpy(constant, completeBinary(5, constant)); concatBinary(21, 5, binary, constant); } else concatBinary(21, 5, binary, constant); } } param[0] = '\0'; j = 0; } i++; } //Minus arguments if(nConstants != nConstAux || nRegisters != nRegsAux || nLabels != nLabAux) return "5"; //Place the shamt and function on binary if(type == 'R'){ concatBinary(26, 6, binary, function); //seh, seb and wsbh instructions if(strcmp(opcode, "011111") == 0 && strcmp(function, "100000") == 0){ if(strcmp(name, "seh") == 0) concatBinary(21, 5, binary, "11000"); else if(strcmp(name, "seb") == 0) concatBinary(21, 5, binary, "10000"); else if(strcmp(name, "wsbh") == 0) concatBinary(21, 5, binary, "00010"); } if(strcmp(name, "rotr") == 0) concatBinary(6, 5, binary, "00001"); else if(strcmp(name, "rotrv") == 0) concatBinary(21, 5, binary, "00001"); } binary[32] = '\0'; return binary; }