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 write a Python interpreter for the given assembly language, we need to understand the instructions and their functionality. Here's a Python function `evaluate(str)` that interprets the given assembly language and returns the resulting memory state after running the 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 | def evaluate(program): # Split the program into lines and remove empty lines and comments lines = [line.strip() for line in program.split('\n') if line.strip() and not line.strip().startswith('//')] # Initialize registers and memory registers = [0] * 10 memory = [0] * 101 # Assuming memory size of 101 to store primes # Dictionary to store labels and their corresponding line numbers labels = {} # Find labels and store their line numbers for i, line in enumerate(lines): if ':' in line: label = line.split(':')[0].strip() labels[label] = i # Execute the program i = 0 while i < len(lines): line = lines[i] parts = line.split() instruction = parts[0] if instruction == 'SET': reg = int(parts[1][1]) value = int(parts[2]) registers[reg] = value elif instruction == 'JMP': label = parts[1] i = labels[label] continue elif instruction == 'EQ': reg1 = int(parts[1][1]) reg2_or_value = parts[2] if reg2_or_value.startswith('R'): reg2 = int(reg2_or_value[1]) registers[8] = int(registers[reg1] == registers[reg2]) else: value = int(reg2_or_value) registers[8] = int(registers[reg1] == value) elif instruction == 'JF': label = parts[1] if registers[8] == 0: i = labels[label] continue elif instruction == 'STORE': reg1 = int(parts[1][1]) reg2 = int(parts[2][1]) memory[registers[reg2]] = registers[reg1] elif instruction == 'INC': reg = int(parts[1][1]) registers[reg] += 1 elif instruction == 'MUL': reg1 = int(parts[1][1]) reg2 = int(parts[2][1]) reg3 = int(parts[3][1]) registers[reg1] = registers[reg2] * registers[reg3] elif instruction == 'GT': reg1 = int(parts[1][1]) reg2 = int(parts[2][1]) registers[8] = int(registers[reg1] > registers[reg2]) elif instruction == 'JT': label = parts[1] if registers[8] == 1: i = labels[label] continue elif instruction == 'MOD': reg1 = int(parts[1][1]) reg2 = int(parts[2][1]) reg3 = int(parts[3][1]) registers[reg1] = registers[reg2] % registers[reg3] i += 1 return memory[1:registers[8]] |
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 | def evaluate(program): # Split the program into lines and remove empty lines and comments lines = [line.strip() for line in program.split('\n') if line.strip() and not line.strip().startswith('//')] # Initialize registers and memory registers = [0] * 10 memory = [0] * 101 # Assuming memory size of 101 to store primes # Dictionary to store labels and their corresponding line numbers labels = {} # Find labels and store their line numbers for i, line in enumerate(lines): if ':' in line: label = line.split(':')[0].strip() labels[label] = i # Execute the program i = 0 while i < len(lines): line = lines[i] parts = line.split() instruction = parts[0] if instruction == 'SET': reg = int(parts[1][1]) value = int(parts[2]) registers[reg] = value elif instruction == 'JMP': label = parts[1] i = labels[label] continue elif instruction == 'EQ': reg1 = int(parts[1][1]) reg2_or_value = parts[2] if reg2_or_value.startswith('R'): reg2 = int(reg2_or_value[1]) registers[8] = int(registers[reg1] == registers[reg2]) else: value = int(reg2_or_value) registers[8] = int(registers[reg1] == value) elif instruction == 'JF': label = parts[1] if registers[8] == 0: i = labels[label] continue elif instruction == 'STORE': reg1 = int(parts[1][1]) reg2 = int(parts[2][1]) memory[registers[reg2]] = registers[reg1] elif instruction == 'INC': reg = int(parts[1][1]) registers[reg] += 1 elif instruction == 'MUL': reg1 = int(parts[1][1]) reg2 = int(parts[2][1]) reg3 = int(parts[3][1]) registers[reg1] = registers[reg2] * registers[reg3] elif instruction == 'GT': reg1 = int(parts[1][1]) reg2 = int(parts[2][1]) registers[8] = int(registers[reg1] > registers[reg2]) elif instruction == 'JT': label = parts[1] if registers[8] == 1: i = labels[label] continue elif instruction == 'MOD': reg1 = int(parts[1][1]) reg2 = int(parts[2][1]) reg3 = int(parts[3][1]) registers[reg1] = registers[reg2] % registers[reg3] i += 1 return memory[1:registers[8]] |
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 | def evaluate(program): # Split the program into lines and remove empty lines and comments lines = [line.strip() for line in program.split('\n') if line.strip() and not line.strip().startswith('//')] # Initialize registers and memory registers = [0] * 10 memory = [0] * 101 # Assuming memory size of 101 to store primes # Dictionary to store labels and their corresponding line numbers labels = {} # Find labels and store their line numbers for i, line in enumerate(lines): if ':' in line: label = line.split(':')[0].strip() labels[label] = i # Execute the program i = 0 while i < len(lines): line = lines[i] parts = line.split() instruction = parts[0] if instruction == 'SET': reg = int(parts[1][1]) value = int(parts[2]) registers[reg] = value elif instruction == 'JMP': label = parts[1] i = labels[label] continue elif instruction == 'EQ': reg1 = int(parts[1][1]) reg2_or_value = parts[2] if reg2_or_value.startswith('R'): reg2 = int(reg2_or_value[1]) registers[8] = int(registers[reg1] == registers[reg2]) else: value = int(reg2_or_value) registers[8] = int(registers[reg1] == value) elif instruction == 'JF': label = parts[1] if registers[8] == 0: i = labels[label] continue elif instruction == 'STORE': reg1 = int(parts[1][1]) reg2 = int(parts[2][1]) memory[registers[reg2]] = registers[reg1] elif instruction == 'INC': reg = int(parts[1][1]) registers[reg] += 1 elif instruction == 'MUL': reg1 = int(parts[1][1]) reg2 = int(parts[2][1]) reg3 = int(parts[3][1]) registers[reg1] = registers[reg2] * registers[reg3] elif instruction == 'GT': reg1 = int(parts[1][1]) reg2 = int(parts[2][1]) registers[8] = int(registers[reg1] > registers[reg2]) elif instruction == 'JT': label = parts[1] if registers[8] == 1: i = labels[label] continue elif instruction == 'MOD': reg1 = int(parts[1][1]) reg2 = int(parts[2][1]) reg3 = int(parts[3][1]) registers[reg1] = registers[reg2] % registers[reg3] i += 1 return memory[1:registers[8]] 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 96, in <module>
assert answer == expected, f'Wrong answer; got {answer} instead of {expected}'
^^^^^^^^^^^^^^^^^^
AssertionError: Wrong answer; got [] instead of [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
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 `evaluate(str)` that returns the resulting memory state after running the program in your custom 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 | def evaluate(program): lines = program.split('\n') memory = [0] * 1000 # Memory array to store values registers = [0] * 10 # Registers R0 to R9 pc = 0 # Program counter def get_value(operand): if operand.startswith('R'): return registers[int(operand[1:])] else: return int(operand) while pc < len(lines): line = lines[pc].strip() if not line or line.startswith('//'): pc += 1 continue parts = line.split() opcode = parts[0] if opcode == 'SET': reg = int(parts[1][1:]) value = get_value(parts[2]) registers[reg] = value elif opcode == 'INC': reg = int(parts[1][1:]) registers[reg] += 1 elif opcode == 'MUL': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] * registers[reg3] elif opcode == 'GT': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[8] = 1 if registers[reg1] > registers[reg2] else 0 elif opcode == 'EQ': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[8] = 1 if registers[reg1] == registers[reg2] else 0 elif opcode == 'JMP': label = parts[1] pc = lines.index(label + ':') continue elif opcode == 'JT': label = parts[1] if registers[8] == 1: pc = lines.index(label + ':') continue elif opcode == 'JF': label = parts[1] if registers[8] == 0: pc = lines.index(label + ':') continue elif opcode == 'MOD': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] % registers[reg3] elif opcode == 'STORE': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) memory[registers[reg2]] = registers[reg1] pc += 1 return memory[:registers[8]] |
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): lines = program.split('\n') memory = [0] * 1000 # Memory array to store values registers = [0] * 10 # Registers R0 to R9 pc = 0 # Program counter def get_value(operand): if operand.startswith('R'): return registers[int(operand[1:])] else: return int(operand) while pc < len(lines): line = lines[pc].strip() if not line or line.startswith('//'): pc += 1 continue parts = line.split() opcode = parts[0] if opcode == 'SET': reg = int(parts[1][1:]) value = get_value(parts[2]) registers[reg] = value elif opcode == 'INC': reg = int(parts[1][1:]) registers[reg] += 1 elif opcode == 'MUL': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] * registers[reg3] elif opcode == 'GT': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[8] = 1 if registers[reg1] > registers[reg2] else 0 elif opcode == 'EQ': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[8] = 1 if registers[reg1] == registers[reg2] else 0 elif opcode == 'JMP': label = parts[1] pc = lines.index(label + ':') continue elif opcode == 'JT': label = parts[1] if registers[8] == 1: pc = lines.index(label + ':') continue elif opcode == 'JF': label = parts[1] if registers[8] == 0: pc = lines.index(label + ':') continue elif opcode == 'MOD': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] % registers[reg3] elif opcode == 'STORE': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) memory[registers[reg2]] = registers[reg1] pc += 1 return memory[:registers[8]] |
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): lines = program.split('\n') memory = [0] * 1000 # Memory array to store values registers = [0] * 10 # Registers R0 to R9 pc = 0 # Program counter def get_value(operand): if operand.startswith('R'): return registers[int(operand[1:])] else: return int(operand) while pc < len(lines): line = lines[pc].strip() if not line or line.startswith('//'): pc += 1 continue parts = line.split() opcode = parts[0] if opcode == 'SET': reg = int(parts[1][1:]) value = get_value(parts[2]) registers[reg] = value elif opcode == 'INC': reg = int(parts[1][1:]) registers[reg] += 1 elif opcode == 'MUL': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] * registers[reg3] elif opcode == 'GT': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[8] = 1 if registers[reg1] > registers[reg2] else 0 elif opcode == 'EQ': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[8] = 1 if registers[reg1] == registers[reg2] else 0 elif opcode == 'JMP': label = parts[1] pc = lines.index(label + ':') continue elif opcode == 'JT': label = parts[1] if registers[8] == 1: pc = lines.index(label + ':') continue elif opcode == 'JF': label = parts[1] if registers[8] == 0: pc = lines.index(label + ':') continue elif opcode == 'MOD': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] % registers[reg3] elif opcode == 'STORE': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) memory[registers[reg2]] = registers[reg1] pc += 1 return memory[:registers[8]] 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 75, in <module>
answer = evaluate("""
^^^^^^^^^^^^
File "/usr/src/app/main.py", line 65, in evaluate
memory[registers[reg2]] = registers[reg1]
~~~~~~^^^^^^^^^^^^^^^^^
IndexError: list assignment 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: |
Here's a Python interpreter `evaluate(str)` that simulates the execution of the given assembly language program 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 | def evaluate(program): lines = program.strip().split('\n') labels = {} instructions = [] # Parse the program and extract labels and instructions for line in lines: line = line.strip() if not line or line.startswith('//'): continue if ':' in line: label, instruction = line.split(':', 1) labels[label.strip()] = len(instructions) if instruction.strip(): instructions.append(instruction.strip()) else: instructions.append(line) # Initialize registers and memory registers = [0] * 10 memory = [0] * 100 # Execute the program pc = 0 while pc < len(instructions): instruction = instructions[pc] parts = instruction.split() opcode = parts[0] if opcode == 'SET': reg = int(parts[1][1:]) value = int(parts[2]) registers[reg] = value elif opcode == 'JMP': label = parts[1] pc = labels[label] continue elif opcode == 'EQ': reg1 = int(parts[1][1:]) reg2 = int(parts[2]) registers[reg1] = int(registers[reg1] == reg2) elif opcode == 'JF': label = parts[1] if not registers[0]: pc = labels[label] continue elif opcode == 'STORE': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) memory[registers[reg2]] = registers[reg1] elif opcode == 'INC': reg = int(parts[1][1:]) registers[reg] += 1 elif opcode == 'MUL': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] * registers[reg3] elif opcode == 'GT': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[reg1] = int(registers[reg1] > registers[reg2]) elif opcode == 'JT': label = parts[1] if registers[0]: pc = labels[label] continue elif opcode == 'MOD': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] % registers[reg3] else: raise ValueError(f"Unknown opcode: {opcode}") pc += 1 return memory[:registers[8]] |
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 | def evaluate(program): lines = program.strip().split('\n') labels = {} instructions = [] # Parse the program and extract labels and instructions for line in lines: line = line.strip() if not line or line.startswith('//'): continue if ':' in line: label, instruction = line.split(':', 1) labels[label.strip()] = len(instructions) if instruction.strip(): instructions.append(instruction.strip()) else: instructions.append(line) # Initialize registers and memory registers = [0] * 10 memory = [0] * 100 # Execute the program pc = 0 while pc < len(instructions): instruction = instructions[pc] parts = instruction.split() opcode = parts[0] if opcode == 'SET': reg = int(parts[1][1:]) value = int(parts[2]) registers[reg] = value elif opcode == 'JMP': label = parts[1] pc = labels[label] continue elif opcode == 'EQ': reg1 = int(parts[1][1:]) reg2 = int(parts[2]) registers[reg1] = int(registers[reg1] == reg2) elif opcode == 'JF': label = parts[1] if not registers[0]: pc = labels[label] continue elif opcode == 'STORE': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) memory[registers[reg2]] = registers[reg1] elif opcode == 'INC': reg = int(parts[1][1:]) registers[reg] += 1 elif opcode == 'MUL': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] * registers[reg3] elif opcode == 'GT': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[reg1] = int(registers[reg1] > registers[reg2]) elif opcode == 'JT': label = parts[1] if registers[0]: pc = labels[label] continue elif opcode == 'MOD': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] % registers[reg3] else: raise ValueError(f"Unknown opcode: {opcode}") pc += 1 return memory[:registers[8]] |
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 | def evaluate(program): lines = program.strip().split('\n') labels = {} instructions = [] # Parse the program and extract labels and instructions for line in lines: line = line.strip() if not line or line.startswith('//'): continue if ':' in line: label, instruction = line.split(':', 1) labels[label.strip()] = len(instructions) if instruction.strip(): instructions.append(instruction.strip()) else: instructions.append(line) # Initialize registers and memory registers = [0] * 10 memory = [0] * 100 # Execute the program pc = 0 while pc < len(instructions): instruction = instructions[pc] parts = instruction.split() opcode = parts[0] if opcode == 'SET': reg = int(parts[1][1:]) value = int(parts[2]) registers[reg] = value elif opcode == 'JMP': label = parts[1] pc = labels[label] continue elif opcode == 'EQ': reg1 = int(parts[1][1:]) reg2 = int(parts[2]) registers[reg1] = int(registers[reg1] == reg2) elif opcode == 'JF': label = parts[1] if not registers[0]: pc = labels[label] continue elif opcode == 'STORE': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) memory[registers[reg2]] = registers[reg1] elif opcode == 'INC': reg = int(parts[1][1:]) registers[reg] += 1 elif opcode == 'MUL': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] * registers[reg3] elif opcode == 'GT': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[reg1] = int(registers[reg1] > registers[reg2]) elif opcode == 'JT': label = parts[1] if registers[0]: pc = labels[label] continue elif opcode == 'MOD': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] % registers[reg3] else: raise ValueError(f"Unknown opcode: {opcode}") pc += 1 return memory[:registers[8]] 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: |
Here's a Python interpreter `evaluate(str)` that takes the assembly language program as input and returns the resulting memory state after running the 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 | def evaluate(program): lines = program.split('\n') registers = [0] * 10 memory = [0] * 1000 pc = 0 def get_value(operand): if operand.startswith('R'): return registers[int(operand[1:])] else: return int(operand) while pc < len(lines): line = lines[pc].strip() if line == '' or line.startswith('//'): pc += 1 continue parts = line.split() instruction = parts[0] if instruction == 'SET': reg, value = parts[1], get_value(parts[2]) registers[int(reg[1:])] = value elif instruction == 'INC': reg = parts[1] registers[int(reg[1:])] += 1 elif instruction == 'MUL': reg1, reg2, reg3 = parts[1], parts[2], parts[3] registers[int(reg1[1:])] = get_value(reg2) * get_value(reg3) elif instruction == 'GT': reg1, reg2 = parts[1], parts[2] registers[9] = int(get_value(reg1) > get_value(reg2)) elif instruction == 'EQ': reg1, reg2 = parts[1], parts[2] registers[9] = int(get_value(reg1) == get_value(reg2)) elif instruction == 'JMP': label = parts[1] pc = lines.index(label + ':') continue elif instruction == 'JT': label = parts[1] if registers[9]: pc = lines.index(label + ':') continue elif instruction == 'JF': label = parts[1] if not registers[9]: pc = lines.index(label + ':') continue elif instruction == 'MOD': reg1, reg2, reg3 = parts[1], parts[2], parts[3] registers[int(reg1[1:])] = get_value(reg2) % get_value(reg3) elif instruction == 'STORE': reg1, reg2 = parts[1], parts[2] memory[get_value(reg2)] = get_value(reg1) pc += 1 return [memory[i] for i in range(100)] # 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: ''' 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 112 113 114 | def evaluate(program): lines = program.split('\n') registers = [0] * 10 memory = [0] * 1000 pc = 0 def get_value(operand): if operand.startswith('R'): return registers[int(operand[1:])] else: return int(operand) while pc < len(lines): line = lines[pc].strip() if line == '' or line.startswith('//'): pc += 1 continue parts = line.split() instruction = parts[0] if instruction == 'SET': reg, value = parts[1], get_value(parts[2]) registers[int(reg[1:])] = value elif instruction == 'INC': reg = parts[1] registers[int(reg[1:])] += 1 elif instruction == 'MUL': reg1, reg2, reg3 = parts[1], parts[2], parts[3] registers[int(reg1[1:])] = get_value(reg2) * get_value(reg3) elif instruction == 'GT': reg1, reg2 = parts[1], parts[2] registers[9] = int(get_value(reg1) > get_value(reg2)) elif instruction == 'EQ': reg1, reg2 = parts[1], parts[2] registers[9] = int(get_value(reg1) == get_value(reg2)) elif instruction == 'JMP': label = parts[1] pc = lines.index(label + ':') continue elif instruction == 'JT': label = parts[1] if registers[9]: pc = lines.index(label + ':') continue elif instruction == 'JF': label = parts[1] if not registers[9]: pc = lines.index(label + ':') continue elif instruction == 'MOD': reg1, reg2, reg3 = parts[1], parts[2], parts[3] registers[int(reg1[1:])] = get_value(reg2) % get_value(reg3) elif instruction == 'STORE': reg1, reg2 = parts[1], parts[2] memory[get_value(reg2)] = get_value(reg1) pc += 1 return [memory[i] for i in range(100)] # 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: ''' 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 186 187 188 | def evaluate(program): lines = program.split('\n') registers = [0] * 10 memory = [0] * 1000 pc = 0 def get_value(operand): if operand.startswith('R'): return registers[int(operand[1:])] else: return int(operand) while pc < len(lines): line = lines[pc].strip() if line == '' or line.startswith('//'): pc += 1 continue parts = line.split() instruction = parts[0] if instruction == 'SET': reg, value = parts[1], get_value(parts[2]) registers[int(reg[1:])] = value elif instruction == 'INC': reg = parts[1] registers[int(reg[1:])] += 1 elif instruction == 'MUL': reg1, reg2, reg3 = parts[1], parts[2], parts[3] registers[int(reg1[1:])] = get_value(reg2) * get_value(reg3) elif instruction == 'GT': reg1, reg2 = parts[1], parts[2] registers[9] = int(get_value(reg1) > get_value(reg2)) elif instruction == 'EQ': reg1, reg2 = parts[1], parts[2] registers[9] = int(get_value(reg1) == get_value(reg2)) elif instruction == 'JMP': label = parts[1] pc = lines.index(label + ':') continue elif instruction == 'JT': label = parts[1] if registers[9]: pc = lines.index(label + ':') continue elif instruction == 'JF': label = parts[1] if not registers[9]: pc = lines.index(label + ':') continue elif instruction == 'MOD': reg1, reg2, reg3 = parts[1], parts[2], parts[3] registers[int(reg1[1:])] = get_value(reg2) % get_value(reg3) elif instruction == 'STORE': reg1, reg2 = parts[1], parts[2] memory[get_value(reg2)] = get_value(reg1) pc += 1 return [memory[i] for i in range(100)] # 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: ''' 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') |
Traceback (most recent call last):
File "/usr/src/app/main.py", line 114, in <module>
result = evaluate(program)
^^^^^^^^^^^^^^^^^
File "/usr/src/app/main.py", line 40, in evaluate
pc = lines.index(label + ':')
^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: 'start_loop:' is not in list
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 `evaluate(str)` that simulates the execution of the assembly language program you provided and returns the resulting memory state after running the 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 | def evaluate(program): lines = program.strip().split('\n') labels = {} memory = [0] * 100 registers = [0] * 10 pc = 0 # First pass: identify labels for i, line in enumerate(lines): if ':' in line: label = line.split(':')[0].strip() labels[label] = i # Second pass: execute instructions while pc < len(lines): line = lines[pc].strip() if ':' in line: pc += 1 continue parts = line.split() if len(parts) == 0 or parts[0].startswith('//'): pc += 1 continue opcode = parts[0] if opcode == 'SET': reg = int(parts[1][1:]) val = int(parts[2]) registers[reg] = val elif opcode == 'JMP': label = parts[1] pc = labels[label] continue elif opcode == 'EQ': reg1 = int(parts[1][1:]) reg2 = int(parts[2]) registers[9] = int(registers[reg1] == reg2) elif opcode == 'JF': label = parts[1] if registers[9] == 0: pc = labels[label] continue elif opcode == 'STORE': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) memory[registers[reg2]] = registers[reg1] elif opcode == 'INC': reg = int(parts[1][1:]) registers[reg] += 1 elif opcode == 'MUL': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] * registers[reg3] elif opcode == 'GT': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = int(registers[reg1] > registers[reg2]) elif opcode == 'JT': label = parts[1] if registers[9] == 1: pc = labels[label] continue elif opcode == 'MOD': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] % registers[reg3] pc += 1 return memory[:registers[8]] |
1 2 3 4 5 6 | program = """ ... (your assembly language program) ... """ 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 | def evaluate(program): lines = program.strip().split('\n') labels = {} memory = [0] * 100 registers = [0] * 10 pc = 0 # First pass: identify labels for i, line in enumerate(lines): if ':' in line: label = line.split(':')[0].strip() labels[label] = i # Second pass: execute instructions while pc < len(lines): line = lines[pc].strip() if ':' in line: pc += 1 continue parts = line.split() if len(parts) == 0 or parts[0].startswith('//'): pc += 1 continue opcode = parts[0] if opcode == 'SET': reg = int(parts[1][1:]) val = int(parts[2]) registers[reg] = val elif opcode == 'JMP': label = parts[1] pc = labels[label] continue elif opcode == 'EQ': reg1 = int(parts[1][1:]) reg2 = int(parts[2]) registers[9] = int(registers[reg1] == reg2) elif opcode == 'JF': label = parts[1] if registers[9] == 0: pc = labels[label] continue elif opcode == 'STORE': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) memory[registers[reg2]] = registers[reg1] elif opcode == 'INC': reg = int(parts[1][1:]) registers[reg] += 1 elif opcode == 'MUL': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] * registers[reg3] elif opcode == 'GT': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = int(registers[reg1] > registers[reg2]) elif opcode == 'JT': label = parts[1] if registers[9] == 1: pc = labels[label] continue elif opcode == 'MOD': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] % registers[reg3] pc += 1 return memory[:registers[8]] |
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 | def evaluate(program): lines = program.strip().split('\n') labels = {} memory = [0] * 100 registers = [0] * 10 pc = 0 # First pass: identify labels for i, line in enumerate(lines): if ':' in line: label = line.split(':')[0].strip() labels[label] = i # Second pass: execute instructions while pc < len(lines): line = lines[pc].strip() if ':' in line: pc += 1 continue parts = line.split() if len(parts) == 0 or parts[0].startswith('//'): pc += 1 continue opcode = parts[0] if opcode == 'SET': reg = int(parts[1][1:]) val = int(parts[2]) registers[reg] = val elif opcode == 'JMP': label = parts[1] pc = labels[label] continue elif opcode == 'EQ': reg1 = int(parts[1][1:]) reg2 = int(parts[2]) registers[9] = int(registers[reg1] == reg2) elif opcode == 'JF': label = parts[1] if registers[9] == 0: pc = labels[label] continue elif opcode == 'STORE': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) memory[registers[reg2]] = registers[reg1] elif opcode == 'INC': reg = int(parts[1][1:]) registers[reg] += 1 elif opcode == 'MUL': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] * registers[reg3] elif opcode == 'GT': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = int(registers[reg1] > registers[reg2]) elif opcode == 'JT': label = parts[1] if registers[9] == 1: pc = labels[label] continue elif opcode == 'MOD': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] % registers[reg3] pc += 1 return memory[:registers[8]] 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 94, in <module>
assert answer == expected, f'Wrong answer; got {answer} instead of {expected}'
^^^^^^^^^^^^^^^^^^
AssertionError: Wrong answer; got [] instead of [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Testing if the previous output contains the string All tests passed
: False