-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
164 lines (130 loc) · 4.24 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const instructions = {
"MOV": (mem, arg1, arg2) => mem[arg1] = mem[arg2] || parseFloat(arg2),
"ADD": (mem, arg1, arg2) => mem[arg1] += mem[arg2] || parseFloat(arg2),
"SUB": (mem, arg1, arg2) => mem[arg1] -= mem[arg2] || parseFloat(arg2),
"MUL": (mem, arg1, arg2) => mem[arg1] *= mem[arg2] || parseFloat(arg2),
"DIV": (mem, arg1, arg2) => mem[arg1] /= mem[arg2] || parseFloat(arg2),
"DEL": (mem, arg1) => delete mem[arg1],
"CMP": (mem, arg1, arg2) => {
let val1 = mem[arg1];
let val2 = mem[arg2];
if(val1 == undefined){
val1 = parseFloat(arg1)
}
if(val2 == undefined){
val2 = parseFloat(arg2)
}
mem["ZF"] = val1 === val2;
console.log(`Compare: ${val1}(${typeof val1}) == ${val2}(${typeof val2}) (${mem["ZF"]})`);
},
//Jump Instructions
"JMP": (lines, mem, arg1) => {
const newPC = lines.findIndex(line => line.startsWith(arg1 + ':'));
if(newPC){
mem["PC"] = newPC;
}else{
mem["PC"]++;
}
},
"JE": (lines, mem, arg1) => {
if(mem["ZF"]){
const newPC = lines.findIndex(line => line.startsWith(arg1 + ':'));
mem["PC"] = newPC;
}else{
mem["PC"]++;
}
},
"JNE": (lines, mem, arg1) => {
if(!mem["ZF"]){
const newPC = lines.findIndex(line => line.startsWith(arg1 + ':'));
mem["PC"] = newPC;
}else{
mem["PC"]++;
}
},
};
function interpret(code) {
document.querySelector("#error").textContent = "";
// Divide o código em linhas e remove quaisquer espaços em branco
const lines = code.split("\n").map(line => line.trim());
// Inicializa a memória
const memory = {
"PC": 0,
"ZF": false,
};
let i = 0; //To prevent loops
while (memory["PC"] < lines.length) {
i++;
if(i > 9999){ //Previne Loops Infinitos
const error = `Erro! Loop Infinito`;
document.querySelector("#error").textContent = error;
return memory;
}
const line = lines[memory["PC"]];
// Se a linha esta vazia ou a instrução é um rótulo, pula para a próxima linha
if(line === "" || line.endsWith(':')){
memory["PC"]++;
continue;
}
// Divide a linha em instrução e argumentos
const [instruction, arg1, arg2] = line.split(" ");
// Verifica se a instrução é suportada
if (instruction in instructions) {
// Executa a instrução
if(instruction.startsWith('J')){
instructions[instruction](lines, memory, arg1, arg2);
} else {
console.log(`${instruction}: ${arg1}(${memory[arg1] || parseFloat(arg1)}), ${arg2}(${memory[arg2] || parseFloat(arg2)})`);
instructions[instruction](memory, arg1, arg2);
//console.log(memory)
memory["PC"]++; //Atualiza o contador de programa (PC)
}
}
else {
const error = `Erro! Instrução não suportada: ${instruction}`;
document.querySelector("#error").textContent = error;
return memory;
}
}
return memory;
}
document.querySelector("#input").textContent =
`START:
MOV X 2
MOV Y 3
ADD X Y
SUB X 1
CMP X 4
JE LABEL1
MOV RESULT 0
JMP END
LABEL1:
MOV RESULT 1
END:
DEL X
DEL Y`;
function run(){
const memory = interpret(document.querySelector("#input").value);
let output = "";
let counter = 0;
for(const i in memory) {
counter++;
if(counter == 1){
output += `Registradores:\n`;
output += `\t$PC: ${memory["PC"]} \n`;
}
else if(counter == 2){
output += `\t$ZF: ${memory["ZF"]} \n\n`;
output += `Variáveis:\n`;
}
else{
if(i == "PC" || i == "ZF")
continue;
output += `\t${i}: ${memory[i]} \n`;
}
}
document.querySelector("#output").textContent = output;
}
document.querySelector("#execute").addEventListener("click", e => {
run();
});