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: |
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 | import re def evaluate(program): memory = {} registers = {} labels = {} # Extract labels and their corresponding line numbers lines = program.strip().split('\n') for i, line in enumerate(lines): if line.endswith(':'): labels[line[:-1]] = i # Initialize program counter pc = 0 # Helper function to get the value of a register or memory address def get_value(operand): if operand in registers: return registers[operand] elif operand.isdigit(): return int(operand) else: return memory.get(operand, 0) # Return 0 for uninitialized memory # Instruction execution loop while pc < len(lines): line = lines[pc].strip() if not line or line.startswith('//'): # Ignore empty lines and comments pc += 1 continue # Extract instruction and operands parts = re.split(r'[,\s]+', line) instruction = parts[0] operands = parts[1:] # Instruction execution if instruction == 'SET': registers[operands[0]] = get_value(operands[1]) elif instruction == 'INC': registers[operands[0]] = get_value(operands[0]) + 1 elif instruction == 'MUL': registers[operands[0]] = get_value(operands[1]) * get_value(operands[2]) elif instruction == 'MOD': registers[operands[0]] = get_value(operands[1]) % get_value(operands[2]) elif instruction == 'GT': registers['FLAG'] = 1 if get_value(operands[0]) > get_value(operands[1]) else 0 elif instruction == 'EQ': registers['FLAG'] = 1 if get_value(operands[0]) == get_value(operands[1]) else 0 elif instruction == 'JT': if registers.get('FLAG', 0) == 1: pc = labels[operands[0]] continue elif instruction == 'JF': if registers.get('FLAG', 0) == 0: pc = labels[operands[0]] continue elif instruction == 'JMP': pc = labels[operands[0]] continue elif instruction == 'STORE': memory[operands[1]] = get_value(operands[0]) pc += 1 # Return the memory state as a list return list(memory.values()) # 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) # Output: [2, 3, 5, 7, ...] |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | import re def evaluate(program): memory = {} registers = {} labels = {} # Extract labels and their corresponding line numbers lines = program.strip().split('\n') for i, line in enumerate(lines): if line.endswith(':'): labels[line[:-1]] = i # Initialize program counter pc = 0 # Helper function to get the value of a register or memory address def get_value(operand): if operand in registers: return registers[operand] elif operand.isdigit(): return int(operand) else: return memory.get(operand, 0) # Return 0 for uninitialized memory # Instruction execution loop while pc < len(lines): line = lines[pc].strip() if not line or line.startswith('//'): # Ignore empty lines and comments pc += 1 continue # Extract instruction and operands parts = re.split(r'[,\s]+', line) instruction = parts[0] operands = parts[1:] # Instruction execution if instruction == 'SET': registers[operands[0]] = get_value(operands[1]) elif instruction == 'INC': registers[operands[0]] = get_value(operands[0]) + 1 elif instruction == 'MUL': registers[operands[0]] = get_value(operands[1]) * get_value(operands[2]) elif instruction == 'MOD': registers[operands[0]] = get_value(operands[1]) % get_value(operands[2]) elif instruction == 'GT': registers['FLAG'] = 1 if get_value(operands[0]) > get_value(operands[1]) else 0 elif instruction == 'EQ': registers['FLAG'] = 1 if get_value(operands[0]) == get_value(operands[1]) else 0 elif instruction == 'JT': if registers.get('FLAG', 0) == 1: pc = labels[operands[0]] continue elif instruction == 'JF': if registers.get('FLAG', 0) == 0: pc = labels[operands[0]] continue elif instruction == 'JMP': pc = labels[operands[0]] continue elif instruction == 'STORE': memory[operands[1]] = get_value(operands[0]) pc += 1 # Return the memory state as a list return list(memory.values()) # 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) # Output: [2, 3, 5, 7, ...] |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | import re def evaluate(program): memory = {} registers = {} labels = {} # Extract labels and their corresponding line numbers lines = program.strip().split('\n') for i, line in enumerate(lines): if line.endswith(':'): labels[line[:-1]] = i # Initialize program counter pc = 0 # Helper function to get the value of a register or memory address def get_value(operand): if operand in registers: return registers[operand] elif operand.isdigit(): return int(operand) else: return memory.get(operand, 0) # Return 0 for uninitialized memory # Instruction execution loop while pc < len(lines): line = lines[pc].strip() if not line or line.startswith('//'): # Ignore empty lines and comments pc += 1 continue # Extract instruction and operands parts = re.split(r'[,\s]+', line) instruction = parts[0] operands = parts[1:] # Instruction execution if instruction == 'SET': registers[operands[0]] = get_value(operands[1]) elif instruction == 'INC': registers[operands[0]] = get_value(operands[0]) + 1 elif instruction == 'MUL': registers[operands[0]] = get_value(operands[1]) * get_value(operands[2]) elif instruction == 'MOD': registers[operands[0]] = get_value(operands[1]) % get_value(operands[2]) elif instruction == 'GT': registers['FLAG'] = 1 if get_value(operands[0]) > get_value(operands[1]) else 0 elif instruction == 'EQ': registers['FLAG'] = 1 if get_value(operands[0]) == get_value(operands[1]) else 0 elif instruction == 'JT': if registers.get('FLAG', 0) == 1: pc = labels[operands[0]] continue elif instruction == 'JF': if registers.get('FLAG', 0) == 0: pc = labels[operands[0]] continue elif instruction == 'JMP': pc = labels[operands[0]] continue elif instruction == 'STORE': memory[operands[1]] = get_value(operands[0]) pc += 1 # Return the memory state as a list return list(memory.values()) # 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) # Output: [2, 3, 5, 7, ...] 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 123, in <module>
result = evaluate(program)
File "/usr/src/app/main.py", line 61, in evaluate
pc = labels[operands[0]]
KeyError: 'start_loop'
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: |
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 | import re def evaluate(program): """ Evaluates a program written in a custom assembly language and returns the memory state. Args: program (str): The program code. Returns: list: The memory state after program execution. """ memory = [0] * 1000 # Initialize memory registers = {f"R{i}": 0 for i in range(10)} # Initialize registers labels = {} # Store label addresses # Preprocess: Find and store label addresses lines = program.strip().split("\n") pc = 0 for line in lines: line = line.strip() if line.endswith(":"): labels[line[:-1]] = pc elif not line.startswith("//") and line: # Ignore comments and empty lines pc += 1 # Execute the program pc = 0 while pc < len(lines): line = lines[pc].strip() if line.startswith("//") or not line: # Ignore comments and empty lines pc += 1 continue parts = re.split(r"[, ]+", line) instruction = parts[0] if instruction == "SET": registers[parts[1]] = int(parts[2]) elif instruction == "INC": registers[parts[1]] += 1 elif instruction == "MUL": registers[parts[1]] = registers[parts[2]] * registers[parts[3]] elif instruction == "MOD": registers[parts[1]] = registers[parts[2]] % registers[parts[3]] elif instruction == "ADD": registers[parts[1]] = registers[parts[2]] + registers[parts[3]] elif instruction == "EQ": registers["R7"] = 1 if registers[parts[1]] == int(parts[2]) else 0 elif instruction == "GT": registers["R7"] = 1 if registers[parts[1]] > registers[parts[2]] else 0 elif instruction == "JT": if registers["R7"] == 1: pc = labels[parts[1]] continue elif instruction == "JF": if registers["R7"] == 0: pc = labels[parts[1]] continue elif instruction == "JMP": pc = labels[parts[1]] continue elif instruction == "STORE": memory[registers[parts[2]]] = registers[parts[1]] pc += 1 return [x for x in memory if x != 0] # Return non-zero memory values # Example usage program = """ SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime // Control will return after executing is_prime 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: """ 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 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 | import re def evaluate(program): """ Evaluates a program written in a custom assembly language and returns the memory state. Args: program (str): The program code. Returns: list: The memory state after program execution. """ memory = [0] * 1000 # Initialize memory registers = {f"R{i}": 0 for i in range(10)} # Initialize registers labels = {} # Store label addresses # Preprocess: Find and store label addresses lines = program.strip().split("\n") pc = 0 for line in lines: line = line.strip() if line.endswith(":"): labels[line[:-1]] = pc elif not line.startswith("//") and line: # Ignore comments and empty lines pc += 1 # Execute the program pc = 0 while pc < len(lines): line = lines[pc].strip() if line.startswith("//") or not line: # Ignore comments and empty lines pc += 1 continue parts = re.split(r"[, ]+", line) instruction = parts[0] if instruction == "SET": registers[parts[1]] = int(parts[2]) elif instruction == "INC": registers[parts[1]] += 1 elif instruction == "MUL": registers[parts[1]] = registers[parts[2]] * registers[parts[3]] elif instruction == "MOD": registers[parts[1]] = registers[parts[2]] % registers[parts[3]] elif instruction == "ADD": registers[parts[1]] = registers[parts[2]] + registers[parts[3]] elif instruction == "EQ": registers["R7"] = 1 if registers[parts[1]] == int(parts[2]) else 0 elif instruction == "GT": registers["R7"] = 1 if registers[parts[1]] > registers[parts[2]] else 0 elif instruction == "JT": if registers["R7"] == 1: pc = labels[parts[1]] continue elif instruction == "JF": if registers["R7"] == 0: pc = labels[parts[1]] continue elif instruction == "JMP": pc = labels[parts[1]] continue elif instruction == "STORE": memory[registers[parts[2]]] = registers[parts[1]] pc += 1 return [x for x in memory if x != 0] # Return non-zero memory values # Example usage program = """ SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime // Control will return after executing is_prime 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: """ 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | import re def evaluate(program): """ Evaluates a program written in a custom assembly language and returns the memory state. Args: program (str): The program code. Returns: list: The memory state after program execution. """ memory = [0] * 1000 # Initialize memory registers = {f"R{i}": 0 for i in range(10)} # Initialize registers labels = {} # Store label addresses # Preprocess: Find and store label addresses lines = program.strip().split("\n") pc = 0 for line in lines: line = line.strip() if line.endswith(":"): labels[line[:-1]] = pc elif not line.startswith("//") and line: # Ignore comments and empty lines pc += 1 # Execute the program pc = 0 while pc < len(lines): line = lines[pc].strip() if line.startswith("//") or not line: # Ignore comments and empty lines pc += 1 continue parts = re.split(r"[, ]+", line) instruction = parts[0] if instruction == "SET": registers[parts[1]] = int(parts[2]) elif instruction == "INC": registers[parts[1]] += 1 elif instruction == "MUL": registers[parts[1]] = registers[parts[2]] * registers[parts[3]] elif instruction == "MOD": registers[parts[1]] = registers[parts[2]] % registers[parts[3]] elif instruction == "ADD": registers[parts[1]] = registers[parts[2]] + registers[parts[3]] elif instruction == "EQ": registers["R7"] = 1 if registers[parts[1]] == int(parts[2]) else 0 elif instruction == "GT": registers["R7"] = 1 if registers[parts[1]] > registers[parts[2]] else 0 elif instruction == "JT": if registers["R7"] == 1: pc = labels[parts[1]] continue elif instruction == "JF": if registers["R7"] == 0: pc = labels[parts[1]] continue elif instruction == "JMP": pc = labels[parts[1]] continue elif instruction == "STORE": memory[registers[parts[2]]] = registers[parts[1]] pc += 1 return [x for x in memory if x != 0] # Return non-zero memory values # Example usage program = """ SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime // Control will return after executing is_prime 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: """ 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') |
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: |
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 | import re def evaluate(program): """ Evaluates the given assembly program and returns the resulting memory state. Args: program (str): The assembly program to evaluate. Returns: list: A list representing the memory state after program execution. """ memory = [0] * 1000 # Initialize memory registers = {f"R{i}": 0 for i in range(10)} # Initialize registers labels = {} # Store label addresses # Preprocess: Find and store label addresses lines = program.strip().split("\n") pc = 0 for line in lines: line = line.strip() if line.endswith(":"): labels[line[:-1]] = pc elif not line.startswith("//") and line: # Skip comments and empty lines pc += 1 # Execute the program pc = 0 while pc < len(lines): line = lines[pc].strip() if line.startswith("//") or not line: # Skip comments and empty lines pc += 1 continue instruction = line.split()[0] operands = line.split()[1:] if instruction == "SET": registers[operands[0]] = int(operands[1]) elif instruction == "INC": registers[operands[0]] += 1 elif instruction == "STORE": memory[registers[operands[1]]] = registers[operands[0]] elif instruction == "JMP": pc = labels[operands[0]] continue elif instruction == "EQ": registers["R7"] = 1 if registers[operands[0]] == int(operands[1]) else 0 elif instruction == "GT": registers["R7"] = 1 if registers[operands[0]] > int(operands[1]) else 0 elif instruction == "JT": if registers["R7"] == 1: pc = labels[operands[0]] continue elif instruction == "JF": if registers["R7"] == 0: pc = labels[operands[0]] continue elif instruction == "MUL": registers[operands[0]] = registers[operands[1]] * registers[operands[2]] elif instruction == "MOD": registers[operands[0]] = registers[operands[1]] % registers[operands[2]] else: raise ValueError(f"Invalid instruction: {instruction}") pc += 1 return [x for x in memory if x != 0] # 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: """ 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 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 | import re def evaluate(program): """ Evaluates the given assembly program and returns the resulting memory state. Args: program (str): The assembly program to evaluate. Returns: list: A list representing the memory state after program execution. """ memory = [0] * 1000 # Initialize memory registers = {f"R{i}": 0 for i in range(10)} # Initialize registers labels = {} # Store label addresses # Preprocess: Find and store label addresses lines = program.strip().split("\n") pc = 0 for line in lines: line = line.strip() if line.endswith(":"): labels[line[:-1]] = pc elif not line.startswith("//") and line: # Skip comments and empty lines pc += 1 # Execute the program pc = 0 while pc < len(lines): line = lines[pc].strip() if line.startswith("//") or not line: # Skip comments and empty lines pc += 1 continue instruction = line.split()[0] operands = line.split()[1:] if instruction == "SET": registers[operands[0]] = int(operands[1]) elif instruction == "INC": registers[operands[0]] += 1 elif instruction == "STORE": memory[registers[operands[1]]] = registers[operands[0]] elif instruction == "JMP": pc = labels[operands[0]] continue elif instruction == "EQ": registers["R7"] = 1 if registers[operands[0]] == int(operands[1]) else 0 elif instruction == "GT": registers["R7"] = 1 if registers[operands[0]] > int(operands[1]) else 0 elif instruction == "JT": if registers["R7"] == 1: pc = labels[operands[0]] continue elif instruction == "JF": if registers["R7"] == 0: pc = labels[operands[0]] continue elif instruction == "MUL": registers[operands[0]] = registers[operands[1]] * registers[operands[2]] elif instruction == "MOD": registers[operands[0]] = registers[operands[1]] % registers[operands[2]] else: raise ValueError(f"Invalid instruction: {instruction}") pc += 1 return [x for x in memory if x != 0] # 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: """ 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | import re def evaluate(program): """ Evaluates the given assembly program and returns the resulting memory state. Args: program (str): The assembly program to evaluate. Returns: list: A list representing the memory state after program execution. """ memory = [0] * 1000 # Initialize memory registers = {f"R{i}": 0 for i in range(10)} # Initialize registers labels = {} # Store label addresses # Preprocess: Find and store label addresses lines = program.strip().split("\n") pc = 0 for line in lines: line = line.strip() if line.endswith(":"): labels[line[:-1]] = pc elif not line.startswith("//") and line: # Skip comments and empty lines pc += 1 # Execute the program pc = 0 while pc < len(lines): line = lines[pc].strip() if line.startswith("//") or not line: # Skip comments and empty lines pc += 1 continue instruction = line.split()[0] operands = line.split()[1:] if instruction == "SET": registers[operands[0]] = int(operands[1]) elif instruction == "INC": registers[operands[0]] += 1 elif instruction == "STORE": memory[registers[operands[1]]] = registers[operands[0]] elif instruction == "JMP": pc = labels[operands[0]] continue elif instruction == "EQ": registers["R7"] = 1 if registers[operands[0]] == int(operands[1]) else 0 elif instruction == "GT": registers["R7"] = 1 if registers[operands[0]] > int(operands[1]) else 0 elif instruction == "JT": if registers["R7"] == 1: pc = labels[operands[0]] continue elif instruction == "JF": if registers["R7"] == 0: pc = labels[operands[0]] continue elif instruction == "MUL": registers[operands[0]] = registers[operands[1]] * registers[operands[2]] elif instruction == "MOD": registers[operands[0]] = registers[operands[1]] % registers[operands[2]] else: raise ValueError(f"Invalid instruction: {instruction}") pc += 1 return [x for x in memory if x != 0] # 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: """ 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 124, in <module>
primes = evaluate(program)
File "/usr/src/app/main.py", line 66, in evaluate
raise ValueError(f"Invalid instruction: {instruction}")
ValueError: Invalid instruction: start_find_primes:
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: |
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 | import re def evaluate(program): """ Evaluates the given assembly program and returns the resulting memory state. Args: program (str): The assembly program to evaluate. Returns: list: A list representing the memory state after program execution. """ memory = [0] * 1000 # Initialize memory registers = {f"R{i}": 0 for i in range(10)} # Initialize registers labels = {} # Store label addresses # Preprocess program, find labels lines = program.strip().split("\n") pc = 0 while pc < len(lines): line = lines[pc].strip() if line.endswith(":"): labels[line[:-1]] = pc pc += 1 # Execute program pc = 0 while pc < len(lines): line = lines[pc].strip() parts = re.split(r"[, ]+", line) if parts[0] == "SET": registers[parts[1]] = int(parts[2]) elif parts[0] == "INC": registers[parts[1]] += 1 elif parts[0] == "STORE": memory[registers[parts[2]]] = registers[parts[1]] elif parts[0] == "EQ": registers["R7"] = 1 if registers[parts[1]] == int(parts[2]) else 0 elif parts[0] == "GT": registers["R7"] = 1 if registers[parts[1]] > registers[parts[2]] else 0 elif parts[0] == "MUL": registers[parts[1]] = registers[parts[2]] * registers[parts[3]] elif parts[0] == "MOD": registers[parts[1]] = registers[parts[2]] % registers[parts[3]] elif parts[0] == "JMP": if parts[1] in labels: pc = labels[parts[1]] - 1 # Adjust for incrementing pc later else: raise ValueError(f"Invalid label: {parts[1]}") elif parts[0] == "JT": if registers["R7"] == 1: if parts[1] in labels: pc = labels[parts[1]] - 1 else: raise ValueError(f"Invalid label: {parts[1]}") elif parts[0] == "JF": if registers["R7"] == 0: if parts[1] in labels: pc = labels[parts[1]] - 1 else: raise ValueError(f"Invalid label: {parts[1]}") pc += 1 return [x for x in memory if x != 0] # Return non-zero memory values |
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 | import re def evaluate(program): """ Evaluates the given assembly program and returns the resulting memory state. Args: program (str): The assembly program to evaluate. Returns: list: A list representing the memory state after program execution. """ memory = [0] * 1000 # Initialize memory registers = {f"R{i}": 0 for i in range(10)} # Initialize registers labels = {} # Store label addresses # Preprocess program, find labels lines = program.strip().split("\n") pc = 0 while pc < len(lines): line = lines[pc].strip() if line.endswith(":"): labels[line[:-1]] = pc pc += 1 # Execute program pc = 0 while pc < len(lines): line = lines[pc].strip() parts = re.split(r"[, ]+", line) if parts[0] == "SET": registers[parts[1]] = int(parts[2]) elif parts[0] == "INC": registers[parts[1]] += 1 elif parts[0] == "STORE": memory[registers[parts[2]]] = registers[parts[1]] elif parts[0] == "EQ": registers["R7"] = 1 if registers[parts[1]] == int(parts[2]) else 0 elif parts[0] == "GT": registers["R7"] = 1 if registers[parts[1]] > registers[parts[2]] else 0 elif parts[0] == "MUL": registers[parts[1]] = registers[parts[2]] * registers[parts[3]] elif parts[0] == "MOD": registers[parts[1]] = registers[parts[2]] % registers[parts[3]] elif parts[0] == "JMP": if parts[1] in labels: pc = labels[parts[1]] - 1 # Adjust for incrementing pc later else: raise ValueError(f"Invalid label: {parts[1]}") elif parts[0] == "JT": if registers["R7"] == 1: if parts[1] in labels: pc = labels[parts[1]] - 1 else: raise ValueError(f"Invalid label: {parts[1]}") elif parts[0] == "JF": if registers["R7"] == 0: if parts[1] in labels: pc = labels[parts[1]] - 1 else: raise ValueError(f"Invalid label: {parts[1]}") pc += 1 return [x for x in memory if x != 0] # Return non-zero memory values |
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 | import re def evaluate(program): """ Evaluates the given assembly program and returns the resulting memory state. Args: program (str): The assembly program to evaluate. Returns: list: A list representing the memory state after program execution. """ memory = [0] * 1000 # Initialize memory registers = {f"R{i}": 0 for i in range(10)} # Initialize registers labels = {} # Store label addresses # Preprocess program, find labels lines = program.strip().split("\n") pc = 0 while pc < len(lines): line = lines[pc].strip() if line.endswith(":"): labels[line[:-1]] = pc pc += 1 # Execute program pc = 0 while pc < len(lines): line = lines[pc].strip() parts = re.split(r"[, ]+", line) if parts[0] == "SET": registers[parts[1]] = int(parts[2]) elif parts[0] == "INC": registers[parts[1]] += 1 elif parts[0] == "STORE": memory[registers[parts[2]]] = registers[parts[1]] elif parts[0] == "EQ": registers["R7"] = 1 if registers[parts[1]] == int(parts[2]) else 0 elif parts[0] == "GT": registers["R7"] = 1 if registers[parts[1]] > registers[parts[2]] else 0 elif parts[0] == "MUL": registers[parts[1]] = registers[parts[2]] * registers[parts[3]] elif parts[0] == "MOD": registers[parts[1]] = registers[parts[2]] % registers[parts[3]] elif parts[0] == "JMP": if parts[1] in labels: pc = labels[parts[1]] - 1 # Adjust for incrementing pc later else: raise ValueError(f"Invalid label: {parts[1]}") elif parts[0] == "JT": if registers["R7"] == 1: if parts[1] in labels: pc = labels[parts[1]] - 1 else: raise ValueError(f"Invalid label: {parts[1]}") elif parts[0] == "JF": if registers["R7"] == 0: if parts[1] in labels: pc = labels[parts[1]] - 1 else: raise ValueError(f"Invalid label: {parts[1]}") pc += 1 return [x for x in memory if x != 0] # Return non-zero memory values 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 89, in <module>
answer = evaluate("""
File "/usr/src/app/main.py", line 41, in evaluate
registers["R7"] = 1 if registers[parts[1]] == int(parts[2]) else 0
ValueError: invalid literal for int() with base 10: 'R6'
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: |
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 | import re def evaluate(program): """ Evaluates the given assembly program and returns the resulting memory state. Args: program (str): The assembly program to evaluate. Returns: list: A list representing the memory state after program execution. """ memory = [0] * 1000 # Initialize memory with 1000 cells registers = {f"R{i}": 0 for i in range(10)} # Initialize registers R0-R9 labels = {} # Store label addresses pc = 0 # Program counter # Preprocess: Find and store label addresses for line in program.splitlines(): line = line.strip() if line and line.endswith(":"): labels[line[:-1]] = pc pc += 1 # Reset program counter pc = 0 while pc < len(program.splitlines()): line = program.splitlines()[pc].strip() if not line or line.endswith(":"): pc += 1 continue # Instruction parsing using regular expressions if match := re.match(r"SET\s+R(\d+)\s+(-?\d+)", line): registers[f"R{match.group(1)}"] = int(match.group(2)) elif match := re.match(r"SET\s+R(\d+)\s+R(\d+)", line): registers[f"R{match.group(1)}"] = registers[f"R{match.group(2)}"] elif match := re.match(r"ADD\s+R(\d+)\s+R(\d+)\s+R(\d+)", line): registers[f"R{match.group(1)}"] = registers[f"R{match.group(2)}"] + registers[f"R{match.group(3)}"] elif match := re.match(r"SUB\s+R(\d+)\s+R(\d+)\s+R(\d+)", line): registers[f"R{match.group(1)}"] = registers[f"R{match.group(2)}"] - registers[f"R{match.group(3)}"] elif match := re.match(r"MUL\s+R(\d+)\s+R(\d+)\s+R(\d+)", line): registers[f"R{match.group(1)}"] = registers[f"R{match.group(2)}"] * registers[f"R{match.group(3)}"] elif match := re.match(r"DIV\s+R(\d+)\s+R(\d+)\s+R(\d+)", line): registers[f"R{match.group(1)}"] = registers[f"R{match.group(2)}"] // registers[f"R{match.group(3)}"] elif match := re.match(r"MOD\s+R(\d+)\s+R(\d+)\s+R(\d+)", line): registers[f"R{match.group(1)}"] = registers[f"R{match.group(2)}"] % registers[f"R{match.group(3)}"] elif match := re.match(r"INC\s+R(\d+)", line): registers[f"R{match.group(1)}"] += 1 elif match := re.match(r"DEC\s+R(\d+)", line): registers[f"R{match.group(1)}"] -= 1 elif match := re.match(r"CMP\s+R(\d+)\s+R(\d+)", line): registers["RF"] = 1 if registers[f"R{match.group(1)}"] == registers[f"R{match.group(2)}"] else 0 elif match := re.match(r"GT\s+R(\d+)\s+R(\d+)", line): registers["RF"] = 1 if registers[f"R{match.group(1)}"] > registers[f"R{match.group(2)}"] else 0 elif match := re.match(r"LT\s+R(\d+)\s+R(\d+)", line): registers["RF"] = 1 if registers[f"R{match.group(1)}"] < registers[f"R{match.group(2)}"] else 0 elif match := re.match(r"EQ\s+R(\d+)\s+(-?\d+)", line): registers["RF"] = 1 if registers[f"R{match.group(1)}"] == int(match.group(2)) else 0 elif match := re.match(r"JMP\s+(\w+)", line): pc = labels[match.group(1)] continue elif match := re.match(r"JT\s+(\w+)", line): if registers["RF"] == 1: pc = labels[match.group(1)] continue elif match := re.match(r"JF\s+(\w+)", line): if registers["RF"] == 0: pc = labels[match.group(1)] continue elif match := re.match(r"STORE\s+R(\d+)\s+R(\d+)", line): memory[registers[f"R{match.group(2)}"]] = registers[f"R{match.group(1)}"] elif match := re.match(r"LOAD\s+R(\d+)\s+R(\d+)", line): registers[f"R{match.group(1)}"] = memory[registers[f"R{match.group(2)}"]] else: raise ValueError(f"Invalid instruction: {line}") pc += 1 return [x for x in memory if x != 0] # Return non-zero memory values # Test the interpreter with the provided program program = """ SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ 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: """ 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 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 | import re def evaluate(program): """ Evaluates the given assembly program and returns the resulting memory state. Args: program (str): The assembly program to evaluate. Returns: list: A list representing the memory state after program execution. """ memory = [0] * 1000 # Initialize memory with 1000 cells registers = {f"R{i}": 0 for i in range(10)} # Initialize registers R0-R9 labels = {} # Store label addresses pc = 0 # Program counter # Preprocess: Find and store label addresses for line in program.splitlines(): line = line.strip() if line and line.endswith(":"): labels[line[:-1]] = pc pc += 1 # Reset program counter pc = 0 while pc < len(program.splitlines()): line = program.splitlines()[pc].strip() if not line or line.endswith(":"): pc += 1 continue # Instruction parsing using regular expressions if match := re.match(r"SET\s+R(\d+)\s+(-?\d+)", line): registers[f"R{match.group(1)}"] = int(match.group(2)) elif match := re.match(r"SET\s+R(\d+)\s+R(\d+)", line): registers[f"R{match.group(1)}"] = registers[f"R{match.group(2)}"] elif match := re.match(r"ADD\s+R(\d+)\s+R(\d+)\s+R(\d+)", line): registers[f"R{match.group(1)}"] = registers[f"R{match.group(2)}"] + registers[f"R{match.group(3)}"] elif match := re.match(r"SUB\s+R(\d+)\s+R(\d+)\s+R(\d+)", line): registers[f"R{match.group(1)}"] = registers[f"R{match.group(2)}"] - registers[f"R{match.group(3)}"] elif match := re.match(r"MUL\s+R(\d+)\s+R(\d+)\s+R(\d+)", line): registers[f"R{match.group(1)}"] = registers[f"R{match.group(2)}"] * registers[f"R{match.group(3)}"] elif match := re.match(r"DIV\s+R(\d+)\s+R(\d+)\s+R(\d+)", line): registers[f"R{match.group(1)}"] = registers[f"R{match.group(2)}"] // registers[f"R{match.group(3)}"] elif match := re.match(r"MOD\s+R(\d+)\s+R(\d+)\s+R(\d+)", line): registers[f"R{match.group(1)}"] = registers[f"R{match.group(2)}"] % registers[f"R{match.group(3)}"] elif match := re.match(r"INC\s+R(\d+)", line): registers[f"R{match.group(1)}"] += 1 elif match := re.match(r"DEC\s+R(\d+)", line): registers[f"R{match.group(1)}"] -= 1 elif match := re.match(r"CMP\s+R(\d+)\s+R(\d+)", line): registers["RF"] = 1 if registers[f"R{match.group(1)}"] == registers[f"R{match.group(2)}"] else 0 elif match := re.match(r"GT\s+R(\d+)\s+R(\d+)", line): registers["RF"] = 1 if registers[f"R{match.group(1)}"] > registers[f"R{match.group(2)}"] else 0 elif match := re.match(r"LT\s+R(\d+)\s+R(\d+)", line): registers["RF"] = 1 if registers[f"R{match.group(1)}"] < registers[f"R{match.group(2)}"] else 0 elif match := re.match(r"EQ\s+R(\d+)\s+(-?\d+)", line): registers["RF"] = 1 if registers[f"R{match.group(1)}"] == int(match.group(2)) else 0 elif match := re.match(r"JMP\s+(\w+)", line): pc = labels[match.group(1)] continue elif match := re.match(r"JT\s+(\w+)", line): if registers["RF"] == 1: pc = labels[match.group(1)] continue elif match := re.match(r"JF\s+(\w+)", line): if registers["RF"] == 0: pc = labels[match.group(1)] continue elif match := re.match(r"STORE\s+R(\d+)\s+R(\d+)", line): memory[registers[f"R{match.group(2)}"]] = registers[f"R{match.group(1)}"] elif match := re.match(r"LOAD\s+R(\d+)\s+R(\d+)", line): registers[f"R{match.group(1)}"] = memory[registers[f"R{match.group(2)}"]] else: raise ValueError(f"Invalid instruction: {line}") pc += 1 return [x for x in memory if x != 0] # Return non-zero memory values # Test the interpreter with the provided program program = """ SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ 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: """ 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | import re def evaluate(program): """ Evaluates the given assembly program and returns the resulting memory state. Args: program (str): The assembly program to evaluate. Returns: list: A list representing the memory state after program execution. """ memory = [0] * 1000 # Initialize memory with 1000 cells registers = {f"R{i}": 0 for i in range(10)} # Initialize registers R0-R9 labels = {} # Store label addresses pc = 0 # Program counter # Preprocess: Find and store label addresses for line in program.splitlines(): line = line.strip() if line and line.endswith(":"): labels[line[:-1]] = pc pc += 1 # Reset program counter pc = 0 while pc < len(program.splitlines()): line = program.splitlines()[pc].strip() if not line or line.endswith(":"): pc += 1 continue # Instruction parsing using regular expressions if match := re.match(r"SET\s+R(\d+)\s+(-?\d+)", line): registers[f"R{match.group(1)}"] = int(match.group(2)) elif match := re.match(r"SET\s+R(\d+)\s+R(\d+)", line): registers[f"R{match.group(1)}"] = registers[f"R{match.group(2)}"] elif match := re.match(r"ADD\s+R(\d+)\s+R(\d+)\s+R(\d+)", line): registers[f"R{match.group(1)}"] = registers[f"R{match.group(2)}"] + registers[f"R{match.group(3)}"] elif match := re.match(r"SUB\s+R(\d+)\s+R(\d+)\s+R(\d+)", line): registers[f"R{match.group(1)}"] = registers[f"R{match.group(2)}"] - registers[f"R{match.group(3)}"] elif match := re.match(r"MUL\s+R(\d+)\s+R(\d+)\s+R(\d+)", line): registers[f"R{match.group(1)}"] = registers[f"R{match.group(2)}"] * registers[f"R{match.group(3)}"] elif match := re.match(r"DIV\s+R(\d+)\s+R(\d+)\s+R(\d+)", line): registers[f"R{match.group(1)}"] = registers[f"R{match.group(2)}"] // registers[f"R{match.group(3)}"] elif match := re.match(r"MOD\s+R(\d+)\s+R(\d+)\s+R(\d+)", line): registers[f"R{match.group(1)}"] = registers[f"R{match.group(2)}"] % registers[f"R{match.group(3)}"] elif match := re.match(r"INC\s+R(\d+)", line): registers[f"R{match.group(1)}"] += 1 elif match := re.match(r"DEC\s+R(\d+)", line): registers[f"R{match.group(1)}"] -= 1 elif match := re.match(r"CMP\s+R(\d+)\s+R(\d+)", line): registers["RF"] = 1 if registers[f"R{match.group(1)}"] == registers[f"R{match.group(2)}"] else 0 elif match := re.match(r"GT\s+R(\d+)\s+R(\d+)", line): registers["RF"] = 1 if registers[f"R{match.group(1)}"] > registers[f"R{match.group(2)}"] else 0 elif match := re.match(r"LT\s+R(\d+)\s+R(\d+)", line): registers["RF"] = 1 if registers[f"R{match.group(1)}"] < registers[f"R{match.group(2)}"] else 0 elif match := re.match(r"EQ\s+R(\d+)\s+(-?\d+)", line): registers["RF"] = 1 if registers[f"R{match.group(1)}"] == int(match.group(2)) else 0 elif match := re.match(r"JMP\s+(\w+)", line): pc = labels[match.group(1)] continue elif match := re.match(r"JT\s+(\w+)", line): if registers["RF"] == 1: pc = labels[match.group(1)] continue elif match := re.match(r"JF\s+(\w+)", line): if registers["RF"] == 0: pc = labels[match.group(1)] continue elif match := re.match(r"STORE\s+R(\d+)\s+R(\d+)", line): memory[registers[f"R{match.group(2)}"]] = registers[f"R{match.group(1)}"] elif match := re.match(r"LOAD\s+R(\d+)\s+R(\d+)", line): registers[f"R{match.group(1)}"] = memory[registers[f"R{match.group(2)}"]] else: raise ValueError(f"Invalid instruction: {line}") pc += 1 return [x for x in memory if x != 0] # Return non-zero memory values # Test the interpreter with the provided program program = """ SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ 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: """ 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 139, in <module>
primes = evaluate(program)
File "/usr/src/app/main.py", line 79, in evaluate
raise ValueError(f"Invalid instruction: {line}")
ValueError: Invalid instruction: start_loop: // Label to start the loop
Testing if the previous output contains the string All tests passed
: False