import subprocess import ctypes import re subprocess.call("./bin/dis foo | sponge z_disassembled", shell=True) vm = ctypes.CDLL('./libvm.so') vm.initialize_lilith() vm.load_lilith(ctypes.create_string_buffer("foo".encode('ascii'))) # vm.get_byte.argtype = ctypes.c_uint # vm.get_byte.restype = ctypes.c_ubyte # print("Memory 0-19 values:") # for i in range(0, 20): # print(vm.get_byte(i)) vm.get_memory.argtype = ctypes.c_uint vm.get_memory.restype = ctypes.c_char_p hexlookup = { 0 : '0', 1 : '1', 2 : '2', 3 : '3', 4 : '4', 5 : '5', 6 : '6', 7 : '7', 8 : '8', 9 : '9', 10 : 'A', 11 : 'B', 12 : 'C', 13 : 'D', 14 : 'E', 15 : 'F' } def formatByte(a): first = a >> 4 second = a % 16 return str(hexlookup[first]+hexlookup[second]) def formatAddress(a): first = a >> 24 second = (a % 16777216) >> 16 third = (a % 65536) >> 8 fourth = a % 256 myreturn = formatByte(first) + formatByte(second) + formatByte(third) + formatByte(fourth) return myreturn[:-1] + "x" def formatRegister(a): first = a >> 24 second = (a % 16777216) >> 16 third = (a % 65536) >> 8 fourth = a % 256 return formatByte(first) + formatByte(second) + formatByte(third) + formatByte(fourth) def get_header(): return """ Knight CPU Debugger
""" def get_spacer1(): return """
Index 0 1 2 3 4 5 6 7 8 8 A B C D E F
""" def get_registers(index): vm.get_register.argtype = ctypes.c_uint vm.get_register.restype = ctypes.c_uint # R0 = vm.get_register(3) # print("Register R0 value:") # print(R0) temp = """ """ for i in range(0,8): temp = temp + """\n" return temp + """
Register Value
R""" + str(index + i) + "" + formatRegister(vm.get_register(index + i)) + "
""" def get_spacer2(): return """
""" def get_disassembled(): f = open('z_disassembled', "r") temp = """""" i = 0 for line in f: pieces = re.split(r'\t+', line) temp = temp + "\n" i = i + 4 return temp + "
" + formatRegister(i) + "" + pieces[0] + "" + pieces[1] + "
" def get_footer(): return """
""" print(get_header()) print( (vm.get_memory(0)).decode('utf-8')) print(get_spacer1()) print(get_registers(0)) print(get_registers(8)) print(get_spacer2()) print(get_disassembled()) print(get_footer())