Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e45028b65d | |||
| e28ad4de5a | |||
| 890f884cce | |||
| 9dc7f61ed7 | |||
| f881b9b4e7 | |||
| 00b0368f03 | |||
| 6eec4d8d3d | |||
| 7ab4540201 | |||
| 7615546f9e | |||
| 9f77c1ec11 | |||
| 0ae55ef466 | |||
| ada82368d0 | |||
| 9f1d80cd9e | |||
| ade88faa0b | |||
| 99a7e32269 | |||
| 348f7bf720 | |||
| 9c8d79bf67 | |||
| 95701058f1 | |||
| c7f52d9ce8 | |||
| 4bc49bb200 | |||
| bd07de84f9 | |||
| fb4883b840 | |||
| 0dd3e6d83f | |||
| 35501025ff |
86
README.md
86
README.md
@@ -16,53 +16,58 @@ There are 3 format types :
|
||||
|
||||
```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
|
||||
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
|
||||
STR R1 R2
|
||||
LD R1 R2
|
||||
sauts:
|
||||
jmp controle
|
||||
jequ R1 R2 io
|
||||
jneq R1 R2 sauts
|
||||
jsup R1 R2 operations
|
||||
jinf R1 R2 controle
|
||||
JMP controle
|
||||
JEQU R1 R2 io
|
||||
JNEQ R1 R2 sauts
|
||||
JSUP R1 R2 operations
|
||||
JINF R1 R2 controle
|
||||
controle:
|
||||
call io
|
||||
ret
|
||||
CALL io
|
||||
RET
|
||||
```
|
||||
|
||||
Produces
|
||||
|
||||
```
|
||||
00000100 10100000 00000000 00000001
|
||||
00010000 10100110 00000000 00000000
|
||||
00100100 10100000 00000000 00100001
|
||||
01000000 10100110 00000000 00000000
|
||||
00001000 10100110 00000000 00000000
|
||||
00010100 10100000 00000000 00100001
|
||||
00100000 10100110 00000000 00000000
|
||||
00011000 10100110 00000000 00000000
|
||||
00101000 10100110 00000000 00000000
|
||||
00110000 10100110 00000000 00000000
|
||||
01010000 10100110 00000000 00000000
|
||||
01100000 10100110 00000000 00000000
|
||||
01110000 10100110 00000000 00000000
|
||||
01000001 01001100 00000000 00000000
|
||||
01010001 01001100 00000000 00000000
|
||||
11000000 00000000 00000000 00010000
|
||||
11010001 01000000 00000000 00001001
|
||||
11100001 01000000 00000000 00001011
|
||||
11110001 01000000 00000000 00000001
|
||||
11000001 01000000 00000000 00010000
|
||||
11010000 00000000 00000000 00001001
|
||||
11100000 00000000 00000000 00000000
|
||||
00111000 10100110 00000000 00000000
|
||||
01000000 10100000 00000000 00000000
|
||||
01001000 10100000 00000000 00000000
|
||||
11000000 00000000 00000000 00001111
|
||||
11001000 10100000 00000000 00001000
|
||||
11010000 10100000 00000000 00001010
|
||||
11011000 10100000 00000000 00000000
|
||||
11100000 10100000 00000000 00001111
|
||||
11101000 00000000 00000000 00001000
|
||||
11111000 00000000 00000000 00000000
|
||||
```
|
||||
|
||||
## Releases
|
||||
|
||||
Pre-compiled binaries are available in the [Release](https://git.ale-pri.com/Persson-dev/Assembleur/releases) section.
|
||||
|
||||
## Build
|
||||
|
||||
You should have [xmake](https://xmake.io) installed
|
||||
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
|
||||
@@ -73,3 +78,18 @@ xmake
|
||||
```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 <algorithm>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
@@ -27,7 +28,7 @@ enum TypeSautControle {
|
||||
Jsup,
|
||||
Jinf,
|
||||
Call,
|
||||
Ret,
|
||||
Ret = 7,
|
||||
};
|
||||
|
||||
static std::map<std::string, Instruction> INSTRUCTION_KEYS = {
|
||||
@@ -50,6 +51,17 @@ static std::map<std::string, Instruction> INSTRUCTION_KEYS = {
|
||||
{"ret", {SautControle, Ret}},
|
||||
};
|
||||
|
||||
constexpr int LINE_LENGTH = 32;
|
||||
constexpr int INSTRUCTION_BITS_COUNT = 2;
|
||||
constexpr int OPERATION_BITS_COUNT = 3;
|
||||
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) {
|
||||
auto it = m_Labels.find(a_Label);
|
||||
|
||||
@@ -57,41 +69,60 @@ std::uint32_t Assembleur::ParseLabel(const std::string& a_Label) {
|
||||
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) {
|
||||
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) {
|
||||
return static_cast<std::uint32_t>(a_Instruction.m_Instruction) << 30 | static_cast<std::uint32_t>(a_Instruction.m_SubInstruction)
|
||||
<< 28;
|
||||
return static_cast<std::uint32_t>(a_Instruction.m_Instruction) << GetOffset(0, INSTRUCTION_BITS_COUNT) |
|
||||
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) {
|
||||
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(
|
||||
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) {
|
||||
return IToInt(a_Instruction) | ParseLabel(a_Label);
|
||||
return IToInt(a_Instruction) | ParseLabel(a_Label) & 0x3FFFFFF;
|
||||
}
|
||||
|
||||
// 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) {
|
||||
return IToInt(a_Instruction) | a_R1 << 24 | a_R2 << 21 | ParseLabel(a_Label);
|
||||
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) |
|
||||
ParseLabel(a_Label) & 0xFFFFF;
|
||||
}
|
||||
|
||||
// 5 bits instruction | 1 bit immédiat (non utilisé)
|
||||
std::uint32_t Assembleur::ParseJump(Instruction 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) {
|
||||
return IToInt(a_Instruction) | a_R1 << 24 | a_R2 << 21 | a_R3 << 18;
|
||||
// 5 bits instruction | 1 bit immédiat (non utilisé) | 3 bits R1 | 3 bits R2
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -122,6 +153,9 @@ std::uint32_t Assembleur::ParseInstruction(const std::string& a_Str, std::uint32
|
||||
std::string 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);
|
||||
if (it == INSTRUCTION_KEYS.end()) {
|
||||
throw std::invalid_argument("[Line " + std::to_string(a_RealLine) + "] " + "Instruction \"" + ins + "\" not found !");
|
||||
@@ -142,9 +176,9 @@ std::uint32_t Assembleur::ParseInstruction(const std::string& a_Str, std::uint32
|
||||
}
|
||||
|
||||
case Memoire: {
|
||||
std::string R1, R2, R3;
|
||||
ss >> R1 >> R2 >> R3;
|
||||
return ParseIO(instruction, ParseRegistry(R1), ParseRegistry(R2), ParseRegistry(R3));
|
||||
std::string R1, R2;
|
||||
ss >> R1 >> R2;
|
||||
return ParseIO(instruction, ParseRegistry(R1), ParseRegistry(R2));
|
||||
}
|
||||
|
||||
case SautControle: {
|
||||
@@ -171,8 +205,8 @@ std::uint32_t Assembleur::ParseInstruction(const std::string& a_Str, std::uint32
|
||||
}
|
||||
}
|
||||
|
||||
} catch (std::invalid_argument& e) {
|
||||
throw std::invalid_argument("[Line " + std::to_string(a_RealLine) + "] " + e.what());
|
||||
} catch (std::exception& e) {
|
||||
throw std::invalid_argument(" [Line " + std::to_string(a_RealLine) + "] " + e.what() + "\n" + a_Str);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -39,5 +39,5 @@ class Assembleur {
|
||||
|
||||
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);
|
||||
};
|
||||
@@ -99,12 +99,8 @@ void OutputFileLogisim(BinaryData& a_Data, const std::string& fileName, const st
|
||||
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::bitset<8>(number >> 24) << " " << std::bitset<8>(number >> 16) << " " << std::bitset<8>(number >> 8) << " "
|
||||
<< std::bitset<8>(number) << " ";
|
||||
} else {
|
||||
file << std::bitset<8>(number >> 24) << " " << std::bitset<8>(number >> 16) << " " << std::bitset<8>(number >> 8) << " "
|
||||
<< std::bitset<8>(number) << "\n";
|
||||
}
|
||||
cursor += 4;
|
||||
file << std::setfill('0') << std::setw(8) << std::hex << number << std::dec << (((cursor + 1) % 8 == 0) ? "\n" : " ");
|
||||
cursor++;
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include "IO.h"
|
||||
|
||||
#define ASSEMBLEUR_VERSION "1.2"
|
||||
#define ASSEMBLEUR_VERSION "1.9"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
|
||||
34
test.asm
34
test.asm
@@ -1,21 +1,21 @@
|
||||
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
|
||||
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
|
||||
STR R1 R2
|
||||
LD R1 R2
|
||||
sauts:
|
||||
jmp controle
|
||||
jequ R1 R2 io
|
||||
jneq R1 R2 sauts
|
||||
jsup R1 R2 operations
|
||||
jinf R1 R2 controle
|
||||
JMP controle
|
||||
JEQU R1 R2 io
|
||||
JNEQ R1 R2 sauts
|
||||
JSUP R1 R2 operations
|
||||
JINF R1 R2 controle
|
||||
controle:
|
||||
call io
|
||||
ret
|
||||
CALL io
|
||||
RET
|
||||
|
||||
Reference in New Issue
Block a user