Here is the description of a new assembly language: * 8 registers (R1, R2, R3, R4, R5, R6, R7, R8) that can hold integers. * 1 flag that can hold a boolean value (True or False). * 100 memory addresses (0-99) that can hold integers. * 1 instruction pointer that points to the current instruction being executed. Each instruction is of the form OP ARG1 ARG2 ... where ARGn can be either a register (e.g., R1) or a constant (e.g., 10). Labels are written with a lowercase word followed by colon. The assembly language supports the following instructions: * SET Rx C: Assigns the value C to register Rx. * ADD Rx Ry Rz: Adds the values of Ry and Rz and stores the result in Rx. * (similarly for SUB, MUL, DIV, MOD) * EQ Rx Ry: Sets the flag to True if Rx and Ry are equal, False otherwise. * (similarly for NEQ, LT (Rx < Ry), LTE, GT, GTE) * INC/DEC Rx: Increments/Decrements the value of Rx by one. * JMP L: Jumps to label L unconditionally. * JT/JF (jump if true / jump if false) L: Jumps to label L if the is set or not set. * LOAD Rx M: Loads the value at memory address M into register Rx. * STORE Rx M: Stores the value of register Rx into memory address M. * HCF: Stops the program (with pizzazz) For example here is a program to compute the first 20 square numbers (1, 4, 9, 16, 25, ...): SET R1 0 // Counter for storing squares SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 INC R1 // Increment address INC R2 // Increment number SET R3 20 EQ R1 R3 // Check if 20 squares are found JF loop // If not 20 squares found yet, continue finding end: HCF // Stop program Write me a python interpreter `evaluate(str)` that returns the resulting memory state after running the program. For example, `evaluate(program)` should return `[1, 4, 9, 16, 25, ...]` for the above program.
Here is the description of a new assembly language: * 8 registers (R1, R2, R3, R4, R5, R6, R7, R8) that can hold integers. * 1 flag that can hold a boolean value (True or False). * 100 memory addresses (0-99) that can hold integers. * 1 instruction pointer that points to the current instruction being executed. Each instruction is of the form OP ARG1 ARG2 ... where ARGn can be either a register (e.g., R1) or a constant (e.g., 10). Labels are written with a lowercase word followed by colon. The assembly language supports the following instructions: * SET Rx C: Assigns the value C to register Rx. * ADD Rx Ry Rz: Adds the values of Ry and Rz and stores the result in Rx. * (similarly for SUB, MUL, DIV, MOD) * EQ Rx Ry: Sets the flag to True if Rx and Ry are equal, False otherwise. * (similarly for NEQ, LT (Rx < Ry), LTE, GT, GTE) * INC/DEC Rx: Increments/Decrements the value of Rx by one. * JMP L: Jumps to label L unconditionally. * JT/JF (jump if true / jump if false) L: Jumps to label L if the is set or not set. * LOAD Rx M: Loads the value at memory address M into register Rx. * STORE Rx M: Stores the value of register Rx into memory address M. * HCF: Stops the program (with pizzazz) For example here is a program to compute the first 20 square numbers (1, 4, 9, 16, 25, ...): SET R1 0 // Counter for storing squares SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 INC R1 // Increment address INC R2 // Increment number SET R3 20 EQ R1 R3 // Check if 20 squares are found JF loop // If not 20 squares found yet, continue finding end: HCF // Stop program Write me a python interpreter `evaluate(str)` that returns the resulting memory state after running the program. For example, `evaluate(program)` should return `[1, 4, 9, 16, 25, ...]` for the above program.
Here is a Python interpreter for the given assembly language:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | def evaluate(program): registers = {f'R{i}': 0 for i in range(1, 9)} flags = {'flag': False} memory = [0] * 100 instruction_pointer = 0 def get_value(arg): if arg[0] == 'R': return registers[arg] else: return int(arg) def set_value(dest, value): if dest[0] == 'R': registers[dest] = value else: memory[int(dest)] = value program = program.split('\n') while instruction_pointer < len(program): line = program[instruction_pointer].strip() if line == '' or line.startswith('//'): instruction_pointer += 1 continue tokens = line.split() opcode = tokens[0] args = tokens[1:] if opcode == 'SET': registers[args[0]] = get_value(args[1]) elif opcode in ['ADD', 'SUB', 'MUL', 'DIV', 'MOD']: op = {'ADD': int.__add__, 'SUB': int.__sub__, 'MUL': int.__mul__, 'DIV': int.__floordiv__, 'MOD': int.__mod__}[opcode] registers[args[0]] = op(get_value(args[1]), get_value(args[2])) elif opcode in ['EQ', 'NEQ', 'LT', 'LTE', 'GT', 'GTE']: comparison = {'EQ': int.__eq__, 'NEQ': int.__ne__, 'LT': int.__lt__, 'LTE': int.__le__, 'GT': int.__gt__, 'GTE': int.__ge__}[opcode] flags['flag'] = comparison(get_value(args[0]), get_value(args[1])) elif opcode == 'INC': registers[args[0]] += 1 elif opcode == 'DEC': registers[args[0]] -= 1 elif opcode == 'JMP': instruction_pointer = program.index(f'{args[0]}:') + 1 continue elif opcode == 'JT': if flags['flag']: instruction_pointer = program.index(f'{args[0]}:') + 1 continue elif opcode == 'JF': if not flags['flag']: instruction_pointer = program.index(f'{args[0]}:') + 1 continue elif opcode == 'LOAD': registers[args[0]] = memory[get_value(args[1])] elif opcode == 'STORE': memory[get_value(args[1])] = get_value(args[0]) elif opcode == 'HCF': break instruction_pointer += 1 return [memory[i] for i in range(registers['R1'])] # Example program to compute the first 20 square numbers program = """ SET R1 0 // Counter for storing squares SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 INC R1 // Increment address INC R2 // Increment number SET R3 20 EQ R1 R3 // Check if 20 squares are found JF loop // If not 20 squares found yet, continue finding end: HCF // Stop program """ result = evaluate(program) print(result) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | def evaluate(program): registers = {f'R{i}': 0 for i in range(1, 9)} flags = {'flag': False} memory = [0] * 100 instruction_pointer = 0 def get_value(arg): if arg[0] == 'R': return registers[arg] else: return int(arg) def set_value(dest, value): if dest[0] == 'R': registers[dest] = value else: memory[int(dest)] = value program = program.split('\n') while instruction_pointer < len(program): line = program[instruction_pointer].strip() if line == '' or line.startswith('//'): instruction_pointer += 1 continue tokens = line.split() opcode = tokens[0] args = tokens[1:] if opcode == 'SET': registers[args[0]] = get_value(args[1]) elif opcode in ['ADD', 'SUB', 'MUL', 'DIV', 'MOD']: op = {'ADD': int.__add__, 'SUB': int.__sub__, 'MUL': int.__mul__, 'DIV': int.__floordiv__, 'MOD': int.__mod__}[opcode] registers[args[0]] = op(get_value(args[1]), get_value(args[2])) elif opcode in ['EQ', 'NEQ', 'LT', 'LTE', 'GT', 'GTE']: comparison = {'EQ': int.__eq__, 'NEQ': int.__ne__, 'LT': int.__lt__, 'LTE': int.__le__, 'GT': int.__gt__, 'GTE': int.__ge__}[opcode] flags['flag'] = comparison(get_value(args[0]), get_value(args[1])) elif opcode == 'INC': registers[args[0]] += 1 elif opcode == 'DEC': registers[args[0]] -= 1 elif opcode == 'JMP': instruction_pointer = program.index(f'{args[0]}:') + 1 continue elif opcode == 'JT': if flags['flag']: instruction_pointer = program.index(f'{args[0]}:') + 1 continue elif opcode == 'JF': if not flags['flag']: instruction_pointer = program.index(f'{args[0]}:') + 1 continue elif opcode == 'LOAD': registers[args[0]] = memory[get_value(args[1])] elif opcode == 'STORE': memory[get_value(args[1])] = get_value(args[0]) elif opcode == 'HCF': break instruction_pointer += 1 return [memory[i] for i in range(registers['R1'])] # Example program to compute the first 20 square numbers program = """ SET R1 0 // Counter for storing squares SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 INC R1 // Increment address INC R2 // Increment number SET R3 20 EQ R1 R3 // Check if 20 squares are found JF loop // If not 20 squares found yet, continue finding end: HCF // Stop program """ 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 | def evaluate(program): registers = {f'R{i}': 0 for i in range(1, 9)} flags = {'flag': False} memory = [0] * 100 instruction_pointer = 0 def get_value(arg): if arg[0] == 'R': return registers[arg] else: return int(arg) def set_value(dest, value): if dest[0] == 'R': registers[dest] = value else: memory[int(dest)] = value program = program.split('\n') while instruction_pointer < len(program): line = program[instruction_pointer].strip() if line == '' or line.startswith('//'): instruction_pointer += 1 continue tokens = line.split() opcode = tokens[0] args = tokens[1:] if opcode == 'SET': registers[args[0]] = get_value(args[1]) elif opcode in ['ADD', 'SUB', 'MUL', 'DIV', 'MOD']: op = {'ADD': int.__add__, 'SUB': int.__sub__, 'MUL': int.__mul__, 'DIV': int.__floordiv__, 'MOD': int.__mod__}[opcode] registers[args[0]] = op(get_value(args[1]), get_value(args[2])) elif opcode in ['EQ', 'NEQ', 'LT', 'LTE', 'GT', 'GTE']: comparison = {'EQ': int.__eq__, 'NEQ': int.__ne__, 'LT': int.__lt__, 'LTE': int.__le__, 'GT': int.__gt__, 'GTE': int.__ge__}[opcode] flags['flag'] = comparison(get_value(args[0]), get_value(args[1])) elif opcode == 'INC': registers[args[0]] += 1 elif opcode == 'DEC': registers[args[0]] -= 1 elif opcode == 'JMP': instruction_pointer = program.index(f'{args[0]}:') + 1 continue elif opcode == 'JT': if flags['flag']: instruction_pointer = program.index(f'{args[0]}:') + 1 continue elif opcode == 'JF': if not flags['flag']: instruction_pointer = program.index(f'{args[0]}:') + 1 continue elif opcode == 'LOAD': registers[args[0]] = memory[get_value(args[1])] elif opcode == 'STORE': memory[get_value(args[1])] = get_value(args[0]) elif opcode == 'HCF': break instruction_pointer += 1 return [memory[i] for i in range(registers['R1'])] # Example program to compute the first 20 square numbers program = """ SET R1 0 // Counter for storing squares SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 INC R1 // Increment address INC R2 // Increment number SET R3 20 EQ R1 R3 // Check if 20 squares are found JF loop // If not 20 squares found yet, continue finding end: HCF // Stop program """ 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 SET R3 20 EQ R1 R3 JF loop """)[: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 // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: SET R7 1 EQ R2 R7 // 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 SET R7 100 EQ R8 R7 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 SET R7 0 EQ R7 R6 // 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') |
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]
Traceback (most recent call last):
File "/usr/src/app/main.py", line 106, in <module>
answer = evaluate("""
File "/usr/src/app/main.py", line 44, in evaluate
instruction_pointer = program.index(f'{args[0]}:') + 1
ValueError: 'start_loop:' is not in list
Testing if the previous output contains the string All tests passed: False
Here is the description of a new assembly language: * 8 registers (R1, R2, R3, R4, R5, R6, R7, R8) that can hold integers. * 1 flag that can hold a boolean value (True or False). * 100 memory addresses (0-99) that can hold integers. * 1 instruction pointer that points to the current instruction being executed. Each instruction is of the form OP ARG1 ARG2 ... where ARGn can be either a register (e.g., R1) or a constant (e.g., 10). Labels are written with a lowercase word followed by colon. The assembly language supports the following instructions: * SET Rx C: Assigns the value C to register Rx. * ADD Rx Ry Rz: Adds the values of Ry and Rz and stores the result in Rx. * (similarly for SUB, MUL, DIV, MOD) * EQ Rx Ry: Sets the flag to True if Rx and Ry are equal, False otherwise. * (similarly for NEQ, LT (Rx < Ry), LTE, GT, GTE) * INC/DEC Rx: Increments/Decrements the value of Rx by one. * JMP L: Jumps to label L unconditionally. * JT/JF (jump if true / jump if false) L: Jumps to label L if the is set or not set. * LOAD Rx M: Loads the value at memory address M into register Rx. * STORE Rx M: Stores the value of register Rx into memory address M. * HCF: Stops the program (with pizzazz) For example here is a program to compute the first 20 square numbers (1, 4, 9, 16, 25, ...): SET R1 0 // Counter for storing squares SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 INC R1 // Increment address INC R2 // Increment number SET R3 20 EQ R1 R3 // Check if 20 squares are found JF loop // If not 20 squares found yet, continue finding end: HCF // Stop program Write me a python interpreter `evaluate(str)` that returns the resulting memory state after running the program. For example, `evaluate(program)` should return `[1, 4, 9, 16, 25, ...]` for the above program.
Here is the description of a new assembly language: * 8 registers (R1, R2, R3, R4, R5, R6, R7, R8) that can hold integers. * 1 flag that can hold a boolean value (True or False). * 100 memory addresses (0-99) that can hold integers. * 1 instruction pointer that points to the current instruction being executed. Each instruction is of the form OP ARG1 ARG2 ... where ARGn can be either a register (e.g., R1) or a constant (e.g., 10). Labels are written with a lowercase word followed by colon. The assembly language supports the following instructions: * SET Rx C: Assigns the value C to register Rx. * ADD Rx Ry Rz: Adds the values of Ry and Rz and stores the result in Rx. * (similarly for SUB, MUL, DIV, MOD) * EQ Rx Ry: Sets the flag to True if Rx and Ry are equal, False otherwise. * (similarly for NEQ, LT (Rx < Ry), LTE, GT, GTE) * INC/DEC Rx: Increments/Decrements the value of Rx by one. * JMP L: Jumps to label L unconditionally. * JT/JF (jump if true / jump if false) L: Jumps to label L if the is set or not set. * LOAD Rx M: Loads the value at memory address M into register Rx. * STORE Rx M: Stores the value of register Rx into memory address M. * HCF: Stops the program (with pizzazz) For example here is a program to compute the first 20 square numbers (1, 4, 9, 16, 25, ...): SET R1 0 // Counter for storing squares SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 INC R1 // Increment address INC R2 // Increment number SET R3 20 EQ R1 R3 // Check if 20 squares are found JF loop // If not 20 squares found yet, continue finding end: HCF // Stop program Write me a python interpreter `evaluate(str)` that returns the resulting memory state after running the program. For example, `evaluate(program)` should return `[1, 4, 9, 16, 25, ...]` for the above program.
Here is a Python interpreter for the given assembly language:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 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): program_lines = program.split('\n') registers = {f'R{i}': 0 for i in range(1, 9)} memory = [0] * 100 flag = False instruction_pointer = 0 while instruction_pointer < len(program_lines): line = program_lines[instruction_pointer].strip() if not line: instruction_pointer += 1 continue parts = line.split() opcode = parts[0] if opcode == 'SET': register = parts[1] value = int(parts[2]) registers[register] = value elif opcode in ['ADD', 'SUB', 'MUL', 'DIV', 'MOD']: register1 = parts[1] register2 = parts[2] register3 = parts[3] if opcode == 'ADD': registers[register1] = registers[register2] + registers[register3] elif opcode == 'SUB': registers[register1] = registers[register2] - registers[register3] elif opcode == 'MUL': registers[register1] = registers[register2] * registers[register3] elif opcode == 'DIV': registers[register1] = registers[register2] // registers[register3] elif opcode == 'MOD': registers[register1] = registers[register2] % registers[register3] elif opcode in ['EQ', 'NEQ', 'LT', 'LTE', 'GT', 'GTE']: register1 = parts[1] register2 = parts[2] if opcode == 'EQ': flag = registers[register1] == registers[register2] elif opcode == 'NEQ': flag = registers[register1] != registers[register2] elif opcode == 'LT': flag = registers[register1] < registers[register2] elif opcode == 'LTE': flag = registers[register1] <= registers[register2] elif opcode == 'GT': flag = registers[register1] > registers[register2] elif opcode == 'GTE': flag = registers[register1] >= registers[register2] elif opcode == 'INC': register = parts[1] registers[register] += 1 elif opcode == 'DEC': register = parts[1] registers[register] -= 1 elif opcode == 'JMP': label = parts[1][:-1] for idx, l in enumerate(program_lines): if l.strip().startswith(label + ':'): instruction_pointer = idx break elif opcode in ['JT', 'JF']: label = parts[2][:-1] if (opcode == 'JT' and flag) or (opcode == 'JF' and not flag): for idx, l in enumerate(program_lines): if l.strip().startswith(label + ':'): instruction_pointer = idx break elif opcode == 'LOAD': register = parts[1] address = int(parts[2]) registers[register] = memory[address] elif opcode == 'STORE': register = parts[1] address = int(parts[2]) memory[address] = registers[register] elif opcode == 'HCF': break instruction_pointer += 1 return [memory[i] for i in range(registers['R1'])] # Example program to compute the first 20 square numbers program = """ SET R1 0 // Counter for storing squares SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 INC R1 // Increment address INC R2 // Increment number SET R3 20 EQ R1 R3 // Check if 20 squares are found JF loop // If not 20 squares found yet, continue finding end: HCF // Stop program """ result = evaluate(program) print(result) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 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): program_lines = program.split('\n') registers = {f'R{i}': 0 for i in range(1, 9)} memory = [0] * 100 flag = False instruction_pointer = 0 while instruction_pointer < len(program_lines): line = program_lines[instruction_pointer].strip() if not line: instruction_pointer += 1 continue parts = line.split() opcode = parts[0] if opcode == 'SET': register = parts[1] value = int(parts[2]) registers[register] = value elif opcode in ['ADD', 'SUB', 'MUL', 'DIV', 'MOD']: register1 = parts[1] register2 = parts[2] register3 = parts[3] if opcode == 'ADD': registers[register1] = registers[register2] + registers[register3] elif opcode == 'SUB': registers[register1] = registers[register2] - registers[register3] elif opcode == 'MUL': registers[register1] = registers[register2] * registers[register3] elif opcode == 'DIV': registers[register1] = registers[register2] // registers[register3] elif opcode == 'MOD': registers[register1] = registers[register2] % registers[register3] elif opcode in ['EQ', 'NEQ', 'LT', 'LTE', 'GT', 'GTE']: register1 = parts[1] register2 = parts[2] if opcode == 'EQ': flag = registers[register1] == registers[register2] elif opcode == 'NEQ': flag = registers[register1] != registers[register2] elif opcode == 'LT': flag = registers[register1] < registers[register2] elif opcode == 'LTE': flag = registers[register1] <= registers[register2] elif opcode == 'GT': flag = registers[register1] > registers[register2] elif opcode == 'GTE': flag = registers[register1] >= registers[register2] elif opcode == 'INC': register = parts[1] registers[register] += 1 elif opcode == 'DEC': register = parts[1] registers[register] -= 1 elif opcode == 'JMP': label = parts[1][:-1] for idx, l in enumerate(program_lines): if l.strip().startswith(label + ':'): instruction_pointer = idx break elif opcode in ['JT', 'JF']: label = parts[2][:-1] if (opcode == 'JT' and flag) or (opcode == 'JF' and not flag): for idx, l in enumerate(program_lines): if l.strip().startswith(label + ':'): instruction_pointer = idx break elif opcode == 'LOAD': register = parts[1] address = int(parts[2]) registers[register] = memory[address] elif opcode == 'STORE': register = parts[1] address = int(parts[2]) memory[address] = registers[register] elif opcode == 'HCF': break instruction_pointer += 1 return [memory[i] for i in range(registers['R1'])] # Example program to compute the first 20 square numbers program = """ SET R1 0 // Counter for storing squares SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 INC R1 // Increment address INC R2 // Increment number SET R3 20 EQ R1 R3 // Check if 20 squares are found JF loop // If not 20 squares found yet, continue finding end: HCF // Stop program """ 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 | def evaluate(program): program_lines = program.split('\n') registers = {f'R{i}': 0 for i in range(1, 9)} memory = [0] * 100 flag = False instruction_pointer = 0 while instruction_pointer < len(program_lines): line = program_lines[instruction_pointer].strip() if not line: instruction_pointer += 1 continue parts = line.split() opcode = parts[0] if opcode == 'SET': register = parts[1] value = int(parts[2]) registers[register] = value elif opcode in ['ADD', 'SUB', 'MUL', 'DIV', 'MOD']: register1 = parts[1] register2 = parts[2] register3 = parts[3] if opcode == 'ADD': registers[register1] = registers[register2] + registers[register3] elif opcode == 'SUB': registers[register1] = registers[register2] - registers[register3] elif opcode == 'MUL': registers[register1] = registers[register2] * registers[register3] elif opcode == 'DIV': registers[register1] = registers[register2] // registers[register3] elif opcode == 'MOD': registers[register1] = registers[register2] % registers[register3] elif opcode in ['EQ', 'NEQ', 'LT', 'LTE', 'GT', 'GTE']: register1 = parts[1] register2 = parts[2] if opcode == 'EQ': flag = registers[register1] == registers[register2] elif opcode == 'NEQ': flag = registers[register1] != registers[register2] elif opcode == 'LT': flag = registers[register1] < registers[register2] elif opcode == 'LTE': flag = registers[register1] <= registers[register2] elif opcode == 'GT': flag = registers[register1] > registers[register2] elif opcode == 'GTE': flag = registers[register1] >= registers[register2] elif opcode == 'INC': register = parts[1] registers[register] += 1 elif opcode == 'DEC': register = parts[1] registers[register] -= 1 elif opcode == 'JMP': label = parts[1][:-1] for idx, l in enumerate(program_lines): if l.strip().startswith(label + ':'): instruction_pointer = idx break elif opcode in ['JT', 'JF']: label = parts[2][:-1] if (opcode == 'JT' and flag) or (opcode == 'JF' and not flag): for idx, l in enumerate(program_lines): if l.strip().startswith(label + ':'): instruction_pointer = idx break elif opcode == 'LOAD': register = parts[1] address = int(parts[2]) registers[register] = memory[address] elif opcode == 'STORE': register = parts[1] address = int(parts[2]) memory[address] = registers[register] elif opcode == 'HCF': break instruction_pointer += 1 return [memory[i] for i in range(registers['R1'])] # Example program to compute the first 20 square numbers program = """ SET R1 0 // Counter for storing squares SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 INC R1 // Increment address INC R2 // Increment number SET R3 20 EQ R1 R3 // Check if 20 squares are found JF loop // If not 20 squares found yet, continue finding end: HCF // Stop program """ 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 SET R3 20 EQ R1 R3 JF loop """)[: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 // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: SET R7 1 EQ R2 R7 // 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 SET R7 100 EQ R8 R7 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 SET R7 0 EQ R7 R6 // 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 112, in <module>
result = evaluate(program)
File "/usr/src/app/main.py", line 84, in evaluate
address = int(parts[2])
ValueError: invalid literal for int() with base 10: 'R1'
Testing if the previous output contains the string All tests passed: False
Here is the description of a new assembly language: * 8 registers (R1, R2, R3, R4, R5, R6, R7, R8) that can hold integers. * 1 flag that can hold a boolean value (True or False). * 100 memory addresses (0-99) that can hold integers. * 1 instruction pointer that points to the current instruction being executed. Each instruction is of the form OP ARG1 ARG2 ... where ARGn can be either a register (e.g., R1) or a constant (e.g., 10). Labels are written with a lowercase word followed by colon. The assembly language supports the following instructions: * SET Rx C: Assigns the value C to register Rx. * ADD Rx Ry Rz: Adds the values of Ry and Rz and stores the result in Rx. * (similarly for SUB, MUL, DIV, MOD) * EQ Rx Ry: Sets the flag to True if Rx and Ry are equal, False otherwise. * (similarly for NEQ, LT (Rx < Ry), LTE, GT, GTE) * INC/DEC Rx: Increments/Decrements the value of Rx by one. * JMP L: Jumps to label L unconditionally. * JT/JF (jump if true / jump if false) L: Jumps to label L if the is set or not set. * LOAD Rx M: Loads the value at memory address M into register Rx. * STORE Rx M: Stores the value of register Rx into memory address M. * HCF: Stops the program (with pizzazz) For example here is a program to compute the first 20 square numbers (1, 4, 9, 16, 25, ...): SET R1 0 // Counter for storing squares SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 INC R1 // Increment address INC R2 // Increment number SET R3 20 EQ R1 R3 // Check if 20 squares are found JF loop // If not 20 squares found yet, continue finding end: HCF // Stop program Write me a python interpreter `evaluate(str)` that returns the resulting memory state after running the program. For example, `evaluate(program)` should return `[1, 4, 9, 16, 25, ...]` for the above program.
Here is the description of a new assembly language: * 8 registers (R1, R2, R3, R4, R5, R6, R7, R8) that can hold integers. * 1 flag that can hold a boolean value (True or False). * 100 memory addresses (0-99) that can hold integers. * 1 instruction pointer that points to the current instruction being executed. Each instruction is of the form OP ARG1 ARG2 ... where ARGn can be either a register (e.g., R1) or a constant (e.g., 10). Labels are written with a lowercase word followed by colon. The assembly language supports the following instructions: * SET Rx C: Assigns the value C to register Rx. * ADD Rx Ry Rz: Adds the values of Ry and Rz and stores the result in Rx. * (similarly for SUB, MUL, DIV, MOD) * EQ Rx Ry: Sets the flag to True if Rx and Ry are equal, False otherwise. * (similarly for NEQ, LT (Rx < Ry), LTE, GT, GTE) * INC/DEC Rx: Increments/Decrements the value of Rx by one. * JMP L: Jumps to label L unconditionally. * JT/JF (jump if true / jump if false) L: Jumps to label L if the is set or not set. * LOAD Rx M: Loads the value at memory address M into register Rx. * STORE Rx M: Stores the value of register Rx into memory address M. * HCF: Stops the program (with pizzazz) For example here is a program to compute the first 20 square numbers (1, 4, 9, 16, 25, ...): SET R1 0 // Counter for storing squares SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 INC R1 // Increment address INC R2 // Increment number SET R3 20 EQ R1 R3 // Check if 20 squares are found JF loop // If not 20 squares found yet, continue finding end: HCF // Stop program Write me a python interpreter `evaluate(str)` that returns the resulting memory state after running the program. For example, `evaluate(program)` should return `[1, 4, 9, 16, 25, ...]` for the above program.
Here is a Python interpreter for the given assembly language:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | def evaluate(program): # Initialize registers, memory, and flags registers = {f'R{i}': 0 for i in range(1, 9)} flags = False memory = [0] * 100 instruction_pointer = 0 # Split the program into lines lines = program.split('\n') # Define a function to parse arguments def parse_arg(arg): if arg[0] == 'R': return registers[arg] else: return int(arg) # Start executing the program while instruction_pointer < len(lines): line = lines[instruction_pointer].strip().split() if line[0][-1] == ':': # Skip labels instruction_pointer += 1 continue if line[0] == 'SET': registers[line[1]] = parse_arg(line[2]) elif line[0] in {'ADD', 'SUB', 'MUL', 'DIV', 'MOD'}: op = eval(line[0]) registers[line[1]] = op(parse_arg(line[2]), parse_arg(line[3])) elif line[0] in {'EQ', 'NEQ', 'LT', 'LTE', 'GT', 'GTE'}: flags = eval(f'{parse_arg(line[1])} {line[0]} {parse_arg(line[2])}') elif line[0] == 'INC': registers[line[1]] += 1 elif line[0] == 'DEC': registers[line[1]] -= 1 elif line[0] == 'JMP': instruction_pointer = lines.index(line[1] + ':') continue elif line[0] == 'JT': if flags: instruction_pointer = lines.index(line[1] + ':') continue elif line[0] == 'JF': if not flags: instruction_pointer = lines.index(line[1] + ':') continue elif line[0] == 'LOAD': registers[line[1]] = memory[parse_arg(line[2])] elif line[0] == 'STORE': memory[parse_arg(line[2])] = registers[line[1]] elif line[0] == 'HCF': break instruction_pointer += 1 return memory[:registers['R1']] # Example program program = """ SET R1 0 // Counter for storing squares SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 INC R1 // Increment address INC R2 // Increment number SET R3 20 EQ R1 R3 // Check if 20 squares are found JF loop // If not 20 squares found yet, continue finding end: HCF // Stop program """ result = evaluate(program) print(result) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | def evaluate(program): # Initialize registers, memory, and flags registers = {f'R{i}': 0 for i in range(1, 9)} flags = False memory = [0] * 100 instruction_pointer = 0 # Split the program into lines lines = program.split('\n') # Define a function to parse arguments def parse_arg(arg): if arg[0] == 'R': return registers[arg] else: return int(arg) # Start executing the program while instruction_pointer < len(lines): line = lines[instruction_pointer].strip().split() if line[0][-1] == ':': # Skip labels instruction_pointer += 1 continue if line[0] == 'SET': registers[line[1]] = parse_arg(line[2]) elif line[0] in {'ADD', 'SUB', 'MUL', 'DIV', 'MOD'}: op = eval(line[0]) registers[line[1]] = op(parse_arg(line[2]), parse_arg(line[3])) elif line[0] in {'EQ', 'NEQ', 'LT', 'LTE', 'GT', 'GTE'}: flags = eval(f'{parse_arg(line[1])} {line[0]} {parse_arg(line[2])}') elif line[0] == 'INC': registers[line[1]] += 1 elif line[0] == 'DEC': registers[line[1]] -= 1 elif line[0] == 'JMP': instruction_pointer = lines.index(line[1] + ':') continue elif line[0] == 'JT': if flags: instruction_pointer = lines.index(line[1] + ':') continue elif line[0] == 'JF': if not flags: instruction_pointer = lines.index(line[1] + ':') continue elif line[0] == 'LOAD': registers[line[1]] = memory[parse_arg(line[2])] elif line[0] == 'STORE': memory[parse_arg(line[2])] = registers[line[1]] elif line[0] == 'HCF': break instruction_pointer += 1 return memory[:registers['R1']] # Example program program = """ SET R1 0 // Counter for storing squares SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 INC R1 // Increment address INC R2 // Increment number SET R3 20 EQ R1 R3 // Check if 20 squares are found JF loop // If not 20 squares found yet, continue finding end: HCF // Stop program """ 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 | def evaluate(program): # Initialize registers, memory, and flags registers = {f'R{i}': 0 for i in range(1, 9)} flags = False memory = [0] * 100 instruction_pointer = 0 # Split the program into lines lines = program.split('\n') # Define a function to parse arguments def parse_arg(arg): if arg[0] == 'R': return registers[arg] else: return int(arg) # Start executing the program while instruction_pointer < len(lines): line = lines[instruction_pointer].strip().split() if line[0][-1] == ':': # Skip labels instruction_pointer += 1 continue if line[0] == 'SET': registers[line[1]] = parse_arg(line[2]) elif line[0] in {'ADD', 'SUB', 'MUL', 'DIV', 'MOD'}: op = eval(line[0]) registers[line[1]] = op(parse_arg(line[2]), parse_arg(line[3])) elif line[0] in {'EQ', 'NEQ', 'LT', 'LTE', 'GT', 'GTE'}: flags = eval(f'{parse_arg(line[1])} {line[0]} {parse_arg(line[2])}') elif line[0] == 'INC': registers[line[1]] += 1 elif line[0] == 'DEC': registers[line[1]] -= 1 elif line[0] == 'JMP': instruction_pointer = lines.index(line[1] + ':') continue elif line[0] == 'JT': if flags: instruction_pointer = lines.index(line[1] + ':') continue elif line[0] == 'JF': if not flags: instruction_pointer = lines.index(line[1] + ':') continue elif line[0] == 'LOAD': registers[line[1]] = memory[parse_arg(line[2])] elif line[0] == 'STORE': memory[parse_arg(line[2])] = registers[line[1]] elif line[0] == 'HCF': break instruction_pointer += 1 return memory[:registers['R1']] # Example program program = """ SET R1 0 // Counter for storing squares SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 INC R1 // Increment address INC R2 // Increment number SET R3 20 EQ R1 R3 // Check if 20 squares are found JF loop // If not 20 squares found yet, continue finding end: HCF // Stop program """ 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 SET R3 20 EQ R1 R3 JF loop """)[: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 // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: SET R7 1 EQ R2 R7 // 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 SET R7 100 EQ R8 R7 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 SET R7 0 EQ R7 R6 // 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 78, in <module>
result = evaluate(program)
File "/usr/src/app/main.py", line 23, in evaluate
if line[0][-1] == ':': # Skip labels
IndexError: list index out of range
Testing if the previous output contains the string All tests passed: False
Here is the description of a new assembly language: * 8 registers (R1, R2, R3, R4, R5, R6, R7, R8) that can hold integers. * 1 flag that can hold a boolean value (True or False). * 100 memory addresses (0-99) that can hold integers. * 1 instruction pointer that points to the current instruction being executed. Each instruction is of the form OP ARG1 ARG2 ... where ARGn can be either a register (e.g., R1) or a constant (e.g., 10). Labels are written with a lowercase word followed by colon. The assembly language supports the following instructions: * SET Rx C: Assigns the value C to register Rx. * ADD Rx Ry Rz: Adds the values of Ry and Rz and stores the result in Rx. * (similarly for SUB, MUL, DIV, MOD) * EQ Rx Ry: Sets the flag to True if Rx and Ry are equal, False otherwise. * (similarly for NEQ, LT (Rx < Ry), LTE, GT, GTE) * INC/DEC Rx: Increments/Decrements the value of Rx by one. * JMP L: Jumps to label L unconditionally. * JT/JF (jump if true / jump if false) L: Jumps to label L if the is set or not set. * LOAD Rx M: Loads the value at memory address M into register Rx. * STORE Rx M: Stores the value of register Rx into memory address M. * HCF: Stops the program (with pizzazz) For example here is a program to compute the first 20 square numbers (1, 4, 9, 16, 25, ...): SET R1 0 // Counter for storing squares SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 INC R1 // Increment address INC R2 // Increment number SET R3 20 EQ R1 R3 // Check if 20 squares are found JF loop // If not 20 squares found yet, continue finding end: HCF // Stop program Write me a python interpreter `evaluate(str)` that returns the resulting memory state after running the program. For example, `evaluate(program)` should return `[1, 4, 9, 16, 25, ...]` for the above program.
Here is the description of a new assembly language: * 8 registers (R1, R2, R3, R4, R5, R6, R7, R8) that can hold integers. * 1 flag that can hold a boolean value (True or False). * 100 memory addresses (0-99) that can hold integers. * 1 instruction pointer that points to the current instruction being executed. Each instruction is of the form OP ARG1 ARG2 ... where ARGn can be either a register (e.g., R1) or a constant (e.g., 10). Labels are written with a lowercase word followed by colon. The assembly language supports the following instructions: * SET Rx C: Assigns the value C to register Rx. * ADD Rx Ry Rz: Adds the values of Ry and Rz and stores the result in Rx. * (similarly for SUB, MUL, DIV, MOD) * EQ Rx Ry: Sets the flag to True if Rx and Ry are equal, False otherwise. * (similarly for NEQ, LT (Rx < Ry), LTE, GT, GTE) * INC/DEC Rx: Increments/Decrements the value of Rx by one. * JMP L: Jumps to label L unconditionally. * JT/JF (jump if true / jump if false) L: Jumps to label L if the is set or not set. * LOAD Rx M: Loads the value at memory address M into register Rx. * STORE Rx M: Stores the value of register Rx into memory address M. * HCF: Stops the program (with pizzazz) For example here is a program to compute the first 20 square numbers (1, 4, 9, 16, 25, ...): SET R1 0 // Counter for storing squares SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 INC R1 // Increment address INC R2 // Increment number SET R3 20 EQ R1 R3 // Check if 20 squares are found JF loop // If not 20 squares found yet, continue finding end: HCF // Stop program Write me a python interpreter `evaluate(str)` that returns the resulting memory state after running the program. For example, `evaluate(program)` should return `[1, 4, 9, 16, 25, ...]` for the above program.
Here is an implementation of the interpreter in Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | class AssemblyInterpreter: def __init__(self): self.registers = {f'R{i}': 0 for i in range(1, 9)} self.memory = [0] * 100 self.flag = False self.instruction_pointer = 0 def evaluate(self, program): instructions = program.split('\n') labels = {} for i, instruction in enumerate(instructions): if instruction.endswith(':'): labels[instruction[:-1]] = i while self.instruction_pointer < len(instructions): instruction = instructions[self.instruction_pointer] self.execute_instruction(instruction, labels) self.instruction_pointer += 1 return self.memory[:self.registers['R1']] def execute_instruction(self, instruction, labels): parts = instruction.split() op = parts[0] if op == 'SET': self.registers[parts[1]] = int(parts[2]) elif op in ['ADD', 'SUB', 'MUL', 'DIV', 'MOD']: x = self.get_value(parts[2]) y = self.get_value(parts[3]) result = eval(f'{x} {op.lower()} {y}') self.registers[parts[1]] = result elif op in ['EQ', 'NEQ', 'LT', 'LTE', 'GT', 'GTE']: x = self.get_value(parts[1]) y = self.get_value(parts[2]) self.flag = eval(f'{x} {op.lower()} {y}') elif op == 'INC': self.registers[parts[1]] += 1 elif op == 'DEC': self.registers[parts[1]] -= 1 elif op == 'JMP': self.instruction_pointer = labels[parts[1]] elif op in ['JT', 'JF']: if (op == 'JT' and self.flag) or (op == 'JF' and not self.flag): self.instruction_pointer = labels[parts[1]] elif op == 'LOAD': self.registers[parts[1]] = self.memory[self.get_value(parts[2])] elif op == 'STORE': self.memory[self.get_value(parts[2])] = self.get_value(parts[1]) elif op == 'HCF': self.instruction_pointer = len(instructions) def get_value(self, arg): if arg.startswith('R'): return self.registers[arg] else: return int(arg) def evaluate(program): interpreter = AssemblyInterpreter() return interpreter.evaluate(program) program = """ SET R1 0 // Counter for storing squares SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 INC R1 // Increment address INC R2 // Increment number SET R3 20 EQ R1 R3 // Check if 20 squares are found JF loop // If not 20 squares found yet, continue finding end: HCF // Stop program """ result = evaluate(program) print(result) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | class AssemblyInterpreter: def __init__(self): self.registers = {f'R{i}': 0 for i in range(1, 9)} self.memory = [0] * 100 self.flag = False self.instruction_pointer = 0 def evaluate(self, program): instructions = program.split('\n') labels = {} for i, instruction in enumerate(instructions): if instruction.endswith(':'): labels[instruction[:-1]] = i while self.instruction_pointer < len(instructions): instruction = instructions[self.instruction_pointer] self.execute_instruction(instruction, labels) self.instruction_pointer += 1 return self.memory[:self.registers['R1']] def execute_instruction(self, instruction, labels): parts = instruction.split() op = parts[0] if op == 'SET': self.registers[parts[1]] = int(parts[2]) elif op in ['ADD', 'SUB', 'MUL', 'DIV', 'MOD']: x = self.get_value(parts[2]) y = self.get_value(parts[3]) result = eval(f'{x} {op.lower()} {y}') self.registers[parts[1]] = result elif op in ['EQ', 'NEQ', 'LT', 'LTE', 'GT', 'GTE']: x = self.get_value(parts[1]) y = self.get_value(parts[2]) self.flag = eval(f'{x} {op.lower()} {y}') elif op == 'INC': self.registers[parts[1]] += 1 elif op == 'DEC': self.registers[parts[1]] -= 1 elif op == 'JMP': self.instruction_pointer = labels[parts[1]] elif op in ['JT', 'JF']: if (op == 'JT' and self.flag) or (op == 'JF' and not self.flag): self.instruction_pointer = labels[parts[1]] elif op == 'LOAD': self.registers[parts[1]] = self.memory[self.get_value(parts[2])] elif op == 'STORE': self.memory[self.get_value(parts[2])] = self.get_value(parts[1]) elif op == 'HCF': self.instruction_pointer = len(instructions) def get_value(self, arg): if arg.startswith('R'): return self.registers[arg] else: return int(arg) def evaluate(program): interpreter = AssemblyInterpreter() return interpreter.evaluate(program) program = """ SET R1 0 // Counter for storing squares SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 INC R1 // Increment address INC R2 // Increment number SET R3 20 EQ R1 R3 // Check if 20 squares are found JF loop // If not 20 squares found yet, continue finding end: HCF // Stop program """ 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 | class AssemblyInterpreter: def __init__(self): self.registers = {f'R{i}': 0 for i in range(1, 9)} self.memory = [0] * 100 self.flag = False self.instruction_pointer = 0 def evaluate(self, program): instructions = program.split('\n') labels = {} for i, instruction in enumerate(instructions): if instruction.endswith(':'): labels[instruction[:-1]] = i while self.instruction_pointer < len(instructions): instruction = instructions[self.instruction_pointer] self.execute_instruction(instruction, labels) self.instruction_pointer += 1 return self.memory[:self.registers['R1']] def execute_instruction(self, instruction, labels): parts = instruction.split() op = parts[0] if op == 'SET': self.registers[parts[1]] = int(parts[2]) elif op in ['ADD', 'SUB', 'MUL', 'DIV', 'MOD']: x = self.get_value(parts[2]) y = self.get_value(parts[3]) result = eval(f'{x} {op.lower()} {y}') self.registers[parts[1]] = result elif op in ['EQ', 'NEQ', 'LT', 'LTE', 'GT', 'GTE']: x = self.get_value(parts[1]) y = self.get_value(parts[2]) self.flag = eval(f'{x} {op.lower()} {y}') elif op == 'INC': self.registers[parts[1]] += 1 elif op == 'DEC': self.registers[parts[1]] -= 1 elif op == 'JMP': self.instruction_pointer = labels[parts[1]] elif op in ['JT', 'JF']: if (op == 'JT' and self.flag) or (op == 'JF' and not self.flag): self.instruction_pointer = labels[parts[1]] elif op == 'LOAD': self.registers[parts[1]] = self.memory[self.get_value(parts[2])] elif op == 'STORE': self.memory[self.get_value(parts[2])] = self.get_value(parts[1]) elif op == 'HCF': self.instruction_pointer = len(instructions) def get_value(self, arg): if arg.startswith('R'): return self.registers[arg] else: return int(arg) def evaluate(program): interpreter = AssemblyInterpreter() return interpreter.evaluate(program) program = """ SET R1 0 // Counter for storing squares SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 INC R1 // Increment address INC R2 // Increment number SET R3 20 EQ R1 R3 // Check if 20 squares are found JF loop // If not 20 squares found yet, continue finding end: HCF // Stop program """ 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 SET R3 20 EQ R1 R3 JF loop """)[: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 // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: SET R7 1 EQ R2 R7 // 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 SET R7 100 EQ R8 R7 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 SET R7 0 EQ R7 R6 // 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 80, in <module>
result = evaluate(program)
File "/usr/src/app/main.py", line 61, in evaluate
return interpreter.evaluate(program)
File "/usr/src/app/main.py", line 18, in evaluate
self.execute_instruction(instruction, labels)
File "/usr/src/app/main.py", line 25, in execute_instruction
op = parts[0]
IndexError: list index out of range
Testing if the previous output contains the string All tests passed: False
Here is the description of a new assembly language: * 8 registers (R1, R2, R3, R4, R5, R6, R7, R8) that can hold integers. * 1 flag that can hold a boolean value (True or False). * 100 memory addresses (0-99) that can hold integers. * 1 instruction pointer that points to the current instruction being executed. Each instruction is of the form OP ARG1 ARG2 ... where ARGn can be either a register (e.g., R1) or a constant (e.g., 10). Labels are written with a lowercase word followed by colon. The assembly language supports the following instructions: * SET Rx C: Assigns the value C to register Rx. * ADD Rx Ry Rz: Adds the values of Ry and Rz and stores the result in Rx. * (similarly for SUB, MUL, DIV, MOD) * EQ Rx Ry: Sets the flag to True if Rx and Ry are equal, False otherwise. * (similarly for NEQ, LT (Rx < Ry), LTE, GT, GTE) * INC/DEC Rx: Increments/Decrements the value of Rx by one. * JMP L: Jumps to label L unconditionally. * JT/JF (jump if true / jump if false) L: Jumps to label L if the is set or not set. * LOAD Rx M: Loads the value at memory address M into register Rx. * STORE Rx M: Stores the value of register Rx into memory address M. * HCF: Stops the program (with pizzazz) For example here is a program to compute the first 20 square numbers (1, 4, 9, 16, 25, ...): SET R1 0 // Counter for storing squares SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 INC R1 // Increment address INC R2 // Increment number SET R3 20 EQ R1 R3 // Check if 20 squares are found JF loop // If not 20 squares found yet, continue finding end: HCF // Stop program Write me a python interpreter `evaluate(str)` that returns the resulting memory state after running the program. For example, `evaluate(program)` should return `[1, 4, 9, 16, 25, ...]` for the above program.
Here is the description of a new assembly language: * 8 registers (R1, R2, R3, R4, R5, R6, R7, R8) that can hold integers. * 1 flag that can hold a boolean value (True or False). * 100 memory addresses (0-99) that can hold integers. * 1 instruction pointer that points to the current instruction being executed. Each instruction is of the form OP ARG1 ARG2 ... where ARGn can be either a register (e.g., R1) or a constant (e.g., 10). Labels are written with a lowercase word followed by colon. The assembly language supports the following instructions: * SET Rx C: Assigns the value C to register Rx. * ADD Rx Ry Rz: Adds the values of Ry and Rz and stores the result in Rx. * (similarly for SUB, MUL, DIV, MOD) * EQ Rx Ry: Sets the flag to True if Rx and Ry are equal, False otherwise. * (similarly for NEQ, LT (Rx < Ry), LTE, GT, GTE) * INC/DEC Rx: Increments/Decrements the value of Rx by one. * JMP L: Jumps to label L unconditionally. * JT/JF (jump if true / jump if false) L: Jumps to label L if the is set or not set. * LOAD Rx M: Loads the value at memory address M into register Rx. * STORE Rx M: Stores the value of register Rx into memory address M. * HCF: Stops the program (with pizzazz) For example here is a program to compute the first 20 square numbers (1, 4, 9, 16, 25, ...): SET R1 0 // Counter for storing squares SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 INC R1 // Increment address INC R2 // Increment number SET R3 20 EQ R1 R3 // Check if 20 squares are found JF loop // If not 20 squares found yet, continue finding end: HCF // Stop program Write me a python interpreter `evaluate(str)` that returns the resulting memory state after running the program. For example, `evaluate(program)` should return `[1, 4, 9, 16, 25, ...]` for the above program.
Here is a Python interpreter for the assembly language described above:
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 | def evaluate(program): instructions = program.split("\n") memory = [0] * 100 registers = [0] * 8 flag = False ip = 0 while ip < len(instructions): instruction = instructions[ip].split() op = instruction[0] if op == "SET": register = int(instruction[1][1]) value = int(instruction[2]) registers[register - 1] = value elif op == "ADD": register1 = int(instruction[1][1]) register2 = int(instruction[2][1]) register3 = int(instruction[3][1]) registers[register1 - 1] = registers[register2 - 1] + registers[register3 - 1] elif op == "SUB": register1 = int(instruction[1][1]) register2 = int(instruction[2][1]) register3 = int(instruction[3][1]) registers[register1 - 1] = registers[register2 - 1] - registers[register3 - 1] elif op == "MUL": register1 = int(instruction[1][1]) register2 = int(instruction[2][1]) register3 = int(instruction[3][1]) registers[register1 - 1] = registers[register2 - 1] * registers[register3 - 1] elif op == "DIV": register1 = int(instruction[1][1]) register2 = int(instruction[2][1]) register3 = int(instruction[3][1]) registers[register1 - 1] = registers[register2 - 1] // registers[register3 - 1] elif op == "MOD": register1 = int(instruction[1][1]) register2 = int(instruction[2][1]) register3 = int(instruction[3][1]) registers[register1 - 1] = registers[register2 - 1] % registers[register3 - 1] elif op == "EQ": register1 = int(instruction[1][1]) register2 = int(instruction[2][1]) flag = registers[register1 - 1] == registers[register2 - 1] elif op == "INC": register = int(instruction[1][1]) registers[register - 1] += 1 elif op == "DEC": register = int(instruction[1][1]) registers[register - 1] -= 1 elif op == "JMP": label = instruction[1] for i, instr in enumerate(instructions): if instr.startswith(label + ":"): ip = i - 1 break elif op == "JT": label = instruction[2] if flag: for i, instr in enumerate(instructions): if instr.startswith(label + ":"): ip = i - 1 break elif op == "JF": label = instruction[2] if not flag: for i, instr in enumerate(instructions): if instr.startswith(label + ":"): ip = i - 1 break elif op == "LOAD": register = int(instruction[1][1]) address = int(instruction[2]) registers[register - 1] = memory[address] elif op == "STORE": register = int(instruction[1][1]) address = int(instruction[2]) memory[address] = registers[register - 1] elif op == "HCF": break ip += 1 return [memory[i] for i in range(registers[0])] # Example program to compute the first 20 square numbers program = """ SET R1 0 SET R2 1 loop: MUL R3 R2 R2 STORE R3 R1 INC R1 INC R2 SET R3 20 EQ R1 R3 JF loop end: HCF """ 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 | def evaluate(program): instructions = program.split("\n") memory = [0] * 100 registers = [0] * 8 flag = False ip = 0 while ip < len(instructions): instruction = instructions[ip].split() op = instruction[0] if op == "SET": register = int(instruction[1][1]) value = int(instruction[2]) registers[register - 1] = value elif op == "ADD": register1 = int(instruction[1][1]) register2 = int(instruction[2][1]) register3 = int(instruction[3][1]) registers[register1 - 1] = registers[register2 - 1] + registers[register3 - 1] elif op == "SUB": register1 = int(instruction[1][1]) register2 = int(instruction[2][1]) register3 = int(instruction[3][1]) registers[register1 - 1] = registers[register2 - 1] - registers[register3 - 1] elif op == "MUL": register1 = int(instruction[1][1]) register2 = int(instruction[2][1]) register3 = int(instruction[3][1]) registers[register1 - 1] = registers[register2 - 1] * registers[register3 - 1] elif op == "DIV": register1 = int(instruction[1][1]) register2 = int(instruction[2][1]) register3 = int(instruction[3][1]) registers[register1 - 1] = registers[register2 - 1] // registers[register3 - 1] elif op == "MOD": register1 = int(instruction[1][1]) register2 = int(instruction[2][1]) register3 = int(instruction[3][1]) registers[register1 - 1] = registers[register2 - 1] % registers[register3 - 1] elif op == "EQ": register1 = int(instruction[1][1]) register2 = int(instruction[2][1]) flag = registers[register1 - 1] == registers[register2 - 1] elif op == "INC": register = int(instruction[1][1]) registers[register - 1] += 1 elif op == "DEC": register = int(instruction[1][1]) registers[register - 1] -= 1 elif op == "JMP": label = instruction[1] for i, instr in enumerate(instructions): if instr.startswith(label + ":"): ip = i - 1 break elif op == "JT": label = instruction[2] if flag: for i, instr in enumerate(instructions): if instr.startswith(label + ":"): ip = i - 1 break elif op == "JF": label = instruction[2] if not flag: for i, instr in enumerate(instructions): if instr.startswith(label + ":"): ip = i - 1 break elif op == "LOAD": register = int(instruction[1][1]) address = int(instruction[2]) registers[register - 1] = memory[address] elif op == "STORE": register = int(instruction[1][1]) address = int(instruction[2]) memory[address] = registers[register - 1] elif op == "HCF": break ip += 1 return [memory[i] for i in range(registers[0])] # Example program to compute the first 20 square numbers program = """ SET R1 0 SET R2 1 loop: MUL R3 R2 R2 STORE R3 R1 INC R1 INC R2 SET R3 20 EQ R1 R3 JF loop end: HCF """ 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 | def evaluate(program): instructions = program.split("\n") memory = [0] * 100 registers = [0] * 8 flag = False ip = 0 while ip < len(instructions): instruction = instructions[ip].split() op = instruction[0] if op == "SET": register = int(instruction[1][1]) value = int(instruction[2]) registers[register - 1] = value elif op == "ADD": register1 = int(instruction[1][1]) register2 = int(instruction[2][1]) register3 = int(instruction[3][1]) registers[register1 - 1] = registers[register2 - 1] + registers[register3 - 1] elif op == "SUB": register1 = int(instruction[1][1]) register2 = int(instruction[2][1]) register3 = int(instruction[3][1]) registers[register1 - 1] = registers[register2 - 1] - registers[register3 - 1] elif op == "MUL": register1 = int(instruction[1][1]) register2 = int(instruction[2][1]) register3 = int(instruction[3][1]) registers[register1 - 1] = registers[register2 - 1] * registers[register3 - 1] elif op == "DIV": register1 = int(instruction[1][1]) register2 = int(instruction[2][1]) register3 = int(instruction[3][1]) registers[register1 - 1] = registers[register2 - 1] // registers[register3 - 1] elif op == "MOD": register1 = int(instruction[1][1]) register2 = int(instruction[2][1]) register3 = int(instruction[3][1]) registers[register1 - 1] = registers[register2 - 1] % registers[register3 - 1] elif op == "EQ": register1 = int(instruction[1][1]) register2 = int(instruction[2][1]) flag = registers[register1 - 1] == registers[register2 - 1] elif op == "INC": register = int(instruction[1][1]) registers[register - 1] += 1 elif op == "DEC": register = int(instruction[1][1]) registers[register - 1] -= 1 elif op == "JMP": label = instruction[1] for i, instr in enumerate(instructions): if instr.startswith(label + ":"): ip = i - 1 break elif op == "JT": label = instruction[2] if flag: for i, instr in enumerate(instructions): if instr.startswith(label + ":"): ip = i - 1 break elif op == "JF": label = instruction[2] if not flag: for i, instr in enumerate(instructions): if instr.startswith(label + ":"): ip = i - 1 break elif op == "LOAD": register = int(instruction[1][1]) address = int(instruction[2]) registers[register - 1] = memory[address] elif op == "STORE": register = int(instruction[1][1]) address = int(instruction[2]) memory[address] = registers[register - 1] elif op == "HCF": break ip += 1 return [memory[i] for i in range(registers[0])] # Example program to compute the first 20 square numbers program = """ SET R1 0 SET R2 1 loop: MUL R3 R2 R2 STORE R3 R1 INC R1 INC R2 SET R3 20 EQ R1 R3 JF loop end: HCF """ print(evaluate(program)) answer = evaluate(""" SET R1 0 SET R2 1 loop: MUL R3 R2 R2 STORE R3 R1 INC R1 INC R2 SET R3 20 EQ R1 R3 JF loop """)[: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 // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: SET R7 1 EQ R2 R7 // 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 SET R7 100 EQ R8 R7 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 SET R7 0 EQ R7 R6 // 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 119, in <module>
print(evaluate(program))
File "/usr/src/app/main.py", line 11, in evaluate
op = instruction[0]
IndexError: list index out of range
Testing if the previous output contains the string All tests passed: False