diff --git a/src/Assembleur.cpp b/src/Assembleur.cpp index 949f7ab..50bf1c5 100644 --- a/src/Assembleur.cpp +++ b/src/Assembleur.cpp @@ -78,16 +78,16 @@ std::uint32_t Assembleur::ParseOperationImmediate( } std::uint32_t Assembleur::ParseJump(Instruction a_Instruction, const std::string& a_Label) { - int jump = a_Instruction.m_Line - ParseLabel(a_Label); + std::int32_t jump = ParseLabel(a_Label) - a_Instruction.m_Line; if (jump < 0) - jump = jump & 0x7FFFFFF | 0x4000000; + jump = std::abs(jump) & 0x7FFFFFF | 0x4000000; return IToInt(a_Instruction) | jump; } std::uint32_t Assembleur::ParseJump(Instruction a_Instruction, std::uint8_t a_R1, std::uint8_t a_R2, const std::string& a_Label) { - int jump = a_Instruction.m_Line - ParseLabel(a_Label); + std::int32_t jump = ParseLabel(a_Label) - a_Instruction.m_Line; if (jump < 0) - jump = jump & 0xFFFFF | 100000; + jump = std::abs(jump) & 0xFFFFF | 0x100000; return IToInt(a_Instruction) | a_R1 << 24 | a_R2 << 21 | jump; } diff --git a/src/IO.cpp b/src/IO.cpp index 9aa9f9c..9619e53 100644 --- a/src/IO.cpp +++ b/src/IO.cpp @@ -6,22 +6,38 @@ BinaryData ParseFile(const std::string& fileName) { std::ifstream file{fileName}; std::uint32_t lineNumber = 0, realLineNumber = 0; - std::string line; + std::string currentLine; + + std::vector lines; Assembleur assembleur; BinaryData output; - while (getline(file, line)) { + // parsing labels + while (getline(file, currentLine)) { + lines.push_back(currentLine); lineNumber++; realLineNumber++; - if (line.find(":") != std::string::npos) { - std::string label = line.substr(0, line.size() - 1); + if (currentLine.find(":") != std::string::npos) { + std::string label = currentLine.substr(0, currentLine.size() - 1); assembleur.AddLabel(label, lineNumber); lineNumber--; - } else { + } + } + + lineNumber = realLineNumber = 0; + + // parsing instructions + for (std::string line : lines) { + lineNumber++; + realLineNumber++; + + if (line.find(":") == std::string::npos) { output.push_back(assembleur.ParseInstruction(line, lineNumber, realLineNumber)); + } else { + lineNumber--; } } diff --git a/test.asm b/test.asm index 7fd01e8..9336eb9 100644 --- a/test.asm +++ b/test.asm @@ -10,11 +10,11 @@ io: str R1 R2 R3 ld R1 R2 R3 sauts: - jmp operations + jmp controle jequ R1 R2 io jneq R1 R2 sauts jsup R1 R2 operations - jinf R1 R2 io + jinf R1 R2 controle controle: call io ret \ No newline at end of file