private bool OP_ADDU(int i) { if (sourceList[i].Length != 4) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.WRONGARGUNUM, "4"); return false; } if (!CheckRegister(sourceList[i][1]) || !CheckRegister(sourceList[i][2]) || !CheckRegister(sourceList[i][3])) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.WRONGREGNAME); return false; } this.codelist.Add(new Instruction(sourceList[i][0], sourceList[i][1], sourceList[i][2], sourceList[i][3])); return true; }
private bool LoadFile() { if (sourcepath != null) { if (File.Exists(sourcepath) == false) { this.error = new AssemblerErrorInfo(0, AssemblerError.NOFILE); return false; } else { StreamReader sr = new StreamReader(sourcepath); string linetext; for (int i = 0; ; i++) { if ((linetext = sr.ReadLine()) == null) { break; } else { sourceString.Add(linetext); linetext = RemoveComment(linetext); if (linetext != "") { if (linetext.Split(new char[] { ' ', '\t', ',' }, StringSplitOptions.RemoveEmptyEntries).Length != 0) { if(Regex.IsMatch(linetext,":")==false) { int temp=linetable.Count; linetable[temp] = i; codeindextable[i] = temp; } sourceList.Add(linetext.Split(new char[] { ' ', '\t', ',' }, StringSplitOptions.RemoveEmptyEntries)); } } } } sr.Close(); return true; } } else { for (int i = 0; i < this.sourceString.Count;i++) { if (sourceString[i] != "") { if (sourceString[i].Split(new char[] { ' ', '\t', ',' }, StringSplitOptions.RemoveEmptyEntries).Length != 0) { linetable[sourceList.Count] = i; sourceList.Add(sourceString[i].Split(new char[] { ' ', '\t', ',' }, StringSplitOptions.RemoveEmptyEntries)); } } } return true; } }
private bool OP_ADDIU(int i) { if (sourceList[i].Length != 4) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.WRONGARGUNUM, "4"); return false; } if (!CheckRegister(sourceList[i][1]) || !CheckRegister(sourceList[i][2])) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.WRONGREGNAME); return false; } int imm = new int(); if (ConvertImmediate(i, sourceList[i][3], out imm, true) == false) { return false; } else { sourceList[i][3] = imm.ToString(); } this.codelist.Add(new Instruction(sourceList[i][0], sourceList[i][1], sourceList[i][2], sourceList[i][3])); return true; }
private bool ConvertImmediate(int i, string str, out int intvalue, bool sign) { int maxvalue, minvalue; if (sign == true) { maxvalue = 32767; minvalue = -32768; } else { maxvalue = 65535; minvalue = 0; } if (str.ToUpper().StartsWith("0X") == true) { try { if (sign == true) { intvalue = Int16.Parse(str.Substring(2), System.Globalization.NumberStyles.HexNumber); } else { intvalue = UInt16.Parse(str.Substring(2), System.Globalization.NumberStyles.HexNumber); } if (intvalue < minvalue || intvalue > maxvalue) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.INVALIDIMMEDIATE, str); return false; } } catch { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.INVALIDIMMEDIATE, str); intvalue = 0; return false; } } else if (str.ToUpper().StartsWith("0") == true) { if (str == "0") { intvalue = 0; return true; } else { try { intvalue = 0; str = str.Substring(1); while (true) { intvalue = intvalue * 8 + Convert.ToInt32(str[0]) % 8; if (str.Length > 1) { str = str.Substring(1); } else { break; } } if (intvalue < minvalue || intvalue > maxvalue) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.INVALIDIMMEDIATE, str); return false; } } catch { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.INVALIDIMMEDIATE, str); intvalue = 0; return false; } } } else { try { intvalue = int.Parse(str); if (intvalue < minvalue || intvalue > maxvalue) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.INVALIDIMMEDIATE, str); return false; } } catch { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.INVALIDIMMEDIATE, str); intvalue = 0; return false; } } return true; }
private bool LoadAddress() { for (int i = 0; i < sourceList.Count; i++) { if(sourceList[i][0].EndsWith(":")) { string label = sourceList[i][0].Substring(0, sourceList[i][0].Length - 1); if (CheckVariableName(label) == false) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.INVALIDLABEL, label); return false; } addAddresstable(sourceList[i][0].Substring(0, sourceList[i][0].Length - 1), i); } } return true; }
public bool DoAssemble() { if (this.LoadFile() == false) { this.error = new AssemblerErrorInfo(0, AssemblerError.NOFILE); return false; } if (LoadAddress() == false) { return false; } if (CheckWord() == false) { return false; } WriteBackAddress(); CalculOffset(); InitInstructionAddress(); AssembleInstructions(); return true; }
private bool CheckWord() { for (int i = 0; i < sourceList.Count; i++) { if (addresstable.ContainsValue(i - 1)) { addresstable[labeltable[i - 1].ToString()] = codelist.Count; } if (addresstable.ContainsValue(i)) continue; switch (sourceList[i][0].ToUpper()) { case ".GLOBL": if (sourceList[i].Length != 2) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.WRONGARGUNUM, "2"); return false; } else if (CheckVariableName(sourceList[i][1]) == false) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.INVALIDLABEL, sourceList[i][1]); return false; } else if (addresstable.ContainsKey(sourceList[i][1].ToString()) == false) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.ADDNOTFOUND, sourceList[i][1]); return false; } else { SetAddress0(sourceList[i][1]); break; } case ".TEXT": case ".DATA": case ".WORD": break; case "ADD": if (OP_ADD(i) == false) return false; break; case "ADDU": if (OP_ADDU(i) == false) return false; break; case "SUB": if (OP_SUB(i) == false) return false; break; case "SUBU": if (OP_SUBU(i) == false) return false; break; case "AND": if (OP_AND(i) == false) return false; break; case "OR": if (OP_OR(i) == false) return false; break; case "XOR": if (OP_XOR(i) == false) return false; break; case "NOR": if (OP_NOR(i) == false) return false; break; case "SLT": if (OP_SLT(i) == false) return false; break; case "SLTU": if (OP_SLTU(i) == false) return false; break; case "SLL": if (OP_SLL(i) == false) return false; break; case "SRL": if (OP_SRL(i) == false) return false; break; case "SRA": if (OP_SRA(i) == false) return false; break; case "SLLV": if (OP_SLLV(i) == false) return false; break; case "SRLV": if (OP_SRLV(i) == false) return false; break; case "SRAV": if (OP_SRAV(i) == false) return false; break; case "JR": if (OP_JR(i) == false) return false; break; case "JALR": if (OP_JALR(i) == false) return false; break; case "ADDI": if (OP_ADDI(i) == false) return false; break; case "ADDIU": if (OP_ADDIU(i) == false) return false; break; case "ANDI": if (OP_ANDI(i) == false) return false; break; case "ORI": if (OP_ORI(i) == false) return false; break; case "XORI": if (OP_XORI(i) == false) return false; break; case "LUI": if (OP_LUI(i) == false) return false; break; case "SLTI": if (OP_SLTI(i) == false) return false; break; case "SLTIU": if (OP_SLTIU(i) == false) return false; break; case "LW": if (OP_LW(i) == false) return false; break; case "SW": if (OP_SW(i) == false) return false; break; case "LB": if (OP_LB(i) == false) return false; break; case "LBU": if ( OP_LBU(i) == false) return false; break; case "LH": if (OP_LH(i) == false) return false; break; case "LHU": if (OP_LHU(i) == false) return false; break; case "SB": if (OP_SB(i) == false) return false; break; case "SH": if (OP_SH(i) == false) return false; break; case "BEQ": if (OP_BEQ(i) == false) return false; break; case "BNE": if (OP_BNE(i) == false) return false; break; case "BGEZ": if (OP_BGEZ(i) == false) return false; break; case "BGEZAL": if (OP_BGEZAL(i) == false) return false; break; case "BGTZ": if (OP_BGTZ(i) == false) return false; break; case "BLEZ": if (OP_BLEZ(i) == false) return false; break; case "BLTZ": if (OP_BLTZ(i) == false) return false; break; case "BLTZAL": if (OP_BLTZAL(i) == false) return false; break; case "J": if (OP_J(i) == false) return false; break; case "JAL": if (OP_JAL(i) == false) return false; break; case "SUBI": if (OP_SUBI(i) == false) return false; break; case "MOVE": if (OP_MOVE(i) == false) return false; break; case "NOP": if (OP_NOP(i) == false) return false; break; case "LI": if (OP_LI(i) == false) return false; break; case "LA": if (OP_LA(i) == false) return false; break; case "SYSCALL": if (OP_SYSCALL(i) == false) return false; break; case "DIV": if (OP_DIV(i) == false) return false; break; case "DIVU": if (OP_DIVU(i) == false) return false; break; case "MULT": if (OP_MULT(i) == false) return false; break; case "MULTU": if (OP_MULTU(i) == false) return false; break; case "BREAK": if (OP_BREAK(i) == false) return false; break; case "ERET": if (OP_ERET(i) == false) return false; break; case "MFHI": if (OP_MFHI(i) == false) return false; break; case "MFLO": if (OP_MFLO(i) == false) return false; break; case "MTHI": if (OP_MTHI(i) == false) return false; break; case "MTLO": if (OP_MTLO(i) == false) return false; break; case "MFC0": if (OP_MFC0(i) == false) return false; break; case "MTC0": if (OP_MTC0(i) == false) return false; break; default: if (sourceList[i].Length == 1 && sourceList[i][0] == string.Empty) break; else { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.UNKNOWNCMD, sourceList[i][0]); return false; } } } return true; }
private bool OP_SYSCALL(int i) { if (sourceList[i].Length != 1) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.WRONGARGUNUM, "1"); return false; } this.codelist.Add(new Instruction(sourceList[i][0], string.Empty, string.Empty, string.Empty)); return true; }
private bool OP_SW(int i) { if (sourceList[i].Length != 3) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.WRONGARGUNUM, "3"); return false; } if (!CheckRegister(sourceList[i][1])) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.WRONGREGNAME); return false; } string[] SubArg = sourceList[i][2].Split(new Char[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries); if (SubArg.Length != 2) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.WRONGARG, this.sourceList[i][2]); return false; } else { sourceList[i] = new string[] { sourceList[i][0], sourceList[i][1], SubArg[1], SubArg[0] }; if (!CheckRegister(sourceList[i][2])) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.INVALIDLABEL, this.sourceList[i][2]); return false; } int imm = new int(); if (ConvertImmediate(i, sourceList[i][3], out imm, true) == false) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.WRONGOFFSET, this.sourceList[i][3]); return false; } this.codelist.Add(new Instruction(sourceList[i][0], sourceList[i][1], sourceList[i][2], imm.ToString())); return true; } }
private bool OP_MTLO(int i) { if (sourceList[i].Length != 2) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.WRONGARGUNUM, "2"); return false; } if (!CheckRegister(sourceList[i][1])) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.WRONGREGNAME); return false; } this.codelist.Add(new Instruction(sourceList[i][0], sourceList[i][1], string.Empty, string.Empty)); return true; }
private bool OP_JAL(int i) { if (sourceList[i].Length != 2) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.WRONGARGUNUM, "2"); return false; } if (CheckAddress(sourceList[i][1]) == false) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.ADDNOTFOUND, sourceList[i][1]); return false; } this.codelist.Add(new Instruction(sourceList[i][0], sourceList[i][1], string.Empty, string.Empty)); return true; }
private bool OP_BLTZAL(int i) { if (sourceList[i].Length != 3) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.WRONGARGUNUM, "3"); return false; } if (!CheckRegister(sourceList[i][1])) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.WRONGREGNAME); return false; } if (CheckAddress(sourceList[i][3]) == false) { this.error = new AssemblerErrorInfo((int)codeindextable[i], AssemblerError.ADDNOTFOUND); return false; } this.codelist.Add(new Instruction(sourceList[i][0], sourceList[i][1], sourceList[i][2], sourceList[i][3])); return true; }