Compare commits

..

No commits in common. "fd45b5c54fe4cc9f0149fd6a6d020143309414fb" and "a6475f35007fbf2f5d65c53c54613267debe49b1" have entirely different histories.

5 changed files with 18 additions and 95 deletions

View File

@ -3,7 +3,6 @@ import re
import struct import struct
import IPython import IPython
import copy import copy
import argparse
class AssemblerException(Exception): class AssemblerException(Exception):
@ -126,8 +125,7 @@ class VMAssembler:
for f in self.functions: for f in self.functions:
print("FUNCTION {}".format(f.name)) print("FUNCTION {}".format(f.name))
for idx, ins in enumerate(f.instructions): for idx, ins in enumerate(f.instructions):
print("{}:\t{}".format( print("{}:\t{}".format(hex(f.offset+ f.offset_of_instruction(idx)), ins))
hex(f.offset + f.offset_of_instruction(idx)), ins))
def imm2reg(self, instruction): def imm2reg(self, instruction):
""" """
@ -481,7 +479,6 @@ ops_sizes = {"reg2reg": 2,
"immonly": 3, "immonly": 3,
"jump": 3, "jump": 3,
"single": 1} "single": 1}
ops = [VMComponent(le[0], i, le[1]) for i, le in enumerate(op_names)] 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)] regs = [VMComponent(s.casefold(), i) for i, s in enumerate(reg_names)]
instruction_re = re.compile( instruction_re = re.compile(
@ -491,34 +488,24 @@ immediate_re = re.compile("(?:0x)?[0-9a-fA-F]+$")
alpha_re = re.compile("^[a-zA-Z]*$") alpha_re = re.compile("^[a-zA-Z]*$")
register_re = re.compile("(^[rRsS][0-4]$)|([iIrRsS][pP]$)") register_re = re.compile("(^[rRsS][0-4]$)|([iIrRsS][pP]$)")
label_re = re.compile("^([a-zA-Z]+)\:(?:\ *\#.*)?$") label_re = re.compile("^([a-zA-Z]+)\:(?:\ *\#.*)?$")
symcall_re = re.compile( symcall_re = re.compile("^(?:[jJ][pPmM][pPaAbBeEnN][iIrR]|(?:[cC][aA][lL]{2}))\ +([\w]+)(?:\ *\#.*)?$")
"^(?:[jJ][pPmM][pPaAbBeEnN][iIrR]|(?:[cC][aA][lL]{2}))\ +([\w]+)(?:\ *\#.*)?$")
commentline_re = re.compile("^\ *\#.*") commentline_re = re.compile("^\ *\#.*")
def main(): def main():
parser = argparse.ArgumentParser(description='Optional app description') if len(sys.argv) < 4:
parser.add_argument( print("Usage: {} opcodes_key file_to_assemble output".format(
'opcodes_key', help='The key used to encrypt the opcodes') sys.argv[0]))
parser.add_argument('asmfile', help='The Pasticciotto assembly file') return
parser.add_argument('outfile', help='The output file')
parser.add_argument('--debug', action='store_true',
help='Enables the DEBG opcode')
args = parser.parse_args()
if args.debug: with open(sys.argv[2], 'r') as f:
global ops
ops.append(VMComponent("DEBG", len(ops), "single"))
with open(args.asmfile, 'r') as f:
filedata = f.readlines() filedata = f.readlines()
filedata = [x.strip() for x in filedata if x.strip() filedata = [x.strip() for x in filedata if x.strip() and not commentline_re.match(x)]
and not commentline_re.match(x)]
vma = VMAssembler(args.opcodes_key, filedata) vma = VMAssembler(sys.argv[1], filedata)
print(vma.functions) print(vma.functions)
vma.parse() vma.parse()
with open(args.outfile, 'wb') as f: with open(sys.argv[3], 'wb') as f:
f.write(vma.assembled_code) f.write(vma.assembled_code)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -6,7 +6,7 @@
void encrypt(uint16_t *v) { void encrypt(uint16_t *v) {
uint16_t v0 = v[0], v1 = v[1], sum = 0, i; /* set up */ uint16_t v0 = v[0], v1 = v[1], sum = 0, i; /* set up */
uint16_t delta = 0x626f; uint16_t delta= 0x626f;
uint16_t k0 = 0x7065; // "pe" uint16_t k0 = 0x7065; // "pe"
uint16_t k1 = 0x7065; // "pe" uint16_t k1 = 0x7065; // "pe"
uint16_t k2 = 0x7275; // "ru" uint16_t k2 = 0x7275; // "ru"
@ -16,7 +16,7 @@ void encrypt(uint16_t *v) {
sum += delta; sum += delta;
v0 += ((v1 << 4) + k0) ^ (v1 + sum) ^ ((v1 >> 5) + k1); v0 += ((v1 << 4) + k0) ^ (v1 + sum) ^ ((v1 >> 5) + k1);
v1 += ((v0 << 4) + k2) ^ (v0 + sum) ^ ((v0 >> 5) + k3); v1 += ((v0 << 4) + k2) ^ (v0 + sum) ^ ((v0 >> 5) + k3);
//printf("Intermediate v0: 0x%x | v1: 0x%x\n", v0, v1); printf("Intermediate v0: 0x%x | v1: 0x%x\n", v0, v1);
} }
printf("SUM: 0x%x\n", sum); printf("SUM: 0x%x\n", sum);
printf("v0: 0x%x, v1: 0x%x\n", v0, v1); printf("v0: 0x%x, v1: 0x%x\n", v0, v1);
@ -27,6 +27,7 @@ void encrypt(uint16_t *v) {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
uint8_t *buf; uint8_t *buf;
uint32_t buflen, i; uint32_t buflen, i;
if (argc != 2) { if (argc != 2) {
printf("Usage: %s text_to_encrypt", argv[0]); printf("Usage: %s text_to_encrypt", argv[0]);
exit(1); exit(1);
@ -34,10 +35,7 @@ int main(int argc, char *argv[]) {
buflen = strlen(argv[1]); buflen = strlen(argv[1]);
buf = (uint8_t *)malloc(buflen); buf = (uint8_t *)malloc(buflen);
memcpy(buf, argv[1], buflen); memcpy(buf, argv[1], buflen);
for (i = 0; i < buflen; i++) { encrypt((uint16_t *)buf);
printf("----\n");
encrypt((uint16_t *)&buf[i]);
}
printf("Result:\n"); printf("Result:\n");
for (i = 0; i < buflen; i++) { for (i = 0; i < buflen; i++) {
printf("%02x", buf[i]); printf("%02x", buf[i]);

View File

@ -26,5 +26,6 @@ int main(int argc, char *argv[]) {
bytecode_if.read((char *)bytecode, bytecode_size); bytecode_if.read((char *)bytecode, bytecode_size);
VM vm((uint8_t *)argv[1], bytecode, bytecode_size); VM vm((uint8_t *)argv[1], bytecode, bytecode_size);
vm.run(); vm.run();
vm.status();
return 0; return 0;
} }

View File

@ -1,7 +1,6 @@
/* /*
MEMORY LOCATIONS AND IMMEDIATES ARE 16 BITS LONG MEMORY LOCATIONS AND IMMEDIATES ARE 16 BITS LONG
*/ */
#ifndef DBG
enum OPS_STARTING_VALUES { enum OPS_STARTING_VALUES {
MOVI, MOVI,
MOVR, MOVR,
@ -53,60 +52,7 @@ enum OPS_STARTING_VALUES {
GRMN, GRMN,
NUM_OPS 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]; uint8_t OPS[NUM_OPS];
/* /*
@ -169,5 +115,4 @@ INSTRUCTION SIZES
#define RETN_SIZE SINGLE #define RETN_SIZE SINGLE
#define SHIT_SIZE SINGLE #define SHIT_SIZE SINGLE
#define NOPE_SIZE SINGLE #define NOPE_SIZE SINGLE
#define GRMN_SIZE SINGLE #define GRMN_SIZE SINGLE
#define DEBG_SIZE SINGLE

View File

@ -3,7 +3,6 @@
#include "opcodes.h" #include "opcodes.h"
#include "vmas.h" #include "vmas.h"
#include <string.h> #include <string.h>
#include <unistd.h>
void VM::encryptOpcodes(uint8_t *key) { void VM::encryptOpcodes(uint8_t *key) {
uint8_t arr[256]; uint8_t arr[256];
@ -947,14 +946,7 @@ void VM::run(void) {
finished = true; finished = true;
} else if (opcode == OPS[NOPE]) { } else if (opcode == OPS[NOPE]) {
regs[IP] += NOPE_SIZE; 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]])); DBG_ERROR(("WAT: 0x%x\n", as.code[regs[IP]]));
finished = true; finished = true;
} }