diff --git a/assembler/assembler.py b/assembler/assembler.py index 19eef5b..09892ba 100644 --- a/assembler/assembler.py +++ b/assembler/assembler.py @@ -3,6 +3,7 @@ import re import struct import IPython import copy +import argparse class AssemblerException(Exception): @@ -125,7 +126,8 @@ class VMAssembler: for f in self.functions: print("FUNCTION {}".format(f.name)) for idx, ins in enumerate(f.instructions): - print("{}:\t{}".format(hex(f.offset+ f.offset_of_instruction(idx)), ins)) + print("{}:\t{}".format( + hex(f.offset + f.offset_of_instruction(idx)), ins)) def imm2reg(self, instruction): """ @@ -479,6 +481,7 @@ ops_sizes = {"reg2reg": 2, "immonly": 3, "jump": 3, "single": 1} + ops = [VMComponent(le[0], i, le[1]) for i, le in enumerate(op_names)] regs = [VMComponent(s.casefold(), i) for i, s in enumerate(reg_names)] instruction_re = re.compile( @@ -488,24 +491,34 @@ immediate_re = re.compile("(?:0x)?[0-9a-fA-F]+$") alpha_re = re.compile("^[a-zA-Z]*$") register_re = re.compile("(^[rRsS][0-4]$)|([iIrRsS][pP]$)") label_re = re.compile("^([a-zA-Z]+)\:(?:\ *\#.*)?$") -symcall_re = re.compile("^(?:[jJ][pPmM][pPaAbBeEnN][iIrR]|(?:[cC][aA][lL]{2}))\ +([\w]+)(?:\ *\#.*)?$") +symcall_re = re.compile( + "^(?:[jJ][pPmM][pPaAbBeEnN][iIrR]|(?:[cC][aA][lL]{2}))\ +([\w]+)(?:\ *\#.*)?$") commentline_re = re.compile("^\ *\#.*") + def main(): - if len(sys.argv) < 4: - print("Usage: {} opcodes_key file_to_assemble output".format( - sys.argv[0])) - return + parser = argparse.ArgumentParser(description='Optional app description') + parser.add_argument( + 'opcodes_key', help='The key used to encrypt the opcodes') + parser.add_argument('asmfile', help='The Pasticciotto assembly file') + parser.add_argument('outfile', help='The output file') + parser.add_argument('--debug', action='store_true', + help='Enables the DEBG opcode') + args = parser.parse_args() - with open(sys.argv[2], 'r') as f: + if args.debug: + global ops + ops.append(VMComponent("DEBG", len(ops), "single")) + with open(args.asmfile, 'r') as f: filedata = f.readlines() - filedata = [x.strip() for x in filedata if x.strip() and not commentline_re.match(x)] + filedata = [x.strip() for x in filedata if x.strip() + and not commentline_re.match(x)] - vma = VMAssembler(sys.argv[1], filedata) + vma = VMAssembler(args.opcodes_key, filedata) print(vma.functions) vma.parse() - with open(sys.argv[3], 'wb') as f: + with open(args.outfile, 'wb') as f: f.write(vma.assembled_code) if __name__ == '__main__': diff --git a/test/tea-encrypt.c b/test/tea-encrypt.c index 7af8cfa..df01e0a 100644 --- a/test/tea-encrypt.c +++ b/test/tea-encrypt.c @@ -34,7 +34,8 @@ int main(int argc, char *argv[]) { buflen = strlen(argv[1]); buf = (uint8_t *)malloc(buflen); memcpy(buf, argv[1], buflen); - for (i = 0; i < buflen; i+=2) { + for (i = 0; i < buflen; i++) { + printf("----\n"); encrypt((uint16_t *)&buf[i]); } printf("Result:\n"); diff --git a/vm/emulator.cpp b/vm/emulator.cpp index ad91509..795e4a5 100644 --- a/vm/emulator.cpp +++ b/vm/emulator.cpp @@ -26,6 +26,5 @@ int main(int argc, char *argv[]) { bytecode_if.read((char *)bytecode, bytecode_size); VM vm((uint8_t *)argv[1], bytecode, bytecode_size); vm.run(); - vm.status(); return 0; } \ No newline at end of file diff --git a/vm/opcodes.h b/vm/opcodes.h index bb94a2d..b6ce7cd 100644 --- a/vm/opcodes.h +++ b/vm/opcodes.h @@ -1,6 +1,7 @@ /* MEMORY LOCATIONS AND IMMEDIATES ARE 16 BITS LONG */ +#ifndef DBG enum OPS_STARTING_VALUES { MOVI, MOVR, @@ -52,7 +53,60 @@ enum OPS_STARTING_VALUES { GRMN, NUM_OPS }; - +#else +enum OPS_STARTING_VALUES { + MOVI, + MOVR, + LODI, + LODR, + STRI, + STRR, + ADDI, + ADDR, + SUBI, + SUBR, + ANDB, + ANDW, + ANDR, + YORB, + YORW, + YORR, + XORB, + XORW, + XORR, + NOTR, + MULI, + MULR, + DIVI, + DIVR, + SHLI, + SHLR, + SHRI, + SHRR, + PUSH, + POOP, + CMPB, + CMPW, + CMPR, + JMPI, + JMPR, + JPAI, + JPAR, + JPBI, + JPBR, + JPEI, + JPER, + JPNI, + JPNR, + CALL, + RETN, + SHIT, + NOPE, + GRMN, + DEBG, + NUM_OPS +}; +#endif uint8_t OPS[NUM_OPS]; /* @@ -115,4 +169,5 @@ INSTRUCTION SIZES #define RETN_SIZE SINGLE #define SHIT_SIZE SINGLE #define NOPE_SIZE SINGLE -#define GRMN_SIZE SINGLE \ No newline at end of file +#define GRMN_SIZE SINGLE +#define DEBG_SIZE SINGLE \ No newline at end of file diff --git a/vm/vm.cpp b/vm/vm.cpp index 1ff916e..ef0b69e 100644 --- a/vm/vm.cpp +++ b/vm/vm.cpp @@ -3,6 +3,7 @@ #include "opcodes.h" #include "vmas.h" #include +#include void VM::encryptOpcodes(uint8_t *key) { uint8_t arr[256]; @@ -946,7 +947,14 @@ void VM::run(void) { finished = true; } else if (opcode == OPS[NOPE]) { regs[IP] += NOPE_SIZE; - } else { + } +#ifdef DBG + else if (opcode == OPS[DEBG]) { + status(); + regs[IP] += DEBG_SIZE; + } +#endif + else { DBG_ERROR(("WAT: 0x%x\n", as.code[regs[IP]])); finished = true; }