private void GenerateOperationLineASM(char opChar, string axReg, string tacWord) { TACFile.GetNextWord(); // skip operator if (opChar == '+') { AssemblyFile.AddLine($" mov ax, {axReg}"); AssemblyFile.AddLine($" add ax, {GetAndFormatWord()}"); AssemblyFile.AddLine($" mov {tacWord}, ax"); } else if (opChar == '-') { AssemblyFile.AddLine($" mov ax, {axReg}"); AssemblyFile.AddLine($" sub ax, {GetAndFormatWord()}"); AssemblyFile.AddLine($" mov {tacWord}, ax"); } else if (opChar == '/') { AssemblyFile.AddLine($" mov ax, {axReg}"); AssemblyFile.AddLine($" cwd"); AssemblyFile.AddLine($" mov bx, {GetAndFormatWord()}"); AssemblyFile.AddLine($" idiv bx"); AssemblyFile.AddLine($" mov {tacWord}, ax"); } else if (opChar == '*') { AssemblyFile.AddLine($" mov ax, {axReg}"); AssemblyFile.AddLine($" mov bx, {GetAndFormatWord()}"); AssemblyFile.AddLine($" imul bx"); AssemblyFile.AddLine($" mov {tacWord}, ax"); } }
private string GetAndFormatWord() { string word = TACFile.GetNextWord(); if (word[0] == '_') { word = word.Replace('_', '['); word += ']'; } return(word); }
private void GenerateProcBodyASM() { string tacWord = GetAndFormatWord(); while (tacWord != "endp") { if (TACKeywords.Contains(tacWord)) { GenerateProcLineUsingKeywordASM(tacWord); } else { GenerateProcLineUsingVariableASM(tacWord); } tacWord = GetAndFormatWord(); } TACFile.GetNextWord(); }
private void GenerateProcLineUsingVariableASM(string tacWord) { if (tacWord == "_ax") { TACFile.GetNextWord(); AssemblyFile.AddLine($" mov ax, {GetAndFormatWord()}"); } else { TACFile.GetNextWord(); // skip equal sign string axReg = GetAndFormatWord(); char opChar = TACFile.PeekNextChar(); if (opChar == '*' || opChar == '/' || opChar == '-' || opChar == '+') { GenerateOperationLineASM(opChar, axReg, tacWord); } else { GenerateAssignmentLineASM(axReg, tacWord); } } }
private void GenerateProcLineUsingKeywordASM(string tacWord) { if (tacWord == "proc") { TACFile.GetNextWord(); } else if (tacWord == "wrs") { AssemblyFile.AddLine($" mov dx, offset {TACFile.GetNextWord()}"); AssemblyFile.AddLine(" call writestr"); } else if (tacWord == "wri") { string bpWord = GetAndFormatWord(); AssemblyFile.AddLine($" mov dx, {bpWord}"); AssemblyFile.AddLine($" call writeint"); } else if (tacWord == "wrln") { AssemblyFile.AddLine($" call writeln"); } else if (tacWord == "rdi") { string bpWord = GetAndFormatWord(); AssemblyFile.AddLine($" call readint"); AssemblyFile.AddLine($" mov {bpWord}, bx"); } else if (tacWord == "call") { AssemblyFile.AddLine($" call {TACFile.GetNextWord()}"); } else if (tacWord == "push") { AssemblyFile.AddLine($" mov ax, {GetAndFormatWord()}"); AssemblyFile.AddLine($" push ax"); } }
private void GenerateAssignmentLineASM(string axReg, string tacWord) { if (axReg.Contains("ax")) { AssemblyFile.AddLine($" mov {tacWord}, ax"); } else if (tacWord.Contains("ax")) { AssemblyFile.AddLine($" mov ax, {axReg}"); } else { AssemblyFile.AddLine($" mov ax, {axReg}"); AssemblyFile.AddLine($" mov {tacWord}, ax"); if (!axReg.Contains("bp") && !int.TryParse(axReg, out int result)) { tacWord = GetAndFormatWord(); TACFile.GetNextWord(); // skip equal sign AssemblyFile.AddLine($" mov ax, {GetAndFormatWord()}"); AssemblyFile.AddLine($" mov {tacWord}, ax"); } } }