-
-
Save fr0zn/3cbe14b712ca145b1ea32badf01c31ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
r = open('level1','rb').read() | |
b = bytearray(r) | |
mem = bytearray([0] * 0x10000) | |
mem[0:len(b)] = b[0:len(b)] | |
pc = 0 | |
stack = [] | |
# def split_n(line,n=4): | |
# return [line[i:i+n] for i in range(0, len(line), n)] | |
def tohex(val, nbits=32): | |
return hex((val + (1 << nbits)) % (1 << nbits)) | |
def parse_instruction(b): | |
global pc | |
if b[pc] == 0: | |
print "NOP" | |
pc += 1 | |
elif b[pc] == 0x10: | |
val = int(str(b[pc+1:pc+1+4]).encode('hex'), 16) | |
print "PUSH " + hex(val) | |
stack.append(val) | |
pc += 5 | |
elif b[pc] == 0x20: | |
addr = stack.pop() | |
value = stack.pop() | |
print "STORE @ {} -> {}".format(hex(addr), hex(value)) | |
mem[addr] = value | |
mem[addr+1] = value >> 8 | |
mem[addr+2] = value >> 16 | |
mem[addr+3] = value >> 24 | |
pc += 1 | |
elif b[pc] == 0x21: | |
addr = stack.pop() | |
# dst = stack.pop() | |
val = 0 | |
val = mem[addr] | |
val += mem[addr+1] << 8 | |
val += mem[addr+2] << 16 | |
val += mem[addr+3] << 24 | |
print "PUSH [{}] -> {}".format(hex(addr), hex(val)) | |
stack.append(val) | |
pc += 1 | |
elif b[pc] == 0x30: | |
n1 = stack.pop() | |
n2 = stack.pop() | |
stack.append(n1 + n2) | |
print "ADD {} + {}".format(hex(n1), hex(n2)) | |
pc += 1 | |
elif b[pc] == 0x31: | |
n1 = stack.pop() | |
n2 = stack.pop() | |
stack.append(n1 ^ n2) | |
print "XOR {} ^ {} -> {}".format(tohex(n1), tohex(n2), tohex(n1 ^ n2)) | |
pc += 1 | |
elif b[pc] == 0x32: | |
n1 = stack.pop() | |
n2 = stack.pop() | |
if n1 == n2: | |
r = 0 | |
elif n1 <= n2: | |
r = -1 | |
else: | |
r = 1 | |
stack.append(r) | |
print "CMP {} and {} -> {}".format(tohex(n1), tohex(n2), r ) | |
pc += 1 | |
elif b[pc] == 0x33: | |
n1 = stack.pop() | |
n2 = stack.pop() | |
stack.append(n1 * n2) | |
print "MULTIPLY {} * {}".format(n1, n2) | |
pc += 1 | |
elif b[pc] == 0x38: | |
n1 = stack.pop() | |
stack.append(~n1) | |
print "NEGATE {} -> {}".format(hex(n1), tohex(~n1)) | |
pc += 1 | |
elif b[pc] == 0x41: | |
addr = stack.pop() | |
print "JMP @ {}".format(hex(addr)) | |
pc = addr | |
elif b[pc] == 0x42: | |
addr = stack.pop() | |
result = stack.pop() | |
print "JMP CONDITIONAL {} to addr {}".format(result, hex(addr)) | |
if result != 0: | |
pc += 1 | |
else: | |
pc = addr | |
elif b[pc] == 0x50: | |
# one of the ops only 2 possible in level1 | |
op = stack.pop() | |
if op == 0: | |
n = stack.pop() | |
n2 = stack.pop() | |
print "READING @ {} ({} bytes) ".format(hex(n2), n) | |
r = raw_input("READING {} bytes: ".format(n)) | |
mem[n2:n2+n] = r[:n] | |
else: | |
print "UNK OP" | |
pc += 1 | |
else: | |
print "UNK " + hex(b[pc]) | |
pc += 1 | |
# while pc <= len(b): | |
while pc <= 230: #and mem[0x1000] != 1: | |
parse_instruction(b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment