asmTron/deCompiler.py
2020-05-29 00:41:13 +02:00

215 lines
7.8 KiB
Python

import asm
import os
import pickle
import zlib
from base64 import *
asmMap = ["NOP","LDA","ADD","SUB","STA","OUT","JMP","LDI","JIF","JIE","---","INP","JMA","HLO","HST","HLT"]
bits = 12
pointer = 0
note="""
newVar [var]
jumpMark [jumpMark name]
jump [jumpMark name]
getVarAddress [var A] [var B]
getArrayAddress [array] [index] [var]
exec [func name]
setVar [var] [value] OK
setVar [var] b[value] OK
setArrayIndex [array] [index] [value]
setArrayIndex [array] [index] b[value]
getArrayIndex [array] [index] [var]
copyVar [var A] [var B]
write [var] OK
read [var] OK
add [var A] [var B] [var OUT] OK
subtract [var A] [var B] [var OUT] OK
if [var A] [??] [var B] [cmd] [pars]
for [goal] [counter] [step] [cmd] [pars]
:[asm] v#[var]
:[asm] d#[par]
:[asm] b#[par]
:[asm] -
:[asm] ~
shellcode [code]
nop OK
halt OK
user defined funtions
"""
def padBin(number,bit=8):
return asm.padBin(asm.toBin(number),bit=bit)
def toDec(binNumber):
return asm.toDec(binNumber)
def load():
print("[*] Avaible saves:")
for f in os.listdir("."):
if f[-8:]==".asmTron":
print("> "+f[:-8])
for f in os.listdir("tronScript"):
if f[-8:]==".asmTron":
print("> tronScript/"+f[:-8])
name = raw_input("name> ")
try:
with open(name+".asmTron", "rb") as f:
t = f.read()
t=zlib.decompress(t)
magicBit,memory = pickle.loads(t)
except Exception as e:
try:
with open("tronScripts/"+name+".asmTron", "rb") as f:
t = f.read()
t=zlib.decompress(t)
magicBit,memory = pickle.loads(t)
except Exception as e:
if b64encode(str(e))[:25]=="W0Vycm5vIDJdIE5vIHN1Y2ggZ":
print(" [!] Error: File '"+name+".asmTron' does not exist")
else:
print(" [!] Error: Unknown Error while reading File '"+name+"' with Errorcode: "+str(b64encode(str(e))))
if not magicBit==12:
print("[!] Error: Invalid Magical Bit: "+str(magicBit))
exit()
lenMem = len(memory)
for i in range(lenMem): #remove end-zeros
if memory[lenMem-i-1]!=0:
endPoint = lenMem-i
break
print("[*] loaded "+name+".asmTron")
return memory[:endPoint]
def main():
pointer = 0
memory = load()
lenMem = len(memory)
deco = []
for i in range(lenMem): #expand deco
deco.append("")
while True:
t = padBin(memory[pointer],bit=12)
cmd = t[:4]
par = t[4:]
#print("["+str(pointer).zfill(3)+"]> "+cmd+" "+par)
if cmd=="0000": #NOP
pointer+=1
elif cmd=="0001": #LDA
if padBin(memory[pointer+1],bit=12)[:4]=="0100": #STA
# LDI STA -> setVar
deco[pointer]="copyVar VAR_"+chr(toDec(padBin(memory[pointer])[4:])+65)+" VAR_"+chr(toDec(padBin(memory[pointer+1])[4:])+65)
pointer+=2
elif padBin(memory[pointer+1],bit=12)[:4]=="0101": #OUT
# LDI OUT -> write
deco[pointer]="write VAR_"+chr(toDec(padBin(memory[pointer])[4:])+65)
pointer+=2
elif padBin(memory[pointer+1],bit=12)[:4]=="0010": #ADD
if padBin(memory[pointer+2],bit=12)[:4]=="0100": #STA
# LDI ADD STA -> add
deco[pointer]="add VAR_"+chr(toDec(padBin(memory[pointer])[4:])+65)+" VAR_"+chr(toDec(padBin(memory[pointer+1])[4:])+65)+" VAR_"+chr(toDec(padBin(memory[pointer+2])[4:])+65)
pointer+=3
else:
deco[pointer]=":"+asmMap[toDec(padBin(memory[pointer],bit=12)[:4])]+" v#VAR_"+chr(toDec(padBin(memory[pointer])[4:])+65)
pointer+=1
elif padBin(memory[pointer+1],bit=12)[:4]=="0011": #SUB
if padBin(memory[pointer+2],bit=12)[:4]=="0100": #STA
# LDI SUB STA -> subtract
deco[pointer]="subtract VAR_"+chr(toDec(padBin(memory[pointer])[4:])+65)+" VAR_"+chr(toDec(padBin(memory[pointer+1])[4:])+65)+" VAR_"+chr(toDec(padBin(memory[pointer+2])[4:])+65)
pointer+=3
else:
deco[pointer]=":"+asmMap[toDec(padBin(memory[pointer],bit=12)[:4])]+" v#VAR_"+chr(toDec(padBin(memory[pointer])[4:])+65)
pointer+=1
else:
deco[pointer]=":"+asmMap[toDec(padBin(memory[pointer],bit=12)[:4])]+" v#VAR_"+chr(toDec(padBin(memory[pointer])[4:])+65)
pointer+=1
elif cmd=="0010": #ADD
deco[pointer]=":"+asmMap[toDec(padBin(memory[pointer],bit=12)[:4])]+" v#VAR_"+chr(toDec(padBin(memory[pointer])[4:])+65)
pointer+=1
elif cmd=="0011": #SUB
deco[pointer]=":"+asmMap[toDec(padBin(memory[pointer],bit=12)[:4])]+" v#VAR_"+chr(toDec(padBin(memory[pointer])[4:])+65)
pointer+=1
elif cmd=="0100": #STA
deco[pointer]=":"+asmMap[toDec(padBin(memory[pointer],bit=12)[:4])]+" v#VAR_"+chr(toDec(padBin(memory[pointer])[4:])+65)
pointer+=1
elif cmd=="0101": #OUT
deco[pointer]=":"+asmMap[toDec(padBin(memory[pointer],bit=12)[:4])]+" v#VAR_"+chr(toDec(padBin(memory[pointer])[4:])+65)
pointer+=1
elif cmd=="0110": #JMP
deco[pointer]=":"+asmMap[toDec(padBin(memory[pointer],bit=12)[:4])]+" v#VAR_"+chr(toDec(padBin(memory[pointer])[4:])+65)
pointer+=1
elif cmd=="0111": #LDI
if padBin(memory[pointer+1],bit=12)[:4]=="0100": #STA
if padBin(memory[pointer+2],bit=12)[:4]=="0001": #LDA
if padBin(memory[pointer+3],bit=12)[:4]=="0010": #ADD
if padBin(memory[pointer+4],bit=12)[:4]=="0011": #SUB
if padBin(memory[pointer+5],bit=12)[:4]=="1001": #JIE
if padBin(memory[pointer+6],bit=12)[:4]=="0110": #JMP
# LDI STA LDA ADD SUB JIE JIF -> if ==
deco[pointer]="if VAR_"+chr(toDec(padBin(memory[pointer+2])[4:])+65)+" == VAR_"+chr(toDec(padBin(memory[pointer+4])[4:])+65)+" jump "+str(toDec(padBin(memory[pointer+6])[4:]))
pointer+=6
else:
# LDI STA LDA ADD SUB JIE -> if !=
deco[pointer]="if VAR_"+chr(toDec(padBin(memory[pointer+2])[4:])+65)+" != VAR_"+chr(toDec(padBin(memory[pointer+4])[4:])+65)+" jump "+str(toDec(padBin(memory[pointer+5])[4:]))
pointer+=5
else:
deco[pointer]=":"+asmMap[toDec(padBin(memory[pointer],bit=12)[:4])]+" v#VAR_"+chr(toDec(padBin(memory[pointer])[4:])+65)
pointer+=1
else:
deco[pointer]=":"+asmMap[toDec(padBin(memory[pointer],bit=12)[:4])]+" v#VAR_"+chr(toDec(padBin(memory[pointer])[4:])+65)
pointer+=1
else:
deco[pointer]=":"+asmMap[toDec(padBin(memory[pointer],bit=12)[:4])]+" v#VAR_"+chr(toDec(padBin(memory[pointer])[4:])+65)
pointer+=1
else:
# LDI STA -> setVar
deco[pointer]="setVar VAR_"+chr(toDec(padBin(memory[pointer+1])[4:])+65)+" "+str(toDec(par))
pointer+=2
else:
deco[pointer]=":"+asmMap[toDec(padBin(memory[pointer],bit=12)[:4])]+" d#"+toDec(padBin(memory[pointer])[4:])
pointer+=1
elif cmd=="1000": #JIF
deco[pointer]=":"+asmMap[toDec(padBin(memory[pointer],bit=12)[:4])]+" v#VAR_"+chr(toDec(padBin(memory[pointer])[4:])+65)
pointer+=1
elif cmd=="1001": #JIE
deco[pointer]=":"+asmMap[toDec(padBin(memory[pointer],bit=12)[:4])]+" v#VAR_"+chr(toDec(padBin(memory[pointer])[4:])+65)
pointer+=1
elif cmd=="1010": #---
deco[pointer]="#<invalid cmd: 1010>"
pointer+=1
elif cmd=="1011": #INP
if padBin(memory[pointer+1],bit=12)[:4]=="0100": #STA
# INP STA -> read
deco[pointer]="read VAR_"+chr(toDec(padBin(memory[pointer+1])[4:])+65)
pointer+=2
else:
deco[pointer]=":"+asmMap[toDec(padBin(memory[pointer],bit=12)[:4])]+" v#VAR_"+chr(toDec(padBin(memory[pointer])[4:])+65)
pointer+=1
elif cmd=="1100": #JMA
deco[pointer]=":"+asmMap[toDec(padBin(memory[pointer],bit=12)[:4])]+" v#VAR_"+chr(toDec(padBin(memory[pointer])[4:])+65)
pointer+=1
elif cmd=="1101": #HLO
deco[pointer]=":"+asmMap[toDec(padBin(memory[pointer],bit=12)[:4])]+" v#VAR_"+chr(toDec(padBin(memory[pointer])[4:])+65)
pointer+=1
elif cmd=="1110": #HST
deco[pointer]=":"+asmMap[toDec(padBin(memory[pointer],bit=12)[:4])]+" v#VAR_"+chr(toDec(padBin(memory[pointer])[4:])+65)
pointer+=1
elif cmd=="1111": #HLT
deco[pointer]="halt"
pointer+=1
if pointer>=lenMem:
break
for d in range(len(deco)):
#if deco[d]!="":
print("["+str(d).zfill(3)+"]> "+deco[d])
if __name__=="__main__":
main()