initial commit
This commit is contained in:
commit
fa58d701f1
495
asm.py
Normal file
495
asm.py
Normal file
@ -0,0 +1,495 @@
|
|||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import pickle
|
||||||
|
import zlib
|
||||||
|
import hashlib
|
||||||
|
import copy
|
||||||
|
from termcolor import colored
|
||||||
|
from base64 import *
|
||||||
|
from colorama import init
|
||||||
|
init()
|
||||||
|
|
||||||
|
OS = "Linux"
|
||||||
|
#OS = "Windows"
|
||||||
|
|
||||||
|
doc = """
|
||||||
|
0000 NOP
|
||||||
|
0001 LDA memory > A
|
||||||
|
0010 ADD A + memspace
|
||||||
|
0011 SUB A - memspace
|
||||||
|
0100 STA A > memory
|
||||||
|
0101 OUT A > OUT
|
||||||
|
0110 JMP Jump to adress
|
||||||
|
0111 LDI par > A
|
||||||
|
1000 JIF Jump if greater
|
||||||
|
1001 JIE Jump if equal
|
||||||
|
1010 LDP memory[A] > A
|
||||||
|
1011 INP IN > A
|
||||||
|
1100 JMA Jump to A
|
||||||
|
1101 --- NOP equivalent
|
||||||
|
1110 STP memory[par] > memory[A]
|
||||||
|
1111 HLT Halt
|
||||||
|
"""
|
||||||
|
|
||||||
|
asmMap = ["NOP","LDA","ADD","SUB","STA","OUT","JMP","LDI","JIF","JIE","---","INP","JMA","HLO","HST","HLT"]
|
||||||
|
|
||||||
|
bits = 12
|
||||||
|
|
||||||
|
pointer = 0
|
||||||
|
|
||||||
|
registerA = 0
|
||||||
|
out = 0
|
||||||
|
|
||||||
|
color = [["green","on_grey"],["blue","on_grey"],["red","on_grey"],["magenta","on_grey"],["cyan","on_grey"],
|
||||||
|
["grey","on_white"],["green","on_white"],["blue","on_white"],["red","on_white"],["magenta","on_white"],["cyan","on_white"],
|
||||||
|
["grey","on_green"],["white","on_green"],["blue","on_green"],["red","on_green"],["magenta","on_green"],["cyan","on_green"],
|
||||||
|
["grey","on_blue"],["white","on_blue"],["green","on_blue"],["red","on_blue"],["magenta","on_blue"],["cyan","on_blue"],
|
||||||
|
["grey","on_red"],["white","on_red"],["green","on_red"],["blue","on_red"],["magenta","on_red"],["cyan","on_red"],
|
||||||
|
["grey","on_magenta"],["white","on_magenta"],["green","on_magenta"],["blue","on_magenta"],["red","on_magenta"],["cyan","on_magenta"],
|
||||||
|
["grey","on_cyan"],["white","on_cyan"],["green","on_cyan"],["blue","on_cyan"],["red","on_cyan"],["magenta","on_cyan"]]
|
||||||
|
|
||||||
|
memory = []
|
||||||
|
|
||||||
|
for i in range(2**8):
|
||||||
|
memory.append(0)
|
||||||
|
|
||||||
|
def clear():
|
||||||
|
if OS=="Windows":
|
||||||
|
os.system("cls")
|
||||||
|
elif OS=="Linux":
|
||||||
|
os.system("clear")
|
||||||
|
else:
|
||||||
|
print("Error: Unknown OS")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
def toDec(binary):
|
||||||
|
try:
|
||||||
|
return int(binary, 2)
|
||||||
|
except:
|
||||||
|
return -1
|
||||||
|
|
||||||
|
def toBin(decimal):
|
||||||
|
try:
|
||||||
|
return int(bin(decimal)[2:])
|
||||||
|
except:
|
||||||
|
print("Error: The dec-number "+decimal+" could not be converted to binary")
|
||||||
|
return decimal
|
||||||
|
|
||||||
|
def padBin(binary,bit=bits):
|
||||||
|
return str(binary).zfill(bit)[-bit:]
|
||||||
|
|
||||||
|
def bin_input(display,bit=bits):
|
||||||
|
raw = raw_input(display)
|
||||||
|
if raw=="x":
|
||||||
|
return -1
|
||||||
|
raw = raw.replace(" ","")
|
||||||
|
ret = toDec(raw)
|
||||||
|
if ret>2**bit:
|
||||||
|
print("[!] input exceted maximal size for "+str(bit)+"-bit integer!")
|
||||||
|
return bin_input(display)
|
||||||
|
if ret==-1:
|
||||||
|
print("[!] "+raw+" is not a valid binary number!")
|
||||||
|
return bin_input(display)
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def parse(cmd,par,verbose=False,adress=0):
|
||||||
|
global registerA
|
||||||
|
global memory
|
||||||
|
global out
|
||||||
|
global pointer
|
||||||
|
v=verbose
|
||||||
|
cmd16 = cmd
|
||||||
|
cmd = cmd[-4:]
|
||||||
|
if cmd=="0000": #nop
|
||||||
|
if v: print padBin(toBin(adress),bit=8)+": NOP"
|
||||||
|
elif cmd=="0001": #lda
|
||||||
|
registerA = memory[par]
|
||||||
|
if v: print padBin(toBin(adress),bit=8)+": LDA memory["+padBin(toBin(par),bit=8)+"] > Register A"
|
||||||
|
elif cmd=="0010": #add
|
||||||
|
registerA = (registerA + memory[par]) % 2**8
|
||||||
|
if v: print padBin(toBin(adress),bit=8)+": ADD Register A + memory["+padBin(toBin(par),bit=8)+"]"
|
||||||
|
elif cmd=="0011": #sub
|
||||||
|
registerA = (registerA - memory[par]) % 2**8
|
||||||
|
if registerA<0:
|
||||||
|
registerA = (2**bits+registerA) % 2**8
|
||||||
|
if v: print padBin(toBin(adress),bit=8)+": SUB Register A - memory["+padBin(toBin(par),bit=8)+"]"
|
||||||
|
elif cmd=="0100": #sta
|
||||||
|
memory[par] = registerA
|
||||||
|
if v: print padBin(toBin(adress),bit=8)+": STA Register A > memory["+padBin(toBin(par),bit=8)+"]"
|
||||||
|
elif cmd=="0101": #out
|
||||||
|
out = registerA
|
||||||
|
print("[OUT] "+padBin(toBin(out),bit=8))
|
||||||
|
if v: print padBin(toBin(adress),bit=8)+": OUT Register A > out"
|
||||||
|
elif cmd=="0110": #jmp
|
||||||
|
pointer = par - 1
|
||||||
|
if v: print padBin(toBin(adress),bit=8)+": JMP "+padBin(toBin(par),bit=8)
|
||||||
|
elif cmd=="0111": #ldi
|
||||||
|
registerA = par
|
||||||
|
if v: print padBin(toBin(adress),bit=8)+": LDI "+padBin(toBin(par),bit=8)+" > Register A"
|
||||||
|
elif cmd=="1000": #jif
|
||||||
|
if registerA>=(2**8)/2: #10000000
|
||||||
|
pointer = par - 1
|
||||||
|
if v: print padBin(toBin(adress),bit=8)+": JIF {TRUE} > "+padBin(toBin(par),bit=8)
|
||||||
|
else:
|
||||||
|
if v: print padBin(toBin(adress),bit=8)+": JIF {FALSE} > "+padBin(toBin(par),bit=8)
|
||||||
|
elif cmd=="1001": #fie
|
||||||
|
if registerA==(2**8)/2: #10000000
|
||||||
|
pointer = par - 1
|
||||||
|
if v: print padBin(toBin(adress),bit=8)+": JIE {TRUE} > "+padBin(toBin(par),bit=8)
|
||||||
|
else:
|
||||||
|
if v: print padBin(toBin(adress),bit=8)+": JIE {FALSE} > "+padBin(toBin(par),bit=8)
|
||||||
|
elif cmd=="1010": #ldp
|
||||||
|
registerA = memory[registerA]
|
||||||
|
if v: print padBin(toBin(adress),bit=8)+": LDP memory["+padBin(toBin(registerA),bit=8)+"] > Register A"
|
||||||
|
elif cmd=="1011": #inp
|
||||||
|
if v: print padBin(toBin(adress),bit=8)+": INP input > Register A"
|
||||||
|
registerA = bin_input("[INPUT]> ")
|
||||||
|
elif cmd=="1100": #jma
|
||||||
|
pointer = registerA - 1
|
||||||
|
if v: print padBin(toBin(adress),bit=8)+": JMA Jump to Register A"
|
||||||
|
elif cmd=="1101": #---
|
||||||
|
if v: print "Got unimplemented op-code: 1101"
|
||||||
|
elif cmd=="1110": #STP
|
||||||
|
memory[par] = memory[registerA]
|
||||||
|
if v: print padBin(toBin(adress),bit=8)+": STP memory["+padBin(toBin(registerA),bit=8)+"] > memory["+padBin(toBin(par),bit=8)+"]"
|
||||||
|
elif cmd=="1111": #hlt
|
||||||
|
return -1
|
||||||
|
return True
|
||||||
|
|
||||||
|
if __name__=="__main__":
|
||||||
|
print("[asmTron by CyberCodeDragon]")
|
||||||
|
while True:
|
||||||
|
x = ""
|
||||||
|
x=raw_input("#> ")
|
||||||
|
x = x.split(" ")
|
||||||
|
for i in range(2):
|
||||||
|
x.append("")
|
||||||
|
y = x[1]
|
||||||
|
z = x[2]
|
||||||
|
x = x[0]
|
||||||
|
if x=="help" or x=="h":
|
||||||
|
print("Avaible Commands:")
|
||||||
|
print("docs - display avaible operants")
|
||||||
|
print("write [w] - write value to adress")
|
||||||
|
print("read [r] - read value from adress")
|
||||||
|
print("dump [d] - dump from adress with length")
|
||||||
|
print("run [u] - execute programm")
|
||||||
|
print("edit [e] - edit from adress til >x<")
|
||||||
|
print("cls - clear screen")
|
||||||
|
print("clear / reset - clear memory,register,pointer")
|
||||||
|
print("highClear [hc] - clear highMem,register,pointer")
|
||||||
|
print("step [s] - run one command from programm")
|
||||||
|
print("debug - execute programm with verbose output")
|
||||||
|
print("pointer - display pointer")
|
||||||
|
print("setPointer - sets pointer")
|
||||||
|
print("resetPointer [rp] - sets pointer to zero")
|
||||||
|
print("register [reg] - displays A register")
|
||||||
|
print("setRegister - sets A register")
|
||||||
|
print("save - saves memory to local file")
|
||||||
|
print("load - loads memory from local file")
|
||||||
|
print("code / exec - execute one line of shellcode")
|
||||||
|
print("guiDebug [gd] - graphical debugger")
|
||||||
|
print("asm - shellcode visualizer (assembly / jump-arrows)")
|
||||||
|
print("halting - tries to determin, if algorythm will diverge")
|
||||||
|
elif x=="docs":
|
||||||
|
print(doc)
|
||||||
|
elif x=="write" or x=="w":
|
||||||
|
if y=="" or z=="":
|
||||||
|
y=bin_input("adress> ")
|
||||||
|
z=bin_input("value> ")
|
||||||
|
try:
|
||||||
|
memory[y] = z
|
||||||
|
except:
|
||||||
|
print("[!] Unknown error while writing")
|
||||||
|
elif x=="read" or x=="r":
|
||||||
|
if y=="":
|
||||||
|
y=bin_input("adress> ")
|
||||||
|
print(padBin(toBin(memory[y])))
|
||||||
|
elif x=="dump" or x=="d":
|
||||||
|
if y=="" and z=="":
|
||||||
|
y=bin_input("adress> ")
|
||||||
|
z=bin_input("length> ")
|
||||||
|
elif z=="":
|
||||||
|
z=toDec(y)
|
||||||
|
y=0
|
||||||
|
for m in range(z):
|
||||||
|
print(padBin(toBin(y+m),bit=8)+": "+padBin(toBin(memory[y+m])))
|
||||||
|
elif x=="run" or x=="u":
|
||||||
|
while True:
|
||||||
|
cmd = padBin(toBin(memory[pointer]))[:4]
|
||||||
|
par = int(toDec(padBin(toBin(memory[pointer]))[-8:]))
|
||||||
|
if parse(cmd,par)==-1:
|
||||||
|
pointer = 0
|
||||||
|
break
|
||||||
|
pointer = (pointer + 1) % 2**bits
|
||||||
|
elif x=="verboseRun" or x=="vu":
|
||||||
|
while True:
|
||||||
|
cmd = padBin(toBin(memory[pointer]))[:4]
|
||||||
|
par = int(toDec(padBin(toBin(memory[pointer]))[-8:]))
|
||||||
|
if parse(cmd,par,verbose=True,adress=pointer)==-1:
|
||||||
|
pointer = 0
|
||||||
|
break
|
||||||
|
pointer = (pointer + 1) % 2**bits
|
||||||
|
elif x=="edit" or x=="e":
|
||||||
|
adress=bin_input("offset> ",bit=8)
|
||||||
|
while True:
|
||||||
|
value=bin_input(padBin(toBin(adress),bit=8)+": "+padBin(toBin(memory[adress]))+" > ")
|
||||||
|
if value==-1:
|
||||||
|
break
|
||||||
|
try:
|
||||||
|
memory[adress] = value
|
||||||
|
except:
|
||||||
|
print("[!] Unknown error while writing")
|
||||||
|
if adress==len(memory)-1:
|
||||||
|
adress = -1
|
||||||
|
print("[*] End of mem")
|
||||||
|
adress = adress + 1
|
||||||
|
elif x=="cls":
|
||||||
|
clear()
|
||||||
|
elif x=="clear" or x=="reset":
|
||||||
|
pointer = 0
|
||||||
|
registerA = 0
|
||||||
|
out = 0
|
||||||
|
memory = []
|
||||||
|
for i in range(2**bits):
|
||||||
|
memory.append(0)
|
||||||
|
elif x=="highClear" or x=="highReset" or x=="hr" or x=="hc":
|
||||||
|
pointer = 0
|
||||||
|
registerA = 0
|
||||||
|
out = 0
|
||||||
|
for i in range((2**bits)/2):
|
||||||
|
memory[(2**bits)/2+i] = 0
|
||||||
|
elif x=="step" or x=="s":
|
||||||
|
cmd = padBin(toBin(memory[pointer]))[:4]
|
||||||
|
par = int(toDec(padBin(toBin(memory[pointer]))[-8:]))
|
||||||
|
if parse(cmd,par,verbose=True,adress=pointer)==-1:
|
||||||
|
pointer = 0
|
||||||
|
print("[*] Programm halted")
|
||||||
|
pointer = (pointer + 1) % (2**bits)
|
||||||
|
elif x=="debug":
|
||||||
|
while True:
|
||||||
|
cmd = padBin(toBin(memory[pointer]))[:4]
|
||||||
|
par = int(toDec(padBin(toBin(memory[pointer]))[-8:]))
|
||||||
|
if parse(cmd,par,verbose=True,adress=pointer)==-1:
|
||||||
|
pointer = -1
|
||||||
|
print("[*] Programm halted")
|
||||||
|
break
|
||||||
|
pointer = (pointer + 1) % (2**bits)
|
||||||
|
elif x=="pointer":
|
||||||
|
print("POINTER: "+padBin(toBin(pointer),bit=8))
|
||||||
|
elif x=="setPointer":
|
||||||
|
if y=="":
|
||||||
|
pointer = bin_input("pointer> ",bit=8)
|
||||||
|
else:
|
||||||
|
pointer = y
|
||||||
|
elif x=="resetPointer" or x=="rp":
|
||||||
|
pointer = 0
|
||||||
|
elif x=="register" or x=="reg":
|
||||||
|
print("REGISTER: "+padBin(toBin(registerA)))
|
||||||
|
elif x=="setRegister":
|
||||||
|
if y=="":
|
||||||
|
registerA = bin_input("register> ")
|
||||||
|
else:
|
||||||
|
registerA = y
|
||||||
|
elif x=="save":
|
||||||
|
if y=="":
|
||||||
|
print("[*] Current saves:")
|
||||||
|
for f in os.listdir("."):
|
||||||
|
if f[-8:]==".asmTron":
|
||||||
|
print("> "+f[:-8])
|
||||||
|
name = raw_input("name> ")
|
||||||
|
else:
|
||||||
|
name = y
|
||||||
|
with open(name+".asmTron", 'wb') as f:
|
||||||
|
t=pickle.dumps([12,memory])
|
||||||
|
t=zlib.compress(t)
|
||||||
|
f.write(t)
|
||||||
|
elif x=="load" or x=="l":
|
||||||
|
if y=="":
|
||||||
|
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> ")
|
||||||
|
else:
|
||||||
|
name = y
|
||||||
|
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()
|
||||||
|
print("[*] loaded "+name+".asmTron")
|
||||||
|
elif x=="code" or x=="exec":
|
||||||
|
if y=="":
|
||||||
|
data=bin_input("code> ")
|
||||||
|
else:
|
||||||
|
data=y
|
||||||
|
cmd = padBin(toBin(memory[pointer]))[:4]
|
||||||
|
par = int(toDec(padBin(toBin(memory[pointer]))[-8:]))
|
||||||
|
parse(cmd,par,verbose=True,adress=0)
|
||||||
|
elif x=="guiDebug" or x=="gd":
|
||||||
|
runtime = 0
|
||||||
|
tracker = raw_input("trackers> ").split(",")
|
||||||
|
running = True
|
||||||
|
lastPointer = 0
|
||||||
|
while running:
|
||||||
|
clear()
|
||||||
|
print(" +"+"-"*34+"+"+"-"*28+"+"+"-"*24+"+")
|
||||||
|
print(" |"+" "*8+" - Instructions - "+" "*8+"|"+" "*8+" - Hi-mem - "+" "*8+"|"+" "*24+"|")
|
||||||
|
if pointer <= 20:
|
||||||
|
starter = 0
|
||||||
|
else:
|
||||||
|
starter = pointer - 20
|
||||||
|
for i in range(36):
|
||||||
|
m = i + starter
|
||||||
|
if pointer == m:
|
||||||
|
outI = "> "+padBin(toBin(m),bit=8)+": "+padBin(toBin(memory[m]))+" ["+doc.split("\n")[toDec(padBin(toBin(memory[m]))[:4])+1][:8][5:]+"]"+" <"
|
||||||
|
elif lastPointer == m and pointer-1 != lastPointer:
|
||||||
|
outI = "- "+padBin(toBin(m),bit=8)+": "+padBin(toBin(memory[m]))+" ["+doc.split("\n")[toDec(padBin(toBin(memory[m]))[:4])+1][:8][5:]+"]"+" -"
|
||||||
|
else:
|
||||||
|
outI = " "+padBin(toBin(m),bit=8)+": "+padBin(toBin(memory[m]))+" ["+doc.split("\n")[toDec(padBin(toBin(memory[m]))[:4])+1][:8][5:]+"]"+" "
|
||||||
|
outII = " "+padBin(toBin(128+i),bit=8)+": "+padBin(toBin(memory[128+i]))+" "
|
||||||
|
if i==1:
|
||||||
|
outIII = " - Register A - "
|
||||||
|
elif i==2:
|
||||||
|
outIII = " "*8+padBin(toBin(registerA),bit=8)+" "*8
|
||||||
|
elif i==4:
|
||||||
|
outIII = " - Runtime - "
|
||||||
|
elif i==5:
|
||||||
|
outIII = " "+padBin(toBin(runtime),bit=12)+" "
|
||||||
|
elif i==7:
|
||||||
|
outIII = " - Command - "
|
||||||
|
elif i==8:
|
||||||
|
temp=doc.split("\n")[toDec(padBin(toBin(memory[pointer]))[:4])+1][5:]
|
||||||
|
outIII = int((24-len(temp))/2-1)*" "+" "+temp+int((24-len(temp))/2)*" "
|
||||||
|
elif i==10:
|
||||||
|
outIII = " - Parameter - "
|
||||||
|
elif i==11:
|
||||||
|
outIII = " "+padBin(toBin(memory[pointer]))[-8:]+" "
|
||||||
|
elif i==13:
|
||||||
|
outIII = " - OUT-Register - "
|
||||||
|
elif i==14:
|
||||||
|
outIII = " "+padBin(toBin(out),bit=8)+" "
|
||||||
|
elif i==16:
|
||||||
|
outIII = " - Pointer - "
|
||||||
|
elif i==17:
|
||||||
|
outIII = " "+padBin(toBin(pointer))+" "
|
||||||
|
elif i==19:
|
||||||
|
if len(tracker[0]):
|
||||||
|
outIII = " - Trackers - "
|
||||||
|
else:
|
||||||
|
outIII = " "*24
|
||||||
|
elif i>19 and i<=19+len(tracker):
|
||||||
|
if len(tracker[0]):
|
||||||
|
outIII = " "+str(tracker[i-19-1]) + ": " + padBin(toBin(memory[toDec(tracker[i-19-1])]))+" "
|
||||||
|
else:
|
||||||
|
outIII = " "*24
|
||||||
|
else:
|
||||||
|
outIII = " "*24
|
||||||
|
print(" | "+outI+" | "+outII+" |"+outIII+"|")
|
||||||
|
print(" |"+" "*34+"|"+" "*28+"|"+" "*24+"|")
|
||||||
|
print(" +"+"-"*34+"+"+"-"*28+"+"+"-"*24+"+")
|
||||||
|
|
||||||
|
lastPointer = pointer
|
||||||
|
|
||||||
|
cmd = padBin(toBin(memory[pointer]))[:4]
|
||||||
|
par = int(toDec(padBin(toBin(memory[pointer]))[-8:]))
|
||||||
|
if parse(cmd,par,verbose=True,adress=pointer)==-1:
|
||||||
|
pointer = 0
|
||||||
|
break
|
||||||
|
pointer = (pointer + 1) % 2**bits
|
||||||
|
|
||||||
|
step=raw_input(" <step> ")
|
||||||
|
if step=="x":
|
||||||
|
break
|
||||||
|
|
||||||
|
runtime = runtime + 1
|
||||||
|
elif x=="asm":
|
||||||
|
end = 0
|
||||||
|
for m in range(len(memory)):
|
||||||
|
if padBin(toBin(memory[len(memory)-m-1]))[:4] != "0000":
|
||||||
|
end = len(memory)-m
|
||||||
|
break
|
||||||
|
toPrint = []
|
||||||
|
for m in range(end):
|
||||||
|
cmd = doc.split("\n")[toDec(padBin(toBin(memory[m]))[:4])+1][:8][5:]
|
||||||
|
if toDec(padBin(toBin(memory[m]))[4:]) >= toDec("10000000") and toDec(padBin(toBin(memory[m]))[4:]) <= toDec("10101000"):
|
||||||
|
toPrint.append(padBin(toBin(m),bit=8)+": "+cmd+" "+colored(padBin(toBin(memory[m]))[4:],color[(toDec(padBin(toBin(memory[m]))[4:])-toDec("10000000"))][0],color[(toDec(padBin(toBin(memory[m]))[4:])-toDec("10000000"))][1]))
|
||||||
|
else:
|
||||||
|
toPrint.append(padBin(toBin(m),bit=8)+": "+cmd+" "+padBin(toBin(memory[m]))[4:])
|
||||||
|
origPrint = copy.copy(toPrint)
|
||||||
|
for l in range(len(toPrint)):
|
||||||
|
cmd = origPrint[l][10:][:3]
|
||||||
|
par = origPrint[l][14:]
|
||||||
|
if cmd == "JMP" or cmd == "JIF" or cmd == "JIE":
|
||||||
|
toPrint[toDec(par)] = toPrint[toDec(par)] + " <-+"
|
||||||
|
toPrint[l] = toPrint[l] + " --+"
|
||||||
|
if l < toDec(par):
|
||||||
|
for i in range(len(toPrint)):
|
||||||
|
if l < i < toDec(par):
|
||||||
|
toPrint[i] = toPrint[i] + " |"
|
||||||
|
elif i==toDec(par) or i==l:
|
||||||
|
t=1
|
||||||
|
else:
|
||||||
|
toPrint[i] = toPrint[i] + " "
|
||||||
|
else:
|
||||||
|
for i in range(len(toPrint)):
|
||||||
|
if toDec(par) < i < l:
|
||||||
|
toPrint[i] = toPrint[i] + " |"
|
||||||
|
elif i==toDec(par) or i==l:
|
||||||
|
t=1
|
||||||
|
else:
|
||||||
|
toPrint[i] = toPrint[i] + " "
|
||||||
|
for p in range(len(toPrint)):
|
||||||
|
print(toPrint[p])
|
||||||
|
elif x=="halting":
|
||||||
|
print("[i] Will try to give you information about the divergity of your algorythm")
|
||||||
|
print("[i] This algorythm may itself not diverge!")
|
||||||
|
bPointer = pointer
|
||||||
|
bRegisterA = registerA
|
||||||
|
bMemory = memory
|
||||||
|
runtime = 0
|
||||||
|
stateHashes = []
|
||||||
|
print("[*] Simulating...")
|
||||||
|
while True:
|
||||||
|
runtime+=1
|
||||||
|
cmd = padBin(toBin(memory[pointer]))[:4]
|
||||||
|
par = int(toDec(padBin(toBin(memory[pointer]))[-8:]))
|
||||||
|
if parse(cmd,par)==-1:
|
||||||
|
pointer = 0
|
||||||
|
halts = True
|
||||||
|
print("[*] Algorythm will diverge after "+str(runtime)+" steps")
|
||||||
|
break
|
||||||
|
pointer = (pointer + 1) % 2**bits
|
||||||
|
stateHash = hashlib.sha256(str(pointer)+str(registerA)+str(memory)).digest()
|
||||||
|
if stateHash in stateHashes:
|
||||||
|
print("[*] Algorythm will not diverge: State-Collision after "+str(runtime)+" steps")
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
stateHashes.append(stateHash)
|
||||||
|
pointer = bPointer
|
||||||
|
registerA = bRegisterA
|
||||||
|
memory = bMemory
|
||||||
|
elif x=="exit":
|
||||||
|
exit()
|
||||||
|
elif x=="":
|
||||||
|
_=0
|
||||||
|
else:
|
||||||
|
print("[!] Error")
|
249
asmCust.py
Normal file
249
asmCust.py
Normal file
@ -0,0 +1,249 @@
|
|||||||
|
import os
|
||||||
|
import pickle
|
||||||
|
|
||||||
|
print("[asmTron by CyberCodeDragon]")
|
||||||
|
|
||||||
|
doc = {8:"""
|
||||||
|
0000 NOP
|
||||||
|
0001 LDA memory > A
|
||||||
|
0010 ADD A + memspace
|
||||||
|
0011 SUB A - memspace
|
||||||
|
0100 STA A > memory
|
||||||
|
0101 OUT A > OUT
|
||||||
|
0110 JMP Jump to adress
|
||||||
|
0111 LDI par > A
|
||||||
|
1000 JIF Jump if (JC)
|
||||||
|
1001 HLO H-mem[memspace] > A
|
||||||
|
1010 HST A > H-mem[memspace]
|
||||||
|
1011 INP IN > A
|
||||||
|
1100 JMA Jump to A
|
||||||
|
1111 HLT Halt
|
||||||
|
""", 16: """
|
||||||
|
0000 0000 NOP
|
||||||
|
0000 0001 LDA memory > A
|
||||||
|
0000 0010 ADD A + memspace
|
||||||
|
0000 0011 SUB A - memspace
|
||||||
|
0000 0100 STA A > memory
|
||||||
|
0000 0101 OUT A > OUT
|
||||||
|
0000 0110 JMP Jump to adress
|
||||||
|
0000 0111 LDI par > A
|
||||||
|
0000 1000 JIF Jump if (JC)
|
||||||
|
0000 1001 HLO H-mem[memspace] > A
|
||||||
|
0000 1010 HST A > H-mem[memspace]
|
||||||
|
0000 1011 INP IN > A
|
||||||
|
0000 1111 HLT Halt
|
||||||
|
"""}
|
||||||
|
|
||||||
|
#bits = int(raw_input("arch [8/16]> "))
|
||||||
|
bits = 8
|
||||||
|
|
||||||
|
pointer = 0
|
||||||
|
|
||||||
|
registerA = 0
|
||||||
|
out = 0
|
||||||
|
|
||||||
|
memory = []
|
||||||
|
|
||||||
|
for i in range(2**bits):
|
||||||
|
memory.append(0)
|
||||||
|
|
||||||
|
def toDec(binary):
|
||||||
|
try:
|
||||||
|
return int(binary, 2)
|
||||||
|
except:
|
||||||
|
return -1
|
||||||
|
|
||||||
|
def toBin(decimal):
|
||||||
|
return int(bin(decimal)[2:])
|
||||||
|
|
||||||
|
def padBin(binary,bit=bits):
|
||||||
|
return str(binary).zfill(bit)
|
||||||
|
|
||||||
|
def bin_input(display,bit=bits):
|
||||||
|
raw = raw_input(display)
|
||||||
|
if raw=="x":
|
||||||
|
return -1
|
||||||
|
raw = raw.replace(" ","")
|
||||||
|
ret = toDec(raw)
|
||||||
|
if ret>2**bit:
|
||||||
|
print("[!] input exceted maxial size for "+str(bit)+"-bit integer")
|
||||||
|
return bin_input(display)
|
||||||
|
if ret==-1:
|
||||||
|
print("[!] "+raw+" is not a valid binary number!")
|
||||||
|
return bin_input(display)
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def parse(cmd,par,verbose=False,adress=0):
|
||||||
|
global registerA
|
||||||
|
global memory
|
||||||
|
global out
|
||||||
|
global pointer
|
||||||
|
v=verbose
|
||||||
|
cmd16 = cmd
|
||||||
|
cmd = cmd[-4:]
|
||||||
|
if cmd=="0000": #nop
|
||||||
|
if v: print padBin(toBin(adress),bit=bits/2)+": NOP"
|
||||||
|
elif cmd=="0001": #lda
|
||||||
|
registerA = memory[par]
|
||||||
|
if v: print padBin(toBin(adress),bit=bits/2)+": LDA memory["+padBin(toBin(par),bit=bits/2)+"] > Register A"
|
||||||
|
elif cmd=="0010": #add
|
||||||
|
registerA = (registerA + memory[par]) % 2**bits
|
||||||
|
if v: print padBin(toBin(adress))+": ADD Register A + memory["+padBin(toBin(par),bit=bits/2)+"]"
|
||||||
|
elif cmd=="0011": #sub
|
||||||
|
registerA = (registerA - memory[par])
|
||||||
|
if registerA<0:
|
||||||
|
registerA = 2**bits+registerA
|
||||||
|
if v: print padBin(toBin(adress),bit=bits/2)+": SUB Register A - memory["+padBin(toBin(par),bit=bits/2)+"]"
|
||||||
|
elif cmd=="0100": #sta
|
||||||
|
memory[par] = registerA
|
||||||
|
if v: print padBin(toBin(adress),bit=bits/2)+": STA Register A > memory["+padBin(toBin(par),bit=bits/2)+"]"
|
||||||
|
elif cmd=="0101": #out
|
||||||
|
out = registerA
|
||||||
|
print("[OUT] "+padBin(toBin(out)))
|
||||||
|
if v: print padBin(toBin(adress),bit=bits/2)+": OUT Register A > out"
|
||||||
|
elif cmd=="0110": #jmp
|
||||||
|
pointer = par - 1
|
||||||
|
if v: print padBin(toBin(adress),bit=bits/2)+": JMP "+padBin(toBin(par),bit=bits/2)
|
||||||
|
elif cmd=="0111": #ldi
|
||||||
|
registerA = par
|
||||||
|
if v: print padBin(toBin(adress),bit=bits/2)+": LDI "+padBin(toBin(par),bit=bits/2)+" > Register A"
|
||||||
|
elif cmd=="1000": #jif
|
||||||
|
if registerA>=(2**bits)/2:
|
||||||
|
pointer = par - 1
|
||||||
|
if v: print padBin(toBin(adress),bit=bits/2)+": JIF {TRUE} > "+padBin(toBin(par),bit=bits/2)
|
||||||
|
else:
|
||||||
|
if v: print padBin(toBin(adress),bit=bits/2)+": JIF {FALSE} > "+padBin(toBin(par),bit=bits/2)
|
||||||
|
elif cmd=="1001": #hlo
|
||||||
|
registerA = memory[memory[par]]
|
||||||
|
elif cmd=="1010": #hst
|
||||||
|
memory[memory[par]] = registerA
|
||||||
|
elif cmd=="1011": #inp
|
||||||
|
if v: print padBin(toBin(adress),bit=bits/2)+": INP input > Register A"
|
||||||
|
registerA = bin_input("[INPUT]> ")
|
||||||
|
elif cmd=="1100":
|
||||||
|
pointer = registerA - 1
|
||||||
|
elif cmd=="1111": #hlt
|
||||||
|
return -1
|
||||||
|
return True
|
||||||
|
|
||||||
|
if __name__=="__main__":
|
||||||
|
while True:
|
||||||
|
x=raw_input("#> ")
|
||||||
|
if x=="help" or x=="h":
|
||||||
|
print("Help Screen goes here")
|
||||||
|
elif x=="docs":
|
||||||
|
print(doc[bits])
|
||||||
|
elif x=="write" or x=="w":
|
||||||
|
adress=bin_input("adress> ")
|
||||||
|
value=bin_input("value> ")
|
||||||
|
try:
|
||||||
|
memory[adress] = value
|
||||||
|
except:
|
||||||
|
print("[!] Unknown error while writing")
|
||||||
|
elif x=="read" or x=="r":
|
||||||
|
adress=bin_input("adress> ")
|
||||||
|
print(padBin(toBin(memory[adress])))
|
||||||
|
elif x=="dump" or x=="d":
|
||||||
|
for m in range(len(memory)):
|
||||||
|
print(padBin(toBin(m),bit=bits/2)+": "+padBin(toBin(memory[m])))
|
||||||
|
elif x=="dump32" or x=="d32":
|
||||||
|
for m in range(32):
|
||||||
|
print(padBin(toBin(m),bit=bits/2)+": "+padBin(toBin(memory[m])))
|
||||||
|
elif x=="dumpSpace" or x=="ds":
|
||||||
|
begin=bin_input("adress> ")
|
||||||
|
length=bin_input("length> ")
|
||||||
|
for m in range(length):
|
||||||
|
print(padBin(toBin(begin+m),bit=bits/2)+": "+padBin(toBin(memory[begin+m])))
|
||||||
|
elif x=="run" or x=="u":
|
||||||
|
while True:
|
||||||
|
cmd = padBin(toBin(memory[pointer]))[:(bits/2)]
|
||||||
|
par = int(toDec(padBin(toBin(memory[pointer]))[-1*(bits/2):]))
|
||||||
|
if parse(cmd,par)==-1:
|
||||||
|
pointer = 0
|
||||||
|
break
|
||||||
|
pointer = (pointer + 1) % bits
|
||||||
|
elif x=="edit" or x=="e":
|
||||||
|
adress=bin_input("offset> ",bit=bits/2)
|
||||||
|
while True:
|
||||||
|
value=bin_input(padBin(toBin(adress),bit=bits/2)+": "+padBin(toBin(memory[adress]))+" > ")
|
||||||
|
if value==-1:
|
||||||
|
break
|
||||||
|
try:
|
||||||
|
memory[adress] = value
|
||||||
|
except:
|
||||||
|
print("[!] Unknown error while writing")
|
||||||
|
if adress==len(memory)-1:
|
||||||
|
adress = -1
|
||||||
|
print("[*] End of mem")
|
||||||
|
adress = adress + 1
|
||||||
|
elif x=="asm":
|
||||||
|
adress=bin_input("offset> ",bit=bits/2)
|
||||||
|
while True:
|
||||||
|
print("[!] Not yet implemented error")
|
||||||
|
break
|
||||||
|
toWrite=raw_input(padBin(toBin(adress),bit=bits/2)+" > ")
|
||||||
|
cmd,par = toWrite.split(" ")
|
||||||
|
if value==-1:
|
||||||
|
break
|
||||||
|
try:
|
||||||
|
memory[adress] = value
|
||||||
|
except:
|
||||||
|
print("[!] Unknown error while writing")
|
||||||
|
adress = adress + 1
|
||||||
|
elif x=="cls":
|
||||||
|
os.system("cls")
|
||||||
|
elif x=="clear":
|
||||||
|
pointer = 0
|
||||||
|
registerA = 0
|
||||||
|
out = 0
|
||||||
|
memory = []
|
||||||
|
for i in range(2**(bits/2)):
|
||||||
|
memory.append(0)
|
||||||
|
elif x=="step" or x=="s":
|
||||||
|
cmd = padBin(toBin(memory[pointer]))[:(bits/2)]
|
||||||
|
par = int(toDec(padBin(toBin(memory[pointer]))[-1*(bits/2):]))
|
||||||
|
if parse(cmd,par,verbose=True,adress=pointer)==-1:
|
||||||
|
pointer = 0
|
||||||
|
print("[*] Programm halted")
|
||||||
|
pointer = (pointer + 1) % bits
|
||||||
|
elif x=="debug":
|
||||||
|
while True:
|
||||||
|
cmd = padBin(toBin(memory[pointer]))[:(bits/2)]
|
||||||
|
par = int(toDec(padBin(toBin(memory[pointer]))[-1*(bits/2):]))
|
||||||
|
if parse(cmd,par,verbose=True,adress=pointer)==-1:
|
||||||
|
pointer = -1
|
||||||
|
print("[*] Programm halted")
|
||||||
|
break
|
||||||
|
pointer = (pointer + 1) % (2**bits/2)
|
||||||
|
elif x=="pointer":
|
||||||
|
print("POINTER: "+padBin(toBin(pointer),bit=bits/2))
|
||||||
|
elif x=="setPointer":
|
||||||
|
pointer = bin_input("pointer> ",bit=bits/2)
|
||||||
|
elif x=="register":
|
||||||
|
print("REGISTER: "+padBin(toBin(registerA)))
|
||||||
|
elif x=="setRegister":
|
||||||
|
registerA = bin_input("register> ")
|
||||||
|
elif x=="save":
|
||||||
|
name = raw_input("name> ")
|
||||||
|
with open(name+".asmTron", 'wb') as f:
|
||||||
|
pickle.dump([bits,memory],f)
|
||||||
|
elif x=="load":
|
||||||
|
print("[*] Avaible saves:")
|
||||||
|
for f in os.listdir("."):
|
||||||
|
if f[-8:]==".asmTron":
|
||||||
|
print("> "+f[:-8])
|
||||||
|
name = raw_input("name> ")
|
||||||
|
with open(name+".asmTron", "rb") as f:
|
||||||
|
newBits,memory = pickle.load(f)
|
||||||
|
if not newBits==bits:
|
||||||
|
print("[!] Error: Uncompatible arch: "+str(newBits))
|
||||||
|
exit()
|
||||||
|
if not pointer==0:
|
||||||
|
print("[*] Pointer has been set to "+str(pointer))
|
||||||
|
elif x=="code":
|
||||||
|
data=bin_input("code> ")
|
||||||
|
cmd = padBin(toBin(memory[pointer]))[:(bits/2)]
|
||||||
|
par = int(toDec(padBin(toBin(memory[pointer]))[-1*(bits/2):]))
|
||||||
|
parse(cmd,par,verbose=True,adress=0)
|
||||||
|
else:
|
||||||
|
print("[!] Error")
|
605
compiler.py
Normal file
605
compiler.py
Normal file
@ -0,0 +1,605 @@
|
|||||||
|
import asm
|
||||||
|
|
||||||
|
def padBin(number,bit=8):
|
||||||
|
return asm.padBin(asm.toBin(number),bit=bit)
|
||||||
|
|
||||||
|
def toDec(binNumber):
|
||||||
|
return asm.toDec(binNumber)
|
||||||
|
|
||||||
|
doc = """
|
||||||
|
0000 NOP
|
||||||
|
0001 LDA memory > A
|
||||||
|
0010 ADD A + memspace
|
||||||
|
0011 SUB A - memspace
|
||||||
|
0100 STA A > memory
|
||||||
|
0101 OUT A > OUT
|
||||||
|
0110 JMP Jump to adress
|
||||||
|
0111 LDI par > A
|
||||||
|
1000 JIF Jump if greater
|
||||||
|
1001 JIE Jump if equal
|
||||||
|
1010 --- nop equivalent
|
||||||
|
1011 INP IN > A
|
||||||
|
1100 JMA Jump to A
|
||||||
|
1101 HLO memspace[memspace[par]] > A
|
||||||
|
1110 HST A > memspace[memspace[par]]
|
||||||
|
1111 HLT Halt
|
||||||
|
"""
|
||||||
|
|
||||||
|
def compile(cmd,par,memAddr):
|
||||||
|
# Every function creates unknown vars, even if it makes no sense in it case,
|
||||||
|
# because if the function sits inside a user-defined function vars may be
|
||||||
|
# defined later
|
||||||
|
try:
|
||||||
|
if cmd=="newVar": # creates new var and allocates memory-adress for it
|
||||||
|
print("Creating var '"+par[0].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()] = len(varRec)+(2**8)/2
|
||||||
|
print("[i] newVar is obsolete in this version of tronScript")
|
||||||
|
elif cmd=="newArray": # creates new array and allocates memory-adress for it
|
||||||
|
print("Creating array '"+par[0].upper()+"' at memory-space "+padBin(len(varRec)+(2**8)/2)+" - "+padBin(len(varRec)+int(par[1])+(2**8)/2))
|
||||||
|
for i in range(int(par[1])):
|
||||||
|
varRec["array_"par[0].upper()+"["+i+"]"] = len(varRec)+(2**8)/2
|
||||||
|
elif cmd=="jumpMark": # creates a jumpMark and saves current adress in the DB for later use by the jump-name-replacement
|
||||||
|
print("Creating jumpMark '"+par[0].upper()+"' at memory-address "+padBin(memAddr))
|
||||||
|
jumpMark[par[0].upper()] = memAddr
|
||||||
|
elif cmd=="jump": # creates dummy jump code for later replacement
|
||||||
|
# we have to do it this way, beacause a jumpMark may be defined after the jump-call
|
||||||
|
return [
|
||||||
|
# will be replaced after script-compilation
|
||||||
|
"Jump_"+par[0].upper()
|
||||||
|
]
|
||||||
|
elif cmd=="getVarAddress": # stores the memory-adress of a var into another
|
||||||
|
# maybe needed, if user want to access vars with
|
||||||
|
# custom asssembly code
|
||||||
|
try:
|
||||||
|
t = varRec[par[0].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[0].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()] = len(varRec)+(2**8)/2
|
||||||
|
try:
|
||||||
|
t = varRec[par[1].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[1].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[1].upper()] = len(varRec)+(2**8)/2
|
||||||
|
return [
|
||||||
|
## load the Var-Adress into A Register and save it into specified var
|
||||||
|
# LDI
|
||||||
|
toDec("0111"+padBin(varRec[par[0].upper()])),
|
||||||
|
# STA variable to store the current pointer in
|
||||||
|
toDec("0100"+padBin(varRec[par[1].upper()])),
|
||||||
|
]
|
||||||
|
elif cmd=="getArrayAddress": # like getVarAdress, but for arrays
|
||||||
|
# arrayName Index storeVar
|
||||||
|
try:
|
||||||
|
t = varRec["array_"par[0].upper()+"["+par[1]+"]"]
|
||||||
|
except:
|
||||||
|
print("Undefined Array")
|
||||||
|
exit()
|
||||||
|
try:
|
||||||
|
_ = int(par[1])
|
||||||
|
except:
|
||||||
|
print("Expected array-index (integer)")
|
||||||
|
exit()
|
||||||
|
return [
|
||||||
|
## load the Var-Adress into A Register and save it into specified var
|
||||||
|
# LDI
|
||||||
|
toDec("0111"+padBin(varRec["array_"par[0].upper()+"["+par[1]+"]"])),
|
||||||
|
# STA variable to store the current pointer in
|
||||||
|
toDec("0100"+padBin(varRec[par[2].upper()])),
|
||||||
|
]
|
||||||
|
elif cmd=="exec": # execute a user defined function
|
||||||
|
try:
|
||||||
|
t = varRec[par[0].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating function-jumpback-var '"+par[0].upper()+"_JUMPBACK' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()+"_JUMPBACK"] = len(varRec)+(2**8)/2
|
||||||
|
return [
|
||||||
|
## remember current pointer, so you can jump back here after function execution is done
|
||||||
|
# LDI
|
||||||
|
toDec("0111"+padBin(pointer)),
|
||||||
|
# STA variable to store the current pointer in
|
||||||
|
toDec("0100"+padBin(varRec[par[0].upper()+"_JUMPBACK"])),
|
||||||
|
# will be replaced after script-compilation
|
||||||
|
"Exec_"+par[0].upper()# jumpback function-call-var
|
||||||
|
]
|
||||||
|
elif cmd=="setVar": # gives a var a defined value
|
||||||
|
try:
|
||||||
|
t = varRec[par[0].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[0].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()] = len(varRec)+(2**8)/2
|
||||||
|
if par[1][0]=="b":
|
||||||
|
par[1] = toDec(par[1][1:])
|
||||||
|
return [
|
||||||
|
## loads desired value into the A register and then stores it at the variables memory adress
|
||||||
|
# LDI value
|
||||||
|
toDec("0111" + padBin(int(par[1]))),
|
||||||
|
# STA var Name
|
||||||
|
toDec("0100" + padBin(varRec[par[0].upper()]))
|
||||||
|
]
|
||||||
|
elif cmd=="copyVar": # gives a var the value of another var
|
||||||
|
try:
|
||||||
|
t = varRec[par[0].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[0].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()] = len(varRec)+(2**8)/2
|
||||||
|
try:
|
||||||
|
t = varRec[par[1].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[1].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[1].upper()] = len(varRec)+(2**8)/2
|
||||||
|
return [
|
||||||
|
## load memory-space of var 1 into register A and the store it into memory-space of var 2
|
||||||
|
# LDA var 1
|
||||||
|
toDec("0001" + padBin(varRec[par[0].upper()])),
|
||||||
|
# STA var 2
|
||||||
|
toDec("0100" + padBin(varRec[par[1].upper()]))
|
||||||
|
]
|
||||||
|
elif cmd=="setArrayIndex": # defines the value of an array (par0) index (par1) to be (par2)
|
||||||
|
try:
|
||||||
|
t = varRec["array_"par[0].upper()+"["+par[1]+"]"]
|
||||||
|
except:
|
||||||
|
print("Undefined array")
|
||||||
|
exit()
|
||||||
|
try:
|
||||||
|
_=int(par[1])
|
||||||
|
except:
|
||||||
|
print("Expected array-index (integer)")
|
||||||
|
exit()
|
||||||
|
if par[2][0]=="b":
|
||||||
|
par[2] = toDec(par[2][1:])
|
||||||
|
return [
|
||||||
|
## loads desired value into the A register and then stores it at the variables memory adress
|
||||||
|
# LDI value
|
||||||
|
toDec("0111" + padBin(int(par[2]))),
|
||||||
|
# STA var Name
|
||||||
|
toDec("0100" + padBin(varRec["array_"par[0].upper()+"["+par[1]+"]"]))
|
||||||
|
]
|
||||||
|
elif cmd=="getArrayIndex": # puts the value of an array (par0) index (par1) into var (par2)
|
||||||
|
try:
|
||||||
|
t = varRec["array_"par[0].upper()+"["+par[1]+"]"]
|
||||||
|
except:
|
||||||
|
print("Undefined Array")
|
||||||
|
exit()
|
||||||
|
try:
|
||||||
|
t = varRec[par[2].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[2].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[2].upper()] = len(varRec)+(2**8)/2
|
||||||
|
try:
|
||||||
|
_=int(par[1])
|
||||||
|
except:
|
||||||
|
print("Expected array-index (integer)")
|
||||||
|
exit()
|
||||||
|
return [
|
||||||
|
## load memory-space of var 1 into register A and the store it into memory-space of var 2
|
||||||
|
# LDA var 1
|
||||||
|
toDec("0001" + padBin(varRec["array_"par[0].upper()+"["+par[1]+"]"])),
|
||||||
|
# STA var 2
|
||||||
|
toDec("0100" + padBin(varRec[par[2].upper()]))
|
||||||
|
]
|
||||||
|
elif cmd=="write": # writes to the console
|
||||||
|
try:
|
||||||
|
t = varRec[par[0].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[0].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()] = len(varRec)+(2**8)/2
|
||||||
|
return [
|
||||||
|
## load the variable into register A, then print it
|
||||||
|
# LDA var Name
|
||||||
|
toDec("0001" + padBin(varRec[par[0].upper()])),
|
||||||
|
# OUT null
|
||||||
|
toDec("0101" + "00000000")
|
||||||
|
]
|
||||||
|
elif cmd=="read": # reads from the console
|
||||||
|
try:
|
||||||
|
t = varRec[par[0].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[0].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()] = len(varRec)+(2**8)/2
|
||||||
|
return [
|
||||||
|
## input > register A; register A > var
|
||||||
|
# INP null
|
||||||
|
toDec("1011" + "00000000"),
|
||||||
|
# STA var Name
|
||||||
|
toDec("0100" + padBin(varRec[par[0].upper()]))
|
||||||
|
]
|
||||||
|
elif cmd=="add": # adds to numbers and stores the answer in another
|
||||||
|
try:
|
||||||
|
t = varRec[par[0].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[0].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()] = len(varRec)+(2**8)/2
|
||||||
|
try:
|
||||||
|
t = varRec[par[1].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[1].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[1].upper()] = len(varRec)+(2**8)/2
|
||||||
|
try:
|
||||||
|
t = varRec[par[2].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[2].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[2].upper()] = len(varRec)+(2**8)/2
|
||||||
|
return [
|
||||||
|
## load var 1 into register A; add var 2, store in var 3
|
||||||
|
# LDA var Name in1
|
||||||
|
toDec("0001" + padBin(varRec[par[0].upper()])),
|
||||||
|
# ADD var Name in2
|
||||||
|
toDec("0010" + padBin(varRec[par[1].upper()])),
|
||||||
|
# STA var Name out
|
||||||
|
toDec("0100" + padBin(varRec[par[2].upper()]))
|
||||||
|
]
|
||||||
|
elif cmd=="subtract": # in1 - in2 = out
|
||||||
|
try:
|
||||||
|
t = varRec[par[0].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[0].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()] = len(varRec)+(2**8)/2
|
||||||
|
try:
|
||||||
|
t = varRec[par[1].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[1].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[1].upper()] = len(varRec)+(2**8)/2
|
||||||
|
try:
|
||||||
|
t = varRec[par[2].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[2].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[2].upper()] = len(varRec)+(2**8)/2
|
||||||
|
return [
|
||||||
|
## laod var 1 into register A; subtract var 2; store in var 3
|
||||||
|
# LDA var Name in1
|
||||||
|
toDec("0001" + padBin(varRec[par[0]])),
|
||||||
|
# SUB var Name in2
|
||||||
|
toDec("0011" + padBin(varRec[par[1]])),
|
||||||
|
# STA var Name out
|
||||||
|
toDec("0100" + padBin(varRec[par[2]]))
|
||||||
|
]
|
||||||
|
elif cmd=="if": # if clause
|
||||||
|
#cmd p0 p1 p2 p3 p4:
|
||||||
|
#if VAR ?? VAR cmd pars
|
||||||
|
try:
|
||||||
|
t = varRec[par[0].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[0].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()] = len(varRec)+(2**8)/2
|
||||||
|
try:
|
||||||
|
t = varRec[par[2].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[2].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[2].upper()] = len(varRec)+(2**8)/2
|
||||||
|
comp1=par[0].upper()
|
||||||
|
comp2=par[2].upper()
|
||||||
|
comp=par[1]
|
||||||
|
shellcode=compile(par[3],par[4:],memAddr+4)
|
||||||
|
if not shellcode:
|
||||||
|
return ["Error","I had problems getting the internal shellcode. Sorry :("]
|
||||||
|
if shellcode[0]=="Error":
|
||||||
|
return ["Error","Internal shellcode compilation thew an Error:\n"+shellcode[1]]
|
||||||
|
## binary code that magicaly works:
|
||||||
|
if comp=="==":
|
||||||
|
ret = [
|
||||||
|
# LDI konst
|
||||||
|
toDec("0111" + "10000000"),
|
||||||
|
# STA temp
|
||||||
|
toDec("0100" + "11111111"),
|
||||||
|
# LDA comp1
|
||||||
|
toDec("0001" + padBin(varRec[comp1])),
|
||||||
|
# ADD konst
|
||||||
|
toDec("0010" + "11111111"),
|
||||||
|
# SUB comp2
|
||||||
|
toDec("0011" + padBin(varRec[comp2])),
|
||||||
|
# JIE
|
||||||
|
toDec("1001" + padBin(memAddr+7)),
|
||||||
|
# JMP
|
||||||
|
toDec("0110" + padBin(memAddr+7+len(shellcode)))
|
||||||
|
]
|
||||||
|
elif comp=="!=":
|
||||||
|
ret = [
|
||||||
|
# LDI konst
|
||||||
|
toDec("0111" + "10000000"),
|
||||||
|
# STA temp
|
||||||
|
toDec("0100" + "11111111"),
|
||||||
|
# LDA comp1
|
||||||
|
toDec("0001" + padBin(varRec[comp1])),
|
||||||
|
# ADD konst
|
||||||
|
toDec("0010" + "11111111"),
|
||||||
|
# SUB comp2
|
||||||
|
toDec("0011" + padBin(varRec[comp2])),
|
||||||
|
# JIE
|
||||||
|
toDec("1001" + padBin(memAddr+6+len(shellcode)))
|
||||||
|
]
|
||||||
|
elif comp==">=":
|
||||||
|
ret = [
|
||||||
|
# LDI konst
|
||||||
|
toDec("0111" + "10000000"),
|
||||||
|
# STA temp
|
||||||
|
toDec("0100" + "11111111"),
|
||||||
|
# LDA comp1
|
||||||
|
toDec("0001" + padBin(varRec[comp1])),
|
||||||
|
# ADD konst
|
||||||
|
toDec("0010" + "11111111"),
|
||||||
|
# SUB comp2
|
||||||
|
toDec("0011" + padBin(varRec[comp2])),
|
||||||
|
# JIF
|
||||||
|
toDec("1000" + padBin(memAddr+6+len(shellcode)))
|
||||||
|
]
|
||||||
|
elif comp==">":
|
||||||
|
ret = [
|
||||||
|
# LDI konst
|
||||||
|
toDec("0111" + "01111111"),
|
||||||
|
# STA temp
|
||||||
|
toDec("0100" + "11111111"),
|
||||||
|
# LDA comp1
|
||||||
|
toDec("0001" + padBin(varRec[comp1])),
|
||||||
|
# ADD konst
|
||||||
|
toDec("0010" + "11111111"),
|
||||||
|
# SUB comp2
|
||||||
|
toDec("0011" + padBin(varRec[comp2])),
|
||||||
|
# JIF
|
||||||
|
toDec("1000" + padBin(memAddr+6+len(shellcode)))
|
||||||
|
]
|
||||||
|
elif comp=="<=":
|
||||||
|
ret = [
|
||||||
|
# LDI konst
|
||||||
|
toDec("0111" + "10000000"),
|
||||||
|
# STA temp
|
||||||
|
toDec("0100" + "11111111"),
|
||||||
|
# LDA comp2
|
||||||
|
toDec("0001" + padBin(varRec[comp2])),
|
||||||
|
# ADD konst
|
||||||
|
toDec("0010" + "11111111"),
|
||||||
|
# SUB comp1
|
||||||
|
toDec("0011" + padBin(varRec[comp1])),
|
||||||
|
# JIF
|
||||||
|
toDec("1000" + padBin(memAddr+6+len(shellcode)))
|
||||||
|
]
|
||||||
|
elif comp=="<":
|
||||||
|
ret = [
|
||||||
|
# LDI konst
|
||||||
|
toDec("0111" + "01111111"),
|
||||||
|
# STA temp
|
||||||
|
toDec("0100" + "11111111"),
|
||||||
|
# LDA comp2
|
||||||
|
toDec("0001" + padBin(varRec[comp2])),
|
||||||
|
# ADD konst
|
||||||
|
toDec("0010" + "11111111"),
|
||||||
|
# SUB comp1
|
||||||
|
toDec("0011" + padBin(varRec[comp1])),
|
||||||
|
# JIF
|
||||||
|
toDec("1000" + padBin(memAddr+6+len(shellcode)))
|
||||||
|
]
|
||||||
|
for s in shellcode:
|
||||||
|
ret.append(s)
|
||||||
|
return ret
|
||||||
|
elif cmd=="for": # for loop: "for [goal] [counter] [step] [cmd] [pars]"
|
||||||
|
# == for [counter] in range(0,[goal],[step]):
|
||||||
|
# [cmd]([pars])
|
||||||
|
try:
|
||||||
|
t = varRec[par[0].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[0].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()] = len(varRec)+(2**8)/2
|
||||||
|
try:
|
||||||
|
t = varRec[par[1].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[1].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[1].upper()] = len(varRec)+(2**8)/2
|
||||||
|
try:
|
||||||
|
t = varRec[par[2].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[2].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[2].upper()] = len(varRec)+(2**8)/2
|
||||||
|
goal = par[0].upper()
|
||||||
|
clock = par[1].upper()
|
||||||
|
step = par[2].upper()
|
||||||
|
shellcode=compile(par[3],par[4:],memAddr+12)
|
||||||
|
ret = [
|
||||||
|
# LDI konst
|
||||||
|
toDec("0111" + "00000000"),
|
||||||
|
# STA temp
|
||||||
|
toDec("0100" + padBin(varRec[clock])),
|
||||||
|
# LDI konst <-- jump back to
|
||||||
|
toDec("0111" + "10000000"),
|
||||||
|
# STA temp
|
||||||
|
toDec("0100" + "11111111"),
|
||||||
|
# LDA clock
|
||||||
|
toDec("0001" + padBin(varRec[clock])),
|
||||||
|
# ADD konst
|
||||||
|
toDec("0010" + "11111111"),
|
||||||
|
# SUB goal
|
||||||
|
toDec("0011" + padBin(varRec[goal])),
|
||||||
|
# JIE
|
||||||
|
toDec("1001" + padBin(memAddr+12+len(shellcode)+1)),
|
||||||
|
# LDA clock
|
||||||
|
toDec("0001" + padBin(varRec[goal])),
|
||||||
|
# ADD step
|
||||||
|
toDec("0010" + padBin(varRec[step])),
|
||||||
|
# STA clock
|
||||||
|
toDec("0100" + padBin(varRec[goal]))
|
||||||
|
]
|
||||||
|
for s in shellcode:
|
||||||
|
ret.append(s)
|
||||||
|
# JMP
|
||||||
|
ret.append(toDec("0110" + padBin(memAddr+2)))
|
||||||
|
return ret
|
||||||
|
elif cmd=="asm" or cmd[0]==":": # compiles assembler to machine-code
|
||||||
|
if cmd=="asm":
|
||||||
|
opcode = par[0]
|
||||||
|
param = par[1]
|
||||||
|
else:
|
||||||
|
opcode = cmd[1:]
|
||||||
|
param = par[0]
|
||||||
|
if param[:2]=="b#":
|
||||||
|
param = toDec(param[2:])
|
||||||
|
elif param[:2]=="d#":
|
||||||
|
param = param[2:]
|
||||||
|
elif param[:2]=="v#":
|
||||||
|
try:
|
||||||
|
t = varRec[param[2:].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+param[2:].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[param[2:].upper()] = len(varRec)+(2**8)/2
|
||||||
|
param = varRec[param[2:].upper()]
|
||||||
|
elif param in ["-","none"]:
|
||||||
|
param = toDec("00000000")
|
||||||
|
elif param in ["~","all"]:
|
||||||
|
param = toDec("11111111")
|
||||||
|
else:
|
||||||
|
return ["Error","Invalid param supplied to asm-command (no valid prefix found)"]
|
||||||
|
for a in range(len(asm.asmMap)):
|
||||||
|
if asm.asmMap[a]==opcode:
|
||||||
|
print("Found matching opcode")
|
||||||
|
return [toDec(padBin(a,bit=4)+padBin(param))]
|
||||||
|
elif cmd=="shellcode": # insertes raw shellcode into the programm
|
||||||
|
return [toDec("".join(par))]
|
||||||
|
elif cmd=="nop": # nothing...
|
||||||
|
return [toDec("0000" + "00000000")]
|
||||||
|
elif cmd=="halt": # halts execution of programm
|
||||||
|
return [toDec("1111" + "11111111")]
|
||||||
|
else:
|
||||||
|
return ["Error","Unimplemented Function: "+str(cmd)]
|
||||||
|
except Exception as e:
|
||||||
|
return ["Error","Unknown Error: "+str(e)]
|
||||||
|
|
||||||
|
bits = 12
|
||||||
|
|
||||||
|
varRec = {} # to store VarName -> memory-address
|
||||||
|
jumpMark = {} # to store jumpMarkName -> jump-address
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print("[tronScript-Compiler by CyberCodeDragon]")
|
||||||
|
memory = []
|
||||||
|
for i in range(2**bits): #expand memory
|
||||||
|
memory.append(0)
|
||||||
|
|
||||||
|
pointer = 0
|
||||||
|
funcTable = [] # [[funcName,funcCode],...]
|
||||||
|
|
||||||
|
for f in asm.os.listdir("tronScript"): #display all found scripts
|
||||||
|
if f[-5:]==".tron":
|
||||||
|
print("> "+"tronScript/"+f)
|
||||||
|
filePath = raw_input("file to compile> ")
|
||||||
|
inFile = filePath.split("\\")
|
||||||
|
inFile = inFile[len(inFile)-1]
|
||||||
|
tmp = inFile.split(".")
|
||||||
|
ext = tmp[len(tmp)-1]
|
||||||
|
name = inFile.split("."+ext)[0]
|
||||||
|
print("[*] Reading "+inFile)
|
||||||
|
with open(filePath) as f:
|
||||||
|
code = f.read().replace(" ","") #read file and remove tabs
|
||||||
|
c = code.split("\n") #c = lines of code
|
||||||
|
#detect functions
|
||||||
|
for l in range(len(c)): #for every line of code
|
||||||
|
elems = c[l].split(" ") #elems = [cmd,pa1,par2,...,parN]
|
||||||
|
if elems[0]=="function":
|
||||||
|
funcName = elems[1]
|
||||||
|
funcCode = code.split("function "+funcName+" {")[1].split("}")[0].split("\n") #get the function Code
|
||||||
|
funcName = funcName.upper()
|
||||||
|
print("detected function '"+funcName+"' in line "+str(l)+":\n"+"\n".join(funcCode))
|
||||||
|
for i in range(len(funcCode)+0):
|
||||||
|
c[l+i]="#function-code" #replace function with a comment -> dont mess up later parsing
|
||||||
|
funcTable.append([funcName,funcCode])
|
||||||
|
try: #create jumpback-var if not existing (does probably not exist)
|
||||||
|
t = varRec[funcName+"_JUMPBACK"]
|
||||||
|
except:
|
||||||
|
print("Creating function-jumpback-var '"+funcName+"_JUMPBACK' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[funcName+"_JUMPBACK"] = len(varRec)+(2**8)/2
|
||||||
|
|
||||||
|
#parse code
|
||||||
|
for l in range(len(c)): #for every line in code
|
||||||
|
if c[l] not in ["","\n"]: #if lines is not empty
|
||||||
|
print("Parsing line "+str(l)+": "+c[l])
|
||||||
|
cmd = c[l].split(" ")[0]
|
||||||
|
if not cmd[0]=="#": #dont parse comments
|
||||||
|
if cmd in ["halt","nop"]: #cmds without params
|
||||||
|
par = []
|
||||||
|
else:
|
||||||
|
par = c[l].split(cmd+" ")[1].split(" ")
|
||||||
|
r = compile(cmd,par,pointer) #compile
|
||||||
|
if r:
|
||||||
|
if r[0]=="Error": #print error
|
||||||
|
print("Error compiling line "+str(l)+" {"+c[l]+"}: \n"+str(r[1]))
|
||||||
|
exit()
|
||||||
|
for ret in r:
|
||||||
|
veryVerbose = False
|
||||||
|
if veryVerbose:
|
||||||
|
try:
|
||||||
|
print("Compiled to: "+padBin(ret,bit=12))
|
||||||
|
except:
|
||||||
|
print("Compiled to: "+str(ret))
|
||||||
|
memory[pointer] = ret #write code to memory
|
||||||
|
pointer+=1
|
||||||
|
try:
|
||||||
|
if padBin(memory[pointer-1],bit=12)[:4]!="1111": #if the last command is not a HLT command
|
||||||
|
print("Appending HLT-Command")
|
||||||
|
# HLT
|
||||||
|
memory[pointer] = toDec("1111"+"11111111")
|
||||||
|
print("HLT at "+padBin(pointer)+": "+padBin(memory[pointer],bit=12))
|
||||||
|
pointer+=1
|
||||||
|
except: #looks like the last command was a placeholder: not a HLT command
|
||||||
|
print("Appending HLT-Command")
|
||||||
|
# HLT
|
||||||
|
memory[pointer] = toDec("1111"+"11111111")
|
||||||
|
print("HLT at "+padBin(pointer)+": "+padBin(memory[pointer],bit=12))
|
||||||
|
pointer+=1
|
||||||
|
|
||||||
|
#replace jump-names
|
||||||
|
for m in range(len(memory)):
|
||||||
|
try:
|
||||||
|
if memory[m][:5]=="Jump_": #if at memory-adress is just a jump-dummy
|
||||||
|
print("Inserting Jump-Adress at "+padBin(m,bit=12))
|
||||||
|
memory[m]=toDec("0110"+padBin(jumpMark[memory[m][5:]])) #replace with real jump adress
|
||||||
|
except:
|
||||||
|
t=1
|
||||||
|
#append function-shellcode
|
||||||
|
funcMapper = {} #map funcName to funcCode
|
||||||
|
for f in range(len(funcTable)):
|
||||||
|
print("Appending shellcode for function "+str(funcTable[f][0])+" at adress "+padBin(pointer))
|
||||||
|
funcMapper[funcTable[f][0]] = pointer #remember where you put dat function
|
||||||
|
for l in range(len(funcTable[f][1])):
|
||||||
|
if funcTable[f][1][l] not in ["","halt","nop"]:
|
||||||
|
ret=compile(funcTable[f][1][l].split(" ")[0],funcTable[f][1][l].split(" ")[1:],pointer)
|
||||||
|
try:
|
||||||
|
if ret[0]=="Error":
|
||||||
|
print("An Error occured, when compiling function shellcode for function '"+funcTable[f][0]+"':\n"+str(e))
|
||||||
|
else:
|
||||||
|
for r in range(len(ret)):
|
||||||
|
memory[pointer]=ret[r] #append compiled function memory
|
||||||
|
pointer+=1
|
||||||
|
except:
|
||||||
|
t=1
|
||||||
|
## jump back after function execution
|
||||||
|
# LDI konst=3
|
||||||
|
memory[pointer] = toDec("0111" + "00000011")
|
||||||
|
pointer+=1
|
||||||
|
# ADD jumpBackVar
|
||||||
|
memory[pointer] = toDec("0010" + padBin(varRec[funcTable[f][0]+"_JUMPBACK"]))
|
||||||
|
pointer+=1
|
||||||
|
# JMA null
|
||||||
|
memory[pointer] = toDec("1100" + "00000000")
|
||||||
|
pointer+=1
|
||||||
|
print("FuncMapper: "+str(funcMapper))
|
||||||
|
print("Var Assignment:")
|
||||||
|
varAss = str(varRec)[1:][:-1].split(", ")
|
||||||
|
print("#VAR-NAME# #ADDRESS#")
|
||||||
|
for va in range(len(varAss)): #print all vars with their memory-location
|
||||||
|
print(varAss[va].split("'")[1]+": "+" "*(12-len(varAss[va].split("'")[1]))+padBin(int(varAss[va].split("': ")[1])))
|
||||||
|
#replace func-names
|
||||||
|
for m in range(len(memory)):
|
||||||
|
try:
|
||||||
|
if memory[m][:5]=="Exec_": #if there is just a function-call-dummy
|
||||||
|
print("Inserting Exec-Call-Adress at "+padBin(m,bit=12))
|
||||||
|
memory[m]=toDec("0110"+padBin(funcMapper[memory[m][5:]])) #replace it with a jump to the function
|
||||||
|
except Exception as e:
|
||||||
|
if str(e) != "'int' object has no attribute '__getitem__'":
|
||||||
|
print "Error while replacing func-name: "+str(e)+"\n (I don't know why, sorry)"
|
||||||
|
try:
|
||||||
|
print("Removing old "+name+".asmTron")
|
||||||
|
os.remove(name+".asmTron")
|
||||||
|
except:
|
||||||
|
t=1
|
||||||
|
print("Saving as "+name+".asmTron")
|
||||||
|
with open(name+".asmTron", 'wb') as f:
|
||||||
|
t=asm.pickle.dumps([12,memory])
|
||||||
|
t=asm.zlib.compress(t)
|
||||||
|
f.write(t)
|
||||||
|
print("Done.")
|
658
compiler_beta.py
Normal file
658
compiler_beta.py
Normal file
@ -0,0 +1,658 @@
|
|||||||
|
import asm
|
||||||
|
|
||||||
|
def padBin(number,bit=8):
|
||||||
|
return asm.padBin(asm.toBin(number),bit=bit)
|
||||||
|
|
||||||
|
def toDec(binNumber):
|
||||||
|
return asm.toDec(binNumber)
|
||||||
|
|
||||||
|
doc = """
|
||||||
|
0000 NOP
|
||||||
|
0001 LDA memory > A
|
||||||
|
0010 ADD A + memspace
|
||||||
|
0011 SUB A - memspace
|
||||||
|
0100 STA A > memory
|
||||||
|
0101 OUT A > OUT
|
||||||
|
0110 JMP Jump to adress
|
||||||
|
0111 LDI par > A
|
||||||
|
1000 JIF Jump if greater
|
||||||
|
1001 JIE Jump if equal
|
||||||
|
1010 LDP memory[A] > A
|
||||||
|
1011 INP IN > A
|
||||||
|
1100 JMA Jump to A
|
||||||
|
1101 --- not implemented
|
||||||
|
1110 STP memory[par] > memory[A]
|
||||||
|
1111 HLT Halt
|
||||||
|
"""
|
||||||
|
|
||||||
|
def compile(cmd,par,memAddr):
|
||||||
|
# Every function creates unknown vars, even if it makes no sense in it case,
|
||||||
|
# because if the function sits inside a user-defined function vars may be
|
||||||
|
# defined later
|
||||||
|
#try:
|
||||||
|
if cmd=="newVar": # creates new var and allocates memory-adress for it
|
||||||
|
print("Creating var '"+par[0].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()] = len(varRec)+(2**8)/2
|
||||||
|
print("[i] newVar is obsolete in this version of tronScript")
|
||||||
|
elif cmd=="newArray": # creates new array and allocates memory-adress for it
|
||||||
|
print("Creating array '"+par[0].upper()+"' at memory-space "+padBin(len(varRec)+(2**8)/2)+" - "+padBin(len(varRec)+int(par[1])+(2**8)/2))
|
||||||
|
for i in range(int(par[1])):
|
||||||
|
varRec["array_"+par[0].upper()+"["+i+"]"] = len(varRec)+(2**8)/2
|
||||||
|
elif cmd=="jumpMark": # creates a jumpMark and saves current adress in the DB for later use by the jump-name-replacement
|
||||||
|
print("Creating jumpMark '"+par[0].upper()+"' at memory-address "+padBin(memAddr))
|
||||||
|
jumpMark[par[0].upper()] = memAddr
|
||||||
|
elif cmd=="jump": # creates dummy jump code for later replacement
|
||||||
|
# we have to do it this way, beacause a jumpMark may be defined after the jump-call
|
||||||
|
return [
|
||||||
|
# will be replaced after script-compilation
|
||||||
|
"Jump_"+par[0].upper()
|
||||||
|
]
|
||||||
|
elif cmd=="getVarAddress": # stores the memory-adress of a var into another
|
||||||
|
# maybe needed, if user want to access vars with
|
||||||
|
# custom asssembly code
|
||||||
|
try:
|
||||||
|
t = varRec[par[0].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[0].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()] = len(varRec)+(2**8)/2
|
||||||
|
try:
|
||||||
|
t = varRec[par[1].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[1].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[1].upper()] = len(varRec)+(2**8)/2
|
||||||
|
return [
|
||||||
|
## load the Var-Adress into A Register and save it into specified var
|
||||||
|
# LDI
|
||||||
|
toDec("0111"+padBin(varRec[par[0].upper()])),
|
||||||
|
# STA variable to store the current pointer in
|
||||||
|
toDec("0100"+padBin(varRec[par[1].upper()])),
|
||||||
|
]
|
||||||
|
elif cmd=="getArrayAddress": # like getVarAdress, but for arrays
|
||||||
|
# arrayName Index storeVar
|
||||||
|
try:
|
||||||
|
t = varRec["array_"+par[0].upper()+"["+par[1]+"]"]
|
||||||
|
except:
|
||||||
|
print("Undefined Array")
|
||||||
|
exit()
|
||||||
|
try:
|
||||||
|
_ = int(par[1])
|
||||||
|
except:
|
||||||
|
print("Expected array-index (integer)")
|
||||||
|
exit()
|
||||||
|
return [
|
||||||
|
## load the Var-Adress into A Register and save it into specified var
|
||||||
|
# LDI
|
||||||
|
toDec("0111"+padBin(varRec["array_"+par[0].upper()+"["+par[1]+"]"])),
|
||||||
|
# STA variable to store the current pointer in
|
||||||
|
toDec("0100"+padBin(varRec[par[2].upper()])),
|
||||||
|
]
|
||||||
|
elif cmd=="exec": # execute a user defined function
|
||||||
|
try:
|
||||||
|
t = varRec[par[0].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating function-jumpback-var '"+par[0].upper()+"_JUMPBACK' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()+"_JUMPBACK"] = len(varRec)+(2**8)/2
|
||||||
|
return [
|
||||||
|
## remember current pointer, so you can jump back here after function execution is done
|
||||||
|
# LDI
|
||||||
|
toDec("0111"+padBin(pointer)),
|
||||||
|
# STA variable to store the current pointer in
|
||||||
|
toDec("0100"+padBin(varRec[par[0].upper()+"_JUMPBACK"])),
|
||||||
|
# will be replaced after script-compilation
|
||||||
|
"Exec_"+par[0].upper()# jumpback function-call-var
|
||||||
|
]
|
||||||
|
elif cmd=="setVar": # gives a var a defined value
|
||||||
|
try:
|
||||||
|
t = varRec[par[0].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[0].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()] = len(varRec)+(2**8)/2
|
||||||
|
if par[1][0]=="b":
|
||||||
|
par[1] = toDec(par[1][1:])
|
||||||
|
return [
|
||||||
|
## loads desired value into the A register and then stores it at the variables memory adress
|
||||||
|
# LDI value
|
||||||
|
toDec("0111" + padBin(int(par[1]))),
|
||||||
|
# STA var Name
|
||||||
|
toDec("0100" + padBin(varRec[par[0].upper()]))
|
||||||
|
]
|
||||||
|
elif cmd=="copyVar": # gives a var the value of another var
|
||||||
|
try:
|
||||||
|
t = varRec[par[0].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[0].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()] = len(varRec)+(2**8)/2
|
||||||
|
try:
|
||||||
|
t = varRec[par[1].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[1].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[1].upper()] = len(varRec)+(2**8)/2
|
||||||
|
return [
|
||||||
|
## load memory-space of var 1 into register A and the store it into memory-space of var 2
|
||||||
|
# LDA var 1
|
||||||
|
toDec("0001" + padBin(varRec[par[0].upper()])),
|
||||||
|
# STA var 2
|
||||||
|
toDec("0100" + padBin(varRec[par[1].upper()]))
|
||||||
|
]
|
||||||
|
elif cmd=="setArrayIndex": # defines the value of an array (par0) index (par1) to be (par2)
|
||||||
|
try:
|
||||||
|
t = varRec["array_"+par[0].upper()+"["+par[1]+"]"]
|
||||||
|
except:
|
||||||
|
print("Undefined array")
|
||||||
|
exit()
|
||||||
|
try:
|
||||||
|
_=int(par[1])
|
||||||
|
except:
|
||||||
|
print("Expected array-index (integer)")
|
||||||
|
exit()
|
||||||
|
if par[2][0]=="b":
|
||||||
|
par[2] = toDec(par[2][1:])
|
||||||
|
return [
|
||||||
|
## loads desired value into the A register and then stores it at the variables memory adress
|
||||||
|
# LDI value
|
||||||
|
toDec("0111" + padBin(int(par[2]))),
|
||||||
|
# STA var Name
|
||||||
|
toDec("0100" + padBin(varRec["array_"+par[0].upper()+"["+par[1]+"]"]))
|
||||||
|
]
|
||||||
|
elif cmd=="getArrayIndex": # puts the value of an array (par0) index (par1) into var (par2)
|
||||||
|
try:
|
||||||
|
t = varRec["array_"+par[0].upper()+"["+par[1]+"]"]
|
||||||
|
except:
|
||||||
|
print("Undefined Array")
|
||||||
|
exit()
|
||||||
|
try:
|
||||||
|
t = varRec[par[2].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[2].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[2].upper()] = len(varRec)+(2**8)/2
|
||||||
|
try:
|
||||||
|
_=int(par[1])
|
||||||
|
except:
|
||||||
|
print("Expected array-index (integer)")
|
||||||
|
exit()
|
||||||
|
return [
|
||||||
|
## load memory-space of var 1 into register A and the store it into memory-space of var 2
|
||||||
|
# LDA var 1
|
||||||
|
toDec("0001" + padBin(varRec["array_"+par[0].upper()+"["+par[1]+"]"])),
|
||||||
|
# STA var 2
|
||||||
|
toDec("0100" + padBin(varRec[par[2].upper()]))
|
||||||
|
]
|
||||||
|
elif cmd=="write": # writes to the console
|
||||||
|
try:
|
||||||
|
t = varRec[par[0].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[0].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()] = len(varRec)+(2**8)/2
|
||||||
|
return [
|
||||||
|
## load the variable into register A, then print it
|
||||||
|
# LDA var Name
|
||||||
|
toDec("0001" + padBin(varRec[par[0].upper()])),
|
||||||
|
# OUT null
|
||||||
|
toDec("0101" + "00000000")
|
||||||
|
]
|
||||||
|
elif cmd=="read": # reads from the console
|
||||||
|
try:
|
||||||
|
t = varRec[par[0].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[0].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()] = len(varRec)+(2**8)/2
|
||||||
|
return [
|
||||||
|
## input > register A; register A > var
|
||||||
|
# INP null
|
||||||
|
toDec("1011" + "00000000"),
|
||||||
|
# STA var Name
|
||||||
|
toDec("0100" + padBin(varRec[par[0].upper()]))
|
||||||
|
]
|
||||||
|
elif cmd=="add": # adds to numbers and stores the answer in another
|
||||||
|
try:
|
||||||
|
t = varRec[par[0].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[0].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()] = len(varRec)+(2**8)/2
|
||||||
|
try:
|
||||||
|
t = varRec[par[1].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[1].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[1].upper()] = len(varRec)+(2**8)/2
|
||||||
|
try:
|
||||||
|
t = varRec[par[2].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[2].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[2].upper()] = len(varRec)+(2**8)/2
|
||||||
|
return [
|
||||||
|
## load var 1 into register A; add var 2, store in var 3
|
||||||
|
# LDA var Name in1
|
||||||
|
toDec("0001" + padBin(varRec[par[0].upper()])),
|
||||||
|
# ADD var Name in2
|
||||||
|
toDec("0010" + padBin(varRec[par[1].upper()])),
|
||||||
|
# STA var Name out
|
||||||
|
toDec("0100" + padBin(varRec[par[2].upper()]))
|
||||||
|
]
|
||||||
|
elif cmd=="subtract": # in1 - in2 = out
|
||||||
|
try:
|
||||||
|
t = varRec[par[0].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[0].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()] = len(varRec)+(2**8)/2
|
||||||
|
try:
|
||||||
|
t = varRec[par[1].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[1].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[1].upper()] = len(varRec)+(2**8)/2
|
||||||
|
try:
|
||||||
|
t = varRec[par[2].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[2].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[2].upper()] = len(varRec)+(2**8)/2
|
||||||
|
return [
|
||||||
|
## laod var 1 into register A; subtract var 2; store in var 3
|
||||||
|
# LDA var Name in1
|
||||||
|
toDec("0001" + padBin(varRec[par[0]])),
|
||||||
|
# SUB var Name in2
|
||||||
|
toDec("0011" + padBin(varRec[par[1]])),
|
||||||
|
# STA var Name out
|
||||||
|
toDec("0100" + padBin(varRec[par[2]]))
|
||||||
|
]
|
||||||
|
elif cmd=="if": # if clause
|
||||||
|
#cmd p0 p1 p2 p3 p4:
|
||||||
|
#if VAR ?? VAR cmd pars
|
||||||
|
try:
|
||||||
|
t = varRec[par[0].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[0].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()] = len(varRec)+(2**8)/2
|
||||||
|
try:
|
||||||
|
t = varRec[par[2].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[2].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[2].upper()] = len(varRec)+(2**8)/2
|
||||||
|
comp1=par[0].upper()
|
||||||
|
comp2=par[2].upper()
|
||||||
|
comp=par[1]
|
||||||
|
shellcode=compile(par[3],par[4:],memAddr+4)
|
||||||
|
if not shellcode:
|
||||||
|
return ["Error","I had problems getting the internal shellcode. Sorry :("]
|
||||||
|
if shellcode[0]=="Error":
|
||||||
|
return ["Error","Internal shellcode compilation thew an Error:\n"+shellcode[1]]
|
||||||
|
## binary code that magicaly works:
|
||||||
|
if comp=="==":
|
||||||
|
ret = [
|
||||||
|
# LDI konst
|
||||||
|
toDec("0111" + "10000000"),
|
||||||
|
# STA temp
|
||||||
|
toDec("0100" + "11111111"),
|
||||||
|
# LDA comp1
|
||||||
|
toDec("0001" + padBin(varRec[comp1])),
|
||||||
|
# ADD konst
|
||||||
|
toDec("0010" + "11111111"),
|
||||||
|
# SUB comp2
|
||||||
|
toDec("0011" + padBin(varRec[comp2])),
|
||||||
|
# JIE
|
||||||
|
toDec("1001" + padBin(memAddr+7)),
|
||||||
|
# JMP
|
||||||
|
toDec("0110" + padBin(memAddr+7+len(shellcode)))
|
||||||
|
]
|
||||||
|
elif comp=="!=":
|
||||||
|
ret = [
|
||||||
|
# LDI konst
|
||||||
|
toDec("0111" + "10000000"),
|
||||||
|
# STA temp
|
||||||
|
toDec("0100" + "11111111"),
|
||||||
|
# LDA comp1
|
||||||
|
toDec("0001" + padBin(varRec[comp1])),
|
||||||
|
# ADD konst
|
||||||
|
toDec("0010" + "11111111"),
|
||||||
|
# SUB comp2
|
||||||
|
toDec("0011" + padBin(varRec[comp2])),
|
||||||
|
# JIE
|
||||||
|
toDec("1001" + padBin(memAddr+6+len(shellcode)))
|
||||||
|
]
|
||||||
|
elif comp==">=":
|
||||||
|
ret = [
|
||||||
|
# LDI konst
|
||||||
|
toDec("0111" + "10000000"),
|
||||||
|
# STA temp
|
||||||
|
toDec("0100" + "11111111"),
|
||||||
|
# LDA comp1
|
||||||
|
toDec("0001" + padBin(varRec[comp1])),
|
||||||
|
# ADD konst
|
||||||
|
toDec("0010" + "11111111"),
|
||||||
|
# SUB comp2
|
||||||
|
toDec("0011" + padBin(varRec[comp2])),
|
||||||
|
# JIF
|
||||||
|
toDec("1000" + padBin(memAddr+6+len(shellcode)))
|
||||||
|
]
|
||||||
|
elif comp==">":
|
||||||
|
ret = [
|
||||||
|
# LDI konst
|
||||||
|
toDec("0111" + "01111111"),
|
||||||
|
# STA temp
|
||||||
|
toDec("0100" + "11111111"),
|
||||||
|
# LDA comp1
|
||||||
|
toDec("0001" + padBin(varRec[comp1])),
|
||||||
|
# ADD konst
|
||||||
|
toDec("0010" + "11111111"),
|
||||||
|
# SUB comp2
|
||||||
|
toDec("0011" + padBin(varRec[comp2])),
|
||||||
|
# JIF
|
||||||
|
toDec("1000" + padBin(memAddr+6+len(shellcode)))
|
||||||
|
]
|
||||||
|
elif comp=="<=":
|
||||||
|
ret = [
|
||||||
|
# LDI konst
|
||||||
|
toDec("0111" + "10000000"),
|
||||||
|
# STA temp
|
||||||
|
toDec("0100" + "11111111"),
|
||||||
|
# LDA comp2
|
||||||
|
toDec("0001" + padBin(varRec[comp2])),
|
||||||
|
# ADD konst
|
||||||
|
toDec("0010" + "11111111"),
|
||||||
|
# SUB comp1
|
||||||
|
toDec("0011" + padBin(varRec[comp1])),
|
||||||
|
# JIF
|
||||||
|
toDec("1000" + padBin(memAddr+6+len(shellcode)))
|
||||||
|
]
|
||||||
|
elif comp=="<":
|
||||||
|
ret = [
|
||||||
|
# LDI konst
|
||||||
|
toDec("0111" + "01111111"),
|
||||||
|
# STA temp
|
||||||
|
toDec("0100" + "11111111"),
|
||||||
|
# LDA comp2
|
||||||
|
toDec("0001" + padBin(varRec[comp2])),
|
||||||
|
# ADD konst
|
||||||
|
toDec("0010" + "11111111"),
|
||||||
|
# SUB comp1
|
||||||
|
toDec("0011" + padBin(varRec[comp1])),
|
||||||
|
# JIF
|
||||||
|
toDec("1000" + padBin(memAddr+6+len(shellcode)))
|
||||||
|
]
|
||||||
|
for s in shellcode:
|
||||||
|
ret.append(s)
|
||||||
|
return ret
|
||||||
|
elif cmd=="for": # for loop: "for [goal] [counter] [step] [cmd] [pars]"
|
||||||
|
# == for [counter] in range(0,[goal],[step]):
|
||||||
|
# [cmd]([pars])
|
||||||
|
try:
|
||||||
|
t = varRec[par[0].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[0].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()] = len(varRec)+(2**8)/2
|
||||||
|
try:
|
||||||
|
t = varRec[par[1].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[1].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[1].upper()] = len(varRec)+(2**8)/2
|
||||||
|
try:
|
||||||
|
t = varRec[par[2].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[2].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[2].upper()] = len(varRec)+(2**8)/2
|
||||||
|
goal = par[0].upper()
|
||||||
|
clock = par[1].upper()
|
||||||
|
step = par[2].upper()
|
||||||
|
shellcode=compile(par[3],par[4:],memAddr+12)
|
||||||
|
ret = [
|
||||||
|
# LDI konst
|
||||||
|
toDec("0111" + "00000000"),
|
||||||
|
# STA temp
|
||||||
|
toDec("0100" + padBin(varRec[clock])),
|
||||||
|
# LDI konst <-- jump back to
|
||||||
|
toDec("0111" + "10000000"),
|
||||||
|
# STA temp
|
||||||
|
toDec("0100" + "11111111"),
|
||||||
|
# LDA clock
|
||||||
|
toDec("0001" + padBin(varRec[clock])),
|
||||||
|
# ADD konst
|
||||||
|
toDec("0010" + "11111111"),
|
||||||
|
# SUB goal
|
||||||
|
toDec("0011" + padBin(varRec[goal])),
|
||||||
|
# JIE
|
||||||
|
toDec("1001" + padBin(memAddr+12+len(shellcode)+1)),
|
||||||
|
# LDA clock
|
||||||
|
toDec("0001" + padBin(varRec[goal])),
|
||||||
|
# ADD step
|
||||||
|
toDec("0010" + padBin(varRec[step])),
|
||||||
|
# STA clock
|
||||||
|
toDec("0100" + padBin(varRec[goal]))
|
||||||
|
]
|
||||||
|
for s in shellcode:
|
||||||
|
ret.append(s)
|
||||||
|
# JMP
|
||||||
|
ret.append(toDec("0110" + padBin(memAddr+2)))
|
||||||
|
return ret
|
||||||
|
elif cmd=="asm" or cmd[0]==":": # compiles assembler to machine-code
|
||||||
|
if cmd=="asm":
|
||||||
|
opcode = par[0]
|
||||||
|
param = par[1]
|
||||||
|
else:
|
||||||
|
opcode = cmd[1:]
|
||||||
|
param = par[0]
|
||||||
|
if param[:2]=="b#":
|
||||||
|
param = toDec(param[2:])
|
||||||
|
elif param[:2]=="d#":
|
||||||
|
param = param[2:]
|
||||||
|
elif param[:2]=="v#":
|
||||||
|
try:
|
||||||
|
t = varRec[param[2:].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+param[2:].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[param[2:].upper()] = len(varRec)+(2**8)/2
|
||||||
|
param = varRec[param[2:].upper()]
|
||||||
|
elif param in ["-","none"]:
|
||||||
|
param = toDec("00000000")
|
||||||
|
elif param in ["~","all"]:
|
||||||
|
param = toDec("11111111")
|
||||||
|
else:
|
||||||
|
return ["Error","Invalid param supplied to asm-command (no valid prefix found)"]
|
||||||
|
for a in range(len(asm.asmMap)):
|
||||||
|
if asm.asmMap[a]==opcode:
|
||||||
|
print("Found matching opcode")
|
||||||
|
return [toDec(padBin(a,bit=4)+padBin(param))]
|
||||||
|
elif cmd=="shellcode": # insertes raw shellcode into the programm
|
||||||
|
return [toDec("".join(par))]
|
||||||
|
elif cmd=="push":
|
||||||
|
try:
|
||||||
|
t = varRec[par[0].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[0].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()] = len(varRec)+(2**8)/2
|
||||||
|
ret = [
|
||||||
|
### value to temp
|
||||||
|
# LDA value
|
||||||
|
toDec("0001" + padBin(varRec[par[0]])),
|
||||||
|
# STA temp
|
||||||
|
toDec("0100" + padBin(varRec["temp"])),
|
||||||
|
|
||||||
|
### increment stackpointer
|
||||||
|
# LDI konst
|
||||||
|
toDec("0111" + "00000001"), # we are going to use a overflow to subtract (2's-complement)
|
||||||
|
# ADD stack_pointer
|
||||||
|
toDec("0010" + padBin(varRec["stack_pointer"])),
|
||||||
|
# STA stack_pointer
|
||||||
|
toDec("0100" + padBin(varRec["stack_pointer"])),
|
||||||
|
|
||||||
|
### A > write as new element
|
||||||
|
# STP temp
|
||||||
|
toDec("1110" + padBin(varRec["temp"]))
|
||||||
|
]
|
||||||
|
return ret
|
||||||
|
elif cmd=="pop":
|
||||||
|
try:
|
||||||
|
t = varRec[par[0].upper()]
|
||||||
|
except:
|
||||||
|
print("Creating var '"+par[0].upper()+"' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[par[0].upper()] = len(varRec)+(2**8)/2
|
||||||
|
ret = [
|
||||||
|
### read the last element > A
|
||||||
|
# LDI stack-base
|
||||||
|
toDec("0111" + "11111111"),
|
||||||
|
# SUB stack_pointer
|
||||||
|
toDec("0011" + padBin(varRec["stack_pointer"])),
|
||||||
|
# LDP konst
|
||||||
|
toDec("1010" + "00000000"),
|
||||||
|
# STA stack_pointer
|
||||||
|
toDec("0100" + padBin(varRec[par[0]])),
|
||||||
|
|
||||||
|
### decrement stackpointer
|
||||||
|
# LDI konst
|
||||||
|
toDec("0111" + "11111111"), # we are going to use a overflow to subtract (2's-complement)
|
||||||
|
# ADD stack_pointer
|
||||||
|
toDec("0010" + padBin(varRec["stack_pointer"])),
|
||||||
|
# STA stack_pointer
|
||||||
|
toDec("0100" + padBin(varRec["stack_pointer"]))
|
||||||
|
]
|
||||||
|
return ret
|
||||||
|
elif cmd=="nop": # nothing...
|
||||||
|
return [toDec("0000" + "00000000")]
|
||||||
|
elif cmd=="halt": # halts execution of programm
|
||||||
|
return [toDec("1111" + "11111111")]
|
||||||
|
else:
|
||||||
|
return ["Error","Unimplemented Function: "+str(cmd)]
|
||||||
|
#except Exception as e:
|
||||||
|
# return ["Error","Unknown Error: "+str(e)]
|
||||||
|
|
||||||
|
bits = 12
|
||||||
|
|
||||||
|
varRec = {"temp": (2**8)/2, "stack_pointer": (2**8)/2 + 1 } # to store VarName -> memory-address
|
||||||
|
jumpMark = {} # to store jumpMarkName -> jump-address
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print("[tronScript-Compiler by CyberCodeDragon]")
|
||||||
|
print("! Unstable Beta-Version with stack")
|
||||||
|
memory = []
|
||||||
|
for i in range(2**bits): #expand memory
|
||||||
|
memory.append(0)
|
||||||
|
|
||||||
|
pointer = 0
|
||||||
|
funcTable = [] # [[funcName,funcCode],...]
|
||||||
|
|
||||||
|
for f in asm.os.listdir("tronScript"): #display all found scripts
|
||||||
|
if f[-5:]==".tron":
|
||||||
|
print("> "+"tronScript/"+f)
|
||||||
|
filePath = raw_input("file to compile> ")
|
||||||
|
inFile = filePath.split("\\")
|
||||||
|
inFile = inFile[len(inFile)-1]
|
||||||
|
tmp = inFile.split(".")
|
||||||
|
ext = tmp[len(tmp)-1]
|
||||||
|
name = inFile.split("."+ext)[0]
|
||||||
|
print("[*] Reading "+inFile)
|
||||||
|
with open(filePath) as f:
|
||||||
|
code = f.read().replace(" ","") #read file and remove tabs
|
||||||
|
c = code.split("\n") #c = lines of code
|
||||||
|
#detect functions
|
||||||
|
for l in range(len(c)): #for every line of code
|
||||||
|
elems = c[l].split(" ") #elems = [cmd,pa1,par2,...,parN]
|
||||||
|
if elems[0]=="function":
|
||||||
|
funcName = elems[1]
|
||||||
|
funcCode = code.split("function "+funcName+" {")[1].split("}")[0].split("\n") #get the function Code
|
||||||
|
funcName = funcName.upper()
|
||||||
|
print("detected function '"+funcName+"' in line "+str(l)+":\n"+"\n".join(funcCode))
|
||||||
|
for i in range(len(funcCode)+0):
|
||||||
|
c[l+i]="#function-code" #replace function with a comment -> dont mess up later parsing
|
||||||
|
funcTable.append([funcName,funcCode])
|
||||||
|
try: #create jumpback-var if not existing (does probably not exist)
|
||||||
|
t = varRec[funcName+"_JUMPBACK"]
|
||||||
|
except:
|
||||||
|
print("Creating function-jumpback-var '"+funcName+"_JUMPBACK' at memory-address "+padBin(len(varRec)+(2**8)/2))
|
||||||
|
varRec[funcName+"_JUMPBACK"] = len(varRec)+(2**8)/2
|
||||||
|
|
||||||
|
#parse code
|
||||||
|
for l in range(len(c)): #for every line in code
|
||||||
|
if c[l] not in ["","\n"]: #if lines is not empty
|
||||||
|
print("Parsing line "+str(l)+": "+c[l])
|
||||||
|
cmd = c[l].split(" ")[0]
|
||||||
|
if not cmd[0]=="#": #dont parse comments
|
||||||
|
if cmd in ["halt","nop"]: #cmds without params
|
||||||
|
par = []
|
||||||
|
else:
|
||||||
|
par = c[l].split(cmd+" ")[1].split(" ")
|
||||||
|
r = compile(cmd,par,pointer) #compile
|
||||||
|
if r:
|
||||||
|
if r[0]=="Error": #print error
|
||||||
|
print("Error compiling line "+str(l)+" {"+c[l]+"}: \n"+str(r[1]))
|
||||||
|
exit()
|
||||||
|
for ret in r:
|
||||||
|
veryVerbose = False
|
||||||
|
if veryVerbose:
|
||||||
|
try:
|
||||||
|
print("Compiled to: "+padBin(ret,bit=12))
|
||||||
|
except:
|
||||||
|
print("Compiled to: "+str(ret))
|
||||||
|
memory[pointer] = ret #write code to memory
|
||||||
|
pointer+=1
|
||||||
|
try:
|
||||||
|
if padBin(memory[pointer-1],bit=12)[:4]!="1111": #if the last command is not a HLT command
|
||||||
|
print("Appending HLT-Command")
|
||||||
|
# HLT
|
||||||
|
memory[pointer] = toDec("1111"+"11111111")
|
||||||
|
print("HLT at "+padBin(pointer)+": "+padBin(memory[pointer],bit=12))
|
||||||
|
pointer+=1
|
||||||
|
except: #looks like the last command was a placeholder: not a HLT command
|
||||||
|
print("Appending HLT-Command")
|
||||||
|
# HLT
|
||||||
|
memory[pointer] = toDec("1111"+"11111111")
|
||||||
|
print("HLT at "+padBin(pointer)+": "+padBin(memory[pointer],bit=12))
|
||||||
|
pointer+=1
|
||||||
|
|
||||||
|
#replace jump-names
|
||||||
|
for m in range(len(memory)):
|
||||||
|
try:
|
||||||
|
if memory[m][:5]=="Jump_": #if at memory-adress is just a jump-dummy
|
||||||
|
print("Inserting Jump-Adress at "+padBin(m,bit=12))
|
||||||
|
memory[m]=toDec("0110"+padBin(jumpMark[memory[m][5:]])) #replace with real jump adress
|
||||||
|
except:
|
||||||
|
t=1
|
||||||
|
#append function-shellcode
|
||||||
|
funcMapper = {} #map funcName to funcCode
|
||||||
|
for f in range(len(funcTable)):
|
||||||
|
print("Appending shellcode for function "+str(funcTable[f][0])+" at adress "+padBin(pointer))
|
||||||
|
funcMapper[funcTable[f][0]] = pointer #remember where you put dat function
|
||||||
|
for l in range(len(funcTable[f][1])):
|
||||||
|
if funcTable[f][1][l] not in ["","halt","nop"]:
|
||||||
|
ret=compile(funcTable[f][1][l].split(" ")[0],funcTable[f][1][l].split(" ")[1:],pointer)
|
||||||
|
try:
|
||||||
|
if ret[0]=="Error":
|
||||||
|
print("An Error occured, when compiling function shellcode for function '"+funcTable[f][0]+"':\n"+str(e))
|
||||||
|
else:
|
||||||
|
for r in range(len(ret)):
|
||||||
|
memory[pointer]=ret[r] #append compiled function memory
|
||||||
|
pointer+=1
|
||||||
|
except:
|
||||||
|
t=1
|
||||||
|
## jump back after function execution
|
||||||
|
# LDI konst=3
|
||||||
|
memory[pointer] = toDec("0111" + "00000011")
|
||||||
|
pointer+=1
|
||||||
|
# ADD jumpBackVar
|
||||||
|
memory[pointer] = toDec("0010" + padBin(varRec[funcTable[f][0]+"_JUMPBACK"]))
|
||||||
|
pointer+=1
|
||||||
|
# JMA null
|
||||||
|
memory[pointer] = toDec("1100" + "00000000")
|
||||||
|
pointer+=1
|
||||||
|
print("FuncMapper: "+str(funcMapper))
|
||||||
|
print("Var Assignment:")
|
||||||
|
varAss = str(varRec)[1:][:-1].split(", ")
|
||||||
|
print("#VAR-NAME# #ADDRESS#")
|
||||||
|
for va in range(len(varAss)): #print all vars with their memory-location
|
||||||
|
print(varAss[va].split("'")[1]+": "+" "*(12-len(varAss[va].split("'")[1]))+padBin(int(varAss[va].split("': ")[1])))
|
||||||
|
#replace func-names
|
||||||
|
for m in range(len(memory)):
|
||||||
|
try:
|
||||||
|
if memory[m][:5]=="Exec_": #if there is just a function-call-dummy
|
||||||
|
print("Inserting Exec-Call-Adress at "+padBin(m,bit=12))
|
||||||
|
memory[m]=toDec("0110"+padBin(funcMapper[memory[m][5:]])) #replace it with a jump to the function
|
||||||
|
except Exception as e:
|
||||||
|
if str(e) != "'int' object has no attribute '__getitem__'":
|
||||||
|
print "Error while replacing func-name: "+str(e)+"\n (I don't know why, sorry)"
|
||||||
|
try:
|
||||||
|
print("Removing old "+name+".asmTron")
|
||||||
|
os.remove(name+".asmTron")
|
||||||
|
except:
|
||||||
|
t=1
|
||||||
|
print("Saving as "+name+".asmTron")
|
||||||
|
with open(name+".asmTron", 'wb') as f:
|
||||||
|
t=asm.pickle.dumps([12,memory])
|
||||||
|
t=asm.zlib.compress(t)
|
||||||
|
f.write(t)
|
||||||
|
print("Done.")
|
214
deCompiler.py
Normal file
214
deCompiler.py
Normal file
@ -0,0 +1,214 @@
|
|||||||
|
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()
|
544
mima.py
Normal file
544
mima.py
Normal file
@ -0,0 +1,544 @@
|
|||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import pickle
|
||||||
|
import zlib
|
||||||
|
import hashlib
|
||||||
|
import copy
|
||||||
|
from termcolor import colored
|
||||||
|
from base64 import *
|
||||||
|
from colorama import init
|
||||||
|
init()
|
||||||
|
|
||||||
|
OS = "Linux"
|
||||||
|
#OS = "Windows"
|
||||||
|
|
||||||
|
doc = """
|
||||||
|
0000 NOP
|
||||||
|
0001 LDC par > A (max 20bit)
|
||||||
|
0010 LDV [par] > A
|
||||||
|
0011 STV A > [par]
|
||||||
|
0100 ADD A + [par] > A
|
||||||
|
0101 AND A AND [par] > A
|
||||||
|
0110 LOR A OR [par] > A
|
||||||
|
0111 XOR A XOR [par] > A
|
||||||
|
1000 EQL if A==[par]: -1 else 0 > A
|
||||||
|
1001 JMP Jump to [par]
|
||||||
|
1010 JMN if A<0: Jump to [par]
|
||||||
|
1011 NOT NOT A > A
|
||||||
|
1100 RAR A >> 1 > A
|
||||||
|
1101 LDI [[par]] > A
|
||||||
|
1110 STI A > [[par]]
|
||||||
|
1111 HLT Halt
|
||||||
|
"""
|
||||||
|
|
||||||
|
asmMap = ["NOP","LDC","LDV","STV","ADD","AND","LOR","XOR","EQL","JMP","JMN","NOT","RAR","LDI","STI","HLT"]
|
||||||
|
|
||||||
|
bits = 24
|
||||||
|
opBits = 4
|
||||||
|
parBits = 20
|
||||||
|
|
||||||
|
pointer = 0
|
||||||
|
|
||||||
|
registerA = 0
|
||||||
|
out = 0
|
||||||
|
|
||||||
|
color = [["green","on_grey"],["blue","on_grey"],["red","on_grey"],["magenta","on_grey"],["cyan","on_grey"],
|
||||||
|
["grey","on_white"],["green","on_white"],["blue","on_white"],["red","on_white"],["magenta","on_white"],["cyan","on_white"],
|
||||||
|
["grey","on_green"],["white","on_green"],["blue","on_green"],["red","on_green"],["magenta","on_green"],["cyan","on_green"],
|
||||||
|
["grey","on_blue"],["white","on_blue"],["green","on_blue"],["red","on_blue"],["magenta","on_blue"],["cyan","on_blue"],
|
||||||
|
["grey","on_red"],["white","on_red"],["green","on_red"],["blue","on_red"],["magenta","on_red"],["cyan","on_red"],
|
||||||
|
["grey","on_magenta"],["white","on_magenta"],["green","on_magenta"],["blue","on_magenta"],["red","on_magenta"],["cyan","on_magenta"],
|
||||||
|
["grey","on_cyan"],["white","on_cyan"],["green","on_cyan"],["blue","on_cyan"],["red","on_cyan"],["magenta","on_cyan"]]
|
||||||
|
|
||||||
|
memory = []
|
||||||
|
|
||||||
|
for i in range(2**parBits):
|
||||||
|
memory.append(0)
|
||||||
|
|
||||||
|
def clear():
|
||||||
|
if OS=="Windows":
|
||||||
|
os.system("cls")
|
||||||
|
elif OS=="Linux":
|
||||||
|
os.system("clear")
|
||||||
|
else:
|
||||||
|
print("Error: Unknown OS")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
def toDec(binary):
|
||||||
|
try:
|
||||||
|
return int(binary, 2)
|
||||||
|
except:
|
||||||
|
return -1
|
||||||
|
|
||||||
|
def toBin(decimal):
|
||||||
|
try:
|
||||||
|
return int(bin(decimal)[2:])
|
||||||
|
except:
|
||||||
|
print("Error: The dec-number "+decimal+" could not be converted to binary")
|
||||||
|
return decimal
|
||||||
|
|
||||||
|
def padBin(binary,bit=bits):
|
||||||
|
return str(binary).zfill(bit)[-bit:]
|
||||||
|
|
||||||
|
def bin_input(display,bit=bits,allowAsm=False):
|
||||||
|
raw = input(display)
|
||||||
|
if raw=="x":
|
||||||
|
return -1
|
||||||
|
if allowAsm:
|
||||||
|
for i in range(len(asmMap)):
|
||||||
|
if raw.find(asmMap[i])!=-1:
|
||||||
|
raw = raw.replace(asmMap[i],"")
|
||||||
|
raw = raw.replace(" ","")
|
||||||
|
if raw.find("_")!=-1:
|
||||||
|
raw = raw.replace("_","0"*(bit-(len(raw)-1)-4))
|
||||||
|
raw = str(toBin(i)).zfill(opBits)+raw.zfill(bits-opBits)
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
raw = raw.replace(" ","")
|
||||||
|
if raw.find("_")!=-1:
|
||||||
|
raw = raw.replace("_","0"*(bit-(len(raw)-1)))
|
||||||
|
else:
|
||||||
|
raw = raw.replace(" ","")
|
||||||
|
if raw.find("_")!=-1:
|
||||||
|
raw = raw.replace("_","0"*(bit-(len(raw)-1)))
|
||||||
|
if raw.find("_")!=-1:
|
||||||
|
print("[!] input contains multiple spread-zero characters ( _ )")
|
||||||
|
return bin_input(display)
|
||||||
|
ret = toDec(raw)
|
||||||
|
if ret>2**bits:
|
||||||
|
print("[!] input exceted maximal size for "+str(bit)+"-bit integer!")
|
||||||
|
return bin_input(display,allowAsm=allowAsm)
|
||||||
|
if ret==-1:
|
||||||
|
print("[!] "+raw+" is not a valid binary number!")
|
||||||
|
return bin_input(display,allowAsm=allowAsm)
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def parse(cmd,par,verbose=False,adress=0):
|
||||||
|
global registerA
|
||||||
|
global memory
|
||||||
|
global out
|
||||||
|
global pointer
|
||||||
|
v=verbose
|
||||||
|
cmd = cmd[-1*opBits:]
|
||||||
|
if cmd=="0000": #nop
|
||||||
|
if v: print(padBin(toBin(adress),bit=parBits)+": NOP")
|
||||||
|
elif cmd=="0001": #ldc
|
||||||
|
registerA = par
|
||||||
|
if v: print(padBin(toBin(adress),bit=parBits)+": LDC "+padBin(toBin(par),bit=parBits)+" > Akku")
|
||||||
|
elif cmd=="0010": #ldv
|
||||||
|
registerA = memory[par]
|
||||||
|
if v: print(padBin(toBin(adress),bit=parBits)+": LDV ["+padBin(toBin(par),bit=parBits)+"] > Akku")
|
||||||
|
elif cmd=="0011": #stv
|
||||||
|
memory[par] = registerA
|
||||||
|
if v: print(padBin(toBin(adress),bit=parBits)+": STA Akku > ["+padBin(toBin(par),bit=parBits)+"]")
|
||||||
|
elif cmd=="0100": #add
|
||||||
|
registerA = (registerA + memory[par]) % 2**parBits
|
||||||
|
if v: print(padBin(toBin(adress),bit=parBits)+": ADD Akku + ["+padBin(toBin(par),bit=parBits)+"]")
|
||||||
|
elif cmd=="0101": #and
|
||||||
|
newRegA = ""
|
||||||
|
for b in range(len(padBin(toBin(registerA)))):
|
||||||
|
if padBin(toBin(int(registerA)))[b]=="1" and padBin(toBin(int(memory[par])))[b]=="1":
|
||||||
|
newRegA+="1"
|
||||||
|
else:
|
||||||
|
newRegA+="0"
|
||||||
|
registerA = toDec(newRegA)
|
||||||
|
if v: print(padBin(toBin(adress),bit=parBits)+": AND Akku, ["+padBin(toBin(par),bit=parBits)+"]")
|
||||||
|
elif cmd=="0110": #lor
|
||||||
|
newRegA = ""
|
||||||
|
for b in range(len(str(registerA))):
|
||||||
|
newRegA+=str(int( int(padBin(toBin(int(registerA)))[b]) or int(padBin(toBin(int(memory[par])))[b]) ))
|
||||||
|
registerA = toDec(newRegA)
|
||||||
|
if v: print(padBin(toBin(adress),bit=parBits)+": OR Akku, ["+padBin(toBin(par),bit=parBits)+"]")
|
||||||
|
elif cmd=="0111": #XOR
|
||||||
|
newRegA = ""
|
||||||
|
for b in range(len(padBin(toBin(registerA)))):
|
||||||
|
if padBin(toBin(registerA))[b] == padBin(toBin(memory[par]))[b]:
|
||||||
|
newRegA+="0"
|
||||||
|
else:
|
||||||
|
newRegA+="1"
|
||||||
|
registerA = toDec(newRegA)
|
||||||
|
if v: print(padBin(toBin(adress),bit=parBits)+": XOR Akku, ["+padBin(toBin(par),bit=parBits)+"]")
|
||||||
|
elif cmd=="1000": #EQL
|
||||||
|
if registerA==memory[par]:
|
||||||
|
registerA = toDec("1"*bits)
|
||||||
|
if v: print(padBin(toBin(adress),bit=8)+": EQL {TRUE} Akku ?= "+padBin(toBin(par),bit=parBits))
|
||||||
|
else:
|
||||||
|
registerA = 0
|
||||||
|
if v: print(padBin(toBin(adress),bit=8)+": EQL {FALSE} Akku ?= "+padBin(toBin(par),bit=parBits))
|
||||||
|
elif cmd=="1001": #jmp
|
||||||
|
pointer = par - 1
|
||||||
|
if v: print(padBin(toBin(adress),bit=8)+": JMP "+padBin(toBin(par),bit=parBits))
|
||||||
|
elif cmd=="1010": #jmn
|
||||||
|
if registerA>=int("1"+"0"*(bits-1)):
|
||||||
|
pointer = par - 1
|
||||||
|
if v: print(padBin(toBin(adress),bit=8)+": JMN {TRUE} > "+padBin(toBin(par),bit=8))
|
||||||
|
else:
|
||||||
|
if v: print(padBin(toBin(adress),bit=8)+": JMN {FALSE} > "+padBin(toBin(par),bit=8))
|
||||||
|
elif cmd=="1011": #not
|
||||||
|
newRegA = ""
|
||||||
|
for b in range(len(padBin(toBin(registerA)))):
|
||||||
|
if padBin(toBin(registerA))[b]=="1":
|
||||||
|
newRegA+="0"
|
||||||
|
elif padBin(toBin(registerA))[b]=="0":
|
||||||
|
newRegA+="1"
|
||||||
|
else:
|
||||||
|
print("FUUUUUUUUUU")
|
||||||
|
exit()
|
||||||
|
registerA = toDec(newRegA)
|
||||||
|
if v: print(padBin(toBin(adress),bit=parBits)+": NOT Akku > Akku")
|
||||||
|
elif cmd=="1100": #rar
|
||||||
|
newRegA = padBin(toBin(registerA))
|
||||||
|
newRegA = newRegA[-1]+newRegA[:-1]
|
||||||
|
registerA = toDec(newRegA)
|
||||||
|
if v: print(padBin(toBin(adress),bit=parBits)+": RAR Akku > Akku")
|
||||||
|
elif cmd=="1101": #LDI
|
||||||
|
mid = memory[par]
|
||||||
|
registerA = memory[mid % parBits]
|
||||||
|
if v: print(padBin(toBin(adress),bit=parBits)+": LDI [["+padBin(toBin(par),bit=parBits)+"]] > Akku")
|
||||||
|
elif cmd=="1110": #STI
|
||||||
|
mid = memory[par]
|
||||||
|
memory[mid] = registerA
|
||||||
|
if v: print(padBin(toBin(adress),bit=parBits)+": STI Akku > [["+padBin(toBin(par),bit=parBits)+"]]")
|
||||||
|
elif cmd=="1111": #hlt
|
||||||
|
return -1
|
||||||
|
return True
|
||||||
|
|
||||||
|
if __name__=="__main__":
|
||||||
|
print("[asmTron by CyberCodeDragon]")
|
||||||
|
while True:
|
||||||
|
x = ""
|
||||||
|
x=input("#> ")
|
||||||
|
x = x.split(" ")
|
||||||
|
for i in range(2):
|
||||||
|
x.append("")
|
||||||
|
y = x[1]
|
||||||
|
z = x[2]
|
||||||
|
x = x[0]
|
||||||
|
if x=="help" or x=="h":
|
||||||
|
print("Avaible Commands:")
|
||||||
|
print("docs - display avaible operants")
|
||||||
|
print("write [w] - write value to adress")
|
||||||
|
print("read [r] - read value from adress")
|
||||||
|
print("dump [d] - dump from adress with length")
|
||||||
|
print("run [u] - execute programm")
|
||||||
|
print("edit [e] - edit from adress til >x<")
|
||||||
|
print("cls - clear screen")
|
||||||
|
print("clear / reset - clear memory,register,pointer")
|
||||||
|
print("step [s] - run one command from programm")
|
||||||
|
print("debug - execute programm with verbose output")
|
||||||
|
print("pointer - display pointer")
|
||||||
|
print("setPointer - sets pointer")
|
||||||
|
print("resetPointer [rp] - sets pointer to zero")
|
||||||
|
print("register [reg] - displays A register")
|
||||||
|
print("setRegister - sets A register")
|
||||||
|
print("save - saves memory to local file")
|
||||||
|
print("load - loads memory from local file")
|
||||||
|
print("code / exec - execute one line of shellcode")
|
||||||
|
print("guiDebug [gd] - graphical debugger <not yet implemented for mima-arch!>")
|
||||||
|
print("asm - shellcode visualizer (assembly / jump-arrows) <not yet implemented for mima-arch!>")
|
||||||
|
print("halting - tries to determin, if algorythm will diverge")
|
||||||
|
elif x=="docs":
|
||||||
|
print(doc)
|
||||||
|
elif x=="write" or x=="w":
|
||||||
|
if y=="" or z=="":
|
||||||
|
y=bin_input("adress> ")
|
||||||
|
z=bin_input("value> ")
|
||||||
|
try:
|
||||||
|
memory[y] = z
|
||||||
|
except:
|
||||||
|
print("[!] Unknown error while writing")
|
||||||
|
elif x=="read" or x=="r":
|
||||||
|
if y=="":
|
||||||
|
y=bin_input("adress> ")
|
||||||
|
print(padBin(toBin(memory[y])))
|
||||||
|
elif x=="dump" or x=="d":
|
||||||
|
if y=="" and z=="":
|
||||||
|
y=bin_input("adress> ")
|
||||||
|
z=bin_input("length> ")
|
||||||
|
elif z=="":
|
||||||
|
z=toDec(y)
|
||||||
|
y=0
|
||||||
|
for m in range(z):
|
||||||
|
print(padBin(toBin(y+m),bit=parBits)+": "+padBin(toBin(memory[y+m])))
|
||||||
|
elif x=="run" or x=="u":
|
||||||
|
while True:
|
||||||
|
cmd = padBin(toBin(memory[pointer]))[:opBits]
|
||||||
|
par = int(toDec(padBin(toBin(memory[pointer]))[-parBits:]))
|
||||||
|
if parse(cmd,par)==-1:
|
||||||
|
pointer = 0
|
||||||
|
break
|
||||||
|
pointer = (pointer + 1) % 2**parBits
|
||||||
|
elif x=="verboseRun" or x=="vu":
|
||||||
|
while True:
|
||||||
|
cmd = padBin(toBin(memory[pointer]))[:opBits]
|
||||||
|
par = int(toDec(padBin(toBin(memory[pointer]))[-parBits:]))
|
||||||
|
if parse(cmd,par,verbose=True,adress=pointer)==-1:
|
||||||
|
pointer = 0
|
||||||
|
break
|
||||||
|
pointer = (pointer + 1) % 2**parBits
|
||||||
|
elif x=="edit" or x=="e":
|
||||||
|
adress=bin_input("offset> ",bit=parBits)
|
||||||
|
while True:
|
||||||
|
value=bin_input(padBin(toBin(adress),bit=parBits)+": "+padBin(toBin(memory[adress]))+" > ",allowAsm=True)
|
||||||
|
if value==-1:
|
||||||
|
break
|
||||||
|
try:
|
||||||
|
memory[adress] = value
|
||||||
|
except:
|
||||||
|
print("[!] Unknown error while writing")
|
||||||
|
if adress==len(memory)-1:
|
||||||
|
adress = -1
|
||||||
|
print("[*] End of mem")
|
||||||
|
adress = adress + 1
|
||||||
|
elif x=="cls":
|
||||||
|
clear()
|
||||||
|
elif x=="clear" or x=="reset":
|
||||||
|
pointer = 0
|
||||||
|
registerA = 0
|
||||||
|
out = 0
|
||||||
|
memory = []
|
||||||
|
for i in range(2**parBits):
|
||||||
|
memory.append(0)
|
||||||
|
elif x=="varClear" or x=="vc":
|
||||||
|
pointer = 0
|
||||||
|
registerA = 0
|
||||||
|
out = 0
|
||||||
|
for i in range((2**parBits)/2):
|
||||||
|
memory[(2**parBits)/2+i] = 0
|
||||||
|
elif x=="step" or x=="s":
|
||||||
|
cmd = padBin(toBin(memory[pointer]))[:opBits]
|
||||||
|
par = int(toDec(padBin(toBin(memory[pointer]))[-parBits:]))
|
||||||
|
if parse(cmd,par,verbose=True,adress=pointer)==-1:
|
||||||
|
pointer = 0
|
||||||
|
print("[*] Programm halted")
|
||||||
|
pointer = (pointer + 1) % (2**parBits)
|
||||||
|
elif x=="debug":
|
||||||
|
while True:
|
||||||
|
cmd = padBin(toBin(memory[pointer]))[:opBits]
|
||||||
|
par = int(toDec(padBin(toBin(memory[pointer]))[-parBits:]))
|
||||||
|
if parse(cmd,par,verbose=True,adress=pointer)==-1:
|
||||||
|
pointer = -1
|
||||||
|
print("[*] Programm halted")
|
||||||
|
break
|
||||||
|
pointer = (pointer + 1) % (2**parBits)
|
||||||
|
elif x=="pointer":
|
||||||
|
print("POINTER: "+padBin(toBin(pointer),bit=parBits))
|
||||||
|
elif x=="setPointer":
|
||||||
|
if y=="":
|
||||||
|
pointer = bin_input("pointer> ",bit=parBits)
|
||||||
|
else:
|
||||||
|
pointer = y
|
||||||
|
elif x=="resetPointer" or x=="rp":
|
||||||
|
pointer = 0
|
||||||
|
elif x=="register" or x=="reg":
|
||||||
|
print("REGISTER: "+padBin(toBin(registerA)))
|
||||||
|
elif x=="setRegister":
|
||||||
|
if y=="":
|
||||||
|
registerA = bin_input("register> ")
|
||||||
|
else:
|
||||||
|
registerA = y
|
||||||
|
elif x=="save":
|
||||||
|
if y=="":
|
||||||
|
print("[*] Current saves:")
|
||||||
|
for f in os.listdir("saves"):
|
||||||
|
if f[-8:]==".asmTron":
|
||||||
|
print("> "+f[:-8])
|
||||||
|
name = input("name> ")
|
||||||
|
else:
|
||||||
|
name = y
|
||||||
|
with open("saves/"+name+".asmTron", 'wb') as f:
|
||||||
|
t=pickle.dumps(["mima24_1",memory])
|
||||||
|
t=zlib.compress(t)
|
||||||
|
f.write(t)
|
||||||
|
elif x=="load" or x=="l":
|
||||||
|
if y=="":
|
||||||
|
print("[*] Avaible saves:")
|
||||||
|
for f in os.listdir("saves"):
|
||||||
|
if f[-8:]==".asmTron":
|
||||||
|
print("> "+f[:-8])
|
||||||
|
for f in os.listdir("tronScript"):
|
||||||
|
if f[-8:]==".asmTron":
|
||||||
|
print("> tronScript/"+f[:-8])
|
||||||
|
name = input("name> ")
|
||||||
|
else:
|
||||||
|
name = y
|
||||||
|
try:
|
||||||
|
with open("saves/"+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=="mima24_1":
|
||||||
|
print("[!] Error: Invalid Magical Bit: "+str(magicBit))
|
||||||
|
exit()
|
||||||
|
print("[*] loaded "+name+".asmTron")
|
||||||
|
elif x=="code" or x=="exec":
|
||||||
|
if y=="":
|
||||||
|
data=bin_input("code> ",allowAsm=True)
|
||||||
|
else:
|
||||||
|
data=y
|
||||||
|
cmd = padBin(toBin(memory[pointer]))[:opBits]
|
||||||
|
par = int(toDec(padBin(toBin(memory[pointer]))[-parBits:]))
|
||||||
|
parse(cmd,par,verbose=True,adress=0)
|
||||||
|
elif x=="guiDebug" or x=="gd":
|
||||||
|
if y=="":
|
||||||
|
hiOffset = 2**(parBits-1)
|
||||||
|
else:
|
||||||
|
hiOffset = 2**(int(y)-1)
|
||||||
|
runtime = 0
|
||||||
|
tracker = input("trackers> ").split(",")
|
||||||
|
running = True
|
||||||
|
lastPointer = 0
|
||||||
|
while running:
|
||||||
|
clear()
|
||||||
|
print(" +"+"-"*58+"+"+"-"*52+"+"+"-"*36+"+")
|
||||||
|
print(" |"+" "*20+" - Instructions - "+" "*20+"|"+" "*20+" - Hi-mem - "+" "*20+"|"+" "*36+"|")
|
||||||
|
if pointer <= 20:
|
||||||
|
starter = 0
|
||||||
|
else:
|
||||||
|
starter = pointer - 20
|
||||||
|
for i in range(36):
|
||||||
|
m = i + starter
|
||||||
|
if pointer == m:
|
||||||
|
outI = "> "+padBin(toBin(m),bit=parBits)+": "+padBin(toBin(memory[m]))+" ["+doc.split("\n")[toDec(padBin(toBin(memory[m]))[:opBits])+1][:parBits][5:][:3]+"]"+" <"
|
||||||
|
elif lastPointer == m and pointer-1 != lastPointer:
|
||||||
|
outI = "- "+padBin(toBin(m),bit=parBits)+": "+padBin(toBin(memory[m]))+" ["+doc.split("\n")[toDec(padBin(toBin(memory[m]))[:opBits])+1][:parBits][5:][:3]+"]"+" -"
|
||||||
|
else:
|
||||||
|
outI = " "+padBin(toBin(m),bit=parBits)+": "+padBin(toBin(memory[m]))+" ["+doc.split("\n")[toDec(padBin(toBin(memory[m]))[:opBits])+1][:parBits][5:][:3]+"]"+" "
|
||||||
|
outII = " "+padBin(toBin(hiOffset+i),bit=parBits)+": "+padBin(toBin(memory[hiOffset+i]))+" "
|
||||||
|
if i==1:
|
||||||
|
outIII = " - Akku - "
|
||||||
|
elif i==2:
|
||||||
|
outIII = " "*8+padBin(toBin(registerA),bit=parBits)+" "*8
|
||||||
|
elif i==4:
|
||||||
|
outIII = " - Runtime - "
|
||||||
|
elif i==5:
|
||||||
|
outIII = " "+padBin(toBin(runtime),bit=bits)+" "
|
||||||
|
elif i==7:
|
||||||
|
outIII = " - Command - "
|
||||||
|
elif i==8:
|
||||||
|
temp=doc.split("\n")[toDec(padBin(toBin(memory[pointer]))[:opBits])+1][5:]
|
||||||
|
outIII = int((36-len(temp)-1)/2)*" "+" "+temp+int((36-len(temp))/2)*" "
|
||||||
|
elif i==10:
|
||||||
|
outIII = " - Parameter - "
|
||||||
|
elif i==11:
|
||||||
|
outIII = " "+padBin(toBin(memory[pointer]))[-parBits:]+" "
|
||||||
|
elif i==13:
|
||||||
|
outIII = " - Pointer - "
|
||||||
|
elif i==14:
|
||||||
|
outIII = " "+padBin(toBin(pointer))+" "
|
||||||
|
elif i==16:
|
||||||
|
if len(tracker[0]):
|
||||||
|
outIII = " - Trackers - "
|
||||||
|
else:
|
||||||
|
outIII = " "*36
|
||||||
|
elif i>16 and i<=16+len(tracker):
|
||||||
|
if len(tracker[0]):
|
||||||
|
outIII = " "+str(tracker[i-16-1]) + ": " + padBin(toBin(memory[toDec(tracker[i-16-1])]))+" "
|
||||||
|
else:
|
||||||
|
outIII = " "*36
|
||||||
|
else:
|
||||||
|
outIII = " "*36
|
||||||
|
print(" | "+outI+" | "+outII+" |"+outIII+"|")
|
||||||
|
print(" |"+" "*58+"|"+" "*52+"|"+" "*36+"|")
|
||||||
|
print(" +"+"-"*58+"+"+"-"*52+"+"+"-"*36+"+")
|
||||||
|
|
||||||
|
lastPointer = pointer
|
||||||
|
|
||||||
|
cmd = padBin(toBin(memory[pointer]))[:4]
|
||||||
|
par = int(toDec(padBin(toBin(memory[pointer]))[-8:]))
|
||||||
|
if parse(cmd,par,verbose=True,adress=pointer)==-1:
|
||||||
|
pointer = 0
|
||||||
|
break
|
||||||
|
pointer = (pointer + 1) % 2**bits
|
||||||
|
|
||||||
|
step=input(" <step> ")
|
||||||
|
if step=="x":
|
||||||
|
break
|
||||||
|
|
||||||
|
runtime = runtime + 1
|
||||||
|
elif x=="asm":
|
||||||
|
end = 0
|
||||||
|
for m in range(len(memory)):
|
||||||
|
if padBin(toBin(memory[len(memory)-m-1]))[:opBits] != "0"*opBits:
|
||||||
|
end = len(memory)-m
|
||||||
|
break
|
||||||
|
toPrint = []
|
||||||
|
for m in range(end):
|
||||||
|
cmd = doc.split("\n")[toDec(padBin(toBin(memory[m]))[:opBits])+1][:parBits][5:]
|
||||||
|
if toDec(padBin(toBin(memory[m]))[opBits:]) >= toDec("1"+"0"*(parBits-1)) and toDec(padBin(toBin(memory[m]))[opBits:]) <= toDec("1"+"0"*(parBits-7)+"101000"):
|
||||||
|
toPrint.append(padBin(toBin(m),bit=parBits)+": "+cmd+" "+colored(padBin(toBin(memory[m]))[opBits:],color[(toDec(padBin(toBin(memory[m]))[opBits:])-toDec("1"+"0"*(parBits-1)))][0],color[(toDec(padBin(toBin(memory[m]))[opBits:])-toDec("1"+"0"*(parBits-1)))][1]))
|
||||||
|
else:
|
||||||
|
toPrint.append(padBin(toBin(m),bit=parBits)+": "+cmd+" "+padBin(toBin(memory[m]))[opBits:])
|
||||||
|
origPrint = copy.copy(toPrint)
|
||||||
|
for l in range(len(toPrint)):
|
||||||
|
cmd = origPrint[l][10:][:3]
|
||||||
|
par = origPrint[l][14:]
|
||||||
|
if cmd == "JMP" or cmd == "JMN":
|
||||||
|
toPrint[toDec(par)] = toPrint[toDec(par)] + " <-+"
|
||||||
|
if cmd=="JMP":
|
||||||
|
toPrint[l] = toPrint[l] + " --+"
|
||||||
|
else:
|
||||||
|
toPrint[l] = toPrint[l] + " --?"
|
||||||
|
if l < toDec(par):
|
||||||
|
for i in range(len(toPrint)):
|
||||||
|
if l < i < toDec(par):
|
||||||
|
toPrint[i] = toPrint[i] + " |"
|
||||||
|
elif i==toDec(par) or i==l:
|
||||||
|
t=1
|
||||||
|
else:
|
||||||
|
toPrint[i] = toPrint[i] + " "
|
||||||
|
else:
|
||||||
|
for i in range(len(toPrint)):
|
||||||
|
if toDec(par) < i < l:
|
||||||
|
toPrint[i] = toPrint[i] + " |"
|
||||||
|
elif i==toDec(par) or i==l:
|
||||||
|
t=1
|
||||||
|
else:
|
||||||
|
toPrint[i] = toPrint[i] + " "
|
||||||
|
for p in range(len(toPrint)):
|
||||||
|
print(toPrint[p])
|
||||||
|
elif x=="halting":
|
||||||
|
print("[i] Will try to give you information about the divergity of your algorythm")
|
||||||
|
print("[i] This algorythm may itself not diverge!")
|
||||||
|
bPointer = pointer
|
||||||
|
bRegisterA = registerA
|
||||||
|
bMemory = memory
|
||||||
|
runtime = 0
|
||||||
|
stateHashes = []
|
||||||
|
print("[*] Simulating...")
|
||||||
|
while True:
|
||||||
|
runtime+=1
|
||||||
|
cmd = padBin(toBin(memory[pointer]))[:opBits]
|
||||||
|
par = int(toDec(padBin(toBin(memory[pointer]))[-parBits:]))
|
||||||
|
if parse(cmd,par)==-1:
|
||||||
|
pointer = 0
|
||||||
|
halts = True
|
||||||
|
print("[*] Algorythm will diverge after "+str(runtime)+" steps")
|
||||||
|
break
|
||||||
|
pointer = (pointer + 1) % 2**parBits
|
||||||
|
stateHash = hashlib.sha256(str(pointer)+str(registerA)+str(memory)).digest()
|
||||||
|
if stateHash in stateHashes:
|
||||||
|
print("[*] Algorythm will not diverge: State-Collision after "+str(runtime)+" steps")
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
stateHashes.append(stateHash)
|
||||||
|
pointer = bPointer
|
||||||
|
registerA = bRegisterA
|
||||||
|
memory = bMemory
|
||||||
|
elif x=="exit":
|
||||||
|
exit()
|
||||||
|
elif x=="":
|
||||||
|
_=0
|
||||||
|
else:
|
||||||
|
print("[!] Error")
|
80
multiply_test.txt
Normal file
80
multiply_test.txt
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
Multiplier V.1:
|
||||||
|
|
||||||
|
Inputs:
|
||||||
|
|
||||||
|
INP
|
||||||
|
STA 10000000 #Input 1
|
||||||
|
INP
|
||||||
|
STA 10000001 #Input 2
|
||||||
|
|
||||||
|
Counter-Setup: 00000100
|
||||||
|
|
||||||
|
LDI 01111111 #konst
|
||||||
|
SUB 10000001 #Input 2
|
||||||
|
STA 10000010 #Round-Pointer
|
||||||
|
|
||||||
|
Add: 00001000
|
||||||
|
|
||||||
|
LDA 10000011 #Out
|
||||||
|
ADD 10000000 #Input 1
|
||||||
|
STA 10000011
|
||||||
|
|
||||||
|
Count: 00001100
|
||||||
|
|
||||||
|
LDI 00000001
|
||||||
|
ADD 10000010
|
||||||
|
STA 10000010
|
||||||
|
|
||||||
|
Jump back: 00010001
|
||||||
|
|
||||||
|
LDA 10000010
|
||||||
|
JIF to Add
|
||||||
|
|
||||||
|
Outputs: 00010011
|
||||||
|
|
||||||
|
LDA 10000011
|
||||||
|
OUT
|
||||||
|
HLT
|
||||||
|
|
||||||
|
|
||||||
|
##########
|
||||||
|
|
||||||
|
Multiplier V.2:
|
||||||
|
|
||||||
|
Inputs:
|
||||||
|
|
||||||
|
INP
|
||||||
|
STA 10000000 #Input 1
|
||||||
|
INP
|
||||||
|
STA 10000001 #Input 2
|
||||||
|
|
||||||
|
Counter-Setup: 00000100
|
||||||
|
|
||||||
|
LDI 10000000 #konst
|
||||||
|
ADD 10000001 #Input 2
|
||||||
|
STA 10000010 #Round-Pointer
|
||||||
|
|
||||||
|
Add: 00001000
|
||||||
|
|
||||||
|
LDA 10000011 #Out
|
||||||
|
ADD 10000000 #Input 1
|
||||||
|
STA 10000011
|
||||||
|
|
||||||
|
Count: 00001100
|
||||||
|
|
||||||
|
LDI 00000001
|
||||||
|
STA 10000100 #temp1
|
||||||
|
LDA 10000010 #Round-Pointer
|
||||||
|
SUB 10000100 #temp1
|
||||||
|
STA 10000010 #Round-Pointer
|
||||||
|
|
||||||
|
Jump back: 00010001
|
||||||
|
|
||||||
|
LDA 10000010
|
||||||
|
JIF to Add
|
||||||
|
|
||||||
|
Outputs: 00010011
|
||||||
|
|
||||||
|
LDA 10000011
|
||||||
|
OUT
|
||||||
|
HLT
|
57
pow_test.txt
Normal file
57
pow_test.txt
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
0000 NOP
|
||||||
|
0001 LDA memory > A
|
||||||
|
0010 ADD A + memspace
|
||||||
|
0011 SUB A - memspace
|
||||||
|
0100 STA A > memory
|
||||||
|
0101 OUT A > OUT
|
||||||
|
0110 JMP Jump to adress
|
||||||
|
0111 LDI par > A
|
||||||
|
1000 JIF Jump if (JC)
|
||||||
|
1001 HLO H-mem[memspace] > A
|
||||||
|
1010 HST A > H-mem[memspace]
|
||||||
|
1011 INP IN > A
|
||||||
|
1100 JMA Jump to A
|
||||||
|
1101 JRF Jump forward by par
|
||||||
|
1110 JRB Jump bachward by par
|
||||||
|
1111 HLT Halt
|
||||||
|
|
||||||
|
#####################
|
||||||
|
|
||||||
|
Start - Inputs #00011000
|
||||||
|
|
||||||
|
INP
|
||||||
|
STA 10001000 # Base
|
||||||
|
STA 10001011 # MultiplyRet
|
||||||
|
INP
|
||||||
|
STA 10001001 # Pow
|
||||||
|
|
||||||
|
Pow Counter #00011101
|
||||||
|
|
||||||
|
LDI 01111111
|
||||||
|
ADD 10001001
|
||||||
|
STA 10001010 # Pow counter
|
||||||
|
|
||||||
|
Call Multiply #00100001
|
||||||
|
|
||||||
|
LDA 10001000
|
||||||
|
STA 10000000 # Multiply Inp 1
|
||||||
|
LDA 10001011
|
||||||
|
STA 10000001 # Mutliply Inp 2
|
||||||
|
JMP 00000001
|
||||||
|
|
||||||
|
Loop # 00100111
|
||||||
|
|
||||||
|
STA 10001011
|
||||||
|
|
||||||
|
LDA 10001010
|
||||||
|
SUB NopAtEnd # Pow Counter
|
||||||
|
STA 10001010
|
||||||
|
JIF to Call Mutliply
|
||||||
|
|
||||||
|
Out
|
||||||
|
|
||||||
|
LDA 10001011
|
||||||
|
OUT
|
||||||
|
HLT 11111111
|
||||||
|
|
||||||
|
NOP 00000001
|
BIN
saves/bug_find.asmTron
Normal file
BIN
saves/bug_find.asmTron
Normal file
Binary file not shown.
1
saves/color.asmTron
Normal file
1
saves/color.asmTron
Normal file
@ -0,0 +1 @@
|
|||||||
|
xœí˹ƒ@QŸH°TÌ!BPøäoIÛY¨ª<C2A8>÷gœ^ïg,Ÿm_®õ~¶ù½—ë·çÜc°»³l²Å6K{ÐmÐmÐmÐmÐmÐ&mÒ&mÒ&mÒ&mÒ&mÒmÑmÑmÑmÑmÑ6mÓ6mÓ6mÏ6Æ9Ï<39>$I’$I’$I’$I’$I’$I’$I’$é¿]¯/¤Y~
|
BIN
saves/gbi_test.asmTron
Normal file
BIN
saves/gbi_test.asmTron
Normal file
Binary file not shown.
BIN
saves/haltingNonDivergent.asmTron
Normal file
BIN
saves/haltingNonDivergent.asmTron
Normal file
Binary file not shown.
BIN
saves/multiply.asmTron
Normal file
BIN
saves/multiply.asmTron
Normal file
Binary file not shown.
BIN
saves/pow.asmTron
Normal file
BIN
saves/pow.asmTron
Normal file
Binary file not shown.
BIN
saves/powPerf.asmTron
Normal file
BIN
saves/powPerf.asmTron
Normal file
Binary file not shown.
BIN
test.asmTron
Normal file
BIN
test.asmTron
Normal file
Binary file not shown.
BIN
tronScript/assemblyTest.asmTron
Normal file
BIN
tronScript/assemblyTest.asmTron
Normal file
Binary file not shown.
6
tronScript/assemblyTest.tron
Normal file
6
tronScript/assemblyTest.tron
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
:INP b#00000000
|
||||||
|
:STA v#A
|
||||||
|
:INP b#00000000
|
||||||
|
:ADD v#A
|
||||||
|
:OUT -
|
||||||
|
:HLT ~
|
BIN
tronScript/forTest.asmTron
Normal file
BIN
tronScript/forTest.asmTron
Normal file
Binary file not shown.
7
tronScript/forTest.tron
Normal file
7
tronScript/forTest.tron
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
read A
|
||||||
|
read B
|
||||||
|
|
||||||
|
setVar STEP 1
|
||||||
|
|
||||||
|
for A CLOCK STEP write B
|
||||||
|
|
BIN
tronScript/henrik.asmTron
Normal file
BIN
tronScript/henrik.asmTron
Normal file
Binary file not shown.
4
tronScript/henrik.tron
Normal file
4
tronScript/henrik.tron
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
setVar A 5
|
||||||
|
setVar B 7
|
||||||
|
add A B C
|
||||||
|
write C
|
BIN
tronScript/powCompiled.asmTron
Normal file
BIN
tronScript/powCompiled.asmTron
Normal file
Binary file not shown.
33
tronScript/powCompiled.tron
Normal file
33
tronScript/powCompiled.tron
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#inputs
|
||||||
|
read BASE
|
||||||
|
read EXPONENT
|
||||||
|
|
||||||
|
setVar ONE 1
|
||||||
|
setVar ZERO 0
|
||||||
|
|
||||||
|
#newVar mulCOUNTER
|
||||||
|
#newVar mulOUT
|
||||||
|
|
||||||
|
copyVar BASE powOUT
|
||||||
|
jumpMark powLoop
|
||||||
|
|
||||||
|
copyVar powOUT IN_A
|
||||||
|
copyVar BASE IN_B
|
||||||
|
copyVar ZERO mulOUT
|
||||||
|
copyVar ZERO mulCOUNTER
|
||||||
|
jump mulLoop
|
||||||
|
jumpMark next
|
||||||
|
add powCOUNTER ONE powCOUNTER
|
||||||
|
if powCOUNTER != EXPONENT jump powLoop
|
||||||
|
|
||||||
|
write mulOUT
|
||||||
|
|
||||||
|
halt
|
||||||
|
|
||||||
|
jumpMark mulLoop
|
||||||
|
add mulOUT IN_A mulOUT
|
||||||
|
add mulCOUNTER ONE mulCOUNTER
|
||||||
|
if IN_B != mulCOUNTER jump mulLoop
|
||||||
|
jump next
|
||||||
|
|
||||||
|
halt
|
BIN
tronScript/stack_test.asmTron
Normal file
BIN
tronScript/stack_test.asmTron
Normal file
Binary file not shown.
14
tronScript/stack_test.tron
Normal file
14
tronScript/stack_test.tron
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
setVar STEP 1
|
||||||
|
setVar A 5
|
||||||
|
push A
|
||||||
|
setVar A 3
|
||||||
|
push A
|
||||||
|
setVar A 7
|
||||||
|
push A
|
||||||
|
|
||||||
|
pop A
|
||||||
|
write A
|
||||||
|
pop A
|
||||||
|
write A
|
||||||
|
pop A
|
||||||
|
write A
|
45
tronScript/tronScript_README.txt
Normal file
45
tronScript/tronScript_README.txt
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
tronScript is a stupid pretty assembly-like low level language for my asmTron emulator
|
||||||
|
|
||||||
|
|
||||||
|
Available Commands:
|
||||||
|
|
||||||
|
newVar [var] - Initializes new var; not needed most of the time:
|
||||||
|
Compiler automatically initializes new vars when used
|
||||||
|
jumpMark [jumpMark name] - Sets an JumpMark to jump to using the "jump" command
|
||||||
|
jump [jumpMark name] - Jumps to an JumpMark
|
||||||
|
getVarAddress [var A] [var B] - Saves [var A]'s memory-address into [var B]
|
||||||
|
getArrayAddress [array] [index] [var] - Saves [array][[index]]'s memory-adress into [var]
|
||||||
|
exec [func name] - Executes function with name [func name]
|
||||||
|
setVar [var] [value] - Sets var [var] to [value] (decimal)
|
||||||
|
setVar [var] b[value] - Sets var [var] to [value] (binary)
|
||||||
|
setArrayIndex [array] [index] [value] - Sets [array][[index]] to [value] (decimal)
|
||||||
|
setArrayIndex [array] [index] b[value] - Sets [array][[index]] to [value] (binary)
|
||||||
|
getArrayIndex [array] [index] [var] - Stores the content of [array][[index]] into var [var]
|
||||||
|
copyVar [var A] [var B] - Copies to value of [var A] to [var B] (B=A)
|
||||||
|
write [var] - Writes value of [var] to console
|
||||||
|
read [var] - Reads input from console into [var]
|
||||||
|
add [var A] [var B] [var OUT] - Adds [var A] + [var B] = [var OUT]
|
||||||
|
subtract [var A] [var B] [var OUT] - Subtracts [var A] - [var B] = [var OUT]
|
||||||
|
if [var A] [??] [var B] [cmd] [pars] - ?? = [==,!=,>,<,>=,<=] if clause is true,
|
||||||
|
executes [cmd] with [pars]
|
||||||
|
for [goal] [counter] [step] [cmd] [pars]- Sets [counter] = 0; Executes [cmd] with [pars] and
|
||||||
|
adds [step] to [counter], until [goal]==[counter]
|
||||||
|
:[asm] v#[var] - Executes [asm]-assembler command with par [var]
|
||||||
|
:[asm] d#[par] - Executes [asm]-assembler command with par [par] (decimal)
|
||||||
|
:[asm] b#[par] - Executes [asm]-assembler command with par [par] (binary)
|
||||||
|
:[asm] - - Executes [asm]-assembler command with par b#00000000 / d#0
|
||||||
|
:[asm] ~ - Executes [asm]-assembler command with par b#11111111 / d#255
|
||||||
|
shellcode [code] - Executes binary code [code]
|
||||||
|
nop - Nothing; sleep one cycle
|
||||||
|
halt - Stops execution of program; automatically added to the end of
|
||||||
|
your program if not present
|
||||||
|
|
||||||
|
function [funcName] { - Defines function [funcName] with code [funcCode]
|
||||||
|
[funcCode line 1] ! Every function can only have one instance at a time:
|
||||||
|
[funcCode line 2] ! A function shall not call itself and a sub function shall
|
||||||
|
[funcCode line 3] ! not call any of its root functions !!!
|
||||||
|
. ! If you need this functionality, implement it in assembly...
|
||||||
|
.
|
||||||
|
. Why is that so? This is a stupid compiler, which does not use
|
||||||
|
[funcCode line n] a stack to return from functions, but a function-specific variable
|
||||||
|
} to store the jump-back-address... :P
|
Loading…
Reference in New Issue
Block a user