Compare commits
3 Commits
47e7a17a74
...
3207de80dd
| Author | SHA1 | Date | |
|---|---|---|---|
| 3207de80dd | |||
| 39eb113385 | |||
| 51f113804a |
5
.gitignore
vendored
5
.gitignore
vendored
@@ -5,5 +5,6 @@ build/
|
||||
# MacOS Cache
|
||||
.DS_Store
|
||||
|
||||
test.bin
|
||||
.vscode
|
||||
.vscode
|
||||
|
||||
a.out
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
44
src/IO.cpp
44
src/IO.cpp
@@ -1,34 +1,66 @@
|
||||
#include "IO.h"
|
||||
|
||||
#include "Assembleur.h"
|
||||
#include <bitset>
|
||||
#include <fstream>
|
||||
|
||||
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<std::string> 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--;
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
void OutputFile(const BinaryData& a_Data, const std::string& fileName) {
|
||||
void OutputFileBinary(const BinaryData& a_Data, const std::string& fileName) {
|
||||
std::ofstream file{fileName};
|
||||
file.write(reinterpret_cast<const char*>(a_Data.data()), a_Data.size() * sizeof(a_Data.at(0)));
|
||||
}
|
||||
|
||||
void OutputFileIntegers(const BinaryData& a_Data, const std::string& fileName) {
|
||||
std::ofstream file{fileName};
|
||||
for (std::uint32_t number : a_Data) {
|
||||
file << number << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
void OutputFileBinIntegers(const BinaryData& a_Data, const std::string& fileName) {
|
||||
std::ofstream file{fileName};
|
||||
for (std::uint32_t number : a_Data) {
|
||||
file << std::bitset<8>(number >> 24) << " " << std::bitset<8>(number >> 16) << " " << std::bitset<8>(number >> 8) <<
|
||||
" " << std::bitset<8>(number) << "\n";
|
||||
}
|
||||
}
|
||||
4
src/IO.h
4
src/IO.h
@@ -7,4 +7,6 @@ using BinaryData = std::vector<std::uint32_t>;
|
||||
|
||||
BinaryData ParseFile(const std::string& fileName);
|
||||
|
||||
void OutputFile(const BinaryData& a_Data, const std::string& fileName);
|
||||
void OutputFileBinary(const BinaryData& a_Data, const std::string& fileName);
|
||||
void OutputFileIntegers(const BinaryData& a_Data, const std::string& fileName);
|
||||
void OutputFileBinIntegers(const BinaryData& a_Data, const std::string& fileName);
|
||||
44
src/main.cpp
44
src/main.cpp
@@ -1,13 +1,51 @@
|
||||
#include <argparse/argparse.hpp>
|
||||
#include <iostream>
|
||||
|
||||
#include "IO.h"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
try {
|
||||
auto output = ParseFile("test.asm");
|
||||
argparse::ArgumentParser program("Assembleur");
|
||||
|
||||
std::string inputFileName;
|
||||
program.add_argument("file")
|
||||
.help("The assembly file to compile")
|
||||
.store_into(inputFileName);
|
||||
|
||||
std::string outputFileName;
|
||||
program.add_argument("-o", "--output")
|
||||
.help("Specify the output file.")
|
||||
.metavar("file")
|
||||
.default_value(std::string("a.out"))
|
||||
.store_into(outputFileName);
|
||||
|
||||
std::string formatType;
|
||||
program.add_argument("-f", "--format")
|
||||
.help("Type of the output. [bin|int|binint]")
|
||||
.metavar("type")
|
||||
.default_value(std::string("binint"))
|
||||
.choices("bin", "int", "binint")
|
||||
.store_into(formatType);
|
||||
|
||||
try {
|
||||
program.parse_args(argc, argv);
|
||||
} catch (const std::exception& err) {
|
||||
std::cerr << err.what() << std::endl;
|
||||
std::cerr << program;
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
try {
|
||||
auto output = ParseFile(inputFileName);
|
||||
|
||||
if (formatType == "bin") {
|
||||
OutputFileBinary(output, outputFileName);
|
||||
} else if (formatType == "int") {
|
||||
OutputFileIntegers(output, outputFileName);
|
||||
} else {
|
||||
OutputFileBinIntegers(output, outputFileName);
|
||||
}
|
||||
|
||||
OutputFile(output, "test.bin");
|
||||
} catch (std::runtime_error& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
return 1;
|
||||
|
||||
4
test.asm
4
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
|
||||
Reference in New Issue
Block a user