print("/q or quit for closing the app")
print("/h or help for list of commands")
def Help():
    print("ALU: ")
    print("add rx: adds register number x to r0.")
    print("sub rx: subtracts register number x from r0 and stores the result in r0.")
    print("not rx: Performs not on register number x and stores it in r0.")
    print("and rx: Performs and on register number x and r0. Then it stores the result into the r0.")
    print("or rx : Performs or on register number x and r0. Then it stores the result into the r0.")
    print("xor rx: Performs xor on register number x and r0. Then it stores the result into the r0.")
    print("lsh rx: Left shifts register number x and stores the result into the r0")
    print("lrsh rx: Performs a logical right shift on register number x and stores the result into the r0")
    print("arsh rx: Performs an arithmetic right shift on register number x and stores the result into the r0")
    print("cycl rx: Cycles register number x left and stores the result into the r0")
    print("cycr rx: Cycles register number x right and stores the result into the r0")
    print()
    print("Other: ")
    print("mov ra rb: Copies ra into rb.")
    print("ld ra: loads the next byte into ra.")
    print("ld ra mar: loads the byte at the address contained in r6 into ra.")
    print("ld ra ram: loads the byte at the address in ram contained in r6 into ra.")
    print("str ra: store ra into the byte in ram at the address contained in r6.")
    print("cmp ra: compares ra to r0 and updates the znc registers.")
    print("jmp znc: jumps to the address in r6 if the znc registers agree with the given values (eg. 010).")

def Get():
    return input("> ").lower().strip()

def reg(res: str):
    res = res.replace("r", "")
    res = int(res)
    res = format(res, '03b')
    return res
    
def printBin(res: str):
    res = int(res, 2)
    res = format(res, "02X")
    print(res)
    
def add(arg1: str):
    res = "10000" + reg(arg1)
    printBin(res)
    
def sub(arg1: str):
    res = "10001" + reg(arg1)
    printBin(res)
    
def Not(arg1: str):
    res = "10100" + reg(arg1)
    printBin(res)
    
def And(arg1: str):
    res = "10101" + reg(arg1)
    printBin(res)
    
def Or(arg1: str):
    res = "10110" + reg(arg1)
    printBin(res)
    
def Xor(arg1: str):
    res = "10111" + reg(arg1)
    printBin(res)
    
def lsh(arg1: str):
    res = "11000" + reg(arg1)
    printBin(res)
    
def lrsh(arg1: str):
    res = "11001" + reg(arg1)
    printBin(res)
    
def arsh(arg1: str):
    res = "10110" + reg(arg1)
    printBin(res)
    
def cycl(arg1: str):
    res = "11100" + reg(arg1)
    printBin(res)
    
def cycr(arg1: str):
    res = "11101" + reg(arg1)
    printBin(res)
    
def mov(arg1: str, arg2:str):
    res = "01" + reg(arg1) + reg(arg2)
    printBin(res)
    
def ld(args: list[str]):
    if len(args) == 1:
        res = "00110" + reg(args[0])
        printBin(res)
        return
    if args[1] == "mar":
        res = "00100" + reg(args[0])
        printBin(res)
        return
    res = "00101" + reg(args[0])
    printBin(res)
    
def Str(arg1: str):
    res = "00001" + reg(arg1)
    printBin(res)
    
def cmp(arg1: str):
    res = "00011" + reg(arg1)
    printBin(res)
    
def jmp(arg1: str):
    res = "00010" + arg1
    printBin(res)
    
ui = Get()
while ui != "/q" or ui != "quit":
    
    if ui == "/h" or ui == "help":
        Help()
        
    parts = ui.split()
    cmd = parts[0]
    
    match cmd:
        case "add":
            add(parts[1])
        case "sub":
            sub(parts[1])
        case "not":
            Not(parts[1])
        case "and":
            And(parts[1])
        case "or":
            Or(parts[1])
        case "xor":
            Xor(parts[1])
        case "lsh":
            lsh(parts[1])
        case "lrsh":
            lrsh(parts[1])
        case "arsh":
            arsh(parts[1])
        case "cycl":
            cycl(parts[1])
        case "cycr":
            cycr(parts[1])
        case "mov":
            mov(parts[1], parts[2])
        case "ld":
            ld(parts[1:])
        case "str":
            Str(parts[1])
        case "cmp":
            cmp(parts[1])
        case "jmp":
            jmp(parts[1])
        case _:
            print("command not recognized")
        
    ui = Get()