Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
Here's a Python interpreter for the assembly language you provided:
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): memory = [0] * 1000 # initialize memory with 1000 addresses registers = [0] * 16 # initialize 16 registers (R0 to R15) pc = 0 # program counter def get_value(operand): if operand.startswith('R'): return registers[int(operand[1:])] else: return int(operand) def set_value(operand, value): if operand.startswith('R'): registers[int(operand[1:])] = value else: memory[int(operand)] = value lines = program.split('\n') while pc < len(lines): line = lines[pc].split() if not line: pc += 1 continue instruction = line[0] if instruction == 'SET': operand1, operand2 = line[1:] set_value(operand1, get_value(operand2)) elif instruction == 'JMP': operand1, operand2 = line[1:] if operand1 == 'is_prime': is_prime(get_value(operand2)) else: pc = get_label_address(operand1) elif instruction == 'JF': operand1 = line[1] if not registers[0]: pc = get_label_address(operand1) else: pc += 1 elif instruction == 'JT': operand1 = line[1] if registers[0]: pc = get_label_address(operand1) else: pc += 1 elif instruction == 'EQ': operand1, operand2, operand3 = line[1:] value1 = get_value(operand1) value2 = get_value(operand2) value3 = get_value(operand3) registers[0] = int(value1 == value2 == value3) elif instruction == 'GT': operand1, operand2 = line[1:] value1 = get_value(operand1) value2 = get_value(operand2) registers[0] = int(value1 > value2) elif instruction == 'MUL': operand1, operand2, operand3 = line[1:] value1 = get_value(operand1) value2 = get_value(operand2) value3 = get_value(operand3) set_value(operand1, value2 * value3) elif instruction == 'MOD': operand1, operand2, operand3 = line[1:] value1 = get_value(operand1) value2 = get_value(operand2) value3 = get_value(operand3) set_value(operand1, value2 % value3) elif instruction == 'INC': operand1 = line[1] set_value(operand1, get_value(operand1) + 1) elif instruction == 'STORE': operand1, operand2 = line[1:] value1 = get_value(operand1) address = get_value(operand2) memory[address] = value1 else: raise ValueError(f"Unknown instruction: {instruction}") pc += 1 def get_label_address(label): for i, line in enumerate(lines): if line.startswith(label + ':'): return i raise ValueError(f"Label not found: {label}") def is_prime(n): if n < 2: set_value('R2', 0) return for i in range(2, int(n**0.5) + 1): if n % i == 0: set_value('R2', 0) return set_value('R2', 1) return [value for value in memory if value != 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: """ print(evaluate(program)) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | def evaluate(program): memory = [0] * 1000 # initialize memory with 1000 addresses registers = [0] * 16 # initialize 16 registers (R0 to R15) pc = 0 # program counter def get_value(operand): if operand.startswith('R'): return registers[int(operand[1:])] else: return int(operand) def set_value(operand, value): if operand.startswith('R'): registers[int(operand[1:])] = value else: memory[int(operand)] = value lines = program.split('\n') while pc < len(lines): line = lines[pc].split() if not line: pc += 1 continue instruction = line[0] if instruction == 'SET': operand1, operand2 = line[1:] set_value(operand1, get_value(operand2)) elif instruction == 'JMP': operand1, operand2 = line[1:] if operand1 == 'is_prime': is_prime(get_value(operand2)) else: pc = get_label_address(operand1) elif instruction == 'JF': operand1 = line[1] if not registers[0]: pc = get_label_address(operand1) else: pc += 1 elif instruction == 'JT': operand1 = line[1] if registers[0]: pc = get_label_address(operand1) else: pc += 1 elif instruction == 'EQ': operand1, operand2, operand3 = line[1:] value1 = get_value(operand1) value2 = get_value(operand2) value3 = get_value(operand3) registers[0] = int(value1 == value2 == value3) elif instruction == 'GT': operand1, operand2 = line[1:] value1 = get_value(operand1) value2 = get_value(operand2) registers[0] = int(value1 > value2) elif instruction == 'MUL': operand1, operand2, operand3 = line[1:] value1 = get_value(operand1) value2 = get_value(operand2) value3 = get_value(operand3) set_value(operand1, value2 * value3) elif instruction == 'MOD': operand1, operand2, operand3 = line[1:] value1 = get_value(operand1) value2 = get_value(operand2) value3 = get_value(operand3) set_value(operand1, value2 % value3) elif instruction == 'INC': operand1 = line[1] set_value(operand1, get_value(operand1) + 1) elif instruction == 'STORE': operand1, operand2 = line[1:] value1 = get_value(operand1) address = get_value(operand2) memory[address] = value1 else: raise ValueError(f"Unknown instruction: {instruction}") pc += 1 def get_label_address(label): for i, line in enumerate(lines): if line.startswith(label + ':'): return i raise ValueError(f"Label not found: {label}") def is_prime(n): if n < 2: set_value('R2', 0) return for i in range(2, int(n**0.5) + 1): if n % i == 0: set_value('R2', 0) return set_value('R2', 1) return [value for value in memory if value != 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: """ print(evaluate(program)) |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | def evaluate(program): memory = [0] * 1000 # initialize memory with 1000 addresses registers = [0] * 16 # initialize 16 registers (R0 to R15) pc = 0 # program counter def get_value(operand): if operand.startswith('R'): return registers[int(operand[1:])] else: return int(operand) def set_value(operand, value): if operand.startswith('R'): registers[int(operand[1:])] = value else: memory[int(operand)] = value lines = program.split('\n') while pc < len(lines): line = lines[pc].split() if not line: pc += 1 continue instruction = line[0] if instruction == 'SET': operand1, operand2 = line[1:] set_value(operand1, get_value(operand2)) elif instruction == 'JMP': operand1, operand2 = line[1:] if operand1 == 'is_prime': is_prime(get_value(operand2)) else: pc = get_label_address(operand1) elif instruction == 'JF': operand1 = line[1] if not registers[0]: pc = get_label_address(operand1) else: pc += 1 elif instruction == 'JT': operand1 = line[1] if registers[0]: pc = get_label_address(operand1) else: pc += 1 elif instruction == 'EQ': operand1, operand2, operand3 = line[1:] value1 = get_value(operand1) value2 = get_value(operand2) value3 = get_value(operand3) registers[0] = int(value1 == value2 == value3) elif instruction == 'GT': operand1, operand2 = line[1:] value1 = get_value(operand1) value2 = get_value(operand2) registers[0] = int(value1 > value2) elif instruction == 'MUL': operand1, operand2, operand3 = line[1:] value1 = get_value(operand1) value2 = get_value(operand2) value3 = get_value(operand3) set_value(operand1, value2 * value3) elif instruction == 'MOD': operand1, operand2, operand3 = line[1:] value1 = get_value(operand1) value2 = get_value(operand2) value3 = get_value(operand3) set_value(operand1, value2 % value3) elif instruction == 'INC': operand1 = line[1] set_value(operand1, get_value(operand1) + 1) elif instruction == 'STORE': operand1, operand2 = line[1:] value1 = get_value(operand1) address = get_value(operand2) memory[address] = value1 else: raise ValueError(f"Unknown instruction: {instruction}") pc += 1 def get_label_address(label): for i, line in enumerate(lines): if line.startswith(label + ':'): return i raise ValueError(f"Label not found: {label}") def is_prime(n): if n < 2: set_value('R2', 0) return for i in range(2, int(n**0.5) + 1): if n % i == 0: set_value('R2', 0) return set_value('R2', 1) return [value for value in memory if value != 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: """ print(evaluate(program)) answer = evaluate(""" SET R1 0 SET R2 1 loop: MUL R3 R2 R2 STORE R3 R1 INC R1 INC R2 EQ R1 20 JF loop HCF """)[:10] expected = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' answer = evaluate(""" SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """)[:10] expected = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
Traceback (most recent call last):
File "/usr/src/app/main.py", line 153, in <module>
print(evaluate(program))
^^^^^^^^^^^^^^^^^
File "/usr/src/app/main.py", line 28, in evaluate
operand1, operand2 = line[1:]
^^^^^^^^^^^^^^^^^^
ValueError: too many values to unpack (expected 2)
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 that simulates the assembly language program you provided:
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 | def evaluate(program): # Initialize registers and memory registers = {f'R{i}': 0 for i in range(9)} memory = [] labels = {} # Parse labels for line in program.splitlines(): if line.strip().endswith(':'): label = line.strip()[:-1] labels[label] = len(memory) # Execute instructions pc = 0 while pc < len(program.splitlines()): instruction = program.splitlines()[pc].split() opcode = instruction[0] if opcode == 'SET': reg1, value = instruction[1:] registers[reg1] = int(value) if value.isdigit() else registers[value] elif opcode == 'INC': reg1 = instruction[1] registers[reg1] += 1 elif opcode == 'JMP': label = instruction[1] if label in labels: pc = labels[label] else: reg1, reg2 = instruction[1:] if registers[reg2]: pc = labels[reg1] elif opcode == 'JT': label = instruction[1] if registers['R15']: pc = labels[label] elif opcode == 'JF': label = instruction[1] if not registers['R15']: pc = labels[label] elif opcode == 'EQ': reg1, op1, op2 = instruction[1:] val1 = registers[op1] if op1 in registers else int(op1) val2 = registers[op2] if op2 in registers else int(op2) registers['R15'] = 1 if val1 == val2 else 0 elif opcode == 'GT': reg1, op1, op2 = instruction[1:] val1 = registers[op1] if op1 in registers else int(op1) val2 = registers[op2] if op2 in registers else int(op2) registers['R15'] = 1 if val1 > val2 else 0 elif opcode == 'MOD': reg1, op1, op2 = instruction[1:] val1 = registers[op1] if op1 in registers else int(op1) val2 = registers[op2] if op2 in registers else int(op2) registers[reg1] = val1 % val2 elif opcode == 'MUL': reg1, op1, op2 = instruction[1:] val1 = registers[op1] if op1 in registers else int(op1) val2 = registers[op2] if op2 in registers else int(op2) registers[reg1] = val1 * val2 elif opcode == 'STORE': reg1, reg2 = instruction[1:] value = registers[reg1] address = registers[reg2] while len(memory) <= address: memory.append(0) memory[address] = value pc += 1 return memory # Example usage program = """ SET R1 2 start_find_primes: JMP is_prime is_prime ready_prime: EQ R2 1 JF increment STORE R1 R8 INC R8 EQ R8 100 JF increment JMP end increment: INC R1 JMP start_find_primes is_prime: SET R2 1 SET R3 2 start_loop: MUL R4 R3 R3 GT R4 R1 JT is_prime_end MOD R6 R1 R3 EQ R7 R6 0 JT not_prime INC R3 JMP start_loop not_prime: SET R2 0 is_prime_end: JMP ready_prime end: """ print(evaluate(program)) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | def evaluate(program): # Initialize registers and memory registers = {f'R{i}': 0 for i in range(9)} memory = [] labels = {} # Parse labels for line in program.splitlines(): if line.strip().endswith(':'): label = line.strip()[:-1] labels[label] = len(memory) # Execute instructions pc = 0 while pc < len(program.splitlines()): instruction = program.splitlines()[pc].split() opcode = instruction[0] if opcode == 'SET': reg1, value = instruction[1:] registers[reg1] = int(value) if value.isdigit() else registers[value] elif opcode == 'INC': reg1 = instruction[1] registers[reg1] += 1 elif opcode == 'JMP': label = instruction[1] if label in labels: pc = labels[label] else: reg1, reg2 = instruction[1:] if registers[reg2]: pc = labels[reg1] elif opcode == 'JT': label = instruction[1] if registers['R15']: pc = labels[label] elif opcode == 'JF': label = instruction[1] if not registers['R15']: pc = labels[label] elif opcode == 'EQ': reg1, op1, op2 = instruction[1:] val1 = registers[op1] if op1 in registers else int(op1) val2 = registers[op2] if op2 in registers else int(op2) registers['R15'] = 1 if val1 == val2 else 0 elif opcode == 'GT': reg1, op1, op2 = instruction[1:] val1 = registers[op1] if op1 in registers else int(op1) val2 = registers[op2] if op2 in registers else int(op2) registers['R15'] = 1 if val1 > val2 else 0 elif opcode == 'MOD': reg1, op1, op2 = instruction[1:] val1 = registers[op1] if op1 in registers else int(op1) val2 = registers[op2] if op2 in registers else int(op2) registers[reg1] = val1 % val2 elif opcode == 'MUL': reg1, op1, op2 = instruction[1:] val1 = registers[op1] if op1 in registers else int(op1) val2 = registers[op2] if op2 in registers else int(op2) registers[reg1] = val1 * val2 elif opcode == 'STORE': reg1, reg2 = instruction[1:] value = registers[reg1] address = registers[reg2] while len(memory) <= address: memory.append(0) memory[address] = value pc += 1 return memory # Example usage program = """ SET R1 2 start_find_primes: JMP is_prime is_prime ready_prime: EQ R2 1 JF increment STORE R1 R8 INC R8 EQ R8 100 JF increment JMP end increment: INC R1 JMP start_find_primes is_prime: SET R2 1 SET R3 2 start_loop: MUL R4 R3 R3 GT R4 R1 JT is_prime_end MOD R6 R1 R3 EQ R7 R6 0 JT not_prime INC R3 JMP start_loop not_prime: SET R2 0 is_prime_end: JMP ready_prime end: """ print(evaluate(program)) |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | def evaluate(program): # Initialize registers and memory registers = {f'R{i}': 0 for i in range(9)} memory = [] labels = {} # Parse labels for line in program.splitlines(): if line.strip().endswith(':'): label = line.strip()[:-1] labels[label] = len(memory) # Execute instructions pc = 0 while pc < len(program.splitlines()): instruction = program.splitlines()[pc].split() opcode = instruction[0] if opcode == 'SET': reg1, value = instruction[1:] registers[reg1] = int(value) if value.isdigit() else registers[value] elif opcode == 'INC': reg1 = instruction[1] registers[reg1] += 1 elif opcode == 'JMP': label = instruction[1] if label in labels: pc = labels[label] else: reg1, reg2 = instruction[1:] if registers[reg2]: pc = labels[reg1] elif opcode == 'JT': label = instruction[1] if registers['R15']: pc = labels[label] elif opcode == 'JF': label = instruction[1] if not registers['R15']: pc = labels[label] elif opcode == 'EQ': reg1, op1, op2 = instruction[1:] val1 = registers[op1] if op1 in registers else int(op1) val2 = registers[op2] if op2 in registers else int(op2) registers['R15'] = 1 if val1 == val2 else 0 elif opcode == 'GT': reg1, op1, op2 = instruction[1:] val1 = registers[op1] if op1 in registers else int(op1) val2 = registers[op2] if op2 in registers else int(op2) registers['R15'] = 1 if val1 > val2 else 0 elif opcode == 'MOD': reg1, op1, op2 = instruction[1:] val1 = registers[op1] if op1 in registers else int(op1) val2 = registers[op2] if op2 in registers else int(op2) registers[reg1] = val1 % val2 elif opcode == 'MUL': reg1, op1, op2 = instruction[1:] val1 = registers[op1] if op1 in registers else int(op1) val2 = registers[op2] if op2 in registers else int(op2) registers[reg1] = val1 * val2 elif opcode == 'STORE': reg1, reg2 = instruction[1:] value = registers[reg1] address = registers[reg2] while len(memory) <= address: memory.append(0) memory[address] = value pc += 1 return memory # Example usage program = """ SET R1 2 start_find_primes: JMP is_prime is_prime ready_prime: EQ R2 1 JF increment STORE R1 R8 INC R8 EQ R8 100 JF increment JMP end increment: INC R1 JMP start_find_primes is_prime: SET R2 1 SET R3 2 start_loop: MUL R4 R3 R3 GT R4 R1 JT is_prime_end MOD R6 R1 R3 EQ R7 R6 0 JT not_prime INC R3 JMP start_loop not_prime: SET R2 0 is_prime_end: JMP ready_prime end: """ print(evaluate(program)) answer = evaluate(""" SET R1 0 SET R2 1 loop: MUL R3 R2 R2 STORE R3 R1 INC R1 INC R2 EQ R1 20 JF loop HCF """)[:10] expected = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' answer = evaluate(""" SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """)[:10] expected = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
Traceback (most recent call last):
File "/usr/src/app/main.py", line 113, in <module>
print(evaluate(program))
^^^^^^^^^^^^^^^^^
File "/usr/src/app/main.py", line 18, in evaluate
opcode = instruction[0]
~~~~~~~~~~~^^^
IndexError: list index out of range
Testing if the previous output contains the string All tests passed
: False
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
Here's a Python interpreter that can execute the assembly language program you provided:
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 | def evaluate(program): # Initialize registers registers = {f"R{i}": 0 for i in range(9)} memory = [] pc = 0 # Program counter # Helper functions def get_value(operand): if operand.startswith("R"): return registers[operand] return int(operand) def set_value(dest, value): if dest.startswith("R"): registers[dest] = value else: memory[value] = registers[dest] # Instruction set instructions = { "SET": lambda dest, operand: set_value(dest, get_value(operand)), "INC": lambda operand: set_value(operand, get_value(operand) + 1), "ADD": lambda dest, op1, op2: set_value(dest, get_value(op1) + get_value(op2)), "MUL": lambda dest, op1, op2: set_value(dest, get_value(op1) * get_value(op2)), "MOD": lambda dest, op1, op2: set_value(dest, get_value(op1) % get_value(op2)), "GT": lambda dest, op1, op2: set_value(dest, int(get_value(op1) > get_value(op2))), "EQ": lambda dest, op1, op2: set_value(dest, int(get_value(op1) == get_value(op2))), "JMP": lambda operand: setattr(pc, "value", labels[operand]), "JT": lambda operand: setattr(pc, "value", labels[operand]) if registers["R1"] else None, "JF": lambda operand: setattr(pc, "value", labels[operand]) if not registers["R1"] else None, "STORE": lambda operand1, operand2: memory.append(get_value(operand1)), } # Parse labels labels = {} for i, line in enumerate(program.split("\n")): if line.strip().endswith(":"): labels[line.strip().rstrip(":")] = i # Execute instructions while pc.value < len(program.split("\n")): line = program.split("\n")[pc.value].strip() if line and not line.startswith("//"): parts = line.split() instruction = parts[0] operands = parts[1:] try: instructions[instruction](*operands) except KeyError: print(f"Invalid instruction: {instruction}") pc.value += 1 return memory |
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 | 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 | import re def evaluate(program): # Initialize registers registers = {f"R{i}": 0 for i in range(9)} memory = [] pc = 0 # Program counter # Helper functions def get_value(operand): if operand.startswith("R"): return registers[operand] return int(operand) def set_value(dest, value): if dest.startswith("R"): registers[dest] = value else: memory[value] = registers[dest] # Instruction set instructions = { "SET": lambda dest, operand: set_value(dest, get_value(operand)), "INC": lambda operand: set_value(operand, get_value(operand) + 1), "ADD": lambda dest, op1, op2: set_value(dest, get_value(op1) + get_value(op2)), "MUL": lambda dest, op1, op2: set_value(dest, get_value(op1) * get_value(op2)), "MOD": lambda dest, op1, op2: set_value(dest, get_value(op1) % get_value(op2)), "GT": lambda dest, op1, op2: set_value(dest, int(get_value(op1) > get_value(op2))), "EQ": lambda dest, op1, op2: set_value(dest, int(get_value(op1) == get_value(op2))), "JMP": lambda operand: setattr(pc, "value", labels[operand]), "JT": lambda operand: setattr(pc, "value", labels[operand]) if registers["R1"] else None, "JF": lambda operand: setattr(pc, "value", labels[operand]) if not registers["R1"] else None, "STORE": lambda operand1, operand2: memory.append(get_value(operand1)), } # Parse labels labels = {} for i, line in enumerate(program.split("\n")): if line.strip().endswith(":"): labels[line.strip().rstrip(":")] = i # Execute instructions while pc.value < len(program.split("\n")): line = program.split("\n")[pc.value].strip() if line and not line.startswith("//"): parts = line.split() instruction = parts[0] operands = parts[1:] try: instructions[instruction](*operands) except KeyError: print(f"Invalid instruction: {instruction}") pc.value += 1 return memory |
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 | import re def evaluate(program): # Initialize registers registers = {f"R{i}": 0 for i in range(9)} memory = [] pc = 0 # Program counter # Helper functions def get_value(operand): if operand.startswith("R"): return registers[operand] return int(operand) def set_value(dest, value): if dest.startswith("R"): registers[dest] = value else: memory[value] = registers[dest] # Instruction set instructions = { "SET": lambda dest, operand: set_value(dest, get_value(operand)), "INC": lambda operand: set_value(operand, get_value(operand) + 1), "ADD": lambda dest, op1, op2: set_value(dest, get_value(op1) + get_value(op2)), "MUL": lambda dest, op1, op2: set_value(dest, get_value(op1) * get_value(op2)), "MOD": lambda dest, op1, op2: set_value(dest, get_value(op1) % get_value(op2)), "GT": lambda dest, op1, op2: set_value(dest, int(get_value(op1) > get_value(op2))), "EQ": lambda dest, op1, op2: set_value(dest, int(get_value(op1) == get_value(op2))), "JMP": lambda operand: setattr(pc, "value", labels[operand]), "JT": lambda operand: setattr(pc, "value", labels[operand]) if registers["R1"] else None, "JF": lambda operand: setattr(pc, "value", labels[operand]) if not registers["R1"] else None, "STORE": lambda operand1, operand2: memory.append(get_value(operand1)), } # Parse labels labels = {} for i, line in enumerate(program.split("\n")): if line.strip().endswith(":"): labels[line.strip().rstrip(":")] = i # Execute instructions while pc.value < len(program.split("\n")): line = program.split("\n")[pc.value].strip() if line and not line.startswith("//"): parts = line.split() instruction = parts[0] operands = parts[1:] try: instructions[instruction](*operands) except KeyError: print(f"Invalid instruction: {instruction}") pc.value += 1 return memory 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 62, in <module>
answer = evaluate("""
^^^^^^^^^^^^
File "/usr/src/app/main.py", line 44, in evaluate
while pc.value < len(program.split("\n")):
^^^^^^^^
AttributeError: 'int' object has no attribute 'value'
Testing if the previous output contains the string All tests passed
: False
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
Here's a Python interpreter `evaluate(str)` that simulates the execution of the given assembly 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 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 | def evaluate(program): # Initialize registers registers = {'R1': 0, 'R2': 0, 'R3': 0, 'R4': 0, 'R5': 0, 'R6': 0, 'R7': 0, 'R8': 0} memory = [] lines = program.strip().split('\n') pc = 0 # Program counter # Helper functions def get_value(operand): if operand.isdigit(): return int(operand) else: return registers[operand] def set_flag(condition): if condition: registers['R7'] = 1 else: registers['R7'] = 0 while pc < len(lines): line = lines[pc].strip() if not line or line.startswith('//'): pc += 1 continue tokens = line.split() instruction = tokens[0] if instruction == 'SET': reg, value = tokens[1:] registers[reg] = get_value(value) elif instruction == 'JMP': if len(tokens) == 2: label = tokens[1] pc = [idx for idx, line in enumerate(lines) if line.startswith(label + ':')][0] else: label1, label2 = tokens[1:] if registers['R7'] == 1: pc = [idx for idx, line in enumerate(lines) if line.startswith(label1 + ':')][0] else: pc = [idx for idx, line in enumerate(lines) if line.startswith(label2 + ':')][0] elif instruction == 'EQ': reg1, reg2 = tokens[1:] set_flag(get_value(reg1) == get_value(reg2)) elif instruction == 'GT': reg1, reg2 = tokens[1:] set_flag(get_value(reg1) > get_value(reg2)) elif instruction == 'JT': label = tokens[1] if registers['R7'] == 1: pc = [idx for idx, line in enumerate(lines) if line.startswith(label + ':')][0] else: pc += 1 elif instruction == 'JF': label = tokens[1] if registers['R7'] == 0: pc = [idx for idx, line in enumerate(lines) if line.startswith(label + ':')][0] else: pc += 1 elif instruction == 'INC': reg = tokens[1] registers[reg] += 1 elif instruction == 'STORE': reg1, reg2 = tokens[1:] value = get_value(reg1) index = get_value(reg2) while len(memory) <= index: memory.append(0) memory[index] = value elif instruction == 'MUL': reg1, reg2, reg3 = tokens[1:] registers[reg1] = get_value(reg2) * get_value(reg3) elif instruction == 'MOD': reg1, reg2, reg3 = tokens[1:] registers[reg1] = get_value(reg2) % get_value(reg3) else: raise ValueError(f"Unknown instruction: {instruction}") pc += 1 return memory # Example usage program = """ SET R1 2 start_find_primes: JMP is_prime is_prime ready_prime: EQ R2 1 JF increment STORE R1 R8 INC R8 EQ R8 100 JF increment JMP end increment: INC R1 JMP start_find_primes is_prime: SET R2 1 SET R3 2 start_loop: MUL R4 R3 R3 GT R4 R1 JT is_prime_end MOD R6 R1 R3 EQ R7 R6 0 JT not_prime INC R3 JMP start_loop not_prime: SET R2 0 is_prime_end: JMP ready_prime end: """ 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | def evaluate(program): # Initialize registers registers = {'R1': 0, 'R2': 0, 'R3': 0, 'R4': 0, 'R5': 0, 'R6': 0, 'R7': 0, 'R8': 0} memory = [] lines = program.strip().split('\n') pc = 0 # Program counter # Helper functions def get_value(operand): if operand.isdigit(): return int(operand) else: return registers[operand] def set_flag(condition): if condition: registers['R7'] = 1 else: registers['R7'] = 0 while pc < len(lines): line = lines[pc].strip() if not line or line.startswith('//'): pc += 1 continue tokens = line.split() instruction = tokens[0] if instruction == 'SET': reg, value = tokens[1:] registers[reg] = get_value(value) elif instruction == 'JMP': if len(tokens) == 2: label = tokens[1] pc = [idx for idx, line in enumerate(lines) if line.startswith(label + ':')][0] else: label1, label2 = tokens[1:] if registers['R7'] == 1: pc = [idx for idx, line in enumerate(lines) if line.startswith(label1 + ':')][0] else: pc = [idx for idx, line in enumerate(lines) if line.startswith(label2 + ':')][0] elif instruction == 'EQ': reg1, reg2 = tokens[1:] set_flag(get_value(reg1) == get_value(reg2)) elif instruction == 'GT': reg1, reg2 = tokens[1:] set_flag(get_value(reg1) > get_value(reg2)) elif instruction == 'JT': label = tokens[1] if registers['R7'] == 1: pc = [idx for idx, line in enumerate(lines) if line.startswith(label + ':')][0] else: pc += 1 elif instruction == 'JF': label = tokens[1] if registers['R7'] == 0: pc = [idx for idx, line in enumerate(lines) if line.startswith(label + ':')][0] else: pc += 1 elif instruction == 'INC': reg = tokens[1] registers[reg] += 1 elif instruction == 'STORE': reg1, reg2 = tokens[1:] value = get_value(reg1) index = get_value(reg2) while len(memory) <= index: memory.append(0) memory[index] = value elif instruction == 'MUL': reg1, reg2, reg3 = tokens[1:] registers[reg1] = get_value(reg2) * get_value(reg3) elif instruction == 'MOD': reg1, reg2, reg3 = tokens[1:] registers[reg1] = get_value(reg2) % get_value(reg3) else: raise ValueError(f"Unknown instruction: {instruction}") pc += 1 return memory # Example usage program = """ SET R1 2 start_find_primes: JMP is_prime is_prime ready_prime: EQ R2 1 JF increment STORE R1 R8 INC R8 EQ R8 100 JF increment JMP end increment: INC R1 JMP start_find_primes is_prime: SET R2 1 SET R3 2 start_loop: MUL R4 R3 R3 GT R4 R1 JT is_prime_end MOD R6 R1 R3 EQ R7 R6 0 JT not_prime INC R3 JMP start_loop not_prime: SET R2 0 is_prime_end: JMP ready_prime end: """ 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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | def evaluate(program): # Initialize registers registers = {'R1': 0, 'R2': 0, 'R3': 0, 'R4': 0, 'R5': 0, 'R6': 0, 'R7': 0, 'R8': 0} memory = [] lines = program.strip().split('\n') pc = 0 # Program counter # Helper functions def get_value(operand): if operand.isdigit(): return int(operand) else: return registers[operand] def set_flag(condition): if condition: registers['R7'] = 1 else: registers['R7'] = 0 while pc < len(lines): line = lines[pc].strip() if not line or line.startswith('//'): pc += 1 continue tokens = line.split() instruction = tokens[0] if instruction == 'SET': reg, value = tokens[1:] registers[reg] = get_value(value) elif instruction == 'JMP': if len(tokens) == 2: label = tokens[1] pc = [idx for idx, line in enumerate(lines) if line.startswith(label + ':')][0] else: label1, label2 = tokens[1:] if registers['R7'] == 1: pc = [idx for idx, line in enumerate(lines) if line.startswith(label1 + ':')][0] else: pc = [idx for idx, line in enumerate(lines) if line.startswith(label2 + ':')][0] elif instruction == 'EQ': reg1, reg2 = tokens[1:] set_flag(get_value(reg1) == get_value(reg2)) elif instruction == 'GT': reg1, reg2 = tokens[1:] set_flag(get_value(reg1) > get_value(reg2)) elif instruction == 'JT': label = tokens[1] if registers['R7'] == 1: pc = [idx for idx, line in enumerate(lines) if line.startswith(label + ':')][0] else: pc += 1 elif instruction == 'JF': label = tokens[1] if registers['R7'] == 0: pc = [idx for idx, line in enumerate(lines) if line.startswith(label + ':')][0] else: pc += 1 elif instruction == 'INC': reg = tokens[1] registers[reg] += 1 elif instruction == 'STORE': reg1, reg2 = tokens[1:] value = get_value(reg1) index = get_value(reg2) while len(memory) <= index: memory.append(0) memory[index] = value elif instruction == 'MUL': reg1, reg2, reg3 = tokens[1:] registers[reg1] = get_value(reg2) * get_value(reg3) elif instruction == 'MOD': reg1, reg2, reg3 = tokens[1:] registers[reg1] = get_value(reg2) % get_value(reg3) else: raise ValueError(f"Unknown instruction: {instruction}") pc += 1 return memory # Example usage program = """ SET R1 2 start_find_primes: JMP is_prime is_prime ready_prime: EQ R2 1 JF increment STORE R1 R8 INC R8 EQ R8 100 JF increment JMP end increment: INC R1 JMP start_find_primes is_prime: SET R2 1 SET R3 2 start_loop: MUL R4 R3 R3 GT R4 R1 JT is_prime_end MOD R6 R1 R3 EQ R7 R6 0 JT not_prime INC R3 JMP start_loop not_prime: SET R2 0 is_prime_end: JMP ready_prime end: """ 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 133, in <module>
result = evaluate(program)
^^^^^^^^^^^^^^^^^
File "/usr/src/app/main.py", line 79, in evaluate
raise ValueError(f"Unknown instruction: {instruction}")
ValueError: Unknown 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: |
Here's a Python interpreter that simulates the execution of your assembly language program and returns the resulting memory state (the list of prime numbers found):
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 | def evaluate(program): registers = {'R1': 0, 'R2': 0, 'R3': 0, 'R4': 0, 'R5': 0, 'R6': 0, 'R7': 0, 'R8': 0} memory = [] labels = {} pc = 0 # Program counter flag = False # Condition flag # Parse labels for line in program.split('\n'): if line.strip().endswith(':'): labels[line.strip().replace(':', '')] = pc pc += 1 pc = 0 # Reset program counter while pc < len(program.split('\n')): instruction = program.split('\n')[pc].strip() if instruction.startswith('SET'): _, reg, value = instruction.split() if value.isdigit(): registers[reg] = int(value) else: registers[reg] = registers[value] elif instruction.startswith('INC'): _, reg = instruction.split() registers[reg] += 1 elif instruction.startswith('MUL'): _, dest, op1, op2 = instruction.split() registers[dest] = registers[op1] * registers[op2] elif instruction.startswith('MOD'): _, dest, op1, op2 = instruction.split() registers[dest] = registers[op1] % registers[op2] elif instruction.startswith('EQ'): _, dest, op1, op2 = instruction.split() registers[dest] = int(registers[op1] == registers[op2]) elif instruction.startswith('GT'): _, dest, op1, op2 = instruction.split() registers[dest] = int(registers[op1] > registers[op2]) flag = (registers[dest] == 1) elif instruction.startswith('STORE'): _, source, dest = instruction.split() memory.append(registers[source]) elif instruction.startswith('JMP'): _, label = instruction.split() if label in labels: pc = labels[label] else: pc += 1 elif instruction.startswith('JT'): _, label = instruction.split() if flag: if label in labels: pc = labels[label] else: pc += 1 else: pc += 1 elif instruction.startswith('JF'): _, label = instruction.split() if not flag: if label in labels: pc = labels[label] else: pc += 1 else: pc += 1 else: pc += 1 return memory # Example usage program = """ SET R1 2 start_find_primes: JMP is_prime is_prime ready_prime: EQ R2 1 JF increment STORE R1 R8 INC R8 EQ R8 100 JF increment JMP end increment: INC R1 JMP start_find_primes is_prime: SET R2 1 SET R3 2 start_loop: MUL R4 R3 R3 GT R4 R1 JT is_prime_end MOD R6 R1 R3 EQ R7 R6 0 JT not_prime INC R3 JMP start_loop not_prime: SET R2 0 is_prime_end: JMP ready_prime end: """ print(evaluate(program)) |
1 | [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] |
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 | def evaluate(program): registers = {'R1': 0, 'R2': 0, 'R3': 0, 'R4': 0, 'R5': 0, 'R6': 0, 'R7': 0, 'R8': 0} memory = [] labels = {} pc = 0 # Program counter flag = False # Condition flag # Parse labels for line in program.split('\n'): if line.strip().endswith(':'): labels[line.strip().replace(':', '')] = pc pc += 1 pc = 0 # Reset program counter while pc < len(program.split('\n')): instruction = program.split('\n')[pc].strip() if instruction.startswith('SET'): _, reg, value = instruction.split() if value.isdigit(): registers[reg] = int(value) else: registers[reg] = registers[value] elif instruction.startswith('INC'): _, reg = instruction.split() registers[reg] += 1 elif instruction.startswith('MUL'): _, dest, op1, op2 = instruction.split() registers[dest] = registers[op1] * registers[op2] elif instruction.startswith('MOD'): _, dest, op1, op2 = instruction.split() registers[dest] = registers[op1] % registers[op2] elif instruction.startswith('EQ'): _, dest, op1, op2 = instruction.split() registers[dest] = int(registers[op1] == registers[op2]) elif instruction.startswith('GT'): _, dest, op1, op2 = instruction.split() registers[dest] = int(registers[op1] > registers[op2]) flag = (registers[dest] == 1) elif instruction.startswith('STORE'): _, source, dest = instruction.split() memory.append(registers[source]) elif instruction.startswith('JMP'): _, label = instruction.split() if label in labels: pc = labels[label] else: pc += 1 elif instruction.startswith('JT'): _, label = instruction.split() if flag: if label in labels: pc = labels[label] else: pc += 1 else: pc += 1 elif instruction.startswith('JF'): _, label = instruction.split() if not flag: if label in labels: pc = labels[label] else: pc += 1 else: pc += 1 else: pc += 1 return memory |
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 | def evaluate(program): registers = {'R1': 0, 'R2': 0, 'R3': 0, 'R4': 0, 'R5': 0, 'R6': 0, 'R7': 0, 'R8': 0} memory = [] labels = {} pc = 0 # Program counter flag = False # Condition flag # Parse labels for line in program.split('\n'): if line.strip().endswith(':'): labels[line.strip().replace(':', '')] = pc pc += 1 pc = 0 # Reset program counter while pc < len(program.split('\n')): instruction = program.split('\n')[pc].strip() if instruction.startswith('SET'): _, reg, value = instruction.split() if value.isdigit(): registers[reg] = int(value) else: registers[reg] = registers[value] elif instruction.startswith('INC'): _, reg = instruction.split() registers[reg] += 1 elif instruction.startswith('MUL'): _, dest, op1, op2 = instruction.split() registers[dest] = registers[op1] * registers[op2] elif instruction.startswith('MOD'): _, dest, op1, op2 = instruction.split() registers[dest] = registers[op1] % registers[op2] elif instruction.startswith('EQ'): _, dest, op1, op2 = instruction.split() registers[dest] = int(registers[op1] == registers[op2]) elif instruction.startswith('GT'): _, dest, op1, op2 = instruction.split() registers[dest] = int(registers[op1] > registers[op2]) flag = (registers[dest] == 1) elif instruction.startswith('STORE'): _, source, dest = instruction.split() memory.append(registers[source]) elif instruction.startswith('JMP'): _, label = instruction.split() if label in labels: pc = labels[label] else: pc += 1 elif instruction.startswith('JT'): _, label = instruction.split() if flag: if label in labels: pc = labels[label] else: pc += 1 else: pc += 1 elif instruction.startswith('JF'): _, label = instruction.split() if not flag: if label in labels: pc = labels[label] else: pc += 1 else: pc += 1 else: pc += 1 return memory 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