Compare commits
2 Commits
a6475f3500
...
fd45b5c54f
Author | SHA1 | Date | |
---|---|---|---|
|
fd45b5c54f | ||
|
2bc3b17123 |
@ -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__':
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
void encrypt(uint16_t *v) {
|
||||
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 k1 = 0x7065; // "pe"
|
||||
uint16_t k2 = 0x7275; // "ru"
|
||||
@ -16,7 +16,7 @@ void encrypt(uint16_t *v) {
|
||||
sum += delta;
|
||||
v0 += ((v1 << 4) + k0) ^ (v1 + sum) ^ ((v1 >> 5) + k1);
|
||||
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("v0: 0x%x, v1: 0x%x\n", v0, v1);
|
||||
@ -27,7 +27,6 @@ void encrypt(uint16_t *v) {
|
||||
int main(int argc, char *argv[]) {
|
||||
uint8_t *buf;
|
||||
uint32_t buflen, i;
|
||||
|
||||
if (argc != 2) {
|
||||
printf("Usage: %s text_to_encrypt", argv[0]);
|
||||
exit(1);
|
||||
@ -35,7 +34,10 @@ int main(int argc, char *argv[]) {
|
||||
buflen = strlen(argv[1]);
|
||||
buf = (uint8_t *)malloc(buflen);
|
||||
memcpy(buf, argv[1], buflen);
|
||||
encrypt((uint16_t *)buf);
|
||||
for (i = 0; i < buflen; i++) {
|
||||
printf("----\n");
|
||||
encrypt((uint16_t *)&buf[i]);
|
||||
}
|
||||
printf("Result:\n");
|
||||
for (i = 0; i < buflen; i++) {
|
||||
printf("%02x", buf[i]);
|
||||
|
@ -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;
|
||||
}
|
59
vm/opcodes.h
59
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
|
||||
#define GRMN_SIZE SINGLE
|
||||
#define DEBG_SIZE SINGLE
|
10
vm/vm.cpp
10
vm/vm.cpp
@ -3,6 +3,7 @@
|
||||
#include "opcodes.h"
|
||||
#include "vmas.h"
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user