Compare commits

...

3 Commits

Author SHA1 Message Date
Giulio De Pasquale
6f9e7db4ee regonly size sistemata in assembler, sistemati flag CF / ZF nei CMP e nei JMP 2017-05-25 15:25:45 +02:00
Giulio De Pasquale
19b6c69237 possibile aggiungere commenti, sul serio 2017-05-25 12:13:28 +02:00
Giulio De Pasquale
7b972fc406 possibile aggiungere commenti 2017-05-25 11:58:54 +02:00
3 changed files with 27 additions and 28 deletions

View File

@ -102,7 +102,6 @@ class VMAssembler:
symcall = symcall_re.match(str(i)) symcall = symcall_re.match(str(i))
if symcall: if symcall:
symname = symcall.group(1) symname = symcall.group(1)
# checking if it's a jump to a label # checking if it's a jump to a label
if symname in [y.label for x in self.functions for y in x.instructions]: if symname in [y.label for x in self.functions for y in x.instructions]:
fun = next( fun = next(
@ -126,7 +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(hex(f.offset_of_instruction(idx)), ins)) print("{}:\t{}".format(hex(f.offset+ f.offset_of_instruction(idx)), ins))
def imm2reg(self, instruction): def imm2reg(self, instruction):
""" """
@ -235,7 +234,6 @@ class VMAssembler:
reg_op_re = re.compile(".*[rR]$") reg_op_re = re.compile(".*[rR]$")
symcall = symcall_re.match(str(instruction)) symcall = symcall_re.match(str(instruction))
dst = instruction.args[0] dst = instruction.args[0]
print(instruction)
# define the kind of jump: to immediate or to register # define the kind of jump: to immediate or to register
if imm_op_re.match(instruction.opcode.name): if imm_op_re.match(instruction.opcode.name):
self.immonly(instruction) self.immonly(instruction)
@ -347,7 +345,6 @@ class VMInstruction:
raise InvalidOperation(opcode) raise InvalidOperation(opcode)
self.size = ops_sizes[self.opcode.method] self.size = ops_sizes[self.opcode.method]
args = [x for x in ins.groups()[1:] if x is not None] args = [x for x in ins.groups()[1:] if x is not None]
print("OP: {} | ARGS: {} | SYMCALL: {}".format(self.opcode, args,symcall))
for a in args: for a in args:
if immediate_re.match(a) or symcall: if immediate_re.match(a) or symcall:
# directly append the immediate # directly append the immediate
@ -478,20 +475,20 @@ ops_sizes = {"reg2reg": 2,
"imm2reg": 4, "imm2reg": 4,
"reg2imm": 4, "reg2imm": 4,
"byt2reg": 3, "byt2reg": 3,
"regonly": 3, "regonly": 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(
"^([\w]{4})(?:\ +(?:([\w]+)\ *(?:,[\ ]*([\w]+))*))?$") # 1: opcode 2+: args "^([\w]{4})(?:(?:\ *\#.*)|(?:\ +(?:([\w]+)\ *(?:,[\ ]*([\w]+))?)(?:\ *\#.*)?))?$") # 1: opcode 2+: args
function_re = re.compile("(?:def\ )([a-zA-Z]*)\:") function_re = re.compile("(?:def\ )([a-zA-Z]*)\:(?:\ *\#.*)?$")
immediate_re = re.compile("(?:0x)?[0-9a-fA-F]+$") 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("^(?:[jJ][pPmM][pPaAbBeEnN][iIrR]|(?:[cC][aA][lL]{2}))\ +([\w]*)$") symcall_re = re.compile("^(?:[jJ][pPmM][pPaAbBeEnN][iIrR]|(?:[cC][aA][lL]{2}))\ +([\w]+)(?:\ *\#.*)?$")
def main(): def main():

View File

@ -4,16 +4,18 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
void encrypt(uint16_t *v, uint16_t *k) { 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 */
//uint32_t delta = 0x9e3779b9; /* a key schedule constant */ uint16_t delta= 0x626f;
uint16_t delta= 0x9e37; uint16_t k0 = 0x7065; // "pe"
uint16_t k0 = k[0], k1 = k[1], k2 = k[2], k3 = k[3]; /* cache key */ uint16_t k1 = 0x7065; // "pe"
for (i = 0; i < 64; i++) { /* basic cycle start */ uint16_t k2 = 0x7275; // "ru"
uint16_t k3 = 0x6e73; // "ns"
for (i = 0; i < 128; i++) {
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);
} /* end cycle */ }
v[0] = v0; v[0] = v0;
v[1] = v1; v[1] = v1;
} }
@ -22,14 +24,14 @@ int main(int argc, char *argv[]) {
uint8_t *buf; uint8_t *buf;
uint32_t buflen, i; uint32_t buflen, i;
if (argc != 3) { if (argc != 2) {
printf("Usage: %s text_to_encrypt key", argv[0]); printf("Usage: %s text_to_encrypt", argv[0]);
exit(1); exit(1);
} }
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);
encrypt((uint16_t *)buf, (uint16_t *)argv[2]); encrypt((uint16_t *)buf);
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

@ -577,9 +577,9 @@ bool VM::execCMPB(void) {
flags.ZF = 0; flags.ZF = 0;
} }
if (*((uint8_t *)&regs[reg]) > imm) { if (*((uint8_t *)&regs[reg]) > imm) {
flags.CF = 1;
} else {
flags.CF = 0; flags.CF = 0;
} else {
flags.CF = 1;
} }
return true; return true;
} }
@ -599,9 +599,9 @@ bool VM::execCMPW(void) {
flags.ZF = 0; flags.ZF = 0;
} }
if (regs[reg] > imm) { if (regs[reg] > imm) {
flags.CF = 1;
} else {
flags.CF = 0; flags.CF = 0;
} else {
flags.CF = 1;
} }
return true; return true;
} }
@ -621,9 +621,9 @@ bool VM::execCMPR(void) {
flags.ZF = 0; flags.ZF = 0;
} }
if (regs[r1] > regs[r2]) { if (regs[r1] > regs[r2]) {
flags.CF = 1;
} else {
flags.CF = 0; flags.CF = 0;
} else {
flags.CF = 1;
} }
return true; return true;
} }
@ -657,7 +657,7 @@ bool VM::execJPAI(void) {
imm = *(uint16_t *)&as.code[regs[IP] + 1]; imm = *(uint16_t *)&as.code[regs[IP] + 1];
DBG_INFO(("JPAI 0x%x\n", imm)); DBG_INFO(("JPAI 0x%x\n", imm));
if (flags.CF == 1) { if (flags.CF == 0 && flags.ZF == 0) {
regs[IP] = imm; regs[IP] = imm;
return true; return true;
} }
@ -671,7 +671,7 @@ bool VM::execJPAR(void) {
reg = as.code[regs[IP] + 1]; reg = as.code[regs[IP] + 1];
DBG_INFO(("JPAR %s = 0x%x\n", getRegName(reg), regs[reg])); DBG_INFO(("JPAR %s = 0x%x\n", getRegName(reg), regs[reg]));
if (flags.CF == 1) { if (flags.CF == 0 && flags.ZF == 0) {
regs[IP] = reg; regs[IP] = reg;
return true; return true;
} }
@ -685,7 +685,7 @@ bool VM::execJPBI(void) {
imm = *(uint16_t *)&as.code[regs[IP] + 1]; imm = *(uint16_t *)&as.code[regs[IP] + 1];
DBG_INFO(("JPBI 0x%x\n", imm)); DBG_INFO(("JPBI 0x%x\n", imm));
if (flags.CF == 0) { if (flags.CF == 1) {
regs[IP] = imm; regs[IP] = imm;
return true; return true;
} }
@ -699,7 +699,7 @@ bool VM::execJPBR(void) {
reg = as.code[regs[IP] + 1]; reg = as.code[regs[IP] + 1];
DBG_INFO(("JPBR %s = 0x%x\n", getRegName(reg), regs[reg])); DBG_INFO(("JPBR %s = 0x%x\n", getRegName(reg), regs[reg]));
if (flags.CF == 0) { if (flags.CF == 1) {
regs[IP] = reg; regs[IP] = reg;
return true; return true;
} }