from sys import stdin
import sys

################################################ change binary to integer #####################################################
def intfrbin(x):
    return int(x,2)

################################################ change binary to integer #####################################################
def binfrbin(x):
    return int(x)

################################################ change to binary 8 bits #####################################################
def binto8(x):
    return str("{0:08b}".format(x))

################################################ change to binary 16 bits ####################################################
def binto16(x):
    return str("{0:016b}".format(x))


################################################  FLAGS bits #####################################################
changebit={"V":8,"L":4,"G":2,"E":1}

################################################ Store cyclevalue ############################################################
CycleNO=[]

################################################ Store PC value ############################################################
PCNO=[]

################################################ ALL Instruvtion ############################################################
memory=[]

################################################ load  Instruvtion ############################################################
variables={}

################################################ REGISTER NAMES ############################################################
Regnames={"000":"R0","001":"R1","010":"R2","011":"R3","100":"R4","101":"R5","110":"R6","111":"FLAGS"}

################################################ REGISTER VALUES ############################################################
Regvalues={"R0":0,"R1":0,"R2":0,"R3":0,"R4":0,"R5":0,"R6":0,"FLAGS":0,}

################################################ TAKING INPUT ############################################################
for line in stdin:
    if line == '': # If empty string is read then stop the loop
        break
    if line[-1:]=="\n":
        line=line[:-1]    
    memory.append(line)

################################################ PROGRAMM COUNTER  ############################################################    
PC=0

################################################ FOR GRAPH ############################################################

############## programm counter is appended in PC no array ##################

################################################ cycleno ############################################################
Cycle=0

################################################ FOR GRAPH ############################################################

############## programm counter is appended in PC no array ##################

################################################ halted ############################################################
halted=False


################################################ SET FLAGS VALUES #####################################################
def setflag(konsa,yesNo): #######konsa matlab konsa bit change karna ha   ################yesNo mean value 1 karni ha ya na
    value=Regvalues.get("FLAGS")
    if (not yesNo):
        Regvalues["FLAGS"]=0
    else:
        Regvalues["FLAGS"]=changebit[konsa]

################################################ compare STATENMENTS #####################################################
def comparereg(r1,r2):
    if r1==r2:
        return 'E'
    elif r1>=r2:
        return 'G'
    else:
        return 'L'
    
################################################ bitwise operator STATENMENTS #####################################################
def xorreg(a, b):                             ########################### XOR FUNCTION
    return (a and not b) or (not a and b)  

def andreg(a,b):                                ######################## AND
    return (a and b)

def orreg(a,b):                                ######################## OR
    return (a or b)
 
def notreg(a):                                ##########################NOT
    return (not a)  

################################################ shift  STATENMENTS #####################################################
def lshift(x,a):
    return x>>a

def rshift(x,a):
    return x<<a
################################################ PRINT STATENMENTS #####################################################        
def PRINTSTATENMENT():
    global PC
    STRING=binto8(PC) + " " + str(binto16(Regvalues["R0"])) + " " + str(binto16(Regvalues["R1"])) + " " + str(binto16(Regvalues["R2"])) 
    STRING=STRING + " " + str(binto16(Regvalues["R3"])) + " " + str(binto16(Regvalues["R4"])) + " " + str(binto16(Regvalues["R5"])) 
    STRING=STRING + " " + str(binto16(Regvalues["R6"])) + " " + str(binto16(Regvalues["FLAGS"])) 
    sys.stdout.write(STRING +"\n")
    
    
def execute(ins):                             ##################################return new programm counter####################
    global PC
    new_PC=PC
    global halted
    opcode=ins[:5]
    
    if opcode=="10011":                     #####################################UPDATE Hallted to end the program #############
        halted=True
        return -1
    
    if opcode[0]!='1' and opcode!="01111":   #####################################UPDATE PC IF NO BRANCH ######################
        new_PC+=1
        
    elif opcode=="01111":
        new_PC=intfrbin(ins[8:])            ##################################### cinvert last 8 bit address into integer#######
        return new_PC
    
    elif opcode=="10000":                  ##################################### branch if less than ##########################
        if Regvalues["FLAGS"]==4:
            new_PC=intfrbin(ins[8:])       ##################################### cinvert last 8 bit address into integer#######
        else:
            new_PC+=1
            return new_PC
        
    elif opcode=="10001" :                   ##################################### branch if greater than #######################
        if Regvalues["FLAGS"]==2:
            new_PC=intfrbin(ins[8:])          ##################################### cinvert last 8 bit address into integer####### 
        else:
            new_PC+=1
        return new_PC
        
    else:                                  ##################################### branch if equal  ############################
        if Regvalues["FLAGS"]==1:
            new_PC=intfrbin(ins[8:])       ##################################### cinvert last 8 bit address into integer#######
        else:
            new_PC+=1
        return new_PC
######################################## MOVE instruction #####################################
    if opcode=="00010":
        reg=Regnames[ins[5:8]]
        value=intfrbin(ins[8:])
        Regvalues[reg]=value
        return new_PC
    elif opcode=="00011":
        reg1=Regnames[ins[10:13]]
        reg2=Regnames[ins[13:]]
        Regvalues[reg1]=Regvalues[reg2]
        return new_PC

######################################## LOAD and STORE  instruction #####################################
    if opcode=="00100":                        #########################   LOAD
        reg=Regnames[5:8]
        value=ins[8:]        ## binary value
        Regvalues[reg]=variables[value]
        return new_PC

    elif opcode=="00100":                      #########################   STORE
        reg=Regnames[5:8]
        value=ins[8:]         ## binary value
        variables[value]=Regvalues[reg]
        return new_PC
        
        
######################################## compare   instruction #####################################
    if opcode=="01110":
        reg1=Regnames[ins[10:13]]
        reg2=Regnames[ins[13:]]
        stre=comparereg(Regvalues[reg1],Regvalues[reg2])       #############################return g,e,l
        setflag(stre,True)                                      ###############################set flag according to g,e,l
        return new_PC
    
######################################## bit wise operator   instruction #####################################
    if opcode=="01101":                             ##################### invert
        reg1=Regnames[ins[10:13]]
        reg2=Regnames[ins[13:]]
        Regvalues[reg1]=notreg(Regvalues[reg2])
        return new_PC
    
    elif opcode=="01100":                                             ######################## ADD
        reg1=Regnames[ins[7:10]]
        reg2=Regnames[ins[10:13]]
        reg3=Regnames[ins[13:]]
        Regvalues[reg1]=andreg(Regvalues[reg2],Regvalues[reg3])
        return new_PC
        
    elif opcode=="01011":                                  ######################## Or
        reg1=Regnames[ins[7:10]]
        reg2=Regnames[ins[10:13]]
        reg3=Regnames[ins[13:]]
        Regvalues[reg1]=orreg(Regvalues[reg2],Regvalues[reg3])
        return new_PC
            
    elif opcode=="01010":                                  ######################## XOR            
        reg1=Regnames[7:10]
        reg2=Regnames[10:13]
        reg3=Regnames[13:]
        Regvalues[reg1]=orreg(Regvalues[reg2],Regvalues[reg3])
        return new_PC
        
######################################## ALL FOR updating FLAGS (V) bit mean FLAGS=8 #####################################
    if opcode=="00000":                           ################################# simple ADD
        reg1=Regnames[ins[7:10]]
        reg2=Regnames[ins[10:13]]
        reg3=Regnames[ins[13:]]
        a=Regvalues[reg2]
        b=Regvalues[reg3]
        c=a+b
        if c>255:
            c=255
            setflag('V',True)
        else:
            setflag('V',False)
        Regvalues[reg1]=c    
        return new_PC
        
    if opcode=="00000":                           ################################# simple sub
        reg1=Regnames[ins[7:10]]
        reg2=Regnames[ins[10:13]]
        reg3=Regnames[ins[13:]]
        a=Regvalues[reg2]
        b=Regvalues[reg3]
        c=a-b
        if c<0:
            c=0
            setflag('V',True)
        else:
            setflag('V',False)
        Regvalues[reg1]=c    
        return new_PC

    if opcode=="00000":                           ################################# simple multiply
        reg1=Regnames[ins[7:10]]
        reg2=Regnames[ins[10:13]]
        reg3=Regnames[ins[13:]]
        a=Regvalues[reg2]
        b=Regvalues[reg3]
        c=a*b
        if c>255:
            c=255
            setflag('V',True)
        else:
            setflag('V',False)
        Regvalues[reg1]=c
        return new_PC
    
    if opcode=="00000":                           ################################# simple divide
        reg1=Regnames[ins[10:13]]
        reg2=Regnames[ins[13:]]
        a=Regvalues[reg1]
        b=Regvalues[reg2]
        c=a/b
        d=a%b
        Regvalues["R0"]=c
        Regvalues["R1"]=d
        return new_PC
    
######################################## Shift  operator   instruction #####################################

    if opcode=="01000":                                   ############################# right shift
        reg=Regnames[ins[5:8]]
        value=intfrbin(ins[8:])                ##int value
        rs=rshift(Regvalues[reg],value)
        Regvalues[reg]=ls
        return new_PC
    
    if opcode=="01001":                                   ############################ left shift
        reg=Regnames[5:8]
        value=intfrbin(ins[8:]) ## int value
        ls=lshift(Regvalues[reg],value)
        Regvalues[reg]=ls
        return new_PC   
    
################################################ Main code  ############################################################
while (not halted):
    CycleNO.append(Cycle)                 ########################################## append value for graph cycle
    Cycle+=1
    PCNO.append(PC)                      ########################################## append value for graph PC
    
    
    Instruction = memory[PC]                          ################################# Get current instructionhalted
    
    new_PC=execute(Instruction)                       #################################Update Program Counter
    
    PRINTSTATENMENT()                                 ############################################# Print the ALL things
    
    PC=new_PC                                       #################################### UPDATE VALUE IN PC 

    
################################################ Print all MEMORY ############################################################ 
for i in memory:
    sys.stdout.write(i+"\n")     

Python Online Compiler

Write, Run & Share Python code online using OneCompiler's Python online compiler for free. It's one of the robust, feature-rich online compilers for python language, supporting both the versions which are Python 3 and Python 2.7. Getting started with the OneCompiler's Python editor is easy and fast. The editor shows sample boilerplate code when you choose language as Python or Python2 and start coding.

Taking inputs (stdin)

OneCompiler's python online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample python program which takes name as input and print your name with hello.

import sys
name = sys.stdin.readline()
print("Hello "+ name)

About Python

Python is a very popular general-purpose programming language which was created by Guido van Rossum, and released in 1991. It is very popular for web development and you can build almost anything like mobile apps, web apps, tools, data analytics, machine learning etc. It is designed to be simple and easy like english language. It's is highly productive and efficient making it a very popular language.

Tutorial & Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition IF-ELSE is used.

if conditional-expression
    #code
elif conditional-expression
    #code
else:
    #code

Note:

Indentation is very important in Python, make sure the indentation is followed correctly

2. For:

For loop is used to iterate over arrays(list, tuple, set, dictionary) or strings.

Example:

mylist=("Iphone","Pixel","Samsung")
for i in mylist:
    print(i)

3. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while condition  
    #code 

Collections

There are four types of collections in Python.

1. List:

List is a collection which is ordered and can be changed. Lists are specified in square brackets.

Example:

mylist=["iPhone","Pixel","Samsung"]
print(mylist)

2. Tuple:

Tuple is a collection which is ordered and can not be changed. Tuples are specified in round brackets.

Example:

myTuple=("iPhone","Pixel","Samsung")
print(myTuple)

Below throws an error if you assign another value to tuple again.

myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
myTuple[1]="onePlus"
print(myTuple)

3. Set:

Set is a collection which is unordered and unindexed. Sets are specified in curly brackets.

Example:

myset = {"iPhone","Pixel","Samsung"}
print(myset)

4. Dictionary:

Dictionary is a collection of key value pairs which is unordered, can be changed, and indexed. They are written in curly brackets with key - value pairs.

Example:

mydict = {
    "brand" :"iPhone",
    "model": "iPhone 11"
}
print(mydict)

Supported Libraries

Following are the libraries supported by OneCompiler's Python compiler

NameDescription
NumPyNumPy python library helps users to work on arrays with ease
SciPySciPy is a scientific computation library which depends on NumPy for convenient and fast N-dimensional array manipulation
SKLearn/Scikit-learnScikit-learn or Scikit-learn is the most useful library for machine learning in Python
PandasPandas is the most efficient Python library for data manipulation and analysis
DOcplexDOcplex is IBM Decision Optimization CPLEX Modeling for Python, is a library composed of Mathematical Programming Modeling and Constraint Programming Modeling