5 Commits

Author SHA1 Message Date
8452b98a97 set c++ standard 2024-10-23 20:51:41 +02:00
3207de80dd parse arguments 2024-10-23 20:48:18 +02:00
39eb113385 add multiple outputs 2024-10-23 20:12:11 +02:00
51f113804a fix jump forward 2024-10-23 20:05:41 +02:00
47e7a17a74 remove compile commands 2024-10-23 11:19:12 +02:00
8 changed files with 96 additions and 29 deletions

5
.gitignore vendored
View File

@@ -5,5 +5,6 @@ build/
# MacOS Cache
.DS_Store
test.bin
.vscode
.vscode
a.out

View File

@@ -1,11 +0,0 @@
[
{
"directory": "/home/simon/Programmation/Assembleur",
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-fvisibility=hidden", "-fvisibility-inlines-hidden", "-O3", "-DNDEBUG", "-o", "build/.objs/Assembleur/linux/x86_64/release/src/main.cpp.o", "src/main.cpp"],
"file": "src/main.cpp"
},
{
"directory": "/home/simon/Programmation/Assembleur",
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-fvisibility=hidden", "-fvisibility-inlines-hidden", "-O3", "-DNDEBUG", "-o", "build/.objs/Assembleur/linux/x86_64/release/src/Assembleur.cpp.o", "src/Assembleur.cpp"],
"file": "src/Assembleur.cpp"
}]

View File

@@ -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;
}

View File

@@ -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";
}
}

View File

@@ -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);

View File

@@ -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;

View File

@@ -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

View File

@@ -1,9 +1,14 @@
add_rules("mode.debug", "mode.release")
add_requires("argparse")
set_languages("c++17")
target("Assembleur")
set_kind("binary")
add_files("src/*.cpp")
set_rundir(".")
add_packages("argparse")
--
-- If you want to known more usage about xmake, please see https://xmake.io