Assembly Simulator

REGISTERS = { 1 : 0,
2 : 0,
3 : 0,
4 : 0
}

MNEMONICS = { 'STOP' : 0,
'ADD' : 1,
'SUB' : 2,
'PRINT' : 3,
'READ' : 4,
'MOVER' : 5, # Moves from memory to register
'MOVEM' : 6,
'DC' : 7,
'DS' : 8,
}

Simulating loading of file in memory
with open("/content/drive/MyDrive/PracsSem2/Compiler/ASM.CODE") as f:
lines = f.readlines()
PROGRAM = [int(i.rstrip('\n')) for i in lines]
print(PROGRAM)

Declaring memory
MEMORY = [0 for i in range(10)]

PC = 0
while PC < len(PROGRAM):
print(MEMORY,REGISTERS)
opcode = int(PROGRAM[PC] / 10000)
operand_1 = int((PROGRAM[PC] % 10000) / 1000)
operand_2 = int((PROGRAM[PC] % 10000) % 1000)
print(opcode,operand_1,operand_2)
PC += 1
if opcode == 0: #stop
break
if opcode == 1: # add
REGISTERS[operand_1] += MEMORY[operand_2]
continue
if opcode == 2: # sub
REGISTERS[operand_1] -= MEMORY[operand_2]
continue
if opcode == 3: # print
print('------------>',MEMORY[operand_2])
continue
if opcode == 4: #input
MEMORY[operand_2] = int(input('Enter value : '))
continue
if opcode == 5: #from memory to reg
REGISTERS[operand_1] = MEMORY[operand_2]
continue
if opcode == 6: # from reg to mem
MEMORY[operand_2] = REGISTERS[operand_1]
continue

ASM.CODE

040000
040001
051000
011001
061002
030001
000000

Edit Report
Pub: 29 Apr 2022 19:32 UTC
Edit: 29 Apr 2022 19:32 UTC
Views: 5