Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | def evaluate(program): memory = {} registers = {"R1": 0, "R2": 0, "R3": 0, "R4": 0, "R5": 0, "R6": 0, "R7": 0, "R8": 0} labels = {} pc = 0 # Parse the program and extract labels for line in program.splitlines(): if ":" in line: label, instruction = line.split(":") labels[label] = pc pc += 1 # Execute the program while pc < len(program): line = program[pc] if line.startswith("SET"): register, value = line.split(" ") registers[register] = int(value) elif line.startswith("JMP"): label = line.split(" ")[1] pc = labels[label] elif line.startswith("EQ"): register, value = line.split(" ") registers[register] = 1 if registers[register] == int(value) else 0 elif line.startswith("JF"): label = line.split(" ")[1] if registers["R2"] == 0: pc = labels[label] elif line.startswith("INC"): register = line.split(" ")[1] registers[register] += 1 elif line.startswith("STORE"): register, address = line.split(" ") memory[int(address)] = registers[register] elif line.startswith("MUL"): register1, register2, register3 = line.split(" ") registers[register3] = registers[register1] * registers[register2] elif line.startswith("GT"): register1, register2 = line.split(" ") registers["R2"] = 1 if registers[register1] > registers[register2] else 0 elif line.startswith("JT"): label = line.split(" ")[1] if registers["R2"] == 1: pc = labels[label] elif line.startswith("MOD"): register1, register2, register3 = line.split(" ") registers[register3] = registers[register1] % registers[register2] elif line.startswith("NOT"): register = line.split(" ")[1] registers[register] = 0 if registers[register] == 1 else 1 elif line.startswith("END"): break pc += 1 # Return the resulting memory state return [memory[i] for i in sorted(memory.keys())] |
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 | def evaluate(program): memory = {} registers = {"R1": 0, "R2": 0, "R3": 0, "R4": 0, "R5": 0, "R6": 0, "R7": 0, "R8": 0} labels = {} pc = 0 # Parse the program and extract labels for line in program.splitlines(): if ":" in line: label, instruction = line.split(":") labels[label] = pc pc += 1 # Execute the program while pc < len(program): line = program[pc] if line.startswith("SET"): register, value = line.split(" ") registers[register] = int(value) elif line.startswith("JMP"): label = line.split(" ")[1] pc = labels[label] elif line.startswith("EQ"): register, value = line.split(" ") registers[register] = 1 if registers[register] == int(value) else 0 elif line.startswith("JF"): label = line.split(" ")[1] if registers["R2"] == 0: pc = labels[label] elif line.startswith("INC"): register = line.split(" ")[1] registers[register] += 1 elif line.startswith("STORE"): register, address = line.split(" ") memory[int(address)] = registers[register] elif line.startswith("MUL"): register1, register2, register3 = line.split(" ") registers[register3] = registers[register1] * registers[register2] elif line.startswith("GT"): register1, register2 = line.split(" ") registers["R2"] = 1 if registers[register1] > registers[register2] else 0 elif line.startswith("JT"): label = line.split(" ")[1] if registers["R2"] == 1: pc = labels[label] elif line.startswith("MOD"): register1, register2, register3 = line.split(" ") registers[register3] = registers[register1] % registers[register2] elif line.startswith("NOT"): register = line.split(" ")[1] registers[register] = 0 if registers[register] == 1 else 1 elif line.startswith("END"): break pc += 1 # Return the resulting memory state return [memory[i] for i in sorted(memory.keys())] |
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 | def evaluate(program): memory = {} registers = {"R1": 0, "R2": 0, "R3": 0, "R4": 0, "R5": 0, "R6": 0, "R7": 0, "R8": 0} labels = {} pc = 0 # Parse the program and extract labels for line in program.splitlines(): if ":" in line: label, instruction = line.split(":") labels[label] = pc pc += 1 # Execute the program while pc < len(program): line = program[pc] if line.startswith("SET"): register, value = line.split(" ") registers[register] = int(value) elif line.startswith("JMP"): label = line.split(" ")[1] pc = labels[label] elif line.startswith("EQ"): register, value = line.split(" ") registers[register] = 1 if registers[register] == int(value) else 0 elif line.startswith("JF"): label = line.split(" ")[1] if registers["R2"] == 0: pc = labels[label] elif line.startswith("INC"): register = line.split(" ")[1] registers[register] += 1 elif line.startswith("STORE"): register, address = line.split(" ") memory[int(address)] = registers[register] elif line.startswith("MUL"): register1, register2, register3 = line.split(" ") registers[register3] = registers[register1] * registers[register2] elif line.startswith("GT"): register1, register2 = line.split(" ") registers["R2"] = 1 if registers[register1] > registers[register2] else 0 elif line.startswith("JT"): label = line.split(" ")[1] if registers["R2"] == 1: pc = labels[label] elif line.startswith("MOD"): register1, register2, register3 = line.split(" ") registers[register3] = registers[register1] % registers[register2] elif line.startswith("NOT"): register = line.split(" ")[1] registers[register] = 0 if registers[register] == 1 else 1 elif line.startswith("END"): break pc += 1 # Return the resulting memory state return [memory[i] for i in sorted(memory.keys())] answer = evaluate(""" SET R1 0 SET R2 1 loop: MUL R3 R2 R2 STORE R3 R1 INC R1 INC R2 EQ R1 20 JF loop HCF """)[:10] expected = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' answer = evaluate(""" SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """)[:10] expected = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
Traceback (most recent call last):
File "/usr/src/app/main.py", line 77, in <module>
assert answer == expected, f'Wrong answer; got {answer} instead of {expected}'
^^^^^^^^^^^^^^^^^^
AssertionError: Wrong answer; got [] instead of [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Testing if the previous output contains the string All tests passed
: False
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | def evaluate(program): """ Evaluate the given assembly program and return the resulting memory state. The program is a string of assembly instructions, each on a separate line. The memory state is a list of integers, representing the contents of memory. The program counter (PC) is initially set to 0. The registers (R1, R2, R3, ...) are initially set to 0. The program is executed by repeatedly fetching the instruction at the PC, incrementing the PC, and executing the instruction. Instructions are of the form: - SET R<n> <value> - EQ R<n> <value> - JF <label> - JT <label> - INC R<n> - STORE R<n> <address> where: - R<n> is a register - <value> is an integer - <label> is a label - <address> is a memory address The following instructions are also supported: - JMP <label> - MOD R<n> R<m> R<o> - MUL R<n> R<m> R<o> - GT R<n> R<m> where: - MOD R<n> R<m> R<o> sets R<n> to the remainder of R<m> / R<o> - MUL R<n> R<m> R<o> sets R<n> to the product of R<m> * R<o> - GT R<n> R<m> sets the flag to 1 if R<n> > R<m>, and 0 otherwise The program terminates when the PC reaches the end of the program. The resulting memory state is returned as a list of integers. """ # Parse the program into a list of instructions. instructions = program.split('\n') # Initialize the memory state. memory = [0] * 1000 # Initialize the registers. registers = {} for i in range(1, 10): registers[f'R{i}'] = 0 # Initialize the program counter. pc = 0 # Execute the program. while pc < len(instructions): # Fetch the instruction. instruction = instructions[pc] # Increment the program counter. pc += 1 # Execute the instruction. if instruction.startswith('SET'): register, value = instruction[4:].split(' ') registers[register] = int(value) elif instruction.startswith('EQ'): register, value = instruction[3:].split(' ') registers[register] = int(registers[register] == int(value)) elif instruction.startswith('JF'): label = instruction[3:] if registers['R2'] == 0: pc = instructions.index(f'{label}:') elif instruction.startswith('JT'): label = instruction[3:] if registers['R2'] == 1: pc = instructions.index(f'{label}:') elif instruction.startswith('INC'): register = instruction[4:] registers[register] += 1 elif instruction.startswith('STORE'): register, address = instruction[6:].split(' ') memory[int(address)] = registers[register] elif instruction.startswith('JMP'): label = instruction[4:] pc = instructions.index(f'{label}:') elif instruction.startswith('MOD'): register1, register2, register3 = instruction[4:].split(' ') registers[register1] = registers[register2] % registers[register3] elif instruction.startswith('MUL'): register1, register2, register3 = instruction[4:].split(' ') registers[register1] = registers[register2] * registers[register3] elif instruction.startswith('GT'): register1, register2 = instruction[3:].split(' ') registers['FLAG'] = int(registers[register1] > registers[register2]) # Return the resulting memory state. return memory |
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 | def evaluate(program): """ Evaluate the given assembly program and return the resulting memory state. The program is a string of assembly instructions, each on a separate line. The memory state is a list of integers, representing the contents of memory. The program counter (PC) is initially set to 0. The registers (R1, R2, R3, ...) are initially set to 0. The program is executed by repeatedly fetching the instruction at the PC, incrementing the PC, and executing the instruction. Instructions are of the form: - SET R<n> <value> - EQ R<n> <value> - JF <label> - JT <label> - INC R<n> - STORE R<n> <address> where: - R<n> is a register - <value> is an integer - <label> is a label - <address> is a memory address The following instructions are also supported: - JMP <label> - MOD R<n> R<m> R<o> - MUL R<n> R<m> R<o> - GT R<n> R<m> where: - MOD R<n> R<m> R<o> sets R<n> to the remainder of R<m> / R<o> - MUL R<n> R<m> R<o> sets R<n> to the product of R<m> * R<o> - GT R<n> R<m> sets the flag to 1 if R<n> > R<m>, and 0 otherwise The program terminates when the PC reaches the end of the program. The resulting memory state is returned as a list of integers. """ # Parse the program into a list of instructions. instructions = program.split('\n') # Initialize the memory state. memory = [0] * 1000 # Initialize the registers. registers = {} for i in range(1, 10): registers[f'R{i}'] = 0 # Initialize the program counter. pc = 0 # Execute the program. while pc < len(instructions): # Fetch the instruction. instruction = instructions[pc] # Increment the program counter. pc += 1 # Execute the instruction. if instruction.startswith('SET'): register, value = instruction[4:].split(' ') registers[register] = int(value) elif instruction.startswith('EQ'): register, value = instruction[3:].split(' ') registers[register] = int(registers[register] == int(value)) elif instruction.startswith('JF'): label = instruction[3:] if registers['R2'] == 0: pc = instructions.index(f'{label}:') elif instruction.startswith('JT'): label = instruction[3:] if registers['R2'] == 1: pc = instructions.index(f'{label}:') elif instruction.startswith('INC'): register = instruction[4:] registers[register] += 1 elif instruction.startswith('STORE'): register, address = instruction[6:].split(' ') memory[int(address)] = registers[register] elif instruction.startswith('JMP'): label = instruction[4:] pc = instructions.index(f'{label}:') elif instruction.startswith('MOD'): register1, register2, register3 = instruction[4:].split(' ') registers[register1] = registers[register2] % registers[register3] elif instruction.startswith('MUL'): register1, register2, register3 = instruction[4:].split(' ') registers[register1] = registers[register2] * registers[register3] elif instruction.startswith('GT'): register1, register2 = instruction[3:].split(' ') registers['FLAG'] = int(registers[register1] > registers[register2]) # Return the resulting memory state. return memory |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 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 | def evaluate(program): """ Evaluate the given assembly program and return the resulting memory state. The program is a string of assembly instructions, each on a separate line. The memory state is a list of integers, representing the contents of memory. The program counter (PC) is initially set to 0. The registers (R1, R2, R3, ...) are initially set to 0. The program is executed by repeatedly fetching the instruction at the PC, incrementing the PC, and executing the instruction. Instructions are of the form: - SET R<n> <value> - EQ R<n> <value> - JF <label> - JT <label> - INC R<n> - STORE R<n> <address> where: - R<n> is a register - <value> is an integer - <label> is a label - <address> is a memory address The following instructions are also supported: - JMP <label> - MOD R<n> R<m> R<o> - MUL R<n> R<m> R<o> - GT R<n> R<m> where: - MOD R<n> R<m> R<o> sets R<n> to the remainder of R<m> / R<o> - MUL R<n> R<m> R<o> sets R<n> to the product of R<m> * R<o> - GT R<n> R<m> sets the flag to 1 if R<n> > R<m>, and 0 otherwise The program terminates when the PC reaches the end of the program. The resulting memory state is returned as a list of integers. """ # Parse the program into a list of instructions. instructions = program.split('\n') # Initialize the memory state. memory = [0] * 1000 # Initialize the registers. registers = {} for i in range(1, 10): registers[f'R{i}'] = 0 # Initialize the program counter. pc = 0 # Execute the program. while pc < len(instructions): # Fetch the instruction. instruction = instructions[pc] # Increment the program counter. pc += 1 # Execute the instruction. if instruction.startswith('SET'): register, value = instruction[4:].split(' ') registers[register] = int(value) elif instruction.startswith('EQ'): register, value = instruction[3:].split(' ') registers[register] = int(registers[register] == int(value)) elif instruction.startswith('JF'): label = instruction[3:] if registers['R2'] == 0: pc = instructions.index(f'{label}:') elif instruction.startswith('JT'): label = instruction[3:] if registers['R2'] == 1: pc = instructions.index(f'{label}:') elif instruction.startswith('INC'): register = instruction[4:] registers[register] += 1 elif instruction.startswith('STORE'): register, address = instruction[6:].split(' ') memory[int(address)] = registers[register] elif instruction.startswith('JMP'): label = instruction[4:] pc = instructions.index(f'{label}:') elif instruction.startswith('MOD'): register1, register2, register3 = instruction[4:].split(' ') registers[register1] = registers[register2] % registers[register3] elif instruction.startswith('MUL'): register1, register2, register3 = instruction[4:].split(' ') registers[register1] = registers[register2] * registers[register3] elif instruction.startswith('GT'): register1, register2 = instruction[3:].split(' ') registers['FLAG'] = int(registers[register1] > registers[register2]) # Return the resulting memory state. return memory answer = evaluate(""" SET R1 0 SET R2 1 loop: MUL R3 R2 R2 STORE R3 R1 INC R1 INC R2 EQ R1 20 JF loop HCF """)[:10] expected = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' answer = evaluate(""" SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """)[:10] expected = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
Traceback (most recent call last):
File "/usr/src/app/main.py", line 134, in <module>
assert answer == expected, f'Wrong answer; got {answer} instead of {expected}'
^^^^^^^^^^^^^^^^^^
AssertionError: Wrong answer; got [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] instead of [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Testing if the previous output contains the string All tests passed
: False
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | def evaluate(program): """ Evaluate the given assembly program and return the resulting memory state. Args: program (str): The assembly program to evaluate. Returns: list: The resulting memory state. """ # Initialize the memory state. memory = {0: 0} # Initialize the registers. registers = {"R1": 2, "R2": 0, "R3": 0, "R4": 0, "R6": 0, "R7": 0, "R8": 0} # Parse the program into a list of instructions. instructions = program.split("\n") # Execute the instructions one by one. for instruction in instructions: # Parse the instruction. opcode, operands = instruction.split(" ") # Execute the instruction. if opcode == "SET": registers[operands[0]] = int(operands[1]) elif opcode == "JMP": pc = operands[1] elif opcode == "EQ": registers[operands[0]] = 1 if registers[operands[1]] == registers[operands[2]] else 0 elif opcode == "JF": if registers[operands[0]] == 0: pc = operands[1] elif opcode == "STORE": memory[registers[operands[1]]] = registers[operands[0]] elif opcode == "INC": registers[operands[0]] += 1 elif opcode == "MUL": registers[operands[0]] = registers[operands[1]] * registers[operands[2]] elif opcode == "GT": registers[operands[0]] = 1 if registers[operands[1]] > registers[operands[2]] else 0 elif opcode == "JT": if registers[operands[0]] == 1: pc = operands[1] elif opcode == "MOD": registers[operands[0]] = registers[operands[1]] % registers[operands[2]] elif opcode == "END": break # Return the resulting memory state. return list(memory.values()) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | def evaluate(program): """ Evaluate the given assembly program and return the resulting memory state. Args: program (str): The assembly program to evaluate. Returns: list: The resulting memory state. """ # Initialize the memory state. memory = {0: 0} # Initialize the registers. registers = {"R1": 2, "R2": 0, "R3": 0, "R4": 0, "R6": 0, "R7": 0, "R8": 0} # Parse the program into a list of instructions. instructions = program.split("\n") # Execute the instructions one by one. for instruction in instructions: # Parse the instruction. opcode, operands = instruction.split(" ") # Execute the instruction. if opcode == "SET": registers[operands[0]] = int(operands[1]) elif opcode == "JMP": pc = operands[1] elif opcode == "EQ": registers[operands[0]] = 1 if registers[operands[1]] == registers[operands[2]] else 0 elif opcode == "JF": if registers[operands[0]] == 0: pc = operands[1] elif opcode == "STORE": memory[registers[operands[1]]] = registers[operands[0]] elif opcode == "INC": registers[operands[0]] += 1 elif opcode == "MUL": registers[operands[0]] = registers[operands[1]] * registers[operands[2]] elif opcode == "GT": registers[operands[0]] = 1 if registers[operands[1]] > registers[operands[2]] else 0 elif opcode == "JT": if registers[operands[0]] == 1: pc = operands[1] elif opcode == "MOD": registers[operands[0]] = registers[operands[1]] % registers[operands[2]] elif opcode == "END": break # Return the resulting memory state. return list(memory.values()) |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | def evaluate(program): """ Evaluate the given assembly program and return the resulting memory state. Args: program (str): The assembly program to evaluate. Returns: list: The resulting memory state. """ # Initialize the memory state. memory = {0: 0} # Initialize the registers. registers = {"R1": 2, "R2": 0, "R3": 0, "R4": 0, "R6": 0, "R7": 0, "R8": 0} # Parse the program into a list of instructions. instructions = program.split("\n") # Execute the instructions one by one. for instruction in instructions: # Parse the instruction. opcode, operands = instruction.split(" ") # Execute the instruction. if opcode == "SET": registers[operands[0]] = int(operands[1]) elif opcode == "JMP": pc = operands[1] elif opcode == "EQ": registers[operands[0]] = 1 if registers[operands[1]] == registers[operands[2]] else 0 elif opcode == "JF": if registers[operands[0]] == 0: pc = operands[1] elif opcode == "STORE": memory[registers[operands[1]]] = registers[operands[0]] elif opcode == "INC": registers[operands[0]] += 1 elif opcode == "MUL": registers[operands[0]] = registers[operands[1]] * registers[operands[2]] elif opcode == "GT": registers[operands[0]] = 1 if registers[operands[1]] > registers[operands[2]] else 0 elif opcode == "JT": if registers[operands[0]] == 1: pc = operands[1] elif opcode == "MOD": registers[operands[0]] = registers[operands[1]] % registers[operands[2]] elif opcode == "END": break # Return the resulting memory state. return list(memory.values()) answer = evaluate(""" SET R1 0 SET R2 1 loop: MUL R3 R2 R2 STORE R3 R1 INC R1 INC R2 EQ R1 20 JF loop HCF """)[:10] expected = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' answer = evaluate(""" SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """)[:10] expected = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
Traceback (most recent call last):
File "/usr/src/app/main.py", line 60, in <module>
answer = evaluate("""
^^^^^^^^^^^^
File "/usr/src/app/main.py", line 25, in evaluate
opcode, operands = instruction.split(" ")
^^^^^^^^^^^^^^^^
ValueError: not enough values to unpack (expected 2, got 1)
Testing if the previous output contains the string All tests passed
: False
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | def evaluate(program): """ Evaluate an assembly program and return the resulting memory state. """ memory = [0] * 256 registers = [0] * 8 labels = {} pc = 0 while pc < len(program): line = program[pc] if line.startswith("SET"): _, reg, value = line.split() registers[int(reg[1:])] = int(value) elif line.startswith("JMP"): _, label = line.split() pc = labels[label] elif line.startswith("ready_prime"): pass # Ignore this line elif line.startswith("increment"): registers[1] += 1 pc = labels["start_find_primes"] elif line.startswith("is_prime"): registers[2] = 1 # Assume number is prime initially registers[3] = 2 # Start divisor from 2 pc = labels["start_loop"] elif line.startswith("start_loop"): r4 = registers[3] * registers[3] if r4 > registers[1]: pc = labels["is_prime_end"] else: r6 = registers[1] % registers[3] if r6 == 0: pc = labels["not_prime"] else: registers[3] += 1 pc = labels["start_loop"] elif line.startswith("not_prime"): registers[2] = 0 # Set result to 0 (not prime) pc = labels["is_prime_end"] elif line.startswith("is_prime_end"): pc = labels["ready_prime"] elif line.startswith("end"): break else: raise Exception("Unknown instruction: " + line) pc += 1 return memory |
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 | def evaluate(program): """ Evaluate an assembly program and return the resulting memory state. """ memory = [0] * 256 registers = [0] * 8 labels = {} pc = 0 while pc < len(program): line = program[pc] if line.startswith("SET"): _, reg, value = line.split() registers[int(reg[1:])] = int(value) elif line.startswith("JMP"): _, label = line.split() pc = labels[label] elif line.startswith("ready_prime"): pass # Ignore this line elif line.startswith("increment"): registers[1] += 1 pc = labels["start_find_primes"] elif line.startswith("is_prime"): registers[2] = 1 # Assume number is prime initially registers[3] = 2 # Start divisor from 2 pc = labels["start_loop"] elif line.startswith("start_loop"): r4 = registers[3] * registers[3] if r4 > registers[1]: pc = labels["is_prime_end"] else: r6 = registers[1] % registers[3] if r6 == 0: pc = labels["not_prime"] else: registers[3] += 1 pc = labels["start_loop"] elif line.startswith("not_prime"): registers[2] = 0 # Set result to 0 (not prime) pc = labels["is_prime_end"] elif line.startswith("is_prime_end"): pc = labels["ready_prime"] elif line.startswith("end"): break else: raise Exception("Unknown instruction: " + line) pc += 1 return memory |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | def evaluate(program): """ Evaluate an assembly program and return the resulting memory state. """ memory = [0] * 256 registers = [0] * 8 labels = {} pc = 0 while pc < len(program): line = program[pc] if line.startswith("SET"): _, reg, value = line.split() registers[int(reg[1:])] = int(value) elif line.startswith("JMP"): _, label = line.split() pc = labels[label] elif line.startswith("ready_prime"): pass # Ignore this line elif line.startswith("increment"): registers[1] += 1 pc = labels["start_find_primes"] elif line.startswith("is_prime"): registers[2] = 1 # Assume number is prime initially registers[3] = 2 # Start divisor from 2 pc = labels["start_loop"] elif line.startswith("start_loop"): r4 = registers[3] * registers[3] if r4 > registers[1]: pc = labels["is_prime_end"] else: r6 = registers[1] % registers[3] if r6 == 0: pc = labels["not_prime"] else: registers[3] += 1 pc = labels["start_loop"] elif line.startswith("not_prime"): registers[2] = 0 # Set result to 0 (not prime) pc = labels["is_prime_end"] elif line.startswith("is_prime_end"): pc = labels["ready_prime"] elif line.startswith("end"): break else: raise Exception("Unknown instruction: " + line) pc += 1 return memory answer = evaluate(""" SET R1 0 SET R2 1 loop: MUL R3 R2 R2 STORE R3 R1 INC R1 INC R2 EQ R1 20 JF loop HCF """)[:10] expected = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' answer = evaluate(""" SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """)[:10] expected = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
Traceback (most recent call last):
File "/usr/src/app/main.py", line 54, in <module>
answer = evaluate("""
^^^^^^^^^^^^
File "/usr/src/app/main.py", line 46, in evaluate
raise Exception("Unknown instruction: " + line)
Exception: Unknown instruction:
Testing if the previous output contains the string All tests passed
: False
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
Here is an example program from a new assmebly language I made up that computes primes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | def evaluate(program): """ Evaluate an assembly language program and return the resulting memory state. Args: program (str): The assembly language program to evaluate. Returns: list: The resulting memory state. """ # Initialize the memory state. memory = [0] * 1000 # Initialize the register file. registers = { "R1": 0, "R2": 0, "R3": 0, "R4": 0, "R5": 0, "R6": 0, "R7": 0, "R8": 0, } # Initialize the program counter. pc = 0 # Main loop: fetch, decode, and execute instructions. while pc < len(program): # Fetch the next instruction. instruction = program[pc] # Decode the instruction. opcode, operands = instruction.split(" ") # Execute the instruction. if opcode == "SET": registers[operands[0]] = int(operands[1]) elif opcode == "INC": registers[operands[0]] += 1 elif opcode == "EQ": registers[operands[0]] = int(registers[operands[1]] == registers[operands[2]]) elif opcode == "JF": if registers[operands[0]] == 0: pc = program.index(operands[1]) - 1 elif opcode == "JMP": pc = program.index(operands[1]) - 1 elif opcode == "STORE": memory[registers[operands[1]]] = registers[operands[0]] elif opcode == "MOD": registers[operands[0]] = registers[operands[1]] % registers[operands[2]] elif opcode == "GT": registers[operands[0]] = int(registers[operands[1]] > registers[operands[2]]) elif opcode == "JT": if registers[operands[0]] != 0: pc = program.index(operands[1]) - 1 elif opcode == "MUL": registers[operands[0]] = registers[operands[1]] * registers[operands[2]] # Increment the program counter. pc += 1 # Return the resulting memory state. return memory |
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 | def evaluate(program): """ Evaluate an assembly language program and return the resulting memory state. Args: program (str): The assembly language program to evaluate. Returns: list: The resulting memory state. """ # Initialize the memory state. memory = [0] * 1000 # Initialize the register file. registers = { "R1": 0, "R2": 0, "R3": 0, "R4": 0, "R5": 0, "R6": 0, "R7": 0, "R8": 0, } # Initialize the program counter. pc = 0 # Main loop: fetch, decode, and execute instructions. while pc < len(program): # Fetch the next instruction. instruction = program[pc] # Decode the instruction. opcode, operands = instruction.split(" ") # Execute the instruction. if opcode == "SET": registers[operands[0]] = int(operands[1]) elif opcode == "INC": registers[operands[0]] += 1 elif opcode == "EQ": registers[operands[0]] = int(registers[operands[1]] == registers[operands[2]]) elif opcode == "JF": if registers[operands[0]] == 0: pc = program.index(operands[1]) - 1 elif opcode == "JMP": pc = program.index(operands[1]) - 1 elif opcode == "STORE": memory[registers[operands[1]]] = registers[operands[0]] elif opcode == "MOD": registers[operands[0]] = registers[operands[1]] % registers[operands[2]] elif opcode == "GT": registers[operands[0]] = int(registers[operands[1]] > registers[operands[2]]) elif opcode == "JT": if registers[operands[0]] != 0: pc = program.index(operands[1]) - 1 elif opcode == "MUL": registers[operands[0]] = registers[operands[1]] * registers[operands[2]] # Increment the program counter. pc += 1 # Return the resulting memory state. return memory |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | def evaluate(program): """ Evaluate an assembly language program and return the resulting memory state. Args: program (str): The assembly language program to evaluate. Returns: list: The resulting memory state. """ # Initialize the memory state. memory = [0] * 1000 # Initialize the register file. registers = { "R1": 0, "R2": 0, "R3": 0, "R4": 0, "R5": 0, "R6": 0, "R7": 0, "R8": 0, } # Initialize the program counter. pc = 0 # Main loop: fetch, decode, and execute instructions. while pc < len(program): # Fetch the next instruction. instruction = program[pc] # Decode the instruction. opcode, operands = instruction.split(" ") # Execute the instruction. if opcode == "SET": registers[operands[0]] = int(operands[1]) elif opcode == "INC": registers[operands[0]] += 1 elif opcode == "EQ": registers[operands[0]] = int(registers[operands[1]] == registers[operands[2]]) elif opcode == "JF": if registers[operands[0]] == 0: pc = program.index(operands[1]) - 1 elif opcode == "JMP": pc = program.index(operands[1]) - 1 elif opcode == "STORE": memory[registers[operands[1]]] = registers[operands[0]] elif opcode == "MOD": registers[operands[0]] = registers[operands[1]] % registers[operands[2]] elif opcode == "GT": registers[operands[0]] = int(registers[operands[1]] > registers[operands[2]]) elif opcode == "JT": if registers[operands[0]] != 0: pc = program.index(operands[1]) - 1 elif opcode == "MUL": registers[operands[0]] = registers[operands[1]] * registers[operands[2]] # Increment the program counter. pc += 1 # Return the resulting memory state. return memory answer = evaluate(""" SET R1 0 SET R2 1 loop: MUL R3 R2 R2 STORE R3 R1 INC R1 INC R2 EQ R1 20 JF loop HCF """)[:10] expected = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' answer = evaluate(""" SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: EQ R2 1 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found EQ R8 100 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 EQ R7 R6 0 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """)[:10] expected = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
Traceback (most recent call last):
File "/usr/src/app/main.py", line 73, in <module>
answer = evaluate("""
^^^^^^^^^^^^
File "/usr/src/app/main.py", line 37, in evaluate
opcode, operands = instruction.split(" ")
^^^^^^^^^^^^^^^^
ValueError: not enough values to unpack (expected 2, got 1)
Testing if the previous output contains the string All tests passed
: False