Compare commits
4 Commits
47e7a17a74
...
v1.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 8452b98a97 | |||
| 3207de80dd | |||
| 39eb113385 | |||
| 51f113804a |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -5,5 +5,6 @@ build/
|
|||||||
# MacOS Cache
|
# MacOS Cache
|
||||||
.DS_Store
|
.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) {
|
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)
|
if (jump < 0)
|
||||||
jump = jump & 0x7FFFFFF | 0x4000000;
|
jump = std::abs(jump) & 0x7FFFFFF | 0x4000000;
|
||||||
return IToInt(a_Instruction) | jump;
|
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) {
|
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)
|
if (jump < 0)
|
||||||
jump = jump & 0xFFFFF | 100000;
|
jump = std::abs(jump) & 0xFFFFF | 0x100000;
|
||||||
return IToInt(a_Instruction) | a_R1 << 24 | a_R2 << 21 | jump;
|
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 "IO.h"
|
||||||
|
|
||||||
#include "Assembleur.h"
|
#include "Assembleur.h"
|
||||||
|
#include <bitset>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
BinaryData ParseFile(const std::string& fileName) {
|
BinaryData ParseFile(const std::string& fileName) {
|
||||||
std::ifstream file{fileName};
|
std::ifstream file{fileName};
|
||||||
std::uint32_t lineNumber = 0, realLineNumber = 0;
|
std::uint32_t lineNumber = 0, realLineNumber = 0;
|
||||||
std::string line;
|
std::string currentLine;
|
||||||
|
|
||||||
|
std::vector<std::string> lines;
|
||||||
|
|
||||||
Assembleur assembleur;
|
Assembleur assembleur;
|
||||||
|
|
||||||
BinaryData output;
|
BinaryData output;
|
||||||
|
|
||||||
while (getline(file, line)) {
|
// parsing labels
|
||||||
|
while (getline(file, currentLine)) {
|
||||||
|
lines.push_back(currentLine);
|
||||||
lineNumber++;
|
lineNumber++;
|
||||||
realLineNumber++;
|
realLineNumber++;
|
||||||
|
|
||||||
if (line.find(":") != std::string::npos) {
|
if (currentLine.find(":") != std::string::npos) {
|
||||||
std::string label = line.substr(0, line.size() - 1);
|
std::string label = currentLine.substr(0, currentLine.size() - 1);
|
||||||
assembleur.AddLabel(label, lineNumber);
|
assembleur.AddLabel(label, lineNumber);
|
||||||
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));
|
output.push_back(assembleur.ParseInstruction(line, lineNumber, realLineNumber));
|
||||||
|
} else {
|
||||||
|
lineNumber--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return output;
|
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};
|
std::ofstream file{fileName};
|
||||||
file.write(reinterpret_cast<const char*>(a_Data.data()), a_Data.size() * sizeof(a_Data.at(0)));
|
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);
|
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 <iostream>
|
||||||
|
|
||||||
#include "IO.h"
|
#include "IO.h"
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
|
|
||||||
try {
|
argparse::ArgumentParser program("Assembleur");
|
||||||
auto output = ParseFile("test.asm");
|
|
||||||
|
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) {
|
} catch (std::runtime_error& e) {
|
||||||
std::cerr << e.what() << std::endl;
|
std::cerr << e.what() << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
4
test.asm
4
test.asm
@@ -10,11 +10,11 @@ io:
|
|||||||
str R1 R2 R3
|
str R1 R2 R3
|
||||||
ld R1 R2 R3
|
ld R1 R2 R3
|
||||||
sauts:
|
sauts:
|
||||||
jmp operations
|
jmp controle
|
||||||
jequ R1 R2 io
|
jequ R1 R2 io
|
||||||
jneq R1 R2 sauts
|
jneq R1 R2 sauts
|
||||||
jsup R1 R2 operations
|
jsup R1 R2 operations
|
||||||
jinf R1 R2 io
|
jinf R1 R2 controle
|
||||||
controle:
|
controle:
|
||||||
call io
|
call io
|
||||||
ret
|
ret
|
||||||
@@ -1,9 +1,14 @@
|
|||||||
add_rules("mode.debug", "mode.release")
|
add_rules("mode.debug", "mode.release")
|
||||||
|
|
||||||
|
add_requires("argparse")
|
||||||
|
|
||||||
|
set_languages("c++17")
|
||||||
|
|
||||||
target("Assembleur")
|
target("Assembleur")
|
||||||
set_kind("binary")
|
set_kind("binary")
|
||||||
add_files("src/*.cpp")
|
add_files("src/*.cpp")
|
||||||
set_rundir(".")
|
set_rundir(".")
|
||||||
|
add_packages("argparse")
|
||||||
|
|
||||||
--
|
--
|
||||||
-- If you want to known more usage about xmake, please see https://xmake.io
|
-- If you want to known more usage about xmake, please see https://xmake.io
|
||||||
|
|||||||
Reference in New Issue
Block a user