Compare commits
28 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9f77c1ec11 | |||
| 0ae55ef466 | |||
| ada82368d0 | |||
| 9f1d80cd9e | |||
| ade88faa0b | |||
| 99a7e32269 | |||
| 348f7bf720 | |||
| 9c8d79bf67 | |||
| 95701058f1 | |||
| c7f52d9ce8 | |||
| 4bc49bb200 | |||
| bd07de84f9 | |||
| fb4883b840 | |||
| 0dd3e6d83f | |||
| 35501025ff | |||
| cf1a4d00af | |||
| 785fb7a8e7 | |||
| a4abd128f5 | |||
| e6b13abc15 | |||
| d5cd5a5849 | |||
| eb6cde02a7 | |||
| 9738ba1afd | |||
| 432d706314 | |||
| be1c9f9fd7 | |||
| f14d05a28a | |||
| 6fcc51f2ca | |||
| 777adffdea | |||
| 7c72155fea |
95
README.md
Normal file
95
README.md
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
# Assembleur
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./Assembleur [--help] [--reversed] [--version] [--output file] [--format type] file
|
||||||
|
```
|
||||||
|
|
||||||
|
There are 3 format types :
|
||||||
|
- "int" : 32 bits integers are written. Exemple : `10878976`
|
||||||
|
- "binint" : bits are written. Exemple : `00000000 10100110 00000000 00000000`
|
||||||
|
- "bin" : the file is written in pure binary
|
||||||
|
- "logisim" (default) : the file is written in binary for use in LogiSim
|
||||||
|
|
||||||
|
## Exemple
|
||||||
|
|
||||||
|
```assembly
|
||||||
|
operations:
|
||||||
|
ADD R1 R2 #1
|
||||||
|
SUB R1 R2 R3
|
||||||
|
AND R1 R2 #33
|
||||||
|
XOR R1 R2 R3
|
||||||
|
OR R1 R2 R3
|
||||||
|
SL R1 R2 R3
|
||||||
|
SR R1 R2 R3
|
||||||
|
MUL R1 R2 R3
|
||||||
|
io:
|
||||||
|
STR R1 R2 R3
|
||||||
|
LD R1 R2 R3
|
||||||
|
sauts:
|
||||||
|
JMP controle
|
||||||
|
JEQU R1 R2 io
|
||||||
|
JNEQ R1 R2 sauts
|
||||||
|
JSUP R1 R2 operations
|
||||||
|
JINF R1 R2 controle
|
||||||
|
controle:
|
||||||
|
CALL io
|
||||||
|
RET
|
||||||
|
```
|
||||||
|
|
||||||
|
Produces
|
||||||
|
|
||||||
|
```
|
||||||
|
00000100 10100000 00000000 00000001
|
||||||
|
00010000 10100110 00000000 00000000
|
||||||
|
00100100 10100000 00000000 00100001
|
||||||
|
01000000 10100110 00000000 00000000
|
||||||
|
00110000 10100110 00000000 00000000
|
||||||
|
01010000 10100110 00000000 00000000
|
||||||
|
01100000 10100110 00000000 00000000
|
||||||
|
01110000 10100110 00000000 00000000
|
||||||
|
01000000 10100110 00000000 00000000
|
||||||
|
01010000 10100110 00000000 00000000
|
||||||
|
11000000 00000000 00000000 00010000
|
||||||
|
11010000 10100000 00000000 00001001
|
||||||
|
11100000 10100000 00000000 00001011
|
||||||
|
11110000 10100000 00000000 00000001
|
||||||
|
11000000 10100000 00000000 00010000
|
||||||
|
11010000 00000000 00000000 00001001
|
||||||
|
11100000 00000000 00000000 00000000
|
||||||
|
```
|
||||||
|
|
||||||
|
## Releases
|
||||||
|
|
||||||
|
Pre-compiled binaries are available in the [Release](https://git.ale-pri.com/Persson-dev/Assembleur/releases) section.
|
||||||
|
|
||||||
|
## Build
|
||||||
|
|
||||||
|
If you wish to compile yourself, you must have [xmake](https://xmake.io) installed.
|
||||||
|
Instructions on how to install (and you should) [here](https://xmake.io/#/guide/installation)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
xmake
|
||||||
|
```
|
||||||
|
|
||||||
|
## Run
|
||||||
|
|
||||||
|
```bash
|
||||||
|
xmake run Assembleur [args]
|
||||||
|
```
|
||||||
|
|
||||||
|
Example :
|
||||||
|
|
||||||
|
```bash
|
||||||
|
xmake run Assembleur test.asm -o memory
|
||||||
|
```
|
||||||
|
|
||||||
|
Parses the file `test.asm` and writes the output into the file `memory`
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
You can also add the binary to your path using
|
||||||
|
```bash
|
||||||
|
xmake install
|
||||||
|
```
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "Assembleur.h"
|
#include "Assembleur.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
@@ -12,7 +13,7 @@ enum TypeArithmetique {
|
|||||||
Xor,
|
Xor,
|
||||||
Sl,
|
Sl,
|
||||||
Sr,
|
Sr,
|
||||||
Not,
|
Mul,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum TypeMemoire {
|
enum TypeMemoire {
|
||||||
@@ -38,6 +39,7 @@ static std::map<std::string, Instruction> INSTRUCTION_KEYS = {
|
|||||||
{"xor", {Arithmetique, Xor}},
|
{"xor", {Arithmetique, Xor}},
|
||||||
{"sl", {Arithmetique, Sl}},
|
{"sl", {Arithmetique, Sl}},
|
||||||
{"sr", {Arithmetique, Sr}},
|
{"sr", {Arithmetique, Sr}},
|
||||||
|
{"mul", {Arithmetique, Mul}},
|
||||||
{"str", {Memoire, Str}},
|
{"str", {Memoire, Str}},
|
||||||
{"ld", {Memoire, Ld}},
|
{"ld", {Memoire, Ld}},
|
||||||
{"jmp", {SautControle, Jump}},
|
{"jmp", {SautControle, Jump}},
|
||||||
@@ -49,54 +51,78 @@ static std::map<std::string, Instruction> INSTRUCTION_KEYS = {
|
|||||||
{"ret", {SautControle, Ret}},
|
{"ret", {SautControle, Ret}},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
constexpr int LINE_LENGTH = 32;
|
||||||
|
constexpr int INSTRUCTION_BITS_COUNT = 2;
|
||||||
|
constexpr int OPERATION_BITS_COUNT = 2;
|
||||||
|
constexpr int IMMEDIATE_BITS_COUNT = 1;
|
||||||
|
constexpr int INSTRUCTION_BLOCK_SIZE = INSTRUCTION_BITS_COUNT + OPERATION_BITS_COUNT + IMMEDIATE_BITS_COUNT;
|
||||||
|
constexpr int REGISTRY_BITS_COUNT = 3;
|
||||||
|
|
||||||
|
static constexpr int GetOffset(int a_Start, int a_BlockSize) {
|
||||||
|
return LINE_LENGTH - a_Start - a_BlockSize;
|
||||||
|
}
|
||||||
|
|
||||||
std::uint32_t Assembleur::ParseLabel(const std::string& a_Label) {
|
std::uint32_t Assembleur::ParseLabel(const std::string& a_Label) {
|
||||||
auto it = m_Labels.find(a_Label);
|
auto it = m_Labels.find(a_Label);
|
||||||
|
|
||||||
if (it == m_Labels.end()) {
|
if (it == m_Labels.end()) {
|
||||||
throw std::runtime_error("Label " + a_Label + " not found !");
|
throw std::invalid_argument("Label " + a_Label + " not found !");
|
||||||
}
|
}
|
||||||
|
|
||||||
return it->second;
|
// the address starts at 0, not 1
|
||||||
|
return it->second - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Assembleur::AddLabel(const std::string& a_Label, std::uint32_t a_Line) {
|
void Assembleur::AddLabel(const std::string& a_Label, std::uint32_t a_Line) {
|
||||||
m_Labels.insert({a_Label, a_Line});
|
m_Labels.insert({a_Label, a_Line});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2 bits type instruction | 3 bits sous-type opération
|
||||||
std::uint32_t Assembleur::IToInt(Instruction a_Instruction) {
|
std::uint32_t Assembleur::IToInt(Instruction a_Instruction) {
|
||||||
return static_cast<std::uint32_t>(a_Instruction.m_Instruction) << 30 | static_cast<std::uint32_t>(a_Instruction.m_SubInstruction)
|
return static_cast<std::uint32_t>(a_Instruction.m_Instruction) << GetOffset(0, INSTRUCTION_BITS_COUNT) |
|
||||||
<< 28;
|
static_cast<std::uint32_t>(a_Instruction.m_SubInstruction) << GetOffset(INSTRUCTION_BITS_COUNT, OPERATION_BITS_COUNT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 5 bits instruction | 1 bit immédiat (non utilisé) | 3 bits R1 | 3 bits R2 | 3 bits R3
|
||||||
std::uint32_t Assembleur::ParseOperation(Instruction a_Instruction, std::uint32_t a_R1, std::uint32_t a_R2, std::uint32_t a_R3) {
|
std::uint32_t Assembleur::ParseOperation(Instruction a_Instruction, std::uint32_t a_R1, std::uint32_t a_R2, std::uint32_t a_R3) {
|
||||||
return IToInt(a_Instruction) | a_R1 << 23 | a_R2 << 20 | a_R3 << 17;
|
return IToInt(a_Instruction) |
|
||||||
|
a_R1 << GetOffset(INSTRUCTION_BLOCK_SIZE, REGISTRY_BITS_COUNT) |
|
||||||
|
a_R2 << GetOffset(INSTRUCTION_BLOCK_SIZE + REGISTRY_BITS_COUNT, REGISTRY_BITS_COUNT) |
|
||||||
|
a_R3 << GetOffset(INSTRUCTION_BLOCK_SIZE + 2 * REGISTRY_BITS_COUNT, REGISTRY_BITS_COUNT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 5 bits instruction | 1 bit immédiat | 3 bits R1 | 3 bits R2 | 20 bits constante
|
||||||
std::uint32_t Assembleur::ParseOperationImmediate(
|
std::uint32_t Assembleur::ParseOperationImmediate(
|
||||||
Instruction a_Instruction, std::uint32_t a_R1, std::uint32_t a_R2, std::uint32_t a_C1) {
|
Instruction a_Instruction, std::uint32_t a_R1, std::uint32_t a_R2, std::uint32_t a_C1) {
|
||||||
return IToInt(a_Instruction) | 1 << 26 | a_R1 << 23 | a_R2 << 20 | a_C1;
|
return IToInt(a_Instruction) | 1 << GetOffset(INSTRUCTION_BITS_COUNT + OPERATION_BITS_COUNT, IMMEDIATE_BITS_COUNT) |
|
||||||
|
a_R1 << GetOffset(INSTRUCTION_BLOCK_SIZE, REGISTRY_BITS_COUNT) |
|
||||||
|
a_R2 << GetOffset(INSTRUCTION_BLOCK_SIZE + REGISTRY_BITS_COUNT, REGISTRY_BITS_COUNT) |
|
||||||
|
a_C1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 5 bits instruction | 1 bit immédiat (non utilisé) | 26 bits adresse du label
|
||||||
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) {
|
||||||
std::int32_t jump = ParseLabel(a_Label) - a_Instruction.m_Line;
|
return IToInt(a_Instruction) | ParseLabel(a_Label) & 0x3FFFFFF;
|
||||||
if (jump < 0)
|
|
||||||
jump = std::abs(jump) & 0x7FFFFFF | 0x4000000;
|
|
||||||
return IToInt(a_Instruction) | jump;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 5 bits instruction | 1 bit immédiat (non utilisé) | 3 bits R1 | 3 bits R2 | 20 bits adresse du label
|
||||||
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) {
|
||||||
std::int32_t jump = ParseLabel(a_Label) - a_Instruction.m_Line;
|
return IToInt(a_Instruction) |
|
||||||
if (jump < 0)
|
a_R1 << GetOffset(INSTRUCTION_BLOCK_SIZE, REGISTRY_BITS_COUNT) |
|
||||||
jump = std::abs(jump) & 0xFFFFF | 0x100000;
|
a_R2 << GetOffset(INSTRUCTION_BLOCK_SIZE + REGISTRY_BITS_COUNT, REGISTRY_BITS_COUNT) |
|
||||||
return IToInt(a_Instruction) | a_R1 << 24 | a_R2 << 21 | jump;
|
ParseLabel(a_Label) & 0xFFFFF;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 5 bits instruction | 1 bit immédiat (non utilisé)
|
||||||
std::uint32_t Assembleur::ParseJump(Instruction a_Instruction) {
|
std::uint32_t Assembleur::ParseJump(Instruction a_Instruction) {
|
||||||
return IToInt(a_Instruction);
|
return IToInt(a_Instruction);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::uint32_t Assembleur::ParseIO(Instruction a_Instruction, std::uint32_t a_R1, std::uint32_t a_R2, std::uint32_t a_R3) {
|
// 5 bits instruction | 1 bit immédiat (non utilisé) | 3 bits R1 | 3 bits R2
|
||||||
return IToInt(a_Instruction) | a_R1 << 24 | a_R2 << 21 | a_R3 << 18;
|
std::uint32_t Assembleur::ParseIO(Instruction a_Instruction, std::uint32_t a_R1, std::uint32_t a_R2) {
|
||||||
|
return IToInt(a_Instruction) |
|
||||||
|
a_R1 << GetOffset(INSTRUCTION_BLOCK_SIZE, REGISTRY_BITS_COUNT) |
|
||||||
|
a_R2 << GetOffset(INSTRUCTION_BLOCK_SIZE + REGISTRY_BITS_COUNT, REGISTRY_BITS_COUNT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -105,8 +131,11 @@ std::uint32_t Assembleur::ParseIO(Instruction a_Instruction, std::uint32_t a_R1,
|
|||||||
|
|
||||||
static std::uint32_t ParseRegistry(const std::string& a_Str) {
|
static std::uint32_t ParseRegistry(const std::string& a_Str) {
|
||||||
if (a_Str.at(0) != 'R')
|
if (a_Str.at(0) != 'R')
|
||||||
throw std::runtime_error("Registry " + a_Str + " not found !");
|
throw std::invalid_argument("Registry " + a_Str + " not found !");
|
||||||
return std::stoi(a_Str.substr(1));
|
std::uint32_t registry = std::stoi(a_Str.substr(1));
|
||||||
|
if (registry > 7)
|
||||||
|
throw std::invalid_argument("You can only have up to 8 registries !");
|
||||||
|
return registry;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool IsConstant(const std::string& a_Str) {
|
static bool IsConstant(const std::string& a_Str) {
|
||||||
@@ -115,7 +144,7 @@ static bool IsConstant(const std::string& a_Str) {
|
|||||||
|
|
||||||
static std::uint32_t ParseConstant(const std::string& a_Str) {
|
static std::uint32_t ParseConstant(const std::string& a_Str) {
|
||||||
if (a_Str.at(0) != '#')
|
if (a_Str.at(0) != '#')
|
||||||
throw std::runtime_error("Registry " + a_Str + " not found !");
|
throw std::invalid_argument("Registry " + a_Str + " not found !");
|
||||||
return std::stoi(a_Str.substr(1));
|
return std::stoi(a_Str.substr(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,13 +153,15 @@ std::uint32_t Assembleur::ParseInstruction(const std::string& a_Str, std::uint32
|
|||||||
std::string ins;
|
std::string ins;
|
||||||
ss >> ins;
|
ss >> ins;
|
||||||
|
|
||||||
|
// to lower case
|
||||||
|
std::transform(ins.begin(), ins.end(), ins.begin(), [](unsigned char c) { return std::tolower(c); });
|
||||||
|
|
||||||
auto it = INSTRUCTION_KEYS.find(ins);
|
auto it = INSTRUCTION_KEYS.find(ins);
|
||||||
if (it == INSTRUCTION_KEYS.end()) {
|
if (it == INSTRUCTION_KEYS.end()) {
|
||||||
throw std::runtime_error("[Line " + std::to_string(a_RealLine) + "] " + "Instruction \"" + ins + "\" not found !");
|
throw std::invalid_argument("[Line " + std::to_string(a_RealLine) + "] " + "Instruction \"" + ins + "\" not found !");
|
||||||
}
|
}
|
||||||
|
|
||||||
Instruction instruction = it->second;
|
Instruction instruction = it->second;
|
||||||
instruction.m_Line = a_Line;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
switch (instruction.m_Instruction) {
|
switch (instruction.m_Instruction) {
|
||||||
@@ -145,9 +176,9 @@ std::uint32_t Assembleur::ParseInstruction(const std::string& a_Str, std::uint32
|
|||||||
}
|
}
|
||||||
|
|
||||||
case Memoire: {
|
case Memoire: {
|
||||||
std::string R1, R2, R3;
|
std::string R1, R2;
|
||||||
ss >> R1 >> R2 >> R3;
|
ss >> R1 >> R2;
|
||||||
return ParseIO(instruction, ParseRegistry(R1), ParseRegistry(R2), ParseRegistry(R3));
|
return ParseIO(instruction, ParseRegistry(R1), ParseRegistry(R2));
|
||||||
}
|
}
|
||||||
|
|
||||||
case SautControle: {
|
case SautControle: {
|
||||||
@@ -174,8 +205,8 @@ std::uint32_t Assembleur::ParseInstruction(const std::string& a_Str, std::uint32
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (std::runtime_error& e) {
|
} catch (std::invalid_argument& e) {
|
||||||
throw std::runtime_error("[Line " + std::to_string(a_RealLine) + "] " + e.what());
|
throw std::invalid_argument("[Line " + std::to_string(a_RealLine) + "] " + e.what());
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ enum TypeInstruction : std::uint8_t {
|
|||||||
struct Instruction {
|
struct Instruction {
|
||||||
TypeInstruction m_Instruction;
|
TypeInstruction m_Instruction;
|
||||||
std::uint8_t m_SubInstruction;
|
std::uint8_t m_SubInstruction;
|
||||||
std::uint32_t m_Line;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Assembleur {
|
class Assembleur {
|
||||||
@@ -40,5 +39,5 @@ class Assembleur {
|
|||||||
|
|
||||||
std::uint32_t ParseJump(Instruction a_Instruction);
|
std::uint32_t ParseJump(Instruction a_Instruction);
|
||||||
|
|
||||||
std::uint32_t ParseIO(Instruction a_Instruction, std::uint32_t a_R1, std::uint32_t a_R2, std::uint32_t a_R3);
|
std::uint32_t ParseIO(Instruction a_Instruction, std::uint32_t a_R1, std::uint32_t a_R2);
|
||||||
};
|
};
|
||||||
50
src/IO.cpp
50
src/IO.cpp
@@ -1,8 +1,27 @@
|
|||||||
#include "IO.h"
|
#include "IO.h"
|
||||||
|
|
||||||
#include "Assembleur.h"
|
#include "Assembleur.h"
|
||||||
|
#include <algorithm>
|
||||||
#include <bitset>
|
#include <bitset>
|
||||||
|
#include <cstring>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
|
static std::uint32_t reverseInt(std::uint32_t a_Int) {
|
||||||
|
std::uint32_t result = 0;
|
||||||
|
for (int i = 0; i < 32; i++) {
|
||||||
|
result |= a_Int & 0x1;
|
||||||
|
result <<= 1;
|
||||||
|
a_Int >>= 1;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void reverseDataByte(BinaryData& a_Data) {
|
||||||
|
for (std::uint32_t& element : a_Data) {
|
||||||
|
element = reverseInt(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
BinaryData ParseFile(const std::string& fileName) {
|
BinaryData ParseFile(const std::string& fileName) {
|
||||||
std::ifstream file{fileName};
|
std::ifstream file{fileName};
|
||||||
@@ -45,22 +64,43 @@ BinaryData ParseFile(const std::string& fileName) {
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OutputFileBinary(const BinaryData& a_Data, const std::string& fileName) {
|
void OutputFileBinary(BinaryData& a_Data, const std::string& fileName, bool a_Reverse) {
|
||||||
std::ofstream file{fileName};
|
std::ofstream file{fileName};
|
||||||
|
if (a_Reverse)
|
||||||
|
reverseDataByte(a_Data);
|
||||||
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) {
|
void OutputFileIntegers(BinaryData& a_Data, const std::string& fileName, bool a_Reverse) {
|
||||||
std::ofstream file{fileName};
|
std::ofstream file{fileName};
|
||||||
|
if (a_Reverse)
|
||||||
|
reverseDataByte(a_Data);
|
||||||
for (std::uint32_t number : a_Data) {
|
for (std::uint32_t number : a_Data) {
|
||||||
file << number << "\n";
|
file << number << "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void OutputFileBinIntegers(const BinaryData& a_Data, const std::string& fileName) {
|
void OutputFileBinIntegers(BinaryData& a_Data, const std::string& fileName, bool a_Reverse) {
|
||||||
std::ofstream file{fileName};
|
std::ofstream file{fileName};
|
||||||
|
if (a_Reverse)
|
||||||
|
reverseDataByte(a_Data);
|
||||||
for (std::uint32_t number : a_Data) {
|
for (std::uint32_t number : a_Data) {
|
||||||
file << std::bitset<8>(number >> 24) << " " << std::bitset<8>(number >> 16) << " " << std::bitset<8>(number >> 8) <<
|
file << std::bitset<8>(number >> 24) << " " << std::bitset<8>(number >> 16) << " " << std::bitset<8>(number >> 8) << " "
|
||||||
" " << std::bitset<8>(number) << "\n";
|
<< std::bitset<8>(number) << "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void OutputFileLogisim(BinaryData& a_Data, const std::string& fileName, const std::string& a_Header, bool a_Reverse) {
|
||||||
|
std::ofstream file{fileName};
|
||||||
|
file << a_Header << "\n";
|
||||||
|
std::uint64_t cursor = 0;
|
||||||
|
if (a_Reverse)
|
||||||
|
reverseDataByte(a_Data);
|
||||||
|
for (std::uint32_t number : a_Data) {
|
||||||
|
if (cursor % 8 == 0) {
|
||||||
|
file << std::setfill('0') << std::setw(4) << std::hex << cursor << std::dec << ": ";
|
||||||
|
}
|
||||||
|
file << std::setfill('0') << std::setw(8) << std::hex << number << std::dec << (((cursor + 1) % 8 == 0) ? "\n" : " ");
|
||||||
|
cursor++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
7
src/IO.h
7
src/IO.h
@@ -7,6 +7,7 @@ using BinaryData = std::vector<std::uint32_t>;
|
|||||||
|
|
||||||
BinaryData ParseFile(const std::string& fileName);
|
BinaryData ParseFile(const std::string& fileName);
|
||||||
|
|
||||||
void OutputFileBinary(const BinaryData& a_Data, const std::string& fileName);
|
void OutputFileBinary(BinaryData& a_Data, const std::string& fileName, bool a_Reverse);
|
||||||
void OutputFileIntegers(const BinaryData& a_Data, const std::string& fileName);
|
void OutputFileIntegers(BinaryData& a_Data, const std::string& fileName, bool a_Reverse);
|
||||||
void OutputFileBinIntegers(const BinaryData& a_Data, const std::string& fileName);
|
void OutputFileBinIntegers(BinaryData& a_Data, const std::string& fileName, bool a_Reverse);
|
||||||
|
void OutputFileLogisim(BinaryData& a_Data, const std::string& fileName, const std::string& a_Header, bool a_Reverse);
|
||||||
30
src/main.cpp
30
src/main.cpp
@@ -3,9 +3,11 @@
|
|||||||
|
|
||||||
#include "IO.h"
|
#include "IO.h"
|
||||||
|
|
||||||
|
#define ASSEMBLEUR_VERSION "1.7"
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
|
|
||||||
argparse::ArgumentParser program("Assembleur");
|
argparse::ArgumentParser program("Assembleur", ASSEMBLEUR_VERSION);
|
||||||
|
|
||||||
std::string inputFileName;
|
std::string inputFileName;
|
||||||
program.add_argument("file")
|
program.add_argument("file")
|
||||||
@@ -21,12 +23,22 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
std::string formatType;
|
std::string formatType;
|
||||||
program.add_argument("-f", "--format")
|
program.add_argument("-f", "--format")
|
||||||
.help("Type of the output. [bin|int|binint]")
|
.help("Type of the output. [logisim|bin|int|binint]")
|
||||||
.metavar("type")
|
.metavar("type")
|
||||||
.default_value(std::string("binint"))
|
.default_value(std::string("logisim"))
|
||||||
.choices("bin", "int", "binint")
|
.choices("logisim", "bin", "int", "binint")
|
||||||
.store_into(formatType);
|
.store_into(formatType);
|
||||||
|
|
||||||
|
std::string header;
|
||||||
|
program.add_argument("-h", "--header")
|
||||||
|
.help("Header of the file")
|
||||||
|
.metavar("header")
|
||||||
|
.default_value(std::string("v3.0 hex words addressed"))
|
||||||
|
.store_into(header);
|
||||||
|
|
||||||
|
bool reverse;
|
||||||
|
program.add_argument("-r", "--reversed").help("inverse bit orders").flag().store_into(reverse);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
program.parse_args(argc, argv);
|
program.parse_args(argc, argv);
|
||||||
} catch (const std::exception& err) {
|
} catch (const std::exception& err) {
|
||||||
@@ -38,12 +50,14 @@ int main(int argc, char** argv) {
|
|||||||
try {
|
try {
|
||||||
auto output = ParseFile(inputFileName);
|
auto output = ParseFile(inputFileName);
|
||||||
|
|
||||||
if (formatType == "bin") {
|
if (formatType == "logisim") {
|
||||||
OutputFileBinary(output, outputFileName);
|
OutputFileLogisim(output, outputFileName, header, reverse);
|
||||||
|
} else if (formatType == "bin") {
|
||||||
|
OutputFileBinary(output, outputFileName, reverse);
|
||||||
} else if (formatType == "int") {
|
} else if (formatType == "int") {
|
||||||
OutputFileIntegers(output, outputFileName);
|
OutputFileIntegers(output, outputFileName, reverse);
|
||||||
} else {
|
} else {
|
||||||
OutputFileBinIntegers(output, outputFileName);
|
OutputFileBinIntegers(output, outputFileName, reverse);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (std::runtime_error& e) {
|
} catch (std::runtime_error& e) {
|
||||||
|
|||||||
33
test.asm
33
test.asm
@@ -1,20 +1,21 @@
|
|||||||
operations:
|
operations:
|
||||||
add R1 R2 R3
|
ADD R1 R2 #1
|
||||||
sub R1 R2 #69
|
SUB R1 R2 R3
|
||||||
and R1 R2 R3
|
AND R1 R2 #33
|
||||||
xor R1 R2 R3
|
XOR R1 R2 R3
|
||||||
or R1 R2 R3
|
OR R1 R2 R3
|
||||||
sl R1 R2 R3
|
SL R1 R2 R3
|
||||||
sr R1 R2 R3
|
SR R1 R2 R3
|
||||||
|
MUL R1 R2 R3
|
||||||
io:
|
io:
|
||||||
str R1 R2 R3
|
STR R1 R2 R3
|
||||||
ld R1 R2 R3
|
LD R1 R2 R3
|
||||||
sauts:
|
sauts:
|
||||||
jmp controle
|
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 controle
|
JINF R1 R2 controle
|
||||||
controle:
|
controle:
|
||||||
call io
|
CALL io
|
||||||
ret
|
RET
|
||||||
|
|||||||
72
xmake.lua
72
xmake.lua
@@ -10,72 +10,6 @@ target("Assembleur")
|
|||||||
set_rundir(".")
|
set_rundir(".")
|
||||||
add_packages("argparse")
|
add_packages("argparse")
|
||||||
|
|
||||||
--
|
if is_os("windows") then
|
||||||
-- If you want to known more usage about xmake, please see https://xmake.io
|
add_ldflags("-static", "-static-libstdc++")
|
||||||
--
|
end
|
||||||
-- ## FAQ
|
|
||||||
--
|
|
||||||
-- You can enter the project directory firstly before building project.
|
|
||||||
--
|
|
||||||
-- $ cd projectdir
|
|
||||||
--
|
|
||||||
-- 1. How to build project?
|
|
||||||
--
|
|
||||||
-- $ xmake
|
|
||||||
--
|
|
||||||
-- 2. How to configure project?
|
|
||||||
--
|
|
||||||
-- $ xmake f -p [macosx|linux|iphoneos ..] -a [x86_64|i386|arm64 ..] -m [debug|release]
|
|
||||||
--
|
|
||||||
-- 3. Where is the build output directory?
|
|
||||||
--
|
|
||||||
-- The default output directory is `./build` and you can configure the output directory.
|
|
||||||
--
|
|
||||||
-- $ xmake f -o outputdir
|
|
||||||
-- $ xmake
|
|
||||||
--
|
|
||||||
-- 4. How to run and debug target after building project?
|
|
||||||
--
|
|
||||||
-- $ xmake run [targetname]
|
|
||||||
-- $ xmake run -d [targetname]
|
|
||||||
--
|
|
||||||
-- 5. How to install target to the system directory or other output directory?
|
|
||||||
--
|
|
||||||
-- $ xmake install
|
|
||||||
-- $ xmake install -o installdir
|
|
||||||
--
|
|
||||||
-- 6. Add some frequently-used compilation flags in xmake.lua
|
|
||||||
--
|
|
||||||
-- @code
|
|
||||||
-- -- add debug and release modes
|
|
||||||
-- add_rules("mode.debug", "mode.release")
|
|
||||||
--
|
|
||||||
-- -- add macro definition
|
|
||||||
-- add_defines("NDEBUG", "_GNU_SOURCE=1")
|
|
||||||
--
|
|
||||||
-- -- set warning all as error
|
|
||||||
-- set_warnings("all", "error")
|
|
||||||
--
|
|
||||||
-- -- set language: c99, c++11
|
|
||||||
-- set_languages("c99", "c++11")
|
|
||||||
--
|
|
||||||
-- -- set optimization: none, faster, fastest, smallest
|
|
||||||
-- set_optimize("fastest")
|
|
||||||
--
|
|
||||||
-- -- add include search directories
|
|
||||||
-- add_includedirs("/usr/include", "/usr/local/include")
|
|
||||||
--
|
|
||||||
-- -- add link libraries and search directories
|
|
||||||
-- add_links("tbox")
|
|
||||||
-- add_linkdirs("/usr/local/lib", "/usr/lib")
|
|
||||||
--
|
|
||||||
-- -- add system link libraries
|
|
||||||
-- add_syslinks("z", "pthread")
|
|
||||||
--
|
|
||||||
-- -- add compilation and link flags
|
|
||||||
-- add_cxflags("-stdnolib", "-fno-strict-aliasing")
|
|
||||||
-- add_ldflags("-L/usr/local/lib", "-lpthread", {force = true})
|
|
||||||
--
|
|
||||||
-- @endcode
|
|
||||||
--
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user