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 a full-fledged Python interpreter for your custom assembly language is quite an extensive task, but I can provide you with a basic implementation that covers the functionality of your given 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 | def evaluate(program): memory = [0] * 100 # Initialize memory with 100 slots registers = { 'R1': 2, # Initialize registers 'R2': 0, 'R3': 0, 'R4': 0, 'R6': 0, 'R7': 0, 'R8': 0, } labels = { 'start_find_primes': 0, 'ready_prime': 6, 'increment': 21, 'is_prime': 26, 'start_loop': 32, 'not_prime': 38, 'is_prime_end': 43, 'end': 46, } pc = 0 # Program counter instructions = program.split('\n') def execute_instruction(): inst = instructions[pc].strip() if inst.startswith('SET'): dest, value = inst[4:].split() registers[dest] = int(value) elif inst == 'INC': registers[inst[3:]] += 1 elif inst == 'STORE': memory[registers['R8']] = registers['R1'] elif inst == 'EQ': registers['R7'] = int(registers[inst[3:]] == registers[inst[5:]]) elif inst.startswith('JMP'): pc = labels[inst[4:]] return elif inst.startswith('JF') or inst.startswith('JT'): condition = registers['R7'] == 0 if inst.startswith('JF') else inst[-2] == 'T' if not condition: pc += 1 return elif inst.startswith('MUL') or inst.startswith('MOD') or inst.startswith('GT'): dest, src1, src2 = inst[4:].split() if inst.startswith('MUL'): registers[dest] = registers[src1] * registers[src2] elif inst.startswith('MOD'): registers[dest] = registers[src1] % registers[src2] else: registers['R7'] = int(registers[src1] > registers[src2]) pc += 1 while pc < len(instructions): execute_instruction() return memory[:registers['R8']] program = ''' SET R1 2 ... end: ''' # Place the entire program here primes = evaluate(program) print(primes) |
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 | def evaluate(program): memory = [0] * 100 # Initialize memory with 100 slots registers = { 'R1': 2, # Initialize registers 'R2': 0, 'R3': 0, 'R4': 0, 'R6': 0, 'R7': 0, 'R8': 0, } labels = { 'start_find_primes': 0, 'ready_prime': 6, 'increment': 21, 'is_prime': 26, 'start_loop': 32, 'not_prime': 38, 'is_prime_end': 43, 'end': 46, } pc = 0 # Program counter instructions = program.split('\n') def execute_instruction(): inst = instructions[pc].strip() if inst.startswith('SET'): dest, value = inst[4:].split() registers[dest] = int(value) elif inst == 'INC': registers[inst[3:]] += 1 elif inst == 'STORE': memory[registers['R8']] = registers['R1'] elif inst == 'EQ': registers['R7'] = int(registers[inst[3:]] == registers[inst[5:]]) elif inst.startswith('JMP'): pc = labels[inst[4:]] return elif inst.startswith('JF') or inst.startswith('JT'): condition = registers['R7'] == 0 if inst.startswith('JF') else inst[-2] == 'T' if not condition: pc += 1 return elif inst.startswith('MUL') or inst.startswith('MOD') or inst.startswith('GT'): dest, src1, src2 = inst[4:].split() if inst.startswith('MUL'): registers[dest] = registers[src1] * registers[src2] elif inst.startswith('MOD'): registers[dest] = registers[src1] % registers[src2] else: registers['R7'] = int(registers[src1] > registers[src2]) pc += 1 while pc < len(instructions): execute_instruction() return memory[:registers['R8']] program = ''' SET R1 2 ... end: ''' # Place the entire program here primes = evaluate(program) print(primes) |
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 | def evaluate(program): memory = [0] * 100 # Initialize memory with 100 slots registers = { 'R1': 2, # Initialize registers 'R2': 0, 'R3': 0, 'R4': 0, 'R6': 0, 'R7': 0, 'R8': 0, } labels = { 'start_find_primes': 0, 'ready_prime': 6, 'increment': 21, 'is_prime': 26, 'start_loop': 32, 'not_prime': 38, 'is_prime_end': 43, 'end': 46, } pc = 0 # Program counter instructions = program.split('\n') def execute_instruction(): inst = instructions[pc].strip() if inst.startswith('SET'): dest, value = inst[4:].split() registers[dest] = int(value) elif inst == 'INC': registers[inst[3:]] += 1 elif inst == 'STORE': memory[registers['R8']] = registers['R1'] elif inst == 'EQ': registers['R7'] = int(registers[inst[3:]] == registers[inst[5:]]) elif inst.startswith('JMP'): pc = labels[inst[4:]] return elif inst.startswith('JF') or inst.startswith('JT'): condition = registers['R7'] == 0 if inst.startswith('JF') else inst[-2] == 'T' if not condition: pc += 1 return elif inst.startswith('MUL') or inst.startswith('MOD') or inst.startswith('GT'): dest, src1, src2 = inst[4:].split() if inst.startswith('MUL'): registers[dest] = registers[src1] * registers[src2] elif inst.startswith('MOD'): registers[dest] = registers[src1] % registers[src2] else: registers['R7'] = int(registers[src1] > registers[src2]) pc += 1 while pc < len(instructions): execute_instruction() return memory[:registers['R8']] program = ''' SET R1 2 ... end: ''' # Place the entire program here primes = evaluate(program) print(primes) 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 68, in <module>
primes = evaluate(program)
^^^^^^^^^^^^^^^^^
File "/usr/src/app/main.py", line 58, in evaluate
execute_instruction()
File "/usr/src/app/main.py", line 29, in execute_instruction
inst = instructions[pc].strip()
^^
UnboundLocalError: cannot access local variable 'pc' where it is not associated with a value
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 a full-fledged interpreter for your custom assembly language in Python is quite a complex task, especially without having a formal specification or grammar for the language. However, I can provide you with an example implementation that should work for the specific program you've given. This example can serve as a starting point for you to build upon and generalize for other programs written in your assembly language.
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 | from collections import defaultdict import math def evaluate(program): memory = defaultdict(int) register = { 'R1': 2, 'R2': 0, 'R3': 0, 'R4': 0, 'R6': 0, 'R7': 0, 'R8': 0, } labels = {} instructions = program.split('\n') pc = 0 for i, line in enumerate(instructions): if ':' in line: labels[line.strip()[:-1]] = i def exec_instruction(): nonlocal pc line = instructions[pc].strip() if not line: return opcode, *operands = line.split() if opcode == 'SET': register[operands[0]] = int(operands[1]) elif opcode == 'INC': register[operands[0]] += 1 elif opcode == 'JMP': pc = labels[operands[0]] return elif opcode == 'JF': if not eval(f'{register[operands[0]]} {operands[1]} 0'): pc += 1 return elif opcode == 'JT': if eval(f'{register[operands[0]]} {operands[1]} {register[operands[2]]}'): pc += 1 return elif opcode == 'EQ': if not eval(f'{register[operands[0]]} == {register[operands[1]]}'): pc += 1 return elif opcode == 'GT': if not eval(f'{register[operands[0]]} > {register[operands[1]]}'): pc += 1 return elif opcode == 'STORE': memory[register[operands[1]]] = register[operands[0]] elif opcode == 'MUL': register[operands[0]] = register[operands[1]] * register[operands[2]] elif opcode == 'MOD': register[operands[0]] = register[operands[1]] % register[operands[2]] pc += 1 while True: exec_instruction() if pc >= len(instructions): break primes = [memory[i] for i in range(100)] return primes program = ''' SET R1 2 ... ''' # Full program 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 | from collections import defaultdict import math def evaluate(program): memory = defaultdict(int) register = { 'R1': 2, 'R2': 0, 'R3': 0, 'R4': 0, 'R6': 0, 'R7': 0, 'R8': 0, } labels = {} instructions = program.split('\n') pc = 0 for i, line in enumerate(instructions): if ':' in line: labels[line.strip()[:-1]] = i def exec_instruction(): nonlocal pc line = instructions[pc].strip() if not line: return opcode, *operands = line.split() if opcode == 'SET': register[operands[0]] = int(operands[1]) elif opcode == 'INC': register[operands[0]] += 1 elif opcode == 'JMP': pc = labels[operands[0]] return elif opcode == 'JF': if not eval(f'{register[operands[0]]} {operands[1]} 0'): pc += 1 return elif opcode == 'JT': if eval(f'{register[operands[0]]} {operands[1]} {register[operands[2]]}'): pc += 1 return elif opcode == 'EQ': if not eval(f'{register[operands[0]]} == {register[operands[1]]}'): pc += 1 return elif opcode == 'GT': if not eval(f'{register[operands[0]]} > {register[operands[1]]}'): pc += 1 return elif opcode == 'STORE': memory[register[operands[1]]] = register[operands[0]] elif opcode == 'MUL': register[operands[0]] = register[operands[1]] * register[operands[2]] elif opcode == 'MOD': register[operands[0]] = register[operands[1]] % register[operands[2]] pc += 1 while True: exec_instruction() if pc >= len(instructions): break primes = [memory[i] for i in range(100)] return primes program = ''' SET R1 2 ... ''' # Full program 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 | from collections import defaultdict import math def evaluate(program): memory = defaultdict(int) register = { 'R1': 2, 'R2': 0, 'R3': 0, 'R4': 0, 'R6': 0, 'R7': 0, 'R8': 0, } labels = {} instructions = program.split('\n') pc = 0 for i, line in enumerate(instructions): if ':' in line: labels[line.strip()[:-1]] = i def exec_instruction(): nonlocal pc line = instructions[pc].strip() if not line: return opcode, *operands = line.split() if opcode == 'SET': register[operands[0]] = int(operands[1]) elif opcode == 'INC': register[operands[0]] += 1 elif opcode == 'JMP': pc = labels[operands[0]] return elif opcode == 'JF': if not eval(f'{register[operands[0]]} {operands[1]} 0'): pc += 1 return elif opcode == 'JT': if eval(f'{register[operands[0]]} {operands[1]} {register[operands[2]]}'): pc += 1 return elif opcode == 'EQ': if not eval(f'{register[operands[0]]} == {register[operands[1]]}'): pc += 1 return elif opcode == 'GT': if not eval(f'{register[operands[0]]} > {register[operands[1]]}'): pc += 1 return elif opcode == 'STORE': memory[register[operands[1]]] = register[operands[0]] elif opcode == 'MUL': register[operands[0]] = register[operands[1]] * register[operands[2]] elif opcode == 'MOD': register[operands[0]] = register[operands[1]] % register[operands[2]] pc += 1 while True: exec_instruction() if pc >= len(instructions): break primes = [memory[i] for i in range(100)] return primes program = ''' SET R1 2 ... ''' # Full program 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') |
Timeout: function took too long to complete
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 a full-fledged interpreter for your custom assembly language in Python is quite a complex task. However, I can provide you with a basic implementation that should work for the given example program. This implementation will not cover all possible instructions and edge cases, but it should give you a good starting point.
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 | def evaluate(program): memory = [0] * 100 registers = { 'R1': 2, 'R2': 0, 'R3': 0, 'R4': 0, 'R6': 0, 'R7': 0, 'R8': 0, } instructions = program.split('\n') instruction_pointer = 0 flag = 0 def execute_instruction(): nonlocal instruction_pointer, flag instr = instructions[instruction_pointer].split() if len(instr) == 0: return opcode = instr[0] if opcode == 'SET': registers[instr[1]] = int(instr[2]) elif opcode == 'INC': registers[instr[1]] += 1 elif opcode == 'EQ': flag = int(registers[instr[1]] == int(instr[2])) elif opcode == 'JF': if not flag: instruction_pointer += int(instr[1]) return elif opcode == 'JMP': instruction_pointer += instructions[instruction_pointer:].index(instr[1]) return elif opcode == 'JT': if flag: instruction_pointer += int(instr[1]) return elif opcode == 'STORE': memory[registers[instr[2]]] = registers[instr[1]] elif opcode == 'MUL': registers[instr[1]] = registers[instr[2]] * registers[instr[3]] elif opcode == 'GT': flag = int(registers[instr[1]] > registers[instr[2]]) elif opcode == 'MOD': registers[instr[1]] = registers[instr[2]] % registers[instr[3]] instruction_pointer += 1 while instruction_pointer < len(instructions): execute_instruction() return memory[:registers['R8']] program = ''' SET R1 2 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 | def evaluate(program): memory = [0] * 100 registers = { 'R1': 2, 'R2': 0, 'R3': 0, 'R4': 0, 'R6': 0, 'R7': 0, 'R8': 0, } instructions = program.split('\n') instruction_pointer = 0 flag = 0 def execute_instruction(): nonlocal instruction_pointer, flag instr = instructions[instruction_pointer].split() if len(instr) == 0: return opcode = instr[0] if opcode == 'SET': registers[instr[1]] = int(instr[2]) elif opcode == 'INC': registers[instr[1]] += 1 elif opcode == 'EQ': flag = int(registers[instr[1]] == int(instr[2])) elif opcode == 'JF': if not flag: instruction_pointer += int(instr[1]) return elif opcode == 'JMP': instruction_pointer += instructions[instruction_pointer:].index(instr[1]) return elif opcode == 'JT': if flag: instruction_pointer += int(instr[1]) return elif opcode == 'STORE': memory[registers[instr[2]]] = registers[instr[1]] elif opcode == 'MUL': registers[instr[1]] = registers[instr[2]] * registers[instr[3]] elif opcode == 'GT': flag = int(registers[instr[1]] > registers[instr[2]]) elif opcode == 'MOD': registers[instr[1]] = registers[instr[2]] % registers[instr[3]] instruction_pointer += 1 while instruction_pointer < len(instructions): execute_instruction() return memory[:registers['R8']] program = ''' SET R1 2 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 | def evaluate(program): memory = [0] * 100 registers = { 'R1': 2, 'R2': 0, 'R3': 0, 'R4': 0, 'R6': 0, 'R7': 0, 'R8': 0, } instructions = program.split('\n') instruction_pointer = 0 flag = 0 def execute_instruction(): nonlocal instruction_pointer, flag instr = instructions[instruction_pointer].split() if len(instr) == 0: return opcode = instr[0] if opcode == 'SET': registers[instr[1]] = int(instr[2]) elif opcode == 'INC': registers[instr[1]] += 1 elif opcode == 'EQ': flag = int(registers[instr[1]] == int(instr[2])) elif opcode == 'JF': if not flag: instruction_pointer += int(instr[1]) return elif opcode == 'JMP': instruction_pointer += instructions[instruction_pointer:].index(instr[1]) return elif opcode == 'JT': if flag: instruction_pointer += int(instr[1]) return elif opcode == 'STORE': memory[registers[instr[2]]] = registers[instr[1]] elif opcode == 'MUL': registers[instr[1]] = registers[instr[2]] * registers[instr[3]] elif opcode == 'GT': flag = int(registers[instr[1]] > registers[instr[2]]) elif opcode == 'MOD': registers[instr[1]] = registers[instr[2]] % registers[instr[3]] instruction_pointer += 1 while instruction_pointer < len(instructions): execute_instruction() return memory[:registers['R8']] program = ''' SET R1 2 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') |
Timeout: function took too long to complete
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 a Python interpreter for your assembly language program is a complex task and requires a deep understanding of both assembly language and Python programming. However, I can provide you with a basic outline of how to approach this problem. 1. Define a memory dictionary to store the values in memory. 2. Create a function `evaluate(program)` that takes the program as input and returns the memory state after running the program. 3. Define a dictionary of instructions that maps instruction names to their corresponding actions. For example, the `SET` instruction would set the value of a register to a given value. 4. Parse the input program into a list of instructions. 5. Iterate over the list of instructions and execute each instruction by looking up its corresponding action in the dictionary of instructions. 6. Update the memory dictionary and register values as necessary during the execution of instructions. Here is an example implementation of the above approach:
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 | # Memory dictionary to store values in memory memory = {} # Register dictionary to store register values registers = { 'R1': 0, 'R2': 0, 'R3': 0, 'R4': 0, 'R6': 0, 'R7': 0, 'R8': 0, } # Dictionary of instructions instructions = { 'SET': lambda register, value: registers[register] = value, 'INC': lambda register: registers[register] += 1, 'EQ': lambda register1, register2, dest: registers[dest] = int(registers[register1] == registers[register2]), 'JF': lambda label: None, # jump if false 'JMP': lambda label: None, # jump to label 'JT': lambda label: None, # jump if true 'GT': lambda register1, register2: registers['R7'] = int(registers[register1] > registers[register2]), 'MUL': lambda register1, register2, dest: registers[dest] = registers[register1] * registers[register2], 'MOD': lambda register1, register2, dest: registers[dest] = registers[register1] % registers[register2], 'STORE': lambda value, address: memory[address] = value, } # Function to evaluate the program def evaluate(program): pc = 0 # program counter labels = {} # dictionary to store labels and their corresponding PC values # Parse the input program into a list of instructions instructions_list = [] for line in program.strip().split('\n'): line = line.strip() if line: label, _, instruction = line.partition(':') if label: labels[label] = pc instructions_list.append((instruction.strip(), labels.get(instruction, pc))) pc += 1 # Execute the program while pc < len(instructions_list): instruction, _ = instructions_list[pc] opcode, *operands = instruction.split() if opcode in ['JMP', 'JF', 'JT']: label = operands[0] pc = labels[label] - 1 # adjust for 0-based indexing continue action = instructions[opcode] action(*operands) pc += 1 return list(memory.values())[:100] # return the first 100 prime numbers in memory # Test the interpreter with the provided program 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: ''' result = evaluate(program) print(result) |
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 | # Memory dictionary to store values in memory memory = {} # Register dictionary to store register values registers = { 'R1': 0, 'R2': 0, 'R3': 0, 'R4': 0, 'R6': 0, 'R7': 0, 'R8': 0, } # Dictionary of instructions instructions = { 'SET': lambda register, value: registers[register] = value, 'INC': lambda register: registers[register] += 1, 'EQ': lambda register1, register2, dest: registers[dest] = int(registers[register1] == registers[register2]), 'JF': lambda label: None, # jump if false 'JMP': lambda label: None, # jump to label 'JT': lambda label: None, # jump if true 'GT': lambda register1, register2: registers['R7'] = int(registers[register1] > registers[register2]), 'MUL': lambda register1, register2, dest: registers[dest] = registers[register1] * registers[register2], 'MOD': lambda register1, register2, dest: registers[dest] = registers[register1] % registers[register2], 'STORE': lambda value, address: memory[address] = value, } # Function to evaluate the program def evaluate(program): pc = 0 # program counter labels = {} # dictionary to store labels and their corresponding PC values # Parse the input program into a list of instructions instructions_list = [] for line in program.strip().split('\n'): line = line.strip() if line: label, _, instruction = line.partition(':') if label: labels[label] = pc instructions_list.append((instruction.strip(), labels.get(instruction, pc))) pc += 1 # Execute the program while pc < len(instructions_list): instruction, _ = instructions_list[pc] opcode, *operands = instruction.split() if opcode in ['JMP', 'JF', 'JT']: label = operands[0] pc = labels[label] - 1 # adjust for 0-based indexing continue action = instructions[opcode] action(*operands) pc += 1 return list(memory.values())[:100] # return the first 100 prime numbers in memory # Test the interpreter with the provided program 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: ''' result = evaluate(program) print(result) |
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 | # Memory dictionary to store values in memory memory = {} # Register dictionary to store register values registers = { 'R1': 0, 'R2': 0, 'R3': 0, 'R4': 0, 'R6': 0, 'R7': 0, 'R8': 0, } # Dictionary of instructions instructions = { 'SET': lambda register, value: registers[register] = value, 'INC': lambda register: registers[register] += 1, 'EQ': lambda register1, register2, dest: registers[dest] = int(registers[register1] == registers[register2]), 'JF': lambda label: None, # jump if false 'JMP': lambda label: None, # jump to label 'JT': lambda label: None, # jump if true 'GT': lambda register1, register2: registers['R7'] = int(registers[register1] > registers[register2]), 'MUL': lambda register1, register2, dest: registers[dest] = registers[register1] * registers[register2], 'MOD': lambda register1, register2, dest: registers[dest] = registers[register1] % registers[register2], 'STORE': lambda value, address: memory[address] = value, } # Function to evaluate the program def evaluate(program): pc = 0 # program counter labels = {} # dictionary to store labels and their corresponding PC values # Parse the input program into a list of instructions instructions_list = [] for line in program.strip().split('\n'): line = line.strip() if line: label, _, instruction = line.partition(':') if label: labels[label] = pc instructions_list.append((instruction.strip(), labels.get(instruction, pc))) pc += 1 # Execute the program while pc < len(instructions_list): instruction, _ = instructions_list[pc] opcode, *operands = instruction.split() if opcode in ['JMP', 'JF', 'JT']: label = operands[0] pc = labels[label] - 1 # adjust for 0-based indexing continue action = instructions[opcode] action(*operands) pc += 1 return list(memory.values())[:100] # return the first 100 prime numbers in memory # Test the interpreter with the provided program 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: ''' result = evaluate(program) print(result) 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') |
File "/usr/src/app/main.py", line 18
'SET': lambda register, value: registers[register] = value,
^
SyntaxError: invalid syntax
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: |
Here's a Python interpreter for your custom assembly language. This interpreter simulates the execution of the given assembly code and returns 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 | def evaluate(program): # Initialize registers and memory registers = {'R1': 2, 'R2': 0, 'R3': 0, 'R4': 0, 'R6': 0, 'R7': 0, 'R8': 0} memory = [] labels = {} def parse_instruction(instruction): opcode, arg1, arg2 = instruction.split() if opcode == 'SET': registers[arg1] = int(arg2) elif opcode == 'INC': registers[arg1] += 1 elif opcode == 'EQ': registers[arg1] = int(registers[arg1] == registers[arg2]) elif opcode == 'JF': global line if not registers[arg1]: line += int(arg2) elif opcode == 'JMP': global line line += int(arg2) - 1 # adjust for next instruction elif opcode == 'JT': global line if registers[arg1]: line += int(arg2) - 1 # adjust for next instruction elif opcode == 'STORE': memory.append(registers[arg1]) registers[arg2] += 1 elif opcode == 'MUL': registers[arg1] = registers[arg2] * registers[arg3] elif opcode == 'GT': registers[arg1] = int(registers[arg2] > registers[arg3]) elif opcode == 'MOD': registers[arg1] = registers[arg2] % registers[arg3] line = 0 for line_number, line in enumerate(program.split('\n')): if line.strip(): if line.startswith(':'): labels[line.strip()[1:]] = line_number else: parse_instruction(line) return memory[:100] # Return the first 100 memory values as per your example # Test the interpreter with your example program 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: ''' memory_state = evaluate(program) print(memory_state) |
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 | def evaluate(program): # Initialize registers and memory registers = {'R1': 2, 'R2': 0, 'R3': 0, 'R4': 0, 'R6': 0, 'R7': 0, 'R8': 0} memory = [] labels = {} def parse_instruction(instruction): opcode, arg1, arg2 = instruction.split() if opcode == 'SET': registers[arg1] = int(arg2) elif opcode == 'INC': registers[arg1] += 1 elif opcode == 'EQ': registers[arg1] = int(registers[arg1] == registers[arg2]) elif opcode == 'JF': global line if not registers[arg1]: line += int(arg2) elif opcode == 'JMP': global line line += int(arg2) - 1 # adjust for next instruction elif opcode == 'JT': global line if registers[arg1]: line += int(arg2) - 1 # adjust for next instruction elif opcode == 'STORE': memory.append(registers[arg1]) registers[arg2] += 1 elif opcode == 'MUL': registers[arg1] = registers[arg2] * registers[arg3] elif opcode == 'GT': registers[arg1] = int(registers[arg2] > registers[arg3]) elif opcode == 'MOD': registers[arg1] = registers[arg2] % registers[arg3] line = 0 for line_number, line in enumerate(program.split('\n')): if line.strip(): if line.startswith(':'): labels[line.strip()[1:]] = line_number else: parse_instruction(line) return memory[:100] # Return the first 100 memory values as per your example # Test the interpreter with your example program 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: ''' memory_state = evaluate(program) print(memory_state) |
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 | def evaluate(program): # Initialize registers and memory registers = {'R1': 2, 'R2': 0, 'R3': 0, 'R4': 0, 'R6': 0, 'R7': 0, 'R8': 0} memory = [] labels = {} def parse_instruction(instruction): opcode, arg1, arg2 = instruction.split() if opcode == 'SET': registers[arg1] = int(arg2) elif opcode == 'INC': registers[arg1] += 1 elif opcode == 'EQ': registers[arg1] = int(registers[arg1] == registers[arg2]) elif opcode == 'JF': global line if not registers[arg1]: line += int(arg2) elif opcode == 'JMP': global line line += int(arg2) - 1 # adjust for next instruction elif opcode == 'JT': global line if registers[arg1]: line += int(arg2) - 1 # adjust for next instruction elif opcode == 'STORE': memory.append(registers[arg1]) registers[arg2] += 1 elif opcode == 'MUL': registers[arg1] = registers[arg2] * registers[arg3] elif opcode == 'GT': registers[arg1] = int(registers[arg2] > registers[arg3]) elif opcode == 'MOD': registers[arg1] = registers[arg2] % registers[arg3] line = 0 for line_number, line in enumerate(program.split('\n')): if line.strip(): if line.startswith(':'): labels[line.strip()[1:]] = line_number else: parse_instruction(line) return memory[:100] # Return the first 100 memory values as per your example # Test the interpreter with your example program 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: ''' memory_state = evaluate(program) print(memory_state) 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') |
File "/usr/src/app/main.py", line 21
global line
^^^^^^^^^^^
SyntaxError: name 'line' is assigned to before global declaration
Testing if the previous output contains the string All tests passed
: False