Skip to content

Instantly share code, notes, and snippets.

@rafaelcorsi
Last active November 13, 2020 13:06
Show Gist options
  • Save rafaelcorsi/24ef51cdbcaec6eb55ec43bcd6195e25 to your computer and use it in GitHub Desktop.
Save rafaelcorsi/24ef51cdbcaec6eb55ec43bcd6195e25 to your computer and use it in GitHub Desktop.
/**
* Retorna o código binário do mnemônico para realizar uma operação de cálculo.
* @param mnemnonic vetor de mnemônicos "instrução" a ser analisada.
* @return Opcode (String de 8 bits) com código em linguagem de máquina para a instrução.
*/
public static String comp(String[] mnemnonic) throws InvalidCompException {
int movInt = 0;
if ( mnemnonic.length == 0 || mnemnonic[0].equals("")) {
Error.error("Instrucao invalida");
throw new InvalidCompException();
}
String comp = "";
if (mnemnonic[0].startsWith("mov")) {
comp = mnemnonic[1];
movInt = 1;
} else if (mnemnonic[0].startsWith("add")) {
comp = mnemnonic[1]+"+"+mnemnonic[2];
} else if (mnemnonic[0].startsWith("sub")) {
comp = mnemnonic[1]+"-"+mnemnonic[2];
} else if (mnemnonic[0].startsWith("rsub")) {
comp = mnemnonic[2]+"-"+mnemnonic[1];
} else if (mnemnonic[0].startsWith("inc")) {
comp = mnemnonic[1]+"+1";
} else if (mnemnonic[0].startsWith("dec")) {
comp = mnemnonic[1]+"-1";
} else if (mnemnonic[0].startsWith("not")) {
comp = "!"+mnemnonic[1];
} else if (mnemnonic[0].startsWith("neg")) {
comp = "-"+mnemnonic[1];
} else if (mnemnonic[0].startsWith("and")) {
comp = mnemnonic[1]+"&"+mnemnonic[2];
} else if (mnemnonic[0].startsWith("or")) {
comp = mnemnonic[1]+"|"+mnemnonic[2];
} else if (mnemnonic[0].startsWith("nop")) {
comp = "$0";
} else if ( mnemnonic[0].startsWith("jg") ||
mnemnonic[0].startsWith("je") ||
mnemnonic[0].startsWith("jge") ||
mnemnonic[0].startsWith("jl") ||
mnemnonic[0].startsWith("jne") ||
mnemnonic[0].startsWith("jle") ||
mnemnonic[0].startsWith("jmp") )
{
if (mnemnonic.length == 1)
comp = "%X";
else
comp = mnemnonic[1];
}
else
{
Error.error("Instrucao invalida: "+String.join(" ",mnemnonic));
throw new InvalidCompException();
}
String r2 = "0"; // Operacao entre S e D
String r1 = "0"; // A = 0; (A) = 1
String r0 = "0"; // A = 0; D = 1
boolean Xs = false;
boolean Xd = false;
for (int i = 0; i < mnemnonic.length - movInt; i++) {
if (mnemnonic[i].contains("(%A)"))
r0 = "1";
}
comp = comp.replace("(%A)", "%Y");
comp = comp.replace("%A", "%Y");
comp = comp.replace("%D", "%X");
comp = comp.replace("$", "");
String c = "000000";
switch (comp) {
case "0": c = "101010"; break;
case "1": c = "111111"; break;
case "-1": c = "111010"; break;
case "%X": c = "001100"; break;
case "%Y": c = "110000"; break;
case "!%X": c = "001101"; break;
case "!%Y": c = "110001"; break;
case "-%X": c = "001111"; break;
case "-%Y": c = "110011"; break;
case "%X+1": case "1+%X": c = "011111"; break;
case "%Y+1": case "1+%Y": c = "110111"; break;
case "%X-1": c = "001110"; break;
case "%Y-1": c = "110010"; break;
case "%X+%Y": c = "000010"; break;
case "%Y+%X": c = "000010"; break;
case "%X-%Y": c = "010011"; break;
case "%Y-%X": c = "000111"; break;
case "%X&%Y": c = "000000"; break;
case "%Y&%X": c = "000000"; break;
case "%X|%Y": c = "010101"; break;
case "%Y|%X": c = "010101"; break;
default: throw new InvalidCompException();
}
return r2 + r1 + r0 + c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment