19 Commits
v1.6 ... master

Author SHA1 Message Date
1412250b36 tests 2024-12-22 18:18:41 +01:00
f737d1e695 examples 2024-12-22 14:08:57 +01:00
02b2ed46c7 add fibo example 2024-12-22 11:47:16 +01:00
e4aac05b9a update README 2024-12-21 11:00:57 +01:00
ddb9853ac4 add facto example 2024-12-21 10:55:33 +01:00
e45028b65d update version to v1.9 2024-12-21 10:46:35 +01:00
e28ad4de5a better error catching 2024-12-17 21:34:19 +01:00
890f884cce update README 2024-12-14 18:03:54 +01:00
9dc7f61ed7 ret vaut 7 faut croire 2024-12-14 17:13:48 +01:00
f881b9b4e7 update version to v1.8 2024-12-13 18:09:06 +01:00
00b0368f03 update test 2024-12-13 18:08:26 +01:00
6eec4d8d3d update README 2024-12-13 18:08:19 +01:00
7ab4540201 Omg je suis aveugle 2024-12-13 16:40:39 +00:00
7615546f9e update README 2024-12-13 16:48:31 +01:00
9f77c1ec11 update to v1.7 2024-12-13 16:41:44 +01:00
0ae55ef466 fix label address 2024-12-13 16:38:56 +01:00
ada82368d0 fix bit padding 2024-12-13 16:35:05 +01:00
9f1d80cd9e align hexa output 2024-12-13 16:04:29 +01:00
ade88faa0b fix load and store 2024-12-10 18:06:23 +01:00
10 changed files with 287 additions and 37 deletions

View File

@@ -10,7 +10,7 @@ There are 3 format types :
- "int" : 32 bits integers are written. Exemple : `10878976` - "int" : 32 bits integers are written. Exemple : `10878976`
- "binint" : bits are written. Exemple : `00000000 10100110 00000000 00000000` - "binint" : bits are written. Exemple : `00000000 10100110 00000000 00000000`
- "bin" : the file is written in pure binary - "bin" : the file is written in pure binary
- "logisim" (default) : the file is written in binary for use in LogiSim - "logisim" (default) : the file is written in hexa for use in LogiSim
## Exemple ## Exemple
@@ -25,8 +25,8 @@ operations:
SR R1 R2 R3 SR R1 R2 R3
MUL R1 R2 R3 MUL R1 R2 R3
io: io:
STR R1 R2 R3 STR R1 R2
LD R1 R2 R3 LD R1 R2
sauts: sauts:
JMP controle JMP controle
JEQU R1 R2 io JEQU R1 R2 io
@@ -42,24 +42,26 @@ Produces
``` ```
00000100 10100000 00000000 00000001 00000100 10100000 00000000 00000001
00010000 10100110 00000000 00000000 00001000 10100110 00000000 00000000
00100100 10100000 00000000 00100001 00010100 10100000 00000000 00100001
01000000 10100110 00000000 00000000 00100000 10100110 00000000 00000000
00011000 10100110 00000000 00000000
00101000 10100110 00000000 00000000
00110000 10100110 00000000 00000000 00110000 10100110 00000000 00000000
01010000 10100110 00000000 00000000 00111000 10100110 00000000 00000000
01100000 10100110 00000000 00000000 01000000 10100000 00000000 00000000
01110000 10100110 00000000 00000000 01001000 10100000 00000000 00000000
01000000 10100110 00000000 00000000 11000000 00000000 00000000 00001111
01010000 10100110 00000000 00000000 11001000 10100000 00000000 00001000
11000000 00000000 00000000 00010000 11010000 10100000 00000000 00001010
11010000 10100000 00000000 00001001 11011000 10100000 00000000 00000000
11100000 10100000 00000000 00001011 11100000 10100000 00000000 00001111
11110000 10100000 00000000 00000001 11101000 00000000 00000000 00001000
11000000 10100000 00000000 00010000 11111000 00000000 00000000 00000000
11010000 00000000 00000000 00001001
11100000 00000000 00000000 00000000
``` ```
Other examples are located in the `examples` folder
## Releases ## Releases
Pre-compiled binaries are available in the [Release](https://git.ale-pri.com/Persson-dev/Assembleur/releases) section. Pre-compiled binaries are available in the [Release](https://git.ale-pri.com/Persson-dev/Assembleur/releases) section.
@@ -93,3 +95,7 @@ You can also add the binary to your path using
```bash ```bash
xmake install xmake install
``` ```
or copy the binary in the `bin` folder after
```
xmake install -o .
```

30
examples/facto.asm Normal file
View File

@@ -0,0 +1,30 @@
XOR R0 R0 R0
ADD R0 R0 #5 # n = 5
XOR R6 R6 R6
XOR R7 R7 R7
ADD R7 R7 #35 # SP = 40
STR R0 R7 # on empile n
CALL facto # on appelle facto dessus
JMP fin # on saute à la fin du programme
facto:
LD R1 R7 # on dépile n
JEQU R1 R6 ff # si n = 0, on retourne 1
SUB R2 R1 #1 # sinon, R2 prend n -1
ADD R7 R7 #1 # on incrémente le SP
STR R2 R7 # on empile n - 1
CALL facto # on appelle facto
LD R3 R7 # on dépile le résultat
SUB R7 R7 #1 # on décrémente le SP
LD R4 R7 # on dépile n
MUL R1 R3 R4 # on multiplie n avec facto(n-1)
STR R1 R7 # on empile facto(n)
RET # on retourne
ff:
XOR R3 R3 R3
ADD R3 R3 #1 # R3 = 1
STR R3 R7 # on empile facto(0) = 1
RET # on retourne
fin:
LD R1 R7 # on met le resultat final dans R1
stop:
JMP stop # boucle infinie de fin

37
examples/fibo.asm Normal file
View File

@@ -0,0 +1,37 @@
XOR R0 R0 R0
ADD R0 R0 #7 # n = 7
XOR R1 R1 R1
ADD R1 R1 #1 # constante à 1
XOR R7 R7 R7
ADD R7 R7 #40 # SP = 40
STR R0 R7 # on empile n
CALL fibo # on appelle fibo dessus
JMP fin # on saute à la fin du programme
fibo:
LD R2 R7 # on dépile n
JSUP R2 R1 suite # si n <= 1, on retourne n (donc on ne fait rien)
RET
suite:
SUB R2 R2 #1
STR R2 R7 # on empile n - 1
ADD R7 R7 #1
SUB R2 R2 #1
STR R2 R7 # on empile n - 2
CALL fibo
LD R2 R7 # on dépile fibo(n-2)
SUB R7 R7 #1
LD R3 R7 # on dépile n - 1
STR R2 R7 # on empile fibo(n-2)
ADD R7 R7 #1
STR R3 R7 # on empile n - 1
CALL fibo
LD R2 R7 # on dépile fibo(n-1)
SUB R7 R7 #1
LD R3 R7 # on dépile fibo(n-2)
ADD R2 R2 R3
STR R2 R7 # on empile fibo(n)
RET
fin:
LD R1 R7
stop:
JMP stop

View File

@@ -28,7 +28,7 @@ enum TypeSautControle {
Jsup, Jsup,
Jinf, Jinf,
Call, Call,
Ret, Ret = 7,
}; };
static std::map<std::string, Instruction> INSTRUCTION_KEYS = { static std::map<std::string, Instruction> INSTRUCTION_KEYS = {
@@ -51,6 +51,17 @@ 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 = 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) { std::uint32_t Assembleur::ParseLabel(const std::string& a_Label) {
auto it = m_Labels.find(a_Label); auto it = m_Labels.find(a_Label);
@@ -58,41 +69,60 @@ std::uint32_t Assembleur::ParseLabel(const std::string& a_Label) {
throw std::invalid_argument("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) {
return IToInt(a_Instruction) | ParseLabel(a_Label) & 0x3FFFFFF; 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) { 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 << 23 | a_R2 << 20 | ParseLabel(a_Label) & 0xFFFFF; 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) { 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 << 23 | a_R2 << 20 | a_R3 << 17; 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);
} }
@@ -146,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: {
@@ -175,8 +205,8 @@ std::uint32_t Assembleur::ParseInstruction(const std::string& a_Str, std::uint32
} }
} }
} catch (std::invalid_argument& e) { } catch (std::exception& e) {
throw std::invalid_argument("[Line " + std::to_string(a_RealLine) + "] " + e.what()); throw std::invalid_argument(" [Line " + std::to_string(a_RealLine) + "] " + e.what() + "\n" + a_Str);
} }
return 0; return 0;

View File

@@ -39,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);
}; };

View File

@@ -100,7 +100,7 @@ void OutputFileLogisim(BinaryData& a_Data, const std::string& fileName, const st
if (cursor % 8 == 0) { if (cursor % 8 == 0) {
file << std::setfill('0') << std::setw(4) << std::hex << cursor << std::dec << ": "; file << std::setfill('0') << std::setw(4) << std::hex << cursor << std::dec << ": ";
} }
file << std::hex << number << std::dec << (((cursor + 1) % 8 == 0) ? "\n" : " "); file << std::setfill('0') << std::setw(8) << std::hex << number << std::dec << (((cursor + 1) % 8 == 0) ? "\n" : " ");
cursor++; cursor++;
} }
} }

View File

@@ -3,7 +3,7 @@
#include "IO.h" #include "IO.h"
#define ASSEMBLEUR_VERSION "1.6" #define ASSEMBLEUR_VERSION "1.9"
int main(int argc, char** argv) { int main(int argc, char** argv) {

View File

@@ -8,8 +8,8 @@ operations:
SR R1 R2 R3 SR R1 R2 R3
MUL R1 R2 R3 MUL R1 R2 R3
io: io:
STR R1 R2 R3 STR R1 R2
LD R1 R2 R3 LD R1 R2
sauts: sauts:
JMP controle JMP controle
JEQU R1 R2 io JEQU R1 R2 io

68
test/testUAL.asm Normal file
View File

@@ -0,0 +1,68 @@
XOR R0 R0 R0
XOR R1 R1 R1
XOR R2 R2 R2
XOR R3 R3 R3
XOR R4 R4 R4
XOR R5 R5 R5
XOR R6 R6 R6
XOR R7 R7 R7
ADD:
ADD R0 R0 #1
ADD R1 R1 R0
XOR R0 R0 R0
XOR R1 R1 R1
SUB:
ADD R0 R0 #1
ADD R1 R1 #1
ADD R2 R2 #2
SUB R0 R0 #1
SUB R3 R2 R1
XOR R0 R0 R0
XOR R1 R1 R1
XOR R2 R2 R2
XOR R3 R3 R3
AND:
ADD R0 R0 #5
ADD R1 R1 #3
AND R2 R0 #1
AND R3 R1 R0
XOR R0 R0 R0
XOR R1 R1 R1
XOR R2 R2 R2
XOR R3 R3 R3
OR:
ADD R0 R0 #5
ADD R1 R1 #2
OR R2 R0 #1
OR R3 R1 R0
XOR R0 R0 R0
XOR R1 R1 R1
XOR R2 R2 R2
XOR R3 R3 R3
SL:
ADD R0 R0 #5
ADD R1 R1 #2
SL R2 R0 #1
SL R3 R0 R1
XOR R0 R0 R0
XOR R1 R1 R1
XOR R2 R2 R2
XOR R3 R3 R3
SR:
ADD R0 R0 #20
ADD R1 R1 #2
SR R2 R0 #1
SR R3 R0 R1
XOR R0 R0 R0
XOR R1 R1 R1
XOR R2 R2 R2
XOR R3 R3 R3
MUL:
ADD R0 R0 #20
ADD R1 R1 #2
MUL R2 R0 #2
MUL R3 R0 R1
XOR R0 R0 R0
XOR R1 R1 R1
XOR R2 R2 R2
XOR R3 R3 R3

79
test/testsAutres.asm Normal file
View File

@@ -0,0 +1,79 @@
debut:
XOR R0 R0 R0
XOR R1 R1 R1
XOR R2 R2 R2
XOR R3 R3 R3
XOR R4 R4 R4
XOR R5 R5 R5
XOR R6 R6 R6
XOR R7 R7 R7
jump:
JMP jump_equ
ADD R7 R7 #1
jump_equ:
ADD R0 R0 #1
ADD R1 R1 #1
JEQU R0 R1 jump_equ2
ADD R7 R7 #2
jump_equ2:
ADD R0 R0 #1
ADD R6 R6 #1
JEQU R0 R1 jump_neq
SUB R6 R6 #1
jump_neq:
JNEQ R0 R1 jump_neq2
ADD R7 R7 #4
jump_neq2:
ADD R2 R2 #1
ADD R6 R6 #2
JNEQ R1 R2 jump_sup
SUB R6 R6 #2
jump_sup:
JSUP R1 R0 jump_sup2
ADD R7 R7 #8
jump_sup2:
ADD R6 R6 #4
JSUP R0 R1 jump_inf
SUB R6 R6 #4
jump_inf:
JINF R0 R1 jump_inf2
ADD R7 R7 #16
jump_inf2:
ADD R6 R6 #8
JSUP R1 R0 store
SUB R6 R6 #8
store:
XOR R0 R0 R0
XOR R1 R1 R1
XOR R2 R2 R2
ADD R0 R0 #1
ADD R1 R1 #24
STR R0 R1
load:
LD R0 R2
JEQU R1 R2 fin_load
ADD R7 R7 #32
fin_load:
XOR R0 R0 R0
XOR R1 R1 R1
XOR R2 R2 R2
XOR R3 R3 R3
XOR R4 R4 R4
XOR R5 R5 R5
foo:
ADD R0 R0 #1
JEQU R0 R1 fin
CALL bar
RET
bar:
ADD R1 R1 #2
CALL foo
RET
fin:
ADD R0 R0 #42
XOR R0 R0 R0
XOR R1 R1 R1
XOR R2 R2 R2
XOR R3 R3 R3
XOR R4 R4 R4
XOR R5 R5 R5