Skip to content

Instantly share code, notes, and snippets.

@pawnlord
Created September 3, 2024 20:09
Show Gist options
  • Save pawnlord/254ee9ec5491085d60abe1c66ac1f132 to your computer and use it in GitHub Desktop.
Save pawnlord/254ee9ec5491085d60abe1c66ac1f132 to your computer and use it in GitHub Desktop.
with open("vmvm.vm", "rb") as vm_file:
vm = vm_file.read()
addrs_to_lines = {}
def read_int(counter):
global vm
return vm[counter] \
+ (vm[counter + 1] << 8) \
+ (vm[counter + 2] << 16) \
+ (vm[counter + 3] << 24)
def read_str(counter):
global vm
addr = read_int(counter) + 8
s = ""
while vm[addr] != 0:
s += chr(vm[addr])
addr += 1
return s.strip()
def address(counter):
global vm
return "memory[" + hex(vm[counter]) + "]"
def constant(counter):
const = read_int(counter)
return hex(const)
def jump_line(counter):
global vm, addrs_to_lines
addr = read_int(counter)
line_num = addrs_to_lines[addr]
return str(line_num)
print(len(vm))
pc = 0
sz = read_int(pc)
other = read_int(pc + 4)
print(other)
pc += 8
print("format: instr arg1, [arg2] ([flag = ...])")
def print_instr(instr, arg1, arg2=None, info=None):
output = instr + " " + arg1
print(instr + " " + arg1, end="")
if arg2:
output += ", " + arg2
print(", " + arg2, end="")
if info:
space = " " + " " * (40 - (len(output)))
print(space + "(" + info + ")", end = "")
print()
line = 1
while True:
instr = vm[pc]
if not instr < 0x1f:
break
addrs_to_lines[pc-8] = line
line += 1
if instr == 0x0:
pc += 3
elif instr == 0x1:
pc += 6
elif instr == 0x2:
pc += 3
elif instr == 0x3:
pc += 6
elif instr == 0x4:
pc += 3
elif instr == 0x5:
pc += 6
elif instr == 0x6:
pc += 3
elif instr == 0x7:
pc += 6
elif instr == 0x8:
pc += 3
elif instr == 0x9:
pc += 6
elif instr == 0xa:
pc += 3
elif instr == 0xb:
pc += 6
elif instr == 0xc:
pc += 3
elif instr == 0xd:
pc += 6
elif instr == 0xe:
pc += 2
elif instr == 0xf:
pc += 5
elif instr == 0x10:
pc += 5
elif instr == 0x11:
pc += 5
elif instr == 0x12:
pc += 5
elif instr == 0x13:
pc += 1
elif instr == 0x14:
pc += 2
elif instr == 0x15:
pc += 2
elif instr == 0x16:
pc += 3
elif instr == 0x17:
pc += 6
elif instr == 0x18:
pc += 3
elif instr == 0x19:
pc += 6
elif instr == 0x1a:
pc += 3
elif instr == 0x1b:
pc += 6
elif instr == 0x1c:
pc += 2
elif instr == 0x1d:
pc += 5
elif instr == 0x1e:
pc += 1
pc = 8
print(addrs_to_lines)
while True:
instr = vm[pc]
if not instr < 0x1f:
break
if instr == 0x0:
print_instr("mov", address(pc + 1), address(pc + 2))
pc += 3
elif instr == 0x1:
print_instr("mov", address(pc + 1), constant(pc + 2))
pc += 6
elif instr == 0x2:
print_instr("add", address(pc + 1), address(pc + 2))
pc += 3
elif instr == 0x3:
print_instr("add", address(pc + 1), constant(pc + 2))
pc += 6
elif instr == 0x4:
print_instr("sub", address(pc + 1), address(pc + 2))
pc += 3
elif instr == 0x5:
print_instr("sub", address(pc + 1), constant(pc + 2))
pc += 6
elif instr == 0x6:
print_instr("mul", address(pc + 1), address(pc + 2), "zero = " + address(pc + 1) + " * " + address(pc + 2) + " == 0")
pc += 3
elif instr == 0x7:
print_instr("mul", address(pc + 1), constant(pc + 2), "zero = " + address(pc + 1) + " * " + constant(pc + 2) + " == 0")
pc += 6
elif instr == 0x8:
print_instr("div", address(pc + 1), address(pc + 2), "zero = " + address(pc + 1) + ">" + address(pc + 2) + " !!!")
pc += 3
elif instr == 0x9:
print_instr("div", address(pc + 1), constant(pc + 2), "zero = " + address(pc + 1) + ">" + constant(pc + 2) + " !!!")
pc += 6
elif instr == 0xa:
print_instr("xor", address(pc + 1), address(pc + 2), "zero = " + address(pc + 1) + " ^ " + address(pc + 2) + " == 0")
pc += 3
elif instr == 0xb:
print_instr("xor", address(pc + 1), constant(pc + 2), "zero = " + address(pc + 1) + " ^ " + constant(pc + 2) + " == 0")
pc += 6
elif instr == 0xc:
print_instr("and", address(pc + 1), address(pc + 2), "zero = " + address(pc + 1) + " & " + address(pc + 2) + " == 0")
pc += 3
elif instr == 0xd:
print_instr("and", address(pc + 1), constant(pc + 2), "zero = " + address(pc + 1) + " & " + constant(pc + 2) + " == 0")
pc += 6
elif instr == 0xe:
print_instr("jump_mem", address(pc + 1))
pc += 2
elif instr == 0xf:
print_instr("jump", jump_line(pc + 1))
pc += 5
elif instr == 0x10:
print_instr("jump_zero", jump_line(pc + 1))
pc += 5
elif instr == 0x11:
print_instr("jump_not_zero", jump_line(pc + 1))
pc += 5
elif instr == 0x12:
print_instr("call", jump_line(pc + 1), info="push pc + 5")
pc += 5
elif instr == 0x13:
print_instr("ret", "", info="pc = pop")
pc += 1
elif instr == 0x14:
print_instr("push", address(pc + 1), info="push")
pc += 2
elif instr == 0x15:
print_instr("pop", address(pc + 1), info="pop")
pc += 2
elif instr == 0x16:
print_instr("eq", address(pc + 1), address(pc + 2), "zero = " + address(pc + 1) + " == " + address(pc + 2))
pc += 3
elif instr == 0x17:
print_instr("eq", address(pc + 1), constant(pc + 2), "zero = " + address(pc + 1) + " == " + constant(pc + 2))
pc += 6
elif instr == 0x18:
print_instr("mov_to_vm", address(pc + 1), address(pc + 2))
pc += 3
elif instr == 0x19:
print_instr("mov_to_vm", address(pc + 1), constant(pc + 2))
pc += 6
elif instr == 0x1a:
print_instr("mov_from_vm", address(pc + 1), address(pc + 2))
pc += 3
elif instr == 0x1b:
print_instr("mov_from_vm", address(pc + 1), constant(pc + 2))
pc += 6
elif instr == 0x1c:
print_instr("getc", address(pc + 1))
pc += 2
elif instr == 0x1d:
print_instr("print", constant(pc + 1), info="msg = \"" + read_str(pc + 1) + "\"")
pc += 5
elif instr == 0x1e:
print("halt")
pc += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment