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.
Okay, let's build the Python interpreter for this 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 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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | import re class AssemblyError(Exception): """Custom exception for errors during assembly execution.""" pass def evaluate(program_str: str) -> list[int]: """ Interprets the given assembly program string and returns the final memory state. Args: program_str: A string containing the assembly program. Returns: A list of 100 integers representing the final state of the memory. Raises: AssemblyError: If there's an error during parsing or execution. """ # --- 1. Initialize Machine State --- registers = {f"R{i}": 0 for i in range(1, 9)} flag = False memory = [0] * 100 ip = 0 # Instruction Pointer labels = {} instructions = [] # --- 2. Parse the Program --- lines = program_str.strip().split('\n') line_number = 0 # For error reporting related to original source lines instruction_index = 0 # Index within the 'instructions' list for line in lines: line_number += 1 original_line = line # Keep original for error messages # Remove comments and strip whitespace line = line.split('//', 1)[0].strip() if not line: continue # Skip empty lines or lines with only comments # Check for labels label_match = re.match(r'^([a-z][a-z0-9_]*):\s*$', line) if label_match: label_name = label_match.group(1) if label_name in labels: raise AssemblyError(f"Line {line_number}: Duplicate label '{label_name}'") labels[label_name] = instruction_index # Label points to the *next* instruction index continue # Don't add label line as an instruction # Parse instruction parts = line.split() if not parts: raise AssemblyError(f"Line {line_number}: Invalid instruction format: '{original_line}'") opcode = parts[0].upper() args = parts[1:] # Store instruction along with original line number for error context instructions.append({'opcode': opcode, 'args': args, 'line': line_number, 'original': original_line}) instruction_index += 1 # --- 3. Helper Function to Get Value (Register or Constant) --- def get_value(arg: str, line_num: int, original_line: str) -> int: if arg.upper() in registers: return registers[arg.upper()] try: return int(arg) except ValueError: raise AssemblyError(f"Line {line_num}: Invalid argument '{arg}'. Expected register (R1-R8) or integer constant in '{original_line}'") # --- 4. Execute Instructions --- max_instructions = len(instructions) * 1000 # Basic infinite loop prevention executed_count = 0 while 0 <= ip < len(instructions): if executed_count > max_instructions: raise AssemblyError("Potential infinite loop detected (exceeded instruction limit)") executed_count += 1 current_instruction = instructions[ip] opcode = current_instruction['opcode'] args = current_instruction['args'] line_num = current_instruction['line'] original_line = current_instruction['original'] # Assume sequential execution unless a jump happens next_ip = ip + 1 try: # --- Instruction Implementations --- if opcode == "SET": if len(args) != 2: raise AssemblyError(f"SET requires 2 arguments") reg = args[0].upper() if reg not in registers: raise AssemblyError(f"Invalid register '{args[0]}'") value = get_value(args[1], line_num, original_line) registers[reg] = value elif opcode in ("ADD", "SUB", "MUL", "DIV", "MOD"): if len(args) != 3: raise AssemblyError(f"{opcode} requires 3 arguments") dest_reg = args[0].upper() if dest_reg not in registers: raise AssemblyError(f"Invalid destination register '{args[0]}'") val1 = get_value(args[1], line_num, original_line) val2 = get_value(args[2], line_num, original_line) if opcode == "ADD": registers[dest_reg] = val1 + val2 elif opcode == "SUB": registers[dest_reg] = val1 - val2 elif opcode == "MUL": registers[dest_reg] = val1 * val2 elif opcode == "DIV": if val2 == 0: raise AssemblyError(f"Division by zero") registers[dest_reg] = val1 // val2 # Integer division elif opcode == "MOD": if val2 == 0: raise AssemblyError(f"Modulo by zero") registers[dest_reg] = val1 % val2 elif opcode in ("EQ", "NEQ", "LT", "LTE", "GT", "GTE"): if len(args) != 2: raise AssemblyError(f"{opcode} requires 2 arguments") val1 = get_value(args[0], line_num, original_line) val2 = get_value(args[1], line_num, original_line) if opcode == "EQ": flag = (val1 == val2) elif opcode == "NEQ": flag = (val1 != val2) elif opcode == "LT": flag = (val1 < val2) elif opcode == "LTE": flag = (val1 <= val2) elif opcode == "GT": flag = (val1 > val2) elif opcode == "GTE": flag = (val1 >= val2) elif opcode == "INC": if len(args) != 1: raise AssemblyError(f"INC requires 1 argument") reg = args[0].upper() if reg not in registers: raise AssemblyError(f"Invalid register '{args[0]}'") registers[reg] += 1 elif opcode == "DEC": if len(args) != 1: raise AssemblyError(f"DEC requires 1 argument") reg = args[0].upper() if reg not in registers: raise AssemblyError(f"Invalid register '{args[0]}'") registers[reg] -= 1 elif opcode == "JMP": if len(args) != 1: raise AssemblyError(f"JMP requires 1 argument (label)") label = args[0] if label not in labels: raise AssemblyError(f"Undefined label '{label}'") next_ip = labels[label] # Jump! elif opcode == "JT": if len(args) != 1: raise AssemblyError(f"JT requires 1 argument (label)") if flag: label = args[0] if label not in labels: raise AssemblyError(f"Undefined label '{label}'") next_ip = labels[label] # Jump! elif opcode == "JF": if len(args) != 1: raise AssemblyError(f"JF requires 1 argument (label)") if not flag: label = args[0] if label not in labels: raise AssemblyError(f"Undefined label '{label}'") next_ip = labels[label] # Jump! elif opcode == "LOAD": if len(args) != 2: raise AssemblyError(f"LOAD requires 2 arguments") reg = args[0].upper() if reg not in registers: raise AssemblyError(f"Invalid register '{args[0]}'") mem_addr = get_value(args[1], line_num, original_line) if not (0 <= mem_addr < 100): raise AssemblyError(f"Memory address {mem_addr} out of bounds (0-99)") registers[reg] = memory[mem_addr] elif opcode == "STORE": if len(args) != 2: raise AssemblyError(f"STORE requires 2 arguments") reg = args[0].upper() if reg not in registers: raise AssemblyError(f"Invalid register '{args[0]}'") value_to_store = registers[reg] mem_addr = get_value(args[1], line_num, original_line) if not (0 <= mem_addr < 100): raise AssemblyError(f"Memory address {mem_addr} out of bounds (0-99)") memory[mem_addr] = value_to_store elif opcode == "HCF": if len(args) != 0: raise AssemblyError(f"HCF takes no arguments") break # Halt and Catch Fire (with pizzazz!) - Stop execution else: raise AssemblyError(f"Unknown opcode '{opcode}'") except AssemblyError as e: # Add context to the error message raise AssemblyError(f"Line {line_num}: {e} in '{original_line}'") from e except IndexError: raise AssemblyError(f"Line {line_num}: Incorrect number of arguments for {opcode} in '{original_line}'") except Exception as e: # Catch unexpected errors raise AssemblyError(f"Line {line_num}: Unexpected error '{e}' during execution of '{original_line}'") from e # Move to the next instruction ip = next_ip # --- 5. Return Final Memory State --- return memory # --- Example Usage --- program = """ SET R1 0 // Counter for storing squares (memory address) SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 (which starts at 0) INC R1 // Increment address for next store INC R2 // Increment number to square SET R4 20 // Target number of squares EQ R1 R4 // Check if R1 (address counter) has reached 20 JF loop // If flag is False (R1 != 20), jump back to loop end: // Optional label, program stops anyway HCF // Stop program """ try: final_memory = evaluate(program) # Print the first 20 memory locations (where the squares were stored) print(f"Final memory state (first 20): {final_memory[:20]}") # Verify the result expected = [i*i for i in range(1, 21)] print(f"Expected result (first 20): {expected}") assert final_memory[:20] == expected print("\nProgram executed successfully and result matches expected.") # Example of another program (simple addition) program_add = """ SET R1 10 SET R2 5 ADD R3 R1 R2 // R3 = 10 + 5 = 15 STORE R3 50 // Store 15 at address 50 HCF """ final_memory_add = evaluate(program_add) print(f"\nAddition program result (memory[50]): {final_memory_add[50]}") assert final_memory_add[50] == 15 print("Addition program executed successfully.") # Example demonstrating conditional jump program_cond = """ SET R1 5 SET R2 10 LT R1 R2 // Set flag = (5 < 10) -> True JF skip_store // Should NOT jump as flag is True SET R3 999 STORE R3 0 // Store 999 at address 0 skip_store: SET R4 111 STORE R4 1 // Store 111 at address 1 HCF """ final_memory_cond = evaluate(program_cond) print(f"\nConditional program result (memory[0], memory[1]): ({final_memory_cond[0]}, {final_memory_cond[1]})") assert final_memory_cond[0] == 999 assert final_memory_cond[1] == 111 print("Conditional program executed successfully.") except AssemblyError as e: print(f"Assembly Error: {e}") except Exception as e: print(f"An unexpected Python error occurred: {e}") |
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 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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | import re class AssemblyError(Exception): """Custom exception for errors during assembly execution.""" pass def evaluate(program_str: str) -> list[int]: """ Interprets the given assembly program string and returns the final memory state. Args: program_str: A string containing the assembly program. Returns: A list of 100 integers representing the final state of the memory. Raises: AssemblyError: If there's an error during parsing or execution. """ # --- 1. Initialize Machine State --- registers = {f"R{i}": 0 for i in range(1, 9)} flag = False memory = [0] * 100 ip = 0 # Instruction Pointer labels = {} instructions = [] # --- 2. Parse the Program --- lines = program_str.strip().split('\n') line_number = 0 # For error reporting related to original source lines instruction_index = 0 # Index within the 'instructions' list for line in lines: line_number += 1 original_line = line # Keep original for error messages # Remove comments and strip whitespace line = line.split('//', 1)[0].strip() if not line: continue # Skip empty lines or lines with only comments # Check for labels label_match = re.match(r'^([a-z][a-z0-9_]*):\s*$', line) if label_match: label_name = label_match.group(1) if label_name in labels: raise AssemblyError(f"Line {line_number}: Duplicate label '{label_name}'") labels[label_name] = instruction_index # Label points to the *next* instruction index continue # Don't add label line as an instruction # Parse instruction parts = line.split() if not parts: raise AssemblyError(f"Line {line_number}: Invalid instruction format: '{original_line}'") opcode = parts[0].upper() args = parts[1:] # Store instruction along with original line number for error context instructions.append({'opcode': opcode, 'args': args, 'line': line_number, 'original': original_line}) instruction_index += 1 # --- 3. Helper Function to Get Value (Register or Constant) --- def get_value(arg: str, line_num: int, original_line: str) -> int: if arg.upper() in registers: return registers[arg.upper()] try: return int(arg) except ValueError: raise AssemblyError(f"Line {line_num}: Invalid argument '{arg}'. Expected register (R1-R8) or integer constant in '{original_line}'") # --- 4. Execute Instructions --- max_instructions = len(instructions) * 1000 # Basic infinite loop prevention executed_count = 0 while 0 <= ip < len(instructions): if executed_count > max_instructions: raise AssemblyError("Potential infinite loop detected (exceeded instruction limit)") executed_count += 1 current_instruction = instructions[ip] opcode = current_instruction['opcode'] args = current_instruction['args'] line_num = current_instruction['line'] original_line = current_instruction['original'] # Assume sequential execution unless a jump happens next_ip = ip + 1 try: # --- Instruction Implementations --- if opcode == "SET": if len(args) != 2: raise AssemblyError(f"SET requires 2 arguments") reg = args[0].upper() if reg not in registers: raise AssemblyError(f"Invalid register '{args[0]}'") value = get_value(args[1], line_num, original_line) registers[reg] = value elif opcode in ("ADD", "SUB", "MUL", "DIV", "MOD"): if len(args) != 3: raise AssemblyError(f"{opcode} requires 3 arguments") dest_reg = args[0].upper() if dest_reg not in registers: raise AssemblyError(f"Invalid destination register '{args[0]}'") val1 = get_value(args[1], line_num, original_line) val2 = get_value(args[2], line_num, original_line) if opcode == "ADD": registers[dest_reg] = val1 + val2 elif opcode == "SUB": registers[dest_reg] = val1 - val2 elif opcode == "MUL": registers[dest_reg] = val1 * val2 elif opcode == "DIV": if val2 == 0: raise AssemblyError(f"Division by zero") registers[dest_reg] = val1 // val2 # Integer division elif opcode == "MOD": if val2 == 0: raise AssemblyError(f"Modulo by zero") registers[dest_reg] = val1 % val2 elif opcode in ("EQ", "NEQ", "LT", "LTE", "GT", "GTE"): if len(args) != 2: raise AssemblyError(f"{opcode} requires 2 arguments") val1 = get_value(args[0], line_num, original_line) val2 = get_value(args[1], line_num, original_line) if opcode == "EQ": flag = (val1 == val2) elif opcode == "NEQ": flag = (val1 != val2) elif opcode == "LT": flag = (val1 < val2) elif opcode == "LTE": flag = (val1 <= val2) elif opcode == "GT": flag = (val1 > val2) elif opcode == "GTE": flag = (val1 >= val2) elif opcode == "INC": if len(args) != 1: raise AssemblyError(f"INC requires 1 argument") reg = args[0].upper() if reg not in registers: raise AssemblyError(f"Invalid register '{args[0]}'") registers[reg] += 1 elif opcode == "DEC": if len(args) != 1: raise AssemblyError(f"DEC requires 1 argument") reg = args[0].upper() if reg not in registers: raise AssemblyError(f"Invalid register '{args[0]}'") registers[reg] -= 1 elif opcode == "JMP": if len(args) != 1: raise AssemblyError(f"JMP requires 1 argument (label)") label = args[0] if label not in labels: raise AssemblyError(f"Undefined label '{label}'") next_ip = labels[label] # Jump! elif opcode == "JT": if len(args) != 1: raise AssemblyError(f"JT requires 1 argument (label)") if flag: label = args[0] if label not in labels: raise AssemblyError(f"Undefined label '{label}'") next_ip = labels[label] # Jump! elif opcode == "JF": if len(args) != 1: raise AssemblyError(f"JF requires 1 argument (label)") if not flag: label = args[0] if label not in labels: raise AssemblyError(f"Undefined label '{label}'") next_ip = labels[label] # Jump! elif opcode == "LOAD": if len(args) != 2: raise AssemblyError(f"LOAD requires 2 arguments") reg = args[0].upper() if reg not in registers: raise AssemblyError(f"Invalid register '{args[0]}'") mem_addr = get_value(args[1], line_num, original_line) if not (0 <= mem_addr < 100): raise AssemblyError(f"Memory address {mem_addr} out of bounds (0-99)") registers[reg] = memory[mem_addr] elif opcode == "STORE": if len(args) != 2: raise AssemblyError(f"STORE requires 2 arguments") reg = args[0].upper() if reg not in registers: raise AssemblyError(f"Invalid register '{args[0]}'") value_to_store = registers[reg] mem_addr = get_value(args[1], line_num, original_line) if not (0 <= mem_addr < 100): raise AssemblyError(f"Memory address {mem_addr} out of bounds (0-99)") memory[mem_addr] = value_to_store elif opcode == "HCF": if len(args) != 0: raise AssemblyError(f"HCF takes no arguments") break # Halt and Catch Fire (with pizzazz!) - Stop execution else: raise AssemblyError(f"Unknown opcode '{opcode}'") except AssemblyError as e: # Add context to the error message raise AssemblyError(f"Line {line_num}: {e} in '{original_line}'") from e except IndexError: raise AssemblyError(f"Line {line_num}: Incorrect number of arguments for {opcode} in '{original_line}'") except Exception as e: # Catch unexpected errors raise AssemblyError(f"Line {line_num}: Unexpected error '{e}' during execution of '{original_line}'") from e # Move to the next instruction ip = next_ip # --- 5. Return Final Memory State --- return memory # --- Example Usage --- program = """ SET R1 0 // Counter for storing squares (memory address) SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 (which starts at 0) INC R1 // Increment address for next store INC R2 // Increment number to square SET R4 20 // Target number of squares EQ R1 R4 // Check if R1 (address counter) has reached 20 JF loop // If flag is False (R1 != 20), jump back to loop end: // Optional label, program stops anyway HCF // Stop program """ try: final_memory = evaluate(program) # Print the first 20 memory locations (where the squares were stored) print(f"Final memory state (first 20): {final_memory[:20]}") # Verify the result expected = [i*i for i in range(1, 21)] print(f"Expected result (first 20): {expected}") assert final_memory[:20] == expected print("\nProgram executed successfully and result matches expected.") # Example of another program (simple addition) program_add = """ SET R1 10 SET R2 5 ADD R3 R1 R2 // R3 = 10 + 5 = 15 STORE R3 50 // Store 15 at address 50 HCF """ final_memory_add = evaluate(program_add) print(f"\nAddition program result (memory[50]): {final_memory_add[50]}") assert final_memory_add[50] == 15 print("Addition program executed successfully.") # Example demonstrating conditional jump program_cond = """ SET R1 5 SET R2 10 LT R1 R2 // Set flag = (5 < 10) -> True JF skip_store // Should NOT jump as flag is True SET R3 999 STORE R3 0 // Store 999 at address 0 skip_store: SET R4 111 STORE R4 1 // Store 111 at address 1 HCF """ final_memory_cond = evaluate(program_cond) print(f"\nConditional program result (memory[0], memory[1]): ({final_memory_cond[0]}, {final_memory_cond[1]})") assert final_memory_cond[0] == 999 assert final_memory_cond[1] == 111 print("Conditional program executed successfully.") except AssemblyError as e: print(f"Assembly Error: {e}") except Exception as e: print(f"An unexpected Python error occurred: {e}") |
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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | import re class AssemblyError(Exception): """Custom exception for errors during assembly execution.""" pass def evaluate(program_str: str) -> list[int]: """ Interprets the given assembly program string and returns the final memory state. Args: program_str: A string containing the assembly program. Returns: A list of 100 integers representing the final state of the memory. Raises: AssemblyError: If there's an error during parsing or execution. """ # --- 1. Initialize Machine State --- registers = {f"R{i}": 0 for i in range(1, 9)} flag = False memory = [0] * 100 ip = 0 # Instruction Pointer labels = {} instructions = [] # --- 2. Parse the Program --- lines = program_str.strip().split('\n') line_number = 0 # For error reporting related to original source lines instruction_index = 0 # Index within the 'instructions' list for line in lines: line_number += 1 original_line = line # Keep original for error messages # Remove comments and strip whitespace line = line.split('//', 1)[0].strip() if not line: continue # Skip empty lines or lines with only comments # Check for labels label_match = re.match(r'^([a-z][a-z0-9_]*):\s*$', line) if label_match: label_name = label_match.group(1) if label_name in labels: raise AssemblyError(f"Line {line_number}: Duplicate label '{label_name}'") labels[label_name] = instruction_index # Label points to the *next* instruction index continue # Don't add label line as an instruction # Parse instruction parts = line.split() if not parts: raise AssemblyError(f"Line {line_number}: Invalid instruction format: '{original_line}'") opcode = parts[0].upper() args = parts[1:] # Store instruction along with original line number for error context instructions.append({'opcode': opcode, 'args': args, 'line': line_number, 'original': original_line}) instruction_index += 1 # --- 3. Helper Function to Get Value (Register or Constant) --- def get_value(arg: str, line_num: int, original_line: str) -> int: if arg.upper() in registers: return registers[arg.upper()] try: return int(arg) except ValueError: raise AssemblyError(f"Line {line_num}: Invalid argument '{arg}'. Expected register (R1-R8) or integer constant in '{original_line}'") # --- 4. Execute Instructions --- max_instructions = len(instructions) * 1000 # Basic infinite loop prevention executed_count = 0 while 0 <= ip < len(instructions): if executed_count > max_instructions: raise AssemblyError("Potential infinite loop detected (exceeded instruction limit)") executed_count += 1 current_instruction = instructions[ip] opcode = current_instruction['opcode'] args = current_instruction['args'] line_num = current_instruction['line'] original_line = current_instruction['original'] # Assume sequential execution unless a jump happens next_ip = ip + 1 try: # --- Instruction Implementations --- if opcode == "SET": if len(args) != 2: raise AssemblyError(f"SET requires 2 arguments") reg = args[0].upper() if reg not in registers: raise AssemblyError(f"Invalid register '{args[0]}'") value = get_value(args[1], line_num, original_line) registers[reg] = value elif opcode in ("ADD", "SUB", "MUL", "DIV", "MOD"): if len(args) != 3: raise AssemblyError(f"{opcode} requires 3 arguments") dest_reg = args[0].upper() if dest_reg not in registers: raise AssemblyError(f"Invalid destination register '{args[0]}'") val1 = get_value(args[1], line_num, original_line) val2 = get_value(args[2], line_num, original_line) if opcode == "ADD": registers[dest_reg] = val1 + val2 elif opcode == "SUB": registers[dest_reg] = val1 - val2 elif opcode == "MUL": registers[dest_reg] = val1 * val2 elif opcode == "DIV": if val2 == 0: raise AssemblyError(f"Division by zero") registers[dest_reg] = val1 // val2 # Integer division elif opcode == "MOD": if val2 == 0: raise AssemblyError(f"Modulo by zero") registers[dest_reg] = val1 % val2 elif opcode in ("EQ", "NEQ", "LT", "LTE", "GT", "GTE"): if len(args) != 2: raise AssemblyError(f"{opcode} requires 2 arguments") val1 = get_value(args[0], line_num, original_line) val2 = get_value(args[1], line_num, original_line) if opcode == "EQ": flag = (val1 == val2) elif opcode == "NEQ": flag = (val1 != val2) elif opcode == "LT": flag = (val1 < val2) elif opcode == "LTE": flag = (val1 <= val2) elif opcode == "GT": flag = (val1 > val2) elif opcode == "GTE": flag = (val1 >= val2) elif opcode == "INC": if len(args) != 1: raise AssemblyError(f"INC requires 1 argument") reg = args[0].upper() if reg not in registers: raise AssemblyError(f"Invalid register '{args[0]}'") registers[reg] += 1 elif opcode == "DEC": if len(args) != 1: raise AssemblyError(f"DEC requires 1 argument") reg = args[0].upper() if reg not in registers: raise AssemblyError(f"Invalid register '{args[0]}'") registers[reg] -= 1 elif opcode == "JMP": if len(args) != 1: raise AssemblyError(f"JMP requires 1 argument (label)") label = args[0] if label not in labels: raise AssemblyError(f"Undefined label '{label}'") next_ip = labels[label] # Jump! elif opcode == "JT": if len(args) != 1: raise AssemblyError(f"JT requires 1 argument (label)") if flag: label = args[0] if label not in labels: raise AssemblyError(f"Undefined label '{label}'") next_ip = labels[label] # Jump! elif opcode == "JF": if len(args) != 1: raise AssemblyError(f"JF requires 1 argument (label)") if not flag: label = args[0] if label not in labels: raise AssemblyError(f"Undefined label '{label}'") next_ip = labels[label] # Jump! elif opcode == "LOAD": if len(args) != 2: raise AssemblyError(f"LOAD requires 2 arguments") reg = args[0].upper() if reg not in registers: raise AssemblyError(f"Invalid register '{args[0]}'") mem_addr = get_value(args[1], line_num, original_line) if not (0 <= mem_addr < 100): raise AssemblyError(f"Memory address {mem_addr} out of bounds (0-99)") registers[reg] = memory[mem_addr] elif opcode == "STORE": if len(args) != 2: raise AssemblyError(f"STORE requires 2 arguments") reg = args[0].upper() if reg not in registers: raise AssemblyError(f"Invalid register '{args[0]}'") value_to_store = registers[reg] mem_addr = get_value(args[1], line_num, original_line) if not (0 <= mem_addr < 100): raise AssemblyError(f"Memory address {mem_addr} out of bounds (0-99)") memory[mem_addr] = value_to_store elif opcode == "HCF": if len(args) != 0: raise AssemblyError(f"HCF takes no arguments") break # Halt and Catch Fire (with pizzazz!) - Stop execution else: raise AssemblyError(f"Unknown opcode '{opcode}'") except AssemblyError as e: # Add context to the error message raise AssemblyError(f"Line {line_num}: {e} in '{original_line}'") from e except IndexError: raise AssemblyError(f"Line {line_num}: Incorrect number of arguments for {opcode} in '{original_line}'") except Exception as e: # Catch unexpected errors raise AssemblyError(f"Line {line_num}: Unexpected error '{e}' during execution of '{original_line}'") from e # Move to the next instruction ip = next_ip # --- 5. Return Final Memory State --- return memory # --- Example Usage --- program = """ SET R1 0 // Counter for storing squares (memory address) SET R2 1 // Number to square loop: MUL R3 R2 R2 // R3 = R2 * R2 STORE R3 R1 // Store R3 at address R1 (which starts at 0) INC R1 // Increment address for next store INC R2 // Increment number to square SET R4 20 // Target number of squares EQ R1 R4 // Check if R1 (address counter) has reached 20 JF loop // If flag is False (R1 != 20), jump back to loop end: // Optional label, program stops anyway HCF // Stop program """ try: final_memory = evaluate(program) # Print the first 20 memory locations (where the squares were stored) print(f"Final memory state (first 20): {final_memory[:20]}") # Verify the result expected = [i*i for i in range(1, 21)] print(f"Expected result (first 20): {expected}") assert final_memory[:20] == expected print("\nProgram executed successfully and result matches expected.") # Example of another program (simple addition) program_add = """ SET R1 10 SET R2 5 ADD R3 R1 R2 // R3 = 10 + 5 = 15 STORE R3 50 // Store 15 at address 50 HCF """ final_memory_add = evaluate(program_add) print(f"\nAddition program result (memory[50]): {final_memory_add[50]}") assert final_memory_add[50] == 15 print("Addition program executed successfully.") # Example demonstrating conditional jump program_cond = """ SET R1 5 SET R2 10 LT R1 R2 // Set flag = (5 < 10) -> True JF skip_store // Should NOT jump as flag is True SET R3 999 STORE R3 0 // Store 999 at address 0 skip_store: SET R4 111 STORE R4 1 // Store 111 at address 1 HCF """ final_memory_cond = evaluate(program_cond) print(f"\nConditional program result (memory[0], memory[1]): ({final_memory_cond[0]}, {final_memory_cond[1]})") assert final_memory_cond[0] == 999 assert final_memory_cond[1] == 111 print("Conditional program executed successfully.") except AssemblyError as e: print(f"Assembly Error: {e}") except Exception as e: print(f"An unexpected Python error occurred: {e}") 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') |
Final memory state (first 20): [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]
Expected result (first 20): [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]
Program executed successfully and result matches expected.
Addition program result (memory[50]): 15
Addition program executed successfully.
Conditional program result (memory[0], memory[1]): (999, 111)
Conditional program executed successfully.
Traceback (most recent call last):
File "/usr/src/app/main.py", line 290, in <module>
answer = evaluate("""
^^^^^^^^^^^^
File "/usr/src/app/main.py", line 81, in evaluate
raise AssemblyError("Potential infinite loop detected (exceeded instruction limit)")
AssemblyError: Potential infinite loop detected (exceeded instruction limit)
Testing if the previous output contains the string All tests passed
: False