static string compile_(string filename) { string line; byte[] bytestream = new byte[600000]; int bytepointer = 0; string out_string = "An error occured or the file did not exist"; if (System.IO.File.Exists(filename) == true) { out_string = "The file was succesfully read"; System.IO.StreamReader objreader; objreader = new System.IO.StreamReader(filename); bytepointer = 0; do { line = objreader.ReadLine(); //Now we're compiling!! Opcode_ByteData tempdata = compile_line(line); int bytes_to_write = tempdata.total_instr_length; for (int t = 0; t < bytes_to_write + 1; t++) { if (bytestream != null) { bytestream[bytepointer] = (byte)tempdata.bytes[t]; //Console.ReadLine(); bytepointer++; } } } while (objreader.Peek() != -1); } try { using (var fs = new FileStream("out.bin", FileMode.Create, FileAccess.Write)) { fs.Write(bytestream, 0, bytepointer - 1); //return true; } } catch (Exception ex) { Console.WriteLine("Exception caught in process: {0}", ex); //return false; } return(out_string); }
public static Opcode_ByteData compile_line(string userline, int bpointer) { userline.Replace("/r/n", ""); bool found_opcode = false; int num = getopcode(userline); Opcode_ByteData byte_data = new Opcode_ByteData(); IEnumerable <Opcode> thislist = doublecheckopcode(userline, num); if (thislist.Count() > 0) { found_opcode = true; } if (found_opcode == true) { Opcode_Data userdata = opcode_matchtest(thislist, userline); string opstring = make_opcode_string(userdata.byte_index); if (userdata.success) { byte_data = get_bytes(userdata, bpointer); byte_data.param1 = userdata.param1; byte_data.param2 = userdata.param2; //Next //For now just print out the byte data, later this will turn into streaming the data to a file int length = byte_data.total_instr_length; string opcode_string = "Opcode Instruction was " + byte_data.memnonic + ": Opcode #" + byte_data.bytes[0] + " "; for (int t = 1; t < length + 1; t++) { opcode_string = opcode_string + "|" + byte_data.bytes[t]; if (t == length) { opcode_string = opcode_string + "|"; } } Console.WriteLine(opcode_string); } } return(byte_data); }
public static Opcode_ByteData get_bytes(Opcode_Data data) { int[] bytes = new int[30]; //We'll use extra just to be safe int firstbyte = data.byte_index; int currentbyte = 1; bytes[0] = firstbyte; int param1length = data.param1.Length; int param2length = data.param2.Length; int param1bytelength = 1; int param2bytelength = 1; //This takes care of only two special cases where the value may not be 4 bytes long if (data.memnonic == "ST8") { param1bytelength = 1; } if (data.memnonic == "ST16") { param1bytelength = 2; } if (data.paramtype1 == Opcode.paramtype.REGISTER || data.paramtype1 == Opcode.paramtype.REGISTERADDRESS) { //If the first parameter is a register then the first byte is just the register's value bytes[currentbyte] = data.reg1value; currentbyte = currentbyte + 1; } if (data.paramtype1 == Opcode.paramtype.VALUE || data.paramtype1 == Opcode.paramtype.ADDRESS) { bool writtenvalue = false; int value = System.Convert.ToInt32(Right(data.param1, param1length - 1)); //Console.WriteLine("The value was " + secondbyte); byte[] results = INT2LE(value); //Console.WriteLine("Bytes are " + results[0] + "," + results[1] + "," + results[2] + "," + results[3]); int[] r = new int[10]; get(value, 8, 0, r); //Console.WriteLine(" for comparison other bytes are " + r[0] + "," + r[1] + "," + r[2] + "," +r[3]); //Pad the value bytes to two if (param1bytelength == 2 && data.memnonic == "ST16") { value = value & 65535; int highbyte = value & 255; // high byte (0x12) int lowbyte = value >> 8; // low byte (0x34) bytes[currentbyte] = lowbyte; bytes[currentbyte + 1] = highbyte; currentbyte = currentbyte + 1; currentbyte = currentbyte + 1; writtenvalue = true; //Console.WriteLine("highbyte was " + highbyte + " and lowbyte was " + lowbyte); } //Pad the value bytes to 1 if (param1bytelength == 1 && data.memnonic == "ST8") { value = value & 255; bytes[currentbyte] = value; currentbyte = currentbyte + 1; writtenvalue = true; } if (writtenvalue == false) { if (data.memnonic != "ST16" && data.memnonic != "ST8") { { results = INT2LE(value); bytes[currentbyte] = results[0]; currentbyte++; bytes[currentbyte] = results[1]; currentbyte++; bytes[currentbyte] = results[2]; currentbyte++; bytes[currentbyte] = results[3]; currentbyte++; } } } } if (data.paramtype2 == Opcode.paramtype.REGISTER || data.paramtype2 == Opcode.paramtype.REGISTERADDRESS) { int value = data.reg2value; //Write the current value bytes[currentbyte] = value; } if (data.paramtype2 == Opcode.paramtype.VALUE || data.paramtype2 == Opcode.paramtype.ADDRESS) { int value = System.Convert.ToInt32(Right(data.param2, param2length - 1)); byte[] results = INT2LE(value); // Console.WriteLine("Bytes are " + results[0] + "," + results[1] + "," + results[2] + "," + results[3]); int[] r = new int[10]; get(value, 8, 0, r); //Console.WriteLine(" for comparison other bytes are " + r[0] + "," + r[1] + "," + r[2] + "," + r[3]); //put the bytes into the opcode array stream bytes[currentbyte] = results[0]; currentbyte++; bytes[currentbyte] = results[1]; currentbyte++; bytes[currentbyte] = results[2]; currentbyte++; bytes[currentbyte] = results[3]; } //Console.WriteLine("Just to confirm the bytes written were " + "opcode byte " + bytes[0] + " , " + bytes[1] + "," + bytes[2] + "," + bytes[3] + "," + bytes[4] + "," + bytes[5]+","+bytes[6]); Opcode_ByteData opcode_bytes = new Opcode_ByteData(); opcode_bytes.total_instr_length = currentbyte; opcode_bytes.bytes = bytes; opcode_bytes.memnonic = data.memnonic; return(opcode_bytes); }
public static Opcode_ByteData get_bytes(Opcode_Data data, int bpointer) { int[] bytes = new int[30]; //We'll use extra just to be safe int firstbyte = data.byte_index; //handle replace goto with JMP because it's basically the same thing but with label if (firstbyte == 42) { firstbyte = 33; } int currentbyte = 1; bytes[0] = firstbyte; int param1length = data.param1.Length; int param2length = data.param2.Length; int param1bytelength = 1; int param2bytelength = 1; int value = 0; //This takes care of only two special cases where the value may not be 4 bytes long if (data.memnonic == "ST8") { param1bytelength = 1; } if (data.memnonic == "ST16") { param1bytelength = 2; } if (data.paramtype1 == Opcode.paramtype.REGISTER || data.paramtype1 == Opcode.paramtype.REGISTERADDRESS) { //If the first parameter is a register then the first byte is just the register's value bytes[currentbyte] = data.reg1value; currentbyte = currentbyte + 1; } if (data.paramtype1 == Opcode.paramtype.VALUE || data.paramtype1 == Opcode.paramtype.ADDRESS) { bool writtenvalue = false; value = System.Convert.ToInt32(Right(data.param1, param1length - 1)); //Console.WriteLine("The value was " + secondbyte); byte[] results = INT2LE(value); //Console.WriteLine("Bytes are " + results[0] + "," + results[1] + "," + results[2] + "," + results[3]); int[] r = new int[10]; get(value, 8, 0, r); //Console.WriteLine(" for comparison other bytes are " + r[0] + "," + r[1] + "," + r[2] + "," +r[3]); //Pad the value bytes to two if (param1bytelength == 2 && data.memnonic == "ST16") { value = value & 65535; int highbyte = value & 255; // high byte (0x12) int lowbyte = value >> 8; // low byte (0x34) bytes[currentbyte] = lowbyte; bytes[currentbyte + 1] = highbyte; currentbyte = currentbyte + 1; currentbyte = currentbyte + 1; writtenvalue = true; //Console.WriteLine("highbyte was " + highbyte + " and lowbyte was " + lowbyte); } //Pad the value bytes to 1 if (param1bytelength == 1 && data.memnonic == "ST8") { value = value & 255; bytes[currentbyte] = value; currentbyte = currentbyte + 1; writtenvalue = true; } if (writtenvalue == false) { if (data.memnonic != "ST16" && data.memnonic != "ST8") { { results = INT2LE(value); bytes[currentbyte] = results[0]; currentbyte++; bytes[currentbyte] = results[1]; currentbyte++; bytes[currentbyte] = results[2]; currentbyte++; bytes[currentbyte] = results[3]; currentbyte++; } } } } if (data.paramtype2 == Opcode.paramtype.REGISTER || data.paramtype2 == Opcode.paramtype.REGISTERADDRESS) { value = data.reg2value; //Write the current value bytes[currentbyte] = value; } if (data.paramtype2 == Opcode.paramtype.STRING) { if (data.paramtype1 != Opcode.paramtype.GOTO) { value = 0; //if (data.paramtype2 == Opcode.paramtype.VALUE || data.paramtype2 == Opcode.paramtype.ADDRESS) { value = System.Convert.ToInt32(Right(data.param2, param2length - 1)); } //patch in checking for byte pointers, only if not label type. if (firstrun) { value = bpointer; if (data.paramtype1 == Opcode.paramtype.LABEL) { labelbytepointers.Add(data.param2, value); } } } if (data.paramtype1 == Opcode.paramtype.GOTO) { value = 0; //we don't know what it could be until recompile :) { //A bit redundant since we declare 'value' 0 later for goto but. whatever. //in theory this should compile ALL the needed bytes and still get the correct pointer if (!firstrun) { if (data.param1 == "goto" && (data.param1 != "label")) { value = labelbytepointers[data.param2]; } Console.WriteLine("Value injection found for goto statement " + data.param2 + " and the injection value was " + value); } byte[] results = INT2LE(value); bytes[currentbyte] = results[0]; currentbyte++; bytes[currentbyte] = results[1]; currentbyte++; bytes[currentbyte] = results[2]; currentbyte++; bytes[currentbyte] = results[3]; currentbyte++; } } } //Console.WriteLine("Just to confirm the bytes written were " + "opcode byte " + bytes[0] + " , " + bytes[1] + "," + bytes[2] + "," + bytes[3] + "," + bytes[4] + "," + bytes[5]+","+bytes[6]); Opcode_ByteData opcode_bytes = new Opcode_ByteData(); opcode_bytes.total_instr_length = currentbyte; opcode_bytes.bytes = bytes; opcode_bytes.memnonic = data.memnonic; return(opcode_bytes); }