Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 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: |
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 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: |
Okay, let's create the Python interpreter for your custom assembly language. We'll need to simulate: 1. Registers (R1-R8) 2. Memory (a list that grows) 3. Program Counter (PC) 4. Comparison Flag (for JT/JF) 5. Labels
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 | import re def evaluate(program_string): """ Interprets the custom assembly program and returns the final memory state. Args: program_string: A string containing the assembly program. Returns: A list representing the final state of the memory used for storing primes. """ lines = program_string.strip().split('\n') instructions = [] labels = {} pc_line_map = {} # Maps actual instruction index to original line number for errors # --- Preprocessing: Parse instructions and find labels --- current_instruction_index = 0 for i, line in enumerate(lines): line = line.strip() if not line or line.startswith('//'): continue # Skip empty lines and comments # Check for labels label_match = re.match(r'^([a-zA-Z_][a-zA-Z0-9_]*):$', line) if label_match: label_name = label_match.group(1) if label_name in labels: raise ValueError(f"Duplicate label '{label_name}' found at line {i+1}") labels[label_name] = current_instruction_index # Don't add labels as instructions continue # Remove inline comments if any (e.g., "SET R1 2 // comment") line = line.split('//')[0].strip() if not line: # Line might have only contained a comment continue instructions.append(line) pc_line_map[current_instruction_index] = i + 1 # Store original line number current_instruction_index += 1 # --- Simulation State --- registers = {f"R{i}": 0 for i in range(1, 9)} # R1 to R8 initialized to 0 memory = [] pc = 0 # Program Counter (index into the 'instructions' list) comparison_flag = False # Result of the last EQ or GT comparison # --- Execution Loop --- max_steps = 1000000 # Safety break to prevent infinite loops steps = 0 while 0 <= pc < len(instructions) and steps < max_steps: current_line_num = pc_line_map.get(pc, "Unknown") instruction = instructions[pc] parts = instruction.split() opcode = parts[0].upper() operands = parts[1:] # Default behavior: advance PC by 1 unless it's a jump next_pc = pc + 1 try: if opcode == "SET": # SET Rx value if len(operands) != 2: raise ValueError("SET requires 2 operands") reg = operands[0].upper() if reg not in registers: raise ValueError(f"Invalid register '{reg}'") try: value = int(operands[1]) except ValueError: raise ValueError(f"Invalid immediate value '{operands[1]}'") registers[reg] = value elif opcode == "INC": # INC Rx if len(operands) != 1: raise ValueError("INC requires 1 operand") reg = operands[0].upper() if reg not in registers: raise ValueError(f"Invalid register '{reg}'") registers[reg] += 1 elif opcode == "EQ": # EQ Rx Ry if len(operands) != 2: raise ValueError("EQ requires 2 operands") reg1, reg2 = operands[0].upper(), operands[1].upper() if reg1 not in registers: raise ValueError(f"Invalid register '{reg1}'") if reg2 not in registers: raise ValueError(f"Invalid register '{reg2}'") comparison_flag = (registers[reg1] == registers[reg2]) elif opcode == "GT": # GT Rx Ry if len(operands) != 2: raise ValueError("GT requires 2 operands") reg1, reg2 = operands[0].upper(), operands[1].upper() if reg1 not in registers: raise ValueError(f"Invalid register '{reg1}'") if reg2 not in registers: raise ValueError(f"Invalid register '{reg2}'") comparison_flag = (registers[reg1] > registers[reg2]) elif opcode == "JF": # JF label if len(operands) != 1: raise ValueError("JF requires 1 operand") label = operands[0] if label not in labels: raise ValueError(f"Undefined label '{label}'") if not comparison_flag: next_pc = labels[label] elif opcode == "JT": # JT label if len(operands) != 1: raise ValueError("JT requires 1 operand") label = operands[0] if label not in labels: raise ValueError(f"Undefined label '{label}'") if comparison_flag: next_pc = labels[label] elif opcode == "JMP": # JMP label if len(operands) != 1: raise ValueError("JMP requires 1 operand") label = operands[0] if label not in labels: raise ValueError(f"Undefined label '{label}'") # Handle the specific 'is_prime' -> 'ready_prime' return mechanism # This isn't a general call stack, just how this specific program uses JMP next_pc = labels[label] elif opcode == "STORE": # STORE Rx Ry (Store value in Rx at address in Ry) if len(operands) != 2: raise ValueError("STORE requires 2 operands") reg_val, reg_addr = operands[0].upper(), operands[1].upper() if reg_val not in registers: raise ValueError(f"Invalid register '{reg_val}'") if reg_addr not in registers: raise ValueError(f"Invalid register '{reg_addr}'") value_to_store = registers[reg_val] address = registers[reg_addr] if address < 0: raise ValueError(f"Attempted to store at negative memory address {address}") # Ensure memory is large enough, fill with 0 if needed (though list append might be better if R8 is always sequential) # Using append based on the program's logic where R8 increments sequentially if address == len(memory): memory.append(value_to_store) elif address < len(memory): # Overwrite existing memory if address is valid but not the end # This might deviate if R8 wasn't just used sequentially print(f"Warning: Overwriting memory at address {address}. Expected sequential writes.") memory[address] = value_to_store else: # Fill gaps if address skips indices - indicates non-sequential use of R8 print(f"Warning: Non-sequential memory write to address {address}. Filling gaps.") memory.extend([0] * (address - len(memory))) memory.append(value_to_store) elif opcode == "MUL": # MUL Rd Rx Ry if len(operands) != 3: raise ValueError("MUL requires 3 operands") reg_d, reg_x, reg_y = operands[0].upper(), operands[1].upper(), operands[2].upper() if reg_d not in registers: raise ValueError(f"Invalid register '{reg_d}'") if reg_x not in registers: raise ValueError(f"Invalid register '{reg_x}'") if reg_y not in registers: raise ValueError(f"Invalid register '{reg_y}'") registers[reg_d] = registers[reg_x] * registers[reg_y] elif opcode == "MOD": # MOD Rd Rx Ry if len(operands) != 3: raise ValueError("MOD requires 3 operands") reg_d, reg_x, reg_y = operands[0].upper(), operands[1].upper(), operands[2].upper() if reg_d not in registers: raise ValueError(f"Invalid register '{reg_d}'") if reg_x not in registers: raise ValueError(f"Invalid register '{reg_x}'") if reg_y not in registers: raise ValueError(f"Invalid register '{reg_y}'") divisor = registers[reg_y] if divisor == 0: raise ZeroDivisionError(f"Division by zero in MOD operation at line {current_line_num}") registers[reg_d] = registers[reg_x] % divisor else: raise ValueError(f"Unknown opcode '{opcode}'") except Exception as e: print(f"Error executing instruction at original line {current_line_num}: '{instruction}'") print(f"Current State: PC={pc}, Registers={registers}, Flag={comparison_flag}") raise e # Re-raise the exception pc = next_pc steps += 1 if steps >= max_steps: print(f"Warning: Maximum execution steps ({max_steps}) reached. Possible infinite loop.") print(f"Final State: PC={pc}, Registers={registers}, Flag={comparison_flag}") # The program stores primes sequentially using R8 as the index/count # Return the memory list which should contain the primes return memory # --- Example Usage --- program = """ SET R1 2 // Starting number to check for prime SET R8 0 // Initialize prime storage index/count 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/next address // Check if 100 primes are found SET R7 100 EQ R8 R7 JT end // If 100 primes found, end program (Jump if True - R8 == 100) increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number // Subroutine to check if R1 is prime. Result in R2 (1=prime, 0=not prime) is_prime: // Handle base cases: 2 is prime SET R7 2 EQ R1 R7 JT is_prime_set_true // If R1 is 2, it's prime SET R2 1 // Assume number is prime initially (for numbers > 2) SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Optimization: Check if divisor R3 * R3 > R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If R3*R3 > R1, then R1 must be prime (no smaller factors found) // Check for divisibility: R6 = R1 % R3 MOD R6 R1 R3 SET R7 0 EQ R7 R6 // Check if R6 is 0 (i.e., R1 % R3 == 0) JT not_prime // If divisible, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop is_prime_set_true: SET R2 1 // Set result to 1 (prime) JMP is_prime_end // Go to end of subroutine not_prime: SET R2 0 // Set result to 0 (not prime) // Fall through to is_prime_end is_prime_end: JMP ready_prime // Return control to the point after the 'call' end: // Program halts when PC reaches here or beyond instructions """ # Add a small fix to the program: Initialize R8 # Also added a check for R1=2 in is_prime for correctness. # Changed the final check: JT end (Jump if True that R8 == 100) primes_found = evaluate(program) print(f"Found {len(primes_found)} primes.") print(primes_found) # Verification (first few primes) expected_primes_start = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] print(f"First 10 primes match expected: {primes_found[:10] == expected_primes_start}") # Verification (count) print(f"Found 100 primes: {len(primes_found) == 100}") |
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 | import re def evaluate(program_string): """ Interprets the custom assembly program and returns the final memory state. Args: program_string: A string containing the assembly program. Returns: A list representing the final state of the memory used for storing primes. """ lines = program_string.strip().split('\n') instructions = [] labels = {} pc_line_map = {} # Maps actual instruction index to original line number for errors # --- Preprocessing: Parse instructions and find labels --- current_instruction_index = 0 for i, line in enumerate(lines): line = line.strip() if not line or line.startswith('//'): continue # Skip empty lines and comments # Check for labels label_match = re.match(r'^([a-zA-Z_][a-zA-Z0-9_]*):$', line) if label_match: label_name = label_match.group(1) if label_name in labels: raise ValueError(f"Duplicate label '{label_name}' found at line {i+1}") labels[label_name] = current_instruction_index # Don't add labels as instructions continue # Remove inline comments if any (e.g., "SET R1 2 // comment") line = line.split('//')[0].strip() if not line: # Line might have only contained a comment continue instructions.append(line) pc_line_map[current_instruction_index] = i + 1 # Store original line number current_instruction_index += 1 # --- Simulation State --- registers = {f"R{i}": 0 for i in range(1, 9)} # R1 to R8 initialized to 0 memory = [] pc = 0 # Program Counter (index into the 'instructions' list) comparison_flag = False # Result of the last EQ or GT comparison # --- Execution Loop --- max_steps = 1000000 # Safety break to prevent infinite loops steps = 0 while 0 <= pc < len(instructions) and steps < max_steps: current_line_num = pc_line_map.get(pc, "Unknown") instruction = instructions[pc] parts = instruction.split() opcode = parts[0].upper() operands = parts[1:] # Default behavior: advance PC by 1 unless it's a jump next_pc = pc + 1 try: if opcode == "SET": # SET Rx value if len(operands) != 2: raise ValueError("SET requires 2 operands") reg = operands[0].upper() if reg not in registers: raise ValueError(f"Invalid register '{reg}'") try: value = int(operands[1]) except ValueError: raise ValueError(f"Invalid immediate value '{operands[1]}'") registers[reg] = value elif opcode == "INC": # INC Rx if len(operands) != 1: raise ValueError("INC requires 1 operand") reg = operands[0].upper() if reg not in registers: raise ValueError(f"Invalid register '{reg}'") registers[reg] += 1 elif opcode == "EQ": # EQ Rx Ry if len(operands) != 2: raise ValueError("EQ requires 2 operands") reg1, reg2 = operands[0].upper(), operands[1].upper() if reg1 not in registers: raise ValueError(f"Invalid register '{reg1}'") if reg2 not in registers: raise ValueError(f"Invalid register '{reg2}'") comparison_flag = (registers[reg1] == registers[reg2]) elif opcode == "GT": # GT Rx Ry if len(operands) != 2: raise ValueError("GT requires 2 operands") reg1, reg2 = operands[0].upper(), operands[1].upper() if reg1 not in registers: raise ValueError(f"Invalid register '{reg1}'") if reg2 not in registers: raise ValueError(f"Invalid register '{reg2}'") comparison_flag = (registers[reg1] > registers[reg2]) elif opcode == "JF": # JF label if len(operands) != 1: raise ValueError("JF requires 1 operand") label = operands[0] if label not in labels: raise ValueError(f"Undefined label '{label}'") if not comparison_flag: next_pc = labels[label] elif opcode == "JT": # JT label if len(operands) != 1: raise ValueError("JT requires 1 operand") label = operands[0] if label not in labels: raise ValueError(f"Undefined label '{label}'") if comparison_flag: next_pc = labels[label] elif opcode == "JMP": # JMP label if len(operands) != 1: raise ValueError("JMP requires 1 operand") label = operands[0] if label not in labels: raise ValueError(f"Undefined label '{label}'") # Handle the specific 'is_prime' -> 'ready_prime' return mechanism # This isn't a general call stack, just how this specific program uses JMP next_pc = labels[label] elif opcode == "STORE": # STORE Rx Ry (Store value in Rx at address in Ry) if len(operands) != 2: raise ValueError("STORE requires 2 operands") reg_val, reg_addr = operands[0].upper(), operands[1].upper() if reg_val not in registers: raise ValueError(f"Invalid register '{reg_val}'") if reg_addr not in registers: raise ValueError(f"Invalid register '{reg_addr}'") value_to_store = registers[reg_val] address = registers[reg_addr] if address < 0: raise ValueError(f"Attempted to store at negative memory address {address}") # Ensure memory is large enough, fill with 0 if needed (though list append might be better if R8 is always sequential) # Using append based on the program's logic where R8 increments sequentially if address == len(memory): memory.append(value_to_store) elif address < len(memory): # Overwrite existing memory if address is valid but not the end # This might deviate if R8 wasn't just used sequentially print(f"Warning: Overwriting memory at address {address}. Expected sequential writes.") memory[address] = value_to_store else: # Fill gaps if address skips indices - indicates non-sequential use of R8 print(f"Warning: Non-sequential memory write to address {address}. Filling gaps.") memory.extend([0] * (address - len(memory))) memory.append(value_to_store) elif opcode == "MUL": # MUL Rd Rx Ry if len(operands) != 3: raise ValueError("MUL requires 3 operands") reg_d, reg_x, reg_y = operands[0].upper(), operands[1].upper(), operands[2].upper() if reg_d not in registers: raise ValueError(f"Invalid register '{reg_d}'") if reg_x not in registers: raise ValueError(f"Invalid register '{reg_x}'") if reg_y not in registers: raise ValueError(f"Invalid register '{reg_y}'") registers[reg_d] = registers[reg_x] * registers[reg_y] elif opcode == "MOD": # MOD Rd Rx Ry if len(operands) != 3: raise ValueError("MOD requires 3 operands") reg_d, reg_x, reg_y = operands[0].upper(), operands[1].upper(), operands[2].upper() if reg_d not in registers: raise ValueError(f"Invalid register '{reg_d}'") if reg_x not in registers: raise ValueError(f"Invalid register '{reg_x}'") if reg_y not in registers: raise ValueError(f"Invalid register '{reg_y}'") divisor = registers[reg_y] if divisor == 0: raise ZeroDivisionError(f"Division by zero in MOD operation at line {current_line_num}") registers[reg_d] = registers[reg_x] % divisor else: raise ValueError(f"Unknown opcode '{opcode}'") except Exception as e: print(f"Error executing instruction at original line {current_line_num}: '{instruction}'") print(f"Current State: PC={pc}, Registers={registers}, Flag={comparison_flag}") raise e # Re-raise the exception pc = next_pc steps += 1 if steps >= max_steps: print(f"Warning: Maximum execution steps ({max_steps}) reached. Possible infinite loop.") print(f"Final State: PC={pc}, Registers={registers}, Flag={comparison_flag}") # The program stores primes sequentially using R8 as the index/count # Return the memory list which should contain the primes return memory # --- Example Usage --- program = """ SET R1 2 // Starting number to check for prime SET R8 0 // Initialize prime storage index/count 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/next address // Check if 100 primes are found SET R7 100 EQ R8 R7 JT end // If 100 primes found, end program (Jump if True - R8 == 100) increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number // Subroutine to check if R1 is prime. Result in R2 (1=prime, 0=not prime) is_prime: // Handle base cases: 2 is prime SET R7 2 EQ R1 R7 JT is_prime_set_true // If R1 is 2, it's prime SET R2 1 // Assume number is prime initially (for numbers > 2) SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Optimization: Check if divisor R3 * R3 > R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If R3*R3 > R1, then R1 must be prime (no smaller factors found) // Check for divisibility: R6 = R1 % R3 MOD R6 R1 R3 SET R7 0 EQ R7 R6 // Check if R6 is 0 (i.e., R1 % R3 == 0) JT not_prime // If divisible, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop is_prime_set_true: SET R2 1 // Set result to 1 (prime) JMP is_prime_end // Go to end of subroutine not_prime: SET R2 0 // Set result to 0 (not prime) // Fall through to is_prime_end is_prime_end: JMP ready_prime // Return control to the point after the 'call' end: // Program halts when PC reaches here or beyond instructions """ # Add a small fix to the program: Initialize R8 # Also added a check for R1=2 in is_prime for correctness. # Changed the final check: JT end (Jump if True that R8 == 100) primes_found = evaluate(program) print(f"Found {len(primes_found)} primes.") print(primes_found) # Verification (first few primes) expected_primes_start = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] print(f"First 10 primes match expected: {primes_found[:10] == expected_primes_start}") # Verification (count) print(f"Found 100 primes: {len(primes_found) == 100}") |
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 | import re def evaluate(program_string): """ Interprets the custom assembly program and returns the final memory state. Args: program_string: A string containing the assembly program. Returns: A list representing the final state of the memory used for storing primes. """ lines = program_string.strip().split('\n') instructions = [] labels = {} pc_line_map = {} # Maps actual instruction index to original line number for errors # --- Preprocessing: Parse instructions and find labels --- current_instruction_index = 0 for i, line in enumerate(lines): line = line.strip() if not line or line.startswith('//'): continue # Skip empty lines and comments # Check for labels label_match = re.match(r'^([a-zA-Z_][a-zA-Z0-9_]*):$', line) if label_match: label_name = label_match.group(1) if label_name in labels: raise ValueError(f"Duplicate label '{label_name}' found at line {i+1}") labels[label_name] = current_instruction_index # Don't add labels as instructions continue # Remove inline comments if any (e.g., "SET R1 2 // comment") line = line.split('//')[0].strip() if not line: # Line might have only contained a comment continue instructions.append(line) pc_line_map[current_instruction_index] = i + 1 # Store original line number current_instruction_index += 1 # --- Simulation State --- registers = {f"R{i}": 0 for i in range(1, 9)} # R1 to R8 initialized to 0 memory = [] pc = 0 # Program Counter (index into the 'instructions' list) comparison_flag = False # Result of the last EQ or GT comparison # --- Execution Loop --- max_steps = 1000000 # Safety break to prevent infinite loops steps = 0 while 0 <= pc < len(instructions) and steps < max_steps: current_line_num = pc_line_map.get(pc, "Unknown") instruction = instructions[pc] parts = instruction.split() opcode = parts[0].upper() operands = parts[1:] # Default behavior: advance PC by 1 unless it's a jump next_pc = pc + 1 try: if opcode == "SET": # SET Rx value if len(operands) != 2: raise ValueError("SET requires 2 operands") reg = operands[0].upper() if reg not in registers: raise ValueError(f"Invalid register '{reg}'") try: value = int(operands[1]) except ValueError: raise ValueError(f"Invalid immediate value '{operands[1]}'") registers[reg] = value elif opcode == "INC": # INC Rx if len(operands) != 1: raise ValueError("INC requires 1 operand") reg = operands[0].upper() if reg not in registers: raise ValueError(f"Invalid register '{reg}'") registers[reg] += 1 elif opcode == "EQ": # EQ Rx Ry if len(operands) != 2: raise ValueError("EQ requires 2 operands") reg1, reg2 = operands[0].upper(), operands[1].upper() if reg1 not in registers: raise ValueError(f"Invalid register '{reg1}'") if reg2 not in registers: raise ValueError(f"Invalid register '{reg2}'") comparison_flag = (registers[reg1] == registers[reg2]) elif opcode == "GT": # GT Rx Ry if len(operands) != 2: raise ValueError("GT requires 2 operands") reg1, reg2 = operands[0].upper(), operands[1].upper() if reg1 not in registers: raise ValueError(f"Invalid register '{reg1}'") if reg2 not in registers: raise ValueError(f"Invalid register '{reg2}'") comparison_flag = (registers[reg1] > registers[reg2]) elif opcode == "JF": # JF label if len(operands) != 1: raise ValueError("JF requires 1 operand") label = operands[0] if label not in labels: raise ValueError(f"Undefined label '{label}'") if not comparison_flag: next_pc = labels[label] elif opcode == "JT": # JT label if len(operands) != 1: raise ValueError("JT requires 1 operand") label = operands[0] if label not in labels: raise ValueError(f"Undefined label '{label}'") if comparison_flag: next_pc = labels[label] elif opcode == "JMP": # JMP label if len(operands) != 1: raise ValueError("JMP requires 1 operand") label = operands[0] if label not in labels: raise ValueError(f"Undefined label '{label}'") # Handle the specific 'is_prime' -> 'ready_prime' return mechanism # This isn't a general call stack, just how this specific program uses JMP next_pc = labels[label] elif opcode == "STORE": # STORE Rx Ry (Store value in Rx at address in Ry) if len(operands) != 2: raise ValueError("STORE requires 2 operands") reg_val, reg_addr = operands[0].upper(), operands[1].upper() if reg_val not in registers: raise ValueError(f"Invalid register '{reg_val}'") if reg_addr not in registers: raise ValueError(f"Invalid register '{reg_addr}'") value_to_store = registers[reg_val] address = registers[reg_addr] if address < 0: raise ValueError(f"Attempted to store at negative memory address {address}") # Ensure memory is large enough, fill with 0 if needed (though list append might be better if R8 is always sequential) # Using append based on the program's logic where R8 increments sequentially if address == len(memory): memory.append(value_to_store) elif address < len(memory): # Overwrite existing memory if address is valid but not the end # This might deviate if R8 wasn't just used sequentially print(f"Warning: Overwriting memory at address {address}. Expected sequential writes.") memory[address] = value_to_store else: # Fill gaps if address skips indices - indicates non-sequential use of R8 print(f"Warning: Non-sequential memory write to address {address}. Filling gaps.") memory.extend([0] * (address - len(memory))) memory.append(value_to_store) elif opcode == "MUL": # MUL Rd Rx Ry if len(operands) != 3: raise ValueError("MUL requires 3 operands") reg_d, reg_x, reg_y = operands[0].upper(), operands[1].upper(), operands[2].upper() if reg_d not in registers: raise ValueError(f"Invalid register '{reg_d}'") if reg_x not in registers: raise ValueError(f"Invalid register '{reg_x}'") if reg_y not in registers: raise ValueError(f"Invalid register '{reg_y}'") registers[reg_d] = registers[reg_x] * registers[reg_y] elif opcode == "MOD": # MOD Rd Rx Ry if len(operands) != 3: raise ValueError("MOD requires 3 operands") reg_d, reg_x, reg_y = operands[0].upper(), operands[1].upper(), operands[2].upper() if reg_d not in registers: raise ValueError(f"Invalid register '{reg_d}'") if reg_x not in registers: raise ValueError(f"Invalid register '{reg_x}'") if reg_y not in registers: raise ValueError(f"Invalid register '{reg_y}'") divisor = registers[reg_y] if divisor == 0: raise ZeroDivisionError(f"Division by zero in MOD operation at line {current_line_num}") registers[reg_d] = registers[reg_x] % divisor else: raise ValueError(f"Unknown opcode '{opcode}'") except Exception as e: print(f"Error executing instruction at original line {current_line_num}: '{instruction}'") print(f"Current State: PC={pc}, Registers={registers}, Flag={comparison_flag}") raise e # Re-raise the exception pc = next_pc steps += 1 if steps >= max_steps: print(f"Warning: Maximum execution steps ({max_steps}) reached. Possible infinite loop.") print(f"Final State: PC={pc}, Registers={registers}, Flag={comparison_flag}") # The program stores primes sequentially using R8 as the index/count # Return the memory list which should contain the primes return memory # --- Example Usage --- program = """ SET R1 2 // Starting number to check for prime SET R8 0 // Initialize prime storage index/count 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/next address // Check if 100 primes are found SET R7 100 EQ R8 R7 JT end // If 100 primes found, end program (Jump if True - R8 == 100) increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number // Subroutine to check if R1 is prime. Result in R2 (1=prime, 0=not prime) is_prime: // Handle base cases: 2 is prime SET R7 2 EQ R1 R7 JT is_prime_set_true // If R1 is 2, it's prime SET R2 1 // Assume number is prime initially (for numbers > 2) SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Optimization: Check if divisor R3 * R3 > R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If R3*R3 > R1, then R1 must be prime (no smaller factors found) // Check for divisibility: R6 = R1 % R3 MOD R6 R1 R3 SET R7 0 EQ R7 R6 // Check if R6 is 0 (i.e., R1 % R3 == 0) JT not_prime // If divisible, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop is_prime_set_true: SET R2 1 // Set result to 1 (prime) JMP is_prime_end // Go to end of subroutine not_prime: SET R2 0 // Set result to 0 (not prime) // Fall through to is_prime_end is_prime_end: JMP ready_prime // Return control to the point after the 'call' end: // Program halts when PC reaches here or beyond instructions """ # Add a small fix to the program: Initialize R8 # Also added a check for R1=2 in is_prime for correctness. # Changed the final check: JT end (Jump if True that R8 == 100) primes_found = evaluate(program) print(f"Found {len(primes_found)} primes.") print(primes_found) # Verification (first few primes) expected_primes_start = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] print(f"First 10 primes match expected: {primes_found[:10] == expected_primes_start}") # Verification (count) print(f"Found 100 primes: {len(primes_found) == 100}") 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') |
Error executing instruction at original line 36: 'start_loop:'
Current State: PC=18, Registers={'R1': 3, 'R2': 1, 'R3': 2, 'R4': 0, 'R5': 0, 'R6': 0, 'R7': 2, 'R8': 1}, Flag=False
Traceback (most recent call last):
File "/usr/src/app/main.py", line 258, in <module>
primes_found = evaluate(program)
^^^^^^^^^^^^^^^^^
File "/usr/src/app/main.py", line 172, in evaluate
raise e # Re-raise the exception
^^^^^^^
File "/usr/src/app/main.py", line 167, in evaluate
raise ValueError(f"Unknown opcode '{opcode}'")
ValueError: Unknown opcode 'START_LOOP:'
Testing if the previous output contains the string All tests passed
: False