Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
To create an interpreter for your custom assembly language in Python, we need to follow these steps: 1. Parse the input program. 2. Implement a virtual machine that can execute the instructions. 3. Maintain the state of the registers and memory as the program runs. 4. Return the memory content after the program completes. Here is a Python implementation of the interpreter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | import re def evaluate(program): # Define the program memory and registers registers = {'R{}'.format(i): 0 for i in range(9)} # R1 to R8, R0 is not used memory = [] # List to store prime numbers # Parse the program into lines and instructions lines = program.strip().split('\n') instructions = [line.split() for line in lines if line and not line.startswith('//')] # Label mapping for jump instructions labels = {} for idx, instr in enumerate(instructions): if instr[0].endswith(':'): labels[instr[0][:-1]] = idx # Helper functions for instruction execution def get_value(op): if op.startswith('R'): return registers[op] return int(op) pc = 0 # Program counter while pc < len(instructions): instr = instructions[pc] opcode = instr[0] if opcode == 'SET': registers[instr[1]] = get_value(instr[2]) elif opcode == 'JMP': if len(instr) > 2: pc = labels[instr[2]] continue else: pc = labels[instr[1]] continue elif opcode == 'EQ': registers[instr[1]] = 1 if get_value(instr[1]) == get_value(instr[2]) else 0 elif opcode == 'JF': if not get_value(instr[1]): pc = labels[instr[2]] continue elif opcode == 'JT': if get_value(instr[1]): pc = labels[instr[2]] continue elif opcode == 'INC': registers[instr[1]] += 1 elif opcode == 'STORE': memory.append(registers[instr[1]]) elif opcode == 'MUL': registers[instr[1]] = get_value(instr[2]) * get_value(instr[3]) elif opcode == 'GT': registers[instr[1]] = 1 if get_value(instr[1]) > get_value(instr[2]) else 0 elif opcode == 'MOD': registers[instr[1]] = get_value(instr[2]) % get_value(instr[3]) elif opcode == 'SET': registers[instr[1]] = get_value(instr[2]) pc += 1 return memory program = """ SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """ print(evaluate(program)) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | import re def evaluate(program): # Define the program memory and registers registers = {'R{}'.format(i): 0 for i in range(9)} # R1 to R8, R0 is not used memory = [] # List to store prime numbers # Parse the program into lines and instructions lines = program.strip().split('\n') instructions = [line.split() for line in lines if line and not line.startswith('//')] # Label mapping for jump instructions labels = {} for idx, instr in enumerate(instructions): if instr[0].endswith(':'): labels[instr[0][:-1]] = idx # Helper functions for instruction execution def get_value(op): if op.startswith('R'): return registers[op] return int(op) pc = 0 # Program counter while pc < len(instructions): instr = instructions[pc] opcode = instr[0] if opcode == 'SET': registers[instr[1]] = get_value(instr[2]) elif opcode == 'JMP': if len(instr) > 2: pc = labels[instr[2]] continue else: pc = labels[instr[1]] continue elif opcode == 'EQ': registers[instr[1]] = 1 if get_value(instr[1]) == get_value(instr[2]) else 0 elif opcode == 'JF': if not get_value(instr[1]): pc = labels[instr[2]] continue elif opcode == 'JT': if get_value(instr[1]): pc = labels[instr[2]] continue elif opcode == 'INC': registers[instr[1]] += 1 elif opcode == 'STORE': memory.append(registers[instr[1]]) elif opcode == 'MUL': registers[instr[1]] = get_value(instr[2]) * get_value(instr[3]) elif opcode == 'GT': registers[instr[1]] = 1 if get_value(instr[1]) > get_value(instr[2]) else 0 elif opcode == 'MOD': registers[instr[1]] = get_value(instr[2]) % get_value(instr[3]) elif opcode == 'SET': registers[instr[1]] = get_value(instr[2]) pc += 1 return memory program = """ SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """ print(evaluate(program)) |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | import re def evaluate(program): # Define the program memory and registers registers = {'R{}'.format(i): 0 for i in range(9)} # R1 to R8, R0 is not used memory = [] # List to store prime numbers # Parse the program into lines and instructions lines = program.strip().split('\n') instructions = [line.split() for line in lines if line and not line.startswith('//')] # Label mapping for jump instructions labels = {} for idx, instr in enumerate(instructions): if instr[0].endswith(':'): labels[instr[0][:-1]] = idx # Helper functions for instruction execution def get_value(op): if op.startswith('R'): return registers[op] return int(op) pc = 0 # Program counter while pc < len(instructions): instr = instructions[pc] opcode = instr[0] if opcode == 'SET': registers[instr[1]] = get_value(instr[2]) elif opcode == 'JMP': if len(instr) > 2: pc = labels[instr[2]] continue else: pc = labels[instr[1]] continue elif opcode == 'EQ': registers[instr[1]] = 1 if get_value(instr[1]) == get_value(instr[2]) else 0 elif opcode == 'JF': if not get_value(instr[1]): pc = labels[instr[2]] continue elif opcode == 'JT': if get_value(instr[1]): pc = labels[instr[2]] continue elif opcode == 'INC': registers[instr[1]] += 1 elif opcode == 'STORE': memory.append(registers[instr[1]]) elif opcode == 'MUL': registers[instr[1]] = get_value(instr[2]) * get_value(instr[3]) elif opcode == 'GT': registers[instr[1]] = 1 if get_value(instr[1]) > get_value(instr[2]) else 0 elif opcode == 'MOD': registers[instr[1]] = get_value(instr[2]) % get_value(instr[3]) elif opcode == 'SET': registers[instr[1]] = get_value(instr[2]) pc += 1 return memory program = """ SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """ print(evaluate(program)) answer = evaluate(""" SET R1 0 SET R2 1 loop: MUL R3 R2 R2 STORE R3 R1 INC R1 INC R2 EQ R1 20 JF loop HCF """)[:10] expected = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' answer = evaluate(""" SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """)[:10] expected = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
Traceback (most recent call last):
File "/usr/src/app/main.py", line 117, in <module>
print(evaluate(program))
File "/usr/src/app/main.py", line 46, in evaluate
if get_value(instr[1]):
File "/usr/src/app/main.py", line 23, in get_value
return int(op)
ValueError: invalid literal for int() with base 10: 'is_prime_end'
Testing if the previous output contains the string All tests passed
: False
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
To create a Python interpreter for your assembly-like language, we'll need to simulate the execution of the program, managing registers, memory, and control flow. Here's a Python implementation to interpret the provided assembly program and return the resulting memory state:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | import math class AssemblyInterpreter: def __init__(self, program): self.program = program.split('\n') self.registers = {f'R{i}': 0 for i in range(1, 10)} self.memory = [] self.labels = {} self.pc = 0 def parse_program(self): for i, line in enumerate(self.program): line = line.strip() if line.endswith(':'): label = line[:-1] self.labels[label] = i def run(self): while self.pc < len(self.program): line = self.program[self.pc].strip() if not line or line.endswith(':'): self.pc += 1 continue self.execute(line) def execute(self, line): parts = line.split() instr = parts[0] if instr == 'SET': self.registers[parts[1]] = int(parts[2]) elif instr == 'INC': self.registers[parts[1]] += 1 elif instr == 'EQ': self.registers[parts[1]] = 1 if self.registers[parts[2]] == int(parts[3]) else 0 elif instr == 'JF': if self.registers[parts[1]] == 0: self.pc = self.labels[parts[2]] return elif instr == 'JT': if self.registers[parts[1]] != 0: self.pc = self.labels[parts[2]] return elif instr == 'JMP': self.pc = self.labels[parts[1]] return elif instr == 'MUL': self.registers[parts[1]] = self.registers[parts[2]] * self.registers[parts[3]] elif instr == 'GT': self.registers[parts[1]] = 1 if self.registers[parts[2]] > self.registers[parts[3]] else 0 elif instr == 'MOD': self.registers[parts[1]] = self.registers[parts[2]] % self.registers[parts[3]] elif instr == 'STORE': addr = self.registers[parts[2]] if addr >= len(self.memory): self.memory.extend([0] * (addr + 1 - len(self.memory))) self.memory[addr] = self.registers[parts[1]] self.pc += 1 def evaluate(program): interpreter = AssemblyInterpreter(program) interpreter.parse_program() interpreter.run() return interpreter.memory # Example usage program = """ SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """ print(evaluate(program)) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | import math class AssemblyInterpreter: def __init__(self, program): self.program = program.split('\n') self.registers = {f'R{i}': 0 for i in range(1, 10)} self.memory = [] self.labels = {} self.pc = 0 def parse_program(self): for i, line in enumerate(self.program): line = line.strip() if line.endswith(':'): label = line[:-1] self.labels[label] = i def run(self): while self.pc < len(self.program): line = self.program[self.pc].strip() if not line or line.endswith(':'): self.pc += 1 continue self.execute(line) def execute(self, line): parts = line.split() instr = parts[0] if instr == 'SET': self.registers[parts[1]] = int(parts[2]) elif instr == 'INC': self.registers[parts[1]] += 1 elif instr == 'EQ': self.registers[parts[1]] = 1 if self.registers[parts[2]] == int(parts[3]) else 0 elif instr == 'JF': if self.registers[parts[1]] == 0: self.pc = self.labels[parts[2]] return elif instr == 'JT': if self.registers[parts[1]] != 0: self.pc = self.labels[parts[2]] return elif instr == 'JMP': self.pc = self.labels[parts[1]] return elif instr == 'MUL': self.registers[parts[1]] = self.registers[parts[2]] * self.registers[parts[3]] elif instr == 'GT': self.registers[parts[1]] = 1 if self.registers[parts[2]] > self.registers[parts[3]] else 0 elif instr == 'MOD': self.registers[parts[1]] = self.registers[parts[2]] % self.registers[parts[3]] elif instr == 'STORE': addr = self.registers[parts[2]] if addr >= len(self.memory): self.memory.extend([0] * (addr + 1 - len(self.memory))) self.memory[addr] = self.registers[parts[1]] self.pc += 1 def evaluate(program): interpreter = AssemblyInterpreter(program) interpreter.parse_program() interpreter.run() return interpreter.memory # Example usage program = """ SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """ print(evaluate(program)) |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | import math class AssemblyInterpreter: def __init__(self, program): self.program = program.split('\n') self.registers = {f'R{i}': 0 for i in range(1, 10)} self.memory = [] self.labels = {} self.pc = 0 def parse_program(self): for i, line in enumerate(self.program): line = line.strip() if line.endswith(':'): label = line[:-1] self.labels[label] = i def run(self): while self.pc < len(self.program): line = self.program[self.pc].strip() if not line or line.endswith(':'): self.pc += 1 continue self.execute(line) def execute(self, line): parts = line.split() instr = parts[0] if instr == 'SET': self.registers[parts[1]] = int(parts[2]) elif instr == 'INC': self.registers[parts[1]] += 1 elif instr == 'EQ': self.registers[parts[1]] = 1 if self.registers[parts[2]] == int(parts[3]) else 0 elif instr == 'JF': if self.registers[parts[1]] == 0: self.pc = self.labels[parts[2]] return elif instr == 'JT': if self.registers[parts[1]] != 0: self.pc = self.labels[parts[2]] return elif instr == 'JMP': self.pc = self.labels[parts[1]] return elif instr == 'MUL': self.registers[parts[1]] = self.registers[parts[2]] * self.registers[parts[3]] elif instr == 'GT': self.registers[parts[1]] = 1 if self.registers[parts[2]] > self.registers[parts[3]] else 0 elif instr == 'MOD': self.registers[parts[1]] = self.registers[parts[2]] % self.registers[parts[3]] elif instr == 'STORE': addr = self.registers[parts[2]] if addr >= len(self.memory): self.memory.extend([0] * (addr + 1 - len(self.memory))) self.memory[addr] = self.registers[parts[1]] self.pc += 1 def evaluate(program): interpreter = AssemblyInterpreter(program) interpreter.parse_program() interpreter.run() return interpreter.memory # Example usage program = """ SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """ print(evaluate(program)) answer = evaluate(""" SET R1 0 SET R2 1 loop: MUL R3 R2 R2 STORE R3 R1 INC R1 INC R2 EQ R1 20 JF loop HCF """)[:10] expected = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' answer = evaluate(""" SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """)[:10] expected = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
Traceback (most recent call last):
File "/usr/src/app/main.py", line 120, in <module>
print(evaluate(program))
File "/usr/src/app/main.py", line 65, in evaluate
interpreter.run()
File "/usr/src/app/main.py", line 25, in run
self.execute(line)
File "/usr/src/app/main.py", line 51, in execute
self.registers[parts[1]] = 1 if self.registers[parts[2]] > self.registers[parts[3]] else 0
KeyError: '//'
Testing if the previous output contains the string All tests passed
: False
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
Creating an interpreter for the given hypothetical assembly language involves parsing the program, maintaining the state of the registers and memory, and executing the instructions step-by-step. Here's a simplified version of the interpreter in Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | import re def evaluate(program): # Initialize registers and memory registers = [0] * 9 # R1 to R8 memory = [] ip = 0 # Instruction pointer program_lines = program.strip().split('\n') labels = {} # Parse labels and instructions instructions = [] for line in program_lines: line = line.strip() if ':' in line: label, instr = line.split(':') labels[label.strip()] = len(instructions) if instr.strip(): instructions.append(instr.strip()) else: instructions.append(line) def debug_state(): print(f"IP: {ip}, Registers: {registers}, Memory: {memory}") while ip < len(instructions): instr = instructions[ip].split() cmd = instr[0] if cmd == 'SET': reg = int(instr[1][1]) val = int(instr[2]) registers[reg] = val elif cmd == 'INC': reg = int(instr[1][1]) registers[reg] += 1 elif cmd == 'JMP': label = instr[1] if len(instr) == 2: ip = labels[label] - 1 else: jump_label = instr[2] if registers[int(instr[1][1])] == 1: ip = labels[jump_label] - 1 elif cmd == 'EQ': reg = int(instr[1][1]) val = int(instr[2]) if registers[reg] == val: registers[9] = 1 # Use register 9 as flag else: registers[9] = 0 elif cmd == 'JF': if registers[9] == 0: ip += 1 ip -= 1 elif cmd == 'STORE': val = registers[int(instr[1][1])] addr = registers[int(instr[2][1])] if addr >= len(memory): memory.extend([0] * (addr - len(memory) + 1)) memory[addr] = val elif cmd == 'MUL': reg_d = int(instr[1][1]) reg_s1 = int(instr[2][1]) reg_s2 = int(instr[3][1]) registers[reg_d] = registers[reg_s1] * registers[reg_s2] elif cmd == 'GT': reg_d = int(instr[1][1]) reg_s = int(instr[2][1]) if registers[reg_d] > registers[reg_s]: registers[9] = 1 else: registers[9] = 0 elif cmd == 'JT': label = instr[1] if registers[9] == 1: ip = labels[label] - 1 elif cmd == 'MOD': reg_d = int(instr[1][1]) reg_s1 = int(instr[2][1]) reg_s2 = int(instr[3][1]) registers[reg_d] = registers[reg_s1] % registers[reg_s2] ip += 1 return memory # Example usage program = """ SET R1 2 start_find_primes: JMP is_prime is_prime ready_prime: EQ R2 1 JF increment STORE R1 R8 INC R8 EQ R8 100 JF increment JMP end increment: INC R1 JMP start_find_primes is_prime: SET R2 1 SET R3 2 start_loop: MUL R4 R3 R3 GT R4 R1 JT is_prime_end MOD R6 R1 R3 EQ R7 R6 0 JT not_prime INC R3 JMP start_loop not_prime: SET R2 0 is_prime_end: JMP ready_prime end: """ print(evaluate(program)) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | import re def evaluate(program): # Initialize registers and memory registers = [0] * 9 # R1 to R8 memory = [] ip = 0 # Instruction pointer program_lines = program.strip().split('\n') labels = {} # Parse labels and instructions instructions = [] for line in program_lines: line = line.strip() if ':' in line: label, instr = line.split(':') labels[label.strip()] = len(instructions) if instr.strip(): instructions.append(instr.strip()) else: instructions.append(line) def debug_state(): print(f"IP: {ip}, Registers: {registers}, Memory: {memory}") while ip < len(instructions): instr = instructions[ip].split() cmd = instr[0] if cmd == 'SET': reg = int(instr[1][1]) val = int(instr[2]) registers[reg] = val elif cmd == 'INC': reg = int(instr[1][1]) registers[reg] += 1 elif cmd == 'JMP': label = instr[1] if len(instr) == 2: ip = labels[label] - 1 else: jump_label = instr[2] if registers[int(instr[1][1])] == 1: ip = labels[jump_label] - 1 elif cmd == 'EQ': reg = int(instr[1][1]) val = int(instr[2]) if registers[reg] == val: registers[9] = 1 # Use register 9 as flag else: registers[9] = 0 elif cmd == 'JF': if registers[9] == 0: ip += 1 ip -= 1 elif cmd == 'STORE': val = registers[int(instr[1][1])] addr = registers[int(instr[2][1])] if addr >= len(memory): memory.extend([0] * (addr - len(memory) + 1)) memory[addr] = val elif cmd == 'MUL': reg_d = int(instr[1][1]) reg_s1 = int(instr[2][1]) reg_s2 = int(instr[3][1]) registers[reg_d] = registers[reg_s1] * registers[reg_s2] elif cmd == 'GT': reg_d = int(instr[1][1]) reg_s = int(instr[2][1]) if registers[reg_d] > registers[reg_s]: registers[9] = 1 else: registers[9] = 0 elif cmd == 'JT': label = instr[1] if registers[9] == 1: ip = labels[label] - 1 elif cmd == 'MOD': reg_d = int(instr[1][1]) reg_s1 = int(instr[2][1]) reg_s2 = int(instr[3][1]) registers[reg_d] = registers[reg_s1] % registers[reg_s2] ip += 1 return memory # Example usage program = """ SET R1 2 start_find_primes: JMP is_prime is_prime ready_prime: EQ R2 1 JF increment STORE R1 R8 INC R8 EQ R8 100 JF increment JMP end increment: INC R1 JMP start_find_primes is_prime: SET R2 1 SET R3 2 start_loop: MUL R4 R3 R3 GT R4 R1 JT is_prime_end MOD R6 R1 R3 EQ R7 R6 0 JT not_prime INC R3 JMP start_loop not_prime: SET R2 0 is_prime_end: JMP ready_prime end: """ print(evaluate(program)) |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | import re def evaluate(program): # Initialize registers and memory registers = [0] * 9 # R1 to R8 memory = [] ip = 0 # Instruction pointer program_lines = program.strip().split('\n') labels = {} # Parse labels and instructions instructions = [] for line in program_lines: line = line.strip() if ':' in line: label, instr = line.split(':') labels[label.strip()] = len(instructions) if instr.strip(): instructions.append(instr.strip()) else: instructions.append(line) def debug_state(): print(f"IP: {ip}, Registers: {registers}, Memory: {memory}") while ip < len(instructions): instr = instructions[ip].split() cmd = instr[0] if cmd == 'SET': reg = int(instr[1][1]) val = int(instr[2]) registers[reg] = val elif cmd == 'INC': reg = int(instr[1][1]) registers[reg] += 1 elif cmd == 'JMP': label = instr[1] if len(instr) == 2: ip = labels[label] - 1 else: jump_label = instr[2] if registers[int(instr[1][1])] == 1: ip = labels[jump_label] - 1 elif cmd == 'EQ': reg = int(instr[1][1]) val = int(instr[2]) if registers[reg] == val: registers[9] = 1 # Use register 9 as flag else: registers[9] = 0 elif cmd == 'JF': if registers[9] == 0: ip += 1 ip -= 1 elif cmd == 'STORE': val = registers[int(instr[1][1])] addr = registers[int(instr[2][1])] if addr >= len(memory): memory.extend([0] * (addr - len(memory) + 1)) memory[addr] = val elif cmd == 'MUL': reg_d = int(instr[1][1]) reg_s1 = int(instr[2][1]) reg_s2 = int(instr[3][1]) registers[reg_d] = registers[reg_s1] * registers[reg_s2] elif cmd == 'GT': reg_d = int(instr[1][1]) reg_s = int(instr[2][1]) if registers[reg_d] > registers[reg_s]: registers[9] = 1 else: registers[9] = 0 elif cmd == 'JT': label = instr[1] if registers[9] == 1: ip = labels[label] - 1 elif cmd == 'MOD': reg_d = int(instr[1][1]) reg_s1 = int(instr[2][1]) reg_s2 = int(instr[3][1]) registers[reg_d] = registers[reg_s1] % registers[reg_s2] ip += 1 return memory # Example usage program = """ SET R1 2 start_find_primes: JMP is_prime is_prime ready_prime: EQ R2 1 JF increment STORE R1 R8 INC R8 EQ R8 100 JF increment JMP end increment: INC R1 JMP start_find_primes is_prime: SET R2 1 SET R3 2 start_loop: MUL R4 R3 R3 GT R4 R1 JT is_prime_end MOD R6 R1 R3 EQ R7 R6 0 JT not_prime INC R3 JMP start_loop not_prime: SET R2 0 is_prime_end: JMP ready_prime end: """ print(evaluate(program)) answer = evaluate(""" SET R1 0 SET R2 1 loop: MUL R3 R2 R2 STORE R3 R1 INC R1 INC R2 EQ R1 20 JF loop HCF """)[:10] expected = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' answer = evaluate(""" SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """)[:10] expected = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
Traceback (most recent call last):
File "/usr/src/app/main.py", line 133, in <module>
print(evaluate(program))
File "/usr/src/app/main.py", line 46, in evaluate
if registers[int(instr[1][1])] == 1:
ValueError: invalid literal for int() with base 10: 's'
Testing if the previous output contains the string All tests passed
: False
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
Let's write a Python interpreter for the custom assembly language you've designed. This interpreter will parse and execute the assembly instructions, maintaining a register set and memory state. Here's a basic implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | import re from math import isqrt def evaluate(program): # Initialize registers and memory registers = {f'R{i}': 0 for i in range(1, 9)} memory = [] labels = {} program = program.strip().split('\n') # Remove comments and store labels for i, line in enumerate(program): if '//' in line: line = line.split('//')[0].strip() if ':' in line: label, code = line.split(':') labels[label.strip()] = i program[i] = code.strip() else: program[i] = line.strip() def get_value(x): return registers[x] if x in registers else int(x) def execute_line(line): parts = re.split(r'\s+', line) instr = parts[0] if instr == 'SET': registers[parts[1]] = int(parts[2]) elif instr == 'INC': registers[parts[1]] += 1 elif instr == 'EQ': registers[parts[1]] = 1 if get_value(parts[2]) == get_value(parts[3]) else 0 elif instr == 'JF': if registers[parts[1]] == 0: return parts[2] elif instr == 'JT': if registers[parts[1]] != 0: return parts[2] elif instr == 'JMP': return parts[1] elif instr == 'STORE': memory.append(registers[parts[1]]) elif instr == 'MUL': registers[parts[1]] = get_value(parts[2]) * get_value(parts[3]) elif instr == 'GT': registers[parts[1]] = 1 if get_value(parts[2]) > get_value(parts[3]) else 0 elif instr == 'MOD': registers[parts[1]] = get_value(parts[2]) % get_value(parts[3]) elif instr == 'NOP': pass return None pc = 0 while pc < len(program): line = program[pc].strip() if not line: pc += 1 continue new_label = execute_line(line) if new_label: pc = labels[new_label] else: pc += 1 return memory # Example usage: program = """ SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """ print(evaluate(program)) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | import re from math import isqrt def evaluate(program): # Initialize registers and memory registers = {f'R{i}': 0 for i in range(1, 9)} memory = [] labels = {} program = program.strip().split('\n') # Remove comments and store labels for i, line in enumerate(program): if '//' in line: line = line.split('//')[0].strip() if ':' in line: label, code = line.split(':') labels[label.strip()] = i program[i] = code.strip() else: program[i] = line.strip() def get_value(x): return registers[x] if x in registers else int(x) def execute_line(line): parts = re.split(r'\s+', line) instr = parts[0] if instr == 'SET': registers[parts[1]] = int(parts[2]) elif instr == 'INC': registers[parts[1]] += 1 elif instr == 'EQ': registers[parts[1]] = 1 if get_value(parts[2]) == get_value(parts[3]) else 0 elif instr == 'JF': if registers[parts[1]] == 0: return parts[2] elif instr == 'JT': if registers[parts[1]] != 0: return parts[2] elif instr == 'JMP': return parts[1] elif instr == 'STORE': memory.append(registers[parts[1]]) elif instr == 'MUL': registers[parts[1]] = get_value(parts[2]) * get_value(parts[3]) elif instr == 'GT': registers[parts[1]] = 1 if get_value(parts[2]) > get_value(parts[3]) else 0 elif instr == 'MOD': registers[parts[1]] = get_value(parts[2]) % get_value(parts[3]) elif instr == 'NOP': pass return None pc = 0 while pc < len(program): line = program[pc].strip() if not line: pc += 1 continue new_label = execute_line(line) if new_label: pc = labels[new_label] else: pc += 1 return memory # Example usage: program = """ SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """ print(evaluate(program)) |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | import re from math import isqrt def evaluate(program): # Initialize registers and memory registers = {f'R{i}': 0 for i in range(1, 9)} memory = [] labels = {} program = program.strip().split('\n') # Remove comments and store labels for i, line in enumerate(program): if '//' in line: line = line.split('//')[0].strip() if ':' in line: label, code = line.split(':') labels[label.strip()] = i program[i] = code.strip() else: program[i] = line.strip() def get_value(x): return registers[x] if x in registers else int(x) def execute_line(line): parts = re.split(r'\s+', line) instr = parts[0] if instr == 'SET': registers[parts[1]] = int(parts[2]) elif instr == 'INC': registers[parts[1]] += 1 elif instr == 'EQ': registers[parts[1]] = 1 if get_value(parts[2]) == get_value(parts[3]) else 0 elif instr == 'JF': if registers[parts[1]] == 0: return parts[2] elif instr == 'JT': if registers[parts[1]] != 0: return parts[2] elif instr == 'JMP': return parts[1] elif instr == 'STORE': memory.append(registers[parts[1]]) elif instr == 'MUL': registers[parts[1]] = get_value(parts[2]) * get_value(parts[3]) elif instr == 'GT': registers[parts[1]] = 1 if get_value(parts[2]) > get_value(parts[3]) else 0 elif instr == 'MOD': registers[parts[1]] = get_value(parts[2]) % get_value(parts[3]) elif instr == 'NOP': pass return None pc = 0 while pc < len(program): line = program[pc].strip() if not line: pc += 1 continue new_label = execute_line(line) if new_label: pc = labels[new_label] else: pc += 1 return memory # Example usage: program = """ SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """ print(evaluate(program)) answer = evaluate(""" SET R1 0 SET R2 1 loop: MUL R3 R2 R2 STORE R3 R1 INC R1 INC R2 EQ R1 20 JF loop HCF """)[:10] expected = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' answer = evaluate(""" SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """)[:10] expected = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
Traceback (most recent call last):
File "/usr/src/app/main.py", line 121, in <module>
print(evaluate(program))
File "/usr/src/app/main.py", line 62, in evaluate
new_label = execute_line(line)
File "/usr/src/app/main.py", line 49, in execute_line
registers[parts[1]] = 1 if get_value(parts[2]) > get_value(parts[3]) else 0
IndexError: list index out of range
Testing if the previous output contains the string All tests passed
: False
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
To create a Python interpreter for the custom assembly language, we need to simulate the behavior of the given assembly instructions and manage a virtual memory and register set. I'll break down the interpreter into several components: 1. **Tokenizing the program**: Splitting the input program into lines and tokens. 2. **Instruction Set**: Defining the behavior for each instruction. 3. **Execution Engine**: Running the instructions step-by-step and managing control flow. Below is a Python implementation of the interpreter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | class AssemblyInterpreter: def __init__(self, program): self.program = program.strip().split('\n') self.instruction_pointer = 0 self.registers = [0] * 10 # General purpose registers R0 to R9 self.memory = [] # Memory to store primes self.labels = {} # Label locations in the program self.parse_labels() # Parse labels for jumps def parse_labels(self): for i, line in enumerate(self.program): tokens = line.strip().split() if tokens and tokens[0].endswith(':'): self.labels[tokens[0][:-1]] = i def get_value(self, operand): if operand.startswith('R'): return self.registers[int(operand[1:])] return int(operand) def set_value(self, operand, value): if operand.startswith('R'): self.registers[int(operand[1:])] = value def execute_instruction(self, tokens): if not tokens: return op = tokens[0] if op == 'SET': _, reg, val = tokens self.set_value(reg, self.get_value(val)) elif op == 'JMP': _, label = tokens self.instruction_pointer = self.labels[label] elif op == 'EQ': _, reg, val = tokens self.set_value(reg, int(self.get_value(reg) == self.get_value(val))) elif op == 'JF': _, label = tokens if not self.registers[0]: # Register R0 is used for flags self.instruction_pointer = self.labels[label] - 1 elif op == 'JT': _, label = tokens if self.registers[0]: # Register R0 is used for flags self.instruction_pointer = self.labels[label] - 1 elif op == 'INC': _, reg = tokens self.set_value(reg, self.get_value(reg) + 1) elif op == 'MUL': _, reg, op1, op2 = tokens self.set_value(reg, self.get_value(op1) * self.get_value(op2)) elif op == 'MOD': _, reg, op1, op2 = tokens self.set_value(reg, self.get_value(op1) % self.get_value(op2)) elif op == 'STORE': _, reg, mem_index = tokens self.memory.append(self.get_value(reg)) else: # Assume it's a label and ignore it pass def run(self): while self.instruction_pointer < len(self.program): line = self.program[self.instruction_pointer].strip() tokens = line.split('//')[0].strip().split() self.execute_instruction(tokens) self.instruction_pointer += 1 return self.memory def evaluate(program): interpreter = AssemblyInterpreter(program) return interpreter.run() # Example usage program = """ SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """ print(evaluate(program)) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | class AssemblyInterpreter: def __init__(self, program): self.program = program.strip().split('\n') self.instruction_pointer = 0 self.registers = [0] * 10 # General purpose registers R0 to R9 self.memory = [] # Memory to store primes self.labels = {} # Label locations in the program self.parse_labels() # Parse labels for jumps def parse_labels(self): for i, line in enumerate(self.program): tokens = line.strip().split() if tokens and tokens[0].endswith(':'): self.labels[tokens[0][:-1]] = i def get_value(self, operand): if operand.startswith('R'): return self.registers[int(operand[1:])] return int(operand) def set_value(self, operand, value): if operand.startswith('R'): self.registers[int(operand[1:])] = value def execute_instruction(self, tokens): if not tokens: return op = tokens[0] if op == 'SET': _, reg, val = tokens self.set_value(reg, self.get_value(val)) elif op == 'JMP': _, label = tokens self.instruction_pointer = self.labels[label] elif op == 'EQ': _, reg, val = tokens self.set_value(reg, int(self.get_value(reg) == self.get_value(val))) elif op == 'JF': _, label = tokens if not self.registers[0]: # Register R0 is used for flags self.instruction_pointer = self.labels[label] - 1 elif op == 'JT': _, label = tokens if self.registers[0]: # Register R0 is used for flags self.instruction_pointer = self.labels[label] - 1 elif op == 'INC': _, reg = tokens self.set_value(reg, self.get_value(reg) + 1) elif op == 'MUL': _, reg, op1, op2 = tokens self.set_value(reg, self.get_value(op1) * self.get_value(op2)) elif op == 'MOD': _, reg, op1, op2 = tokens self.set_value(reg, self.get_value(op1) % self.get_value(op2)) elif op == 'STORE': _, reg, mem_index = tokens self.memory.append(self.get_value(reg)) else: # Assume it's a label and ignore it pass def run(self): while self.instruction_pointer < len(self.program): line = self.program[self.instruction_pointer].strip() tokens = line.split('//')[0].strip().split() self.execute_instruction(tokens) self.instruction_pointer += 1 return self.memory def evaluate(program): interpreter = AssemblyInterpreter(program) return interpreter.run() # Example usage program = """ SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """ print(evaluate(program)) |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | class AssemblyInterpreter: def __init__(self, program): self.program = program.strip().split('\n') self.instruction_pointer = 0 self.registers = [0] * 10 # General purpose registers R0 to R9 self.memory = [] # Memory to store primes self.labels = {} # Label locations in the program self.parse_labels() # Parse labels for jumps def parse_labels(self): for i, line in enumerate(self.program): tokens = line.strip().split() if tokens and tokens[0].endswith(':'): self.labels[tokens[0][:-1]] = i def get_value(self, operand): if operand.startswith('R'): return self.registers[int(operand[1:])] return int(operand) def set_value(self, operand, value): if operand.startswith('R'): self.registers[int(operand[1:])] = value def execute_instruction(self, tokens): if not tokens: return op = tokens[0] if op == 'SET': _, reg, val = tokens self.set_value(reg, self.get_value(val)) elif op == 'JMP': _, label = tokens self.instruction_pointer = self.labels[label] elif op == 'EQ': _, reg, val = tokens self.set_value(reg, int(self.get_value(reg) == self.get_value(val))) elif op == 'JF': _, label = tokens if not self.registers[0]: # Register R0 is used for flags self.instruction_pointer = self.labels[label] - 1 elif op == 'JT': _, label = tokens if self.registers[0]: # Register R0 is used for flags self.instruction_pointer = self.labels[label] - 1 elif op == 'INC': _, reg = tokens self.set_value(reg, self.get_value(reg) + 1) elif op == 'MUL': _, reg, op1, op2 = tokens self.set_value(reg, self.get_value(op1) * self.get_value(op2)) elif op == 'MOD': _, reg, op1, op2 = tokens self.set_value(reg, self.get_value(op1) % self.get_value(op2)) elif op == 'STORE': _, reg, mem_index = tokens self.memory.append(self.get_value(reg)) else: # Assume it's a label and ignore it pass def run(self): while self.instruction_pointer < len(self.program): line = self.program[self.instruction_pointer].strip() tokens = line.split('//')[0].strip().split() self.execute_instruction(tokens) self.instruction_pointer += 1 return self.memory def evaluate(program): interpreter = AssemblyInterpreter(program) return interpreter.run() # Example usage program = """ SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """ print(evaluate(program)) answer = evaluate(""" SET R1 0 SET R2 1 loop: MUL R3 R2 R2 STORE R3 R1 INC R1 INC R2 EQ R1 20 JF loop HCF """)[:10] expected = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' answer = evaluate(""" SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """)[:10] expected = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
Traceback (most recent call last):
File "/usr/src/app/main.py", line 127, in <module>
print(evaluate(program))
File "/usr/src/app/main.py", line 73, in evaluate
return interpreter.run()
File "/usr/src/app/main.py", line 67, in run
self.execute_instruction(tokens)
File "/usr/src/app/main.py", line 34, in execute_instruction
_, label = tokens
ValueError: too many values to unpack (expected 2)
Testing if the previous output contains the string All tests passed
: False