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: |
Here's a Python interpreter `evaluate(str)` function that takes the assembly code as a string and returns the resulting memory state after running the 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 | def evaluate(program): lines = program.split('\n') registers = [0] * 10 # 10 registers (R0 to R9) memory = {} # Memory storage labels = {} # Label addresses # First pass: Find label addresses pc = 0 # Program counter for line in lines: line = line.strip() if not line or line.startswith('//'): continue if ':' in line: label = line.split(':')[0].strip() labels[label] = pc else: pc += 1 # Second pass: Execute instructions pc = 0 # Reset program counter while pc < len(lines): line = lines[pc].strip() if not line or line.startswith('//'): pc += 1 continue parts = line.split() instruction = parts[0] if instruction == 'SET': reg = int(parts[1][1:]) value = int(parts[2]) registers[reg] = value elif instruction == 'INC': reg = int(parts[1][1:]) registers[reg] += 1 elif instruction == 'MUL': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] * registers[reg3] elif instruction == 'GT': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = int(registers[reg1] > registers[reg2]) elif instruction == 'EQ': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = int(registers[reg1] == registers[reg2]) elif instruction == 'JMP': label = parts[1] pc = labels[label] continue elif instruction == 'JT': label = parts[1] if registers[9]: pc = labels[label] continue elif instruction == 'JF': label = parts[1] if not registers[9]: pc = labels[label] continue elif instruction == 'MOD': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] % registers[reg3] elif instruction == 'STORE': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) memory[registers[reg2]] = registers[reg1] pc += 1 return [memory[i] for i in range(registers[8])] # Example usage program = ''' ... (assembly code) ... ''' result = evaluate(program) print(result) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | def evaluate(program): lines = program.split('\n') registers = [0] * 10 # 10 registers (R0 to R9) memory = {} # Memory storage labels = {} # Label addresses # First pass: Find label addresses pc = 0 # Program counter for line in lines: line = line.strip() if not line or line.startswith('//'): continue if ':' in line: label = line.split(':')[0].strip() labels[label] = pc else: pc += 1 # Second pass: Execute instructions pc = 0 # Reset program counter while pc < len(lines): line = lines[pc].strip() if not line or line.startswith('//'): pc += 1 continue parts = line.split() instruction = parts[0] if instruction == 'SET': reg = int(parts[1][1:]) value = int(parts[2]) registers[reg] = value elif instruction == 'INC': reg = int(parts[1][1:]) registers[reg] += 1 elif instruction == 'MUL': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] * registers[reg3] elif instruction == 'GT': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = int(registers[reg1] > registers[reg2]) elif instruction == 'EQ': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = int(registers[reg1] == registers[reg2]) elif instruction == 'JMP': label = parts[1] pc = labels[label] continue elif instruction == 'JT': label = parts[1] if registers[9]: pc = labels[label] continue elif instruction == 'JF': label = parts[1] if not registers[9]: pc = labels[label] continue elif instruction == 'MOD': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] % registers[reg3] elif instruction == 'STORE': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) memory[registers[reg2]] = registers[reg1] pc += 1 return [memory[i] for i in range(registers[8])] # Example usage program = ''' ... (assembly code) ... ''' result = evaluate(program) print(result) |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | def evaluate(program): lines = program.split('\n') registers = [0] * 10 # 10 registers (R0 to R9) memory = {} # Memory storage labels = {} # Label addresses # First pass: Find label addresses pc = 0 # Program counter for line in lines: line = line.strip() if not line or line.startswith('//'): continue if ':' in line: label = line.split(':')[0].strip() labels[label] = pc else: pc += 1 # Second pass: Execute instructions pc = 0 # Reset program counter while pc < len(lines): line = lines[pc].strip() if not line or line.startswith('//'): pc += 1 continue parts = line.split() instruction = parts[0] if instruction == 'SET': reg = int(parts[1][1:]) value = int(parts[2]) registers[reg] = value elif instruction == 'INC': reg = int(parts[1][1:]) registers[reg] += 1 elif instruction == 'MUL': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] * registers[reg3] elif instruction == 'GT': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = int(registers[reg1] > registers[reg2]) elif instruction == 'EQ': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = int(registers[reg1] == registers[reg2]) elif instruction == 'JMP': label = parts[1] pc = labels[label] continue elif instruction == 'JT': label = parts[1] if registers[9]: pc = labels[label] continue elif instruction == 'JF': label = parts[1] if not registers[9]: pc = labels[label] continue elif instruction == 'MOD': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] % registers[reg3] elif instruction == 'STORE': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) memory[registers[reg2]] = registers[reg1] pc += 1 return [memory[i] for i in range(registers[8])] # Example usage program = ''' ... (assembly code) ... ''' result = evaluate(program) print(result) answer = evaluate(""" SET R1 0 SET R2 1 loop: MUL R3 R2 R2 STORE R3 R1 INC R1 INC R2 SET R3 20 EQ R1 R3 JF loop """)[:10] expected = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' answer = evaluate(""" SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: SET R7 1 EQ R2 R7 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found SET R7 100 EQ R8 R7 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 SET R7 0 EQ R7 R6 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """)[:10] expected = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
[]
Traceback (most recent call last):
File "/usr/src/app/main.py", line 104, 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 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: |
Here's a Python interpreter `evaluate(str)` that simulates the assembly language program you provided and returns the resulting memory state after running the 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 | def evaluate(program): lines = program.strip().split('\n') registers = {'R1': 0, 'R2': 0, 'R3': 0, 'R4': 0, 'R5': 0, 'R6': 0, 'R7': 0, 'R8': 0} memory = [0] * 100 labels = {} pc = 0 # First pass: extract labels for i, line in enumerate(lines): if ':' in line: label = line.split(':')[0].strip() labels[label] = i while pc < len(lines): line = lines[pc].strip() if '//' in line: line = line.split('//')[0].strip() if not line: pc += 1 continue parts = line.split() opcode = parts[0] if opcode == 'SET': reg, val = parts[1], int(parts[2]) registers[reg] = val elif opcode == 'INC': reg = parts[1] registers[reg] += 1 elif opcode == 'MUL': reg1, reg2, reg3 = parts[1], parts[2], parts[3] registers[reg1] = registers[reg2] * registers[reg3] elif opcode == 'GT': reg1, reg2 = parts[1], parts[2] registers['FLAG'] = int(registers[reg1] > registers[reg2]) elif opcode == 'EQ': reg1, reg2 = parts[1], parts[2] registers['FLAG'] = int(registers[reg1] == registers[reg2]) elif opcode == 'JMP': label = parts[1] pc = labels[label] continue elif opcode == 'JT': label = parts[1] if registers['FLAG']: pc = labels[label] continue elif opcode == 'JF': label = parts[1] if not registers['FLAG']: pc = labels[label] continue elif opcode == 'MOD': reg1, reg2, reg3 = parts[1], parts[2], parts[3] registers[reg1] = registers[reg2] % registers[reg3] elif opcode == 'STORE': reg, addr = parts[1], registers[parts[2]] memory[addr] = registers[reg] pc += 1 primes = [num for num in memory if num != 0] return primes |
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 | def evaluate(program): lines = program.strip().split('\n') registers = {'R1': 0, 'R2': 0, 'R3': 0, 'R4': 0, 'R5': 0, 'R6': 0, 'R7': 0, 'R8': 0} memory = [0] * 100 labels = {} pc = 0 # First pass: extract labels for i, line in enumerate(lines): if ':' in line: label = line.split(':')[0].strip() labels[label] = i while pc < len(lines): line = lines[pc].strip() if '//' in line: line = line.split('//')[0].strip() if not line: pc += 1 continue parts = line.split() opcode = parts[0] if opcode == 'SET': reg, val = parts[1], int(parts[2]) registers[reg] = val elif opcode == 'INC': reg = parts[1] registers[reg] += 1 elif opcode == 'MUL': reg1, reg2, reg3 = parts[1], parts[2], parts[3] registers[reg1] = registers[reg2] * registers[reg3] elif opcode == 'GT': reg1, reg2 = parts[1], parts[2] registers['FLAG'] = int(registers[reg1] > registers[reg2]) elif opcode == 'EQ': reg1, reg2 = parts[1], parts[2] registers['FLAG'] = int(registers[reg1] == registers[reg2]) elif opcode == 'JMP': label = parts[1] pc = labels[label] continue elif opcode == 'JT': label = parts[1] if registers['FLAG']: pc = labels[label] continue elif opcode == 'JF': label = parts[1] if not registers['FLAG']: pc = labels[label] continue elif opcode == 'MOD': reg1, reg2, reg3 = parts[1], parts[2], parts[3] registers[reg1] = registers[reg2] % registers[reg3] elif opcode == 'STORE': reg, addr = parts[1], registers[parts[2]] memory[addr] = registers[reg] pc += 1 primes = [num for num in memory if num != 0] return primes |
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 | def evaluate(program): lines = program.strip().split('\n') registers = {'R1': 0, 'R2': 0, 'R3': 0, 'R4': 0, 'R5': 0, 'R6': 0, 'R7': 0, 'R8': 0} memory = [0] * 100 labels = {} pc = 0 # First pass: extract labels for i, line in enumerate(lines): if ':' in line: label = line.split(':')[0].strip() labels[label] = i while pc < len(lines): line = lines[pc].strip() if '//' in line: line = line.split('//')[0].strip() if not line: pc += 1 continue parts = line.split() opcode = parts[0] if opcode == 'SET': reg, val = parts[1], int(parts[2]) registers[reg] = val elif opcode == 'INC': reg = parts[1] registers[reg] += 1 elif opcode == 'MUL': reg1, reg2, reg3 = parts[1], parts[2], parts[3] registers[reg1] = registers[reg2] * registers[reg3] elif opcode == 'GT': reg1, reg2 = parts[1], parts[2] registers['FLAG'] = int(registers[reg1] > registers[reg2]) elif opcode == 'EQ': reg1, reg2 = parts[1], parts[2] registers['FLAG'] = int(registers[reg1] == registers[reg2]) elif opcode == 'JMP': label = parts[1] pc = labels[label] continue elif opcode == 'JT': label = parts[1] if registers['FLAG']: pc = labels[label] continue elif opcode == 'JF': label = parts[1] if not registers['FLAG']: pc = labels[label] continue elif opcode == 'MOD': reg1, reg2, reg3 = parts[1], parts[2], parts[3] registers[reg1] = registers[reg2] % registers[reg3] elif opcode == 'STORE': reg, addr = parts[1], registers[parts[2]] memory[addr] = registers[reg] pc += 1 primes = [num for num in memory if num != 0] return primes 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') |
All tests passed
Testing if the previous output contains the string All tests passed
: True
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: |
Here's a Python interpreter `evaluate(str)` function that simulates the execution of the assembly language program you provided and returns the resulting memory state after running the 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 | def evaluate(program): lines = program.split('\n') registers = [0] * 10 memory = [0] * 1000 pc = 0 labels = {} # First pass: Extract labels for i, line in enumerate(lines): line = line.strip() if line and not line.startswith('//'): if ':' in line: label = line.split(':')[0].strip() labels[label] = i # Second pass: Execute instructions while pc < len(lines): line = lines[pc].strip() if line and not line.startswith('//'): parts = line.split() instruction = parts[0] if instruction == 'SET': reg = int(parts[1][1:]) value = int(parts[2]) registers[reg] = value elif instruction == 'JMP': label = parts[1] pc = labels[label] continue elif instruction == 'EQ': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = 1 if registers[reg1] == registers[reg2] else 0 elif instruction == 'JF': label = parts[1] if registers[9] == 0: pc = labels[label] continue elif instruction == 'STORE': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) memory[registers[reg2]] = registers[reg1] elif instruction == 'INC': reg = int(parts[1][1:]) registers[reg] += 1 elif instruction == 'JT': label = parts[1] if registers[9] == 1: pc = labels[label] continue elif instruction == 'MUL': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] * registers[reg3] elif instruction == 'GT': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = 1 if registers[reg1] > registers[reg2] else 0 elif instruction == 'MOD': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] % registers[reg3] pc += 1 # Extract prime numbers from memory primes = memory[:registers[8]] return primes # Example usage program = ''' ... (your assembly language program) ... ''' result = evaluate(program) print(result) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | def evaluate(program): lines = program.split('\n') registers = [0] * 10 memory = [0] * 1000 pc = 0 labels = {} # First pass: Extract labels for i, line in enumerate(lines): line = line.strip() if line and not line.startswith('//'): if ':' in line: label = line.split(':')[0].strip() labels[label] = i # Second pass: Execute instructions while pc < len(lines): line = lines[pc].strip() if line and not line.startswith('//'): parts = line.split() instruction = parts[0] if instruction == 'SET': reg = int(parts[1][1:]) value = int(parts[2]) registers[reg] = value elif instruction == 'JMP': label = parts[1] pc = labels[label] continue elif instruction == 'EQ': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = 1 if registers[reg1] == registers[reg2] else 0 elif instruction == 'JF': label = parts[1] if registers[9] == 0: pc = labels[label] continue elif instruction == 'STORE': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) memory[registers[reg2]] = registers[reg1] elif instruction == 'INC': reg = int(parts[1][1:]) registers[reg] += 1 elif instruction == 'JT': label = parts[1] if registers[9] == 1: pc = labels[label] continue elif instruction == 'MUL': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] * registers[reg3] elif instruction == 'GT': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = 1 if registers[reg1] > registers[reg2] else 0 elif instruction == 'MOD': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] % registers[reg3] pc += 1 # Extract prime numbers from memory primes = memory[:registers[8]] return primes # Example usage program = ''' ... (your assembly language program) ... ''' result = evaluate(program) print(result) |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | def evaluate(program): lines = program.split('\n') registers = [0] * 10 memory = [0] * 1000 pc = 0 labels = {} # First pass: Extract labels for i, line in enumerate(lines): line = line.strip() if line and not line.startswith('//'): if ':' in line: label = line.split(':')[0].strip() labels[label] = i # Second pass: Execute instructions while pc < len(lines): line = lines[pc].strip() if line and not line.startswith('//'): parts = line.split() instruction = parts[0] if instruction == 'SET': reg = int(parts[1][1:]) value = int(parts[2]) registers[reg] = value elif instruction == 'JMP': label = parts[1] pc = labels[label] continue elif instruction == 'EQ': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = 1 if registers[reg1] == registers[reg2] else 0 elif instruction == 'JF': label = parts[1] if registers[9] == 0: pc = labels[label] continue elif instruction == 'STORE': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) memory[registers[reg2]] = registers[reg1] elif instruction == 'INC': reg = int(parts[1][1:]) registers[reg] += 1 elif instruction == 'JT': label = parts[1] if registers[9] == 1: pc = labels[label] continue elif instruction == 'MUL': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] * registers[reg3] elif instruction == 'GT': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = 1 if registers[reg1] > registers[reg2] else 0 elif instruction == 'MOD': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] % registers[reg3] pc += 1 # Extract prime numbers from memory primes = memory[:registers[8]] return primes # Example usage program = ''' ... (your assembly language program) ... ''' result = evaluate(program) print(result) answer = evaluate(""" SET R1 0 SET R2 1 loop: MUL R3 R2 R2 STORE R3 R1 INC R1 INC R2 SET R3 20 EQ R1 R3 JF loop """)[:10] expected = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' answer = evaluate(""" SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: SET R7 1 EQ R2 R7 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found SET R7 100 EQ R8 R7 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 SET R7 0 EQ R7 R6 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """)[:10] expected = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
[]
Traceback (most recent call last):
File "/usr/src/app/main.py", line 99, 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 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: |
Here's a Python interpreter `evaluate(str)` function that simulates the execution of the assembly language program you provided and returns the resulting memory state after running the 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 | def evaluate(program): lines = program.strip().split('\n') registers = [0] * 10 memory = [0] * 1000 labels = {} # First pass: Find labels and store their positions for i, line in enumerate(lines): if ':' in line: label = line.split(':')[0].strip() labels[label] = i # Second pass: Execute the program i = 0 while i < len(lines): line = lines[i].strip() if not line or line.startswith('//'): i += 1 continue parts = line.split() instruction = parts[0] if instruction == 'SET': reg = int(parts[1][1:]) val = int(parts[2]) registers[reg] = val elif instruction == 'INC': reg = int(parts[1][1:]) registers[reg] += 1 elif instruction == 'MUL': dest_reg = int(parts[1][1:]) reg1 = int(parts[2][1:]) reg2 = int(parts[3][1:]) registers[dest_reg] = registers[reg1] * registers[reg2] elif instruction == 'GT': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = int(registers[reg1] > registers[reg2]) elif instruction == 'EQ': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = int(registers[reg1] == registers[reg2]) elif instruction == 'JMP': label = parts[1] i = labels[label] continue elif instruction == 'JT': label = parts[1] if registers[9]: i = labels[label] continue elif instruction == 'JF': label = parts[1] if not registers[9]: i = labels[label] continue elif instruction == 'MOD': dest_reg = int(parts[1][1:]) reg1 = int(parts[2][1:]) reg2 = int(parts[3][1:]) registers[dest_reg] = registers[reg1] % registers[reg2] elif instruction == 'STORE': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) memory[registers[reg2]] = registers[reg1] i += 1 # Return the memory state (prime numbers) return [num for num in memory if num != 0] |
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 | def evaluate(program): lines = program.strip().split('\n') registers = [0] * 10 memory = [0] * 1000 labels = {} # First pass: Find labels and store their positions for i, line in enumerate(lines): if ':' in line: label = line.split(':')[0].strip() labels[label] = i # Second pass: Execute the program i = 0 while i < len(lines): line = lines[i].strip() if not line or line.startswith('//'): i += 1 continue parts = line.split() instruction = parts[0] if instruction == 'SET': reg = int(parts[1][1:]) val = int(parts[2]) registers[reg] = val elif instruction == 'INC': reg = int(parts[1][1:]) registers[reg] += 1 elif instruction == 'MUL': dest_reg = int(parts[1][1:]) reg1 = int(parts[2][1:]) reg2 = int(parts[3][1:]) registers[dest_reg] = registers[reg1] * registers[reg2] elif instruction == 'GT': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = int(registers[reg1] > registers[reg2]) elif instruction == 'EQ': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = int(registers[reg1] == registers[reg2]) elif instruction == 'JMP': label = parts[1] i = labels[label] continue elif instruction == 'JT': label = parts[1] if registers[9]: i = labels[label] continue elif instruction == 'JF': label = parts[1] if not registers[9]: i = labels[label] continue elif instruction == 'MOD': dest_reg = int(parts[1][1:]) reg1 = int(parts[2][1:]) reg2 = int(parts[3][1:]) registers[dest_reg] = registers[reg1] % registers[reg2] elif instruction == 'STORE': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) memory[registers[reg2]] = registers[reg1] i += 1 # Return the memory state (prime numbers) return [num for num in memory if num != 0] |
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 | def evaluate(program): lines = program.strip().split('\n') registers = [0] * 10 memory = [0] * 1000 labels = {} # First pass: Find labels and store their positions for i, line in enumerate(lines): if ':' in line: label = line.split(':')[0].strip() labels[label] = i # Second pass: Execute the program i = 0 while i < len(lines): line = lines[i].strip() if not line or line.startswith('//'): i += 1 continue parts = line.split() instruction = parts[0] if instruction == 'SET': reg = int(parts[1][1:]) val = int(parts[2]) registers[reg] = val elif instruction == 'INC': reg = int(parts[1][1:]) registers[reg] += 1 elif instruction == 'MUL': dest_reg = int(parts[1][1:]) reg1 = int(parts[2][1:]) reg2 = int(parts[3][1:]) registers[dest_reg] = registers[reg1] * registers[reg2] elif instruction == 'GT': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = int(registers[reg1] > registers[reg2]) elif instruction == 'EQ': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = int(registers[reg1] == registers[reg2]) elif instruction == 'JMP': label = parts[1] i = labels[label] continue elif instruction == 'JT': label = parts[1] if registers[9]: i = labels[label] continue elif instruction == 'JF': label = parts[1] if not registers[9]: i = labels[label] continue elif instruction == 'MOD': dest_reg = int(parts[1][1:]) reg1 = int(parts[2][1:]) reg2 = int(parts[3][1:]) registers[dest_reg] = registers[reg1] % registers[reg2] elif instruction == 'STORE': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) memory[registers[reg2]] = registers[reg1] i += 1 # Return the memory state (prime numbers) return [num for num in memory if num != 0] 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') |
All tests passed
Testing if the previous output contains the string All tests passed
: True
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: |
Here's a Python interpreter `evaluate(str)` that takes the assembly language program as a string and returns the resulting memory state after running the 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 | def evaluate(program): lines = program.strip().split('\n') memory = [0] * 1000 # Simulate memory with 1000 slots registers = [0] * 10 # 10 registers (R0 to R9) labels = {} # First pass: Find labels and store their positions for i, line in enumerate(lines): line = line.strip() if line and not line.startswith('//'): parts = line.split() if len(parts) == 1 and parts[0].endswith(':'): label = parts[0][:-1] labels[label] = i # Second pass: Execute the program pc = 0 # Program counter while pc < len(lines): line = lines[pc].strip() if line and not line.startswith('//'): parts = line.split() instruction = parts[0] if instruction == 'SET': reg = int(parts[1][1:]) value = int(parts[2]) registers[reg] = value elif instruction == 'JMP': label = parts[1] pc = labels[label] continue elif instruction == 'EQ': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = int(registers[reg1] == registers[reg2]) elif instruction == 'JF': label = parts[1] if registers[9] == 0: pc = labels[label] continue elif instruction == 'STORE': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) memory[registers[reg2]] = registers[reg1] elif instruction == 'INC': reg = int(parts[1][1:]) registers[reg] += 1 elif instruction == 'MUL': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] * registers[reg3] elif instruction == 'GT': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = int(registers[reg1] > registers[reg2]) elif instruction == 'JT': label = parts[1] if registers[9] == 1: pc = labels[label] continue elif instruction == 'MOD': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] % registers[reg3] pc += 1 return [value for value in memory if value != 0] |
1 2 3 4 5 6 | program = ''' ... (assembly language program) ... ''' result = evaluate(program) print(result) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | def evaluate(program): lines = program.strip().split('\n') memory = [0] * 1000 # Simulate memory with 1000 slots registers = [0] * 10 # 10 registers (R0 to R9) labels = {} # First pass: Find labels and store their positions for i, line in enumerate(lines): line = line.strip() if line and not line.startswith('//'): parts = line.split() if len(parts) == 1 and parts[0].endswith(':'): label = parts[0][:-1] labels[label] = i # Second pass: Execute the program pc = 0 # Program counter while pc < len(lines): line = lines[pc].strip() if line and not line.startswith('//'): parts = line.split() instruction = parts[0] if instruction == 'SET': reg = int(parts[1][1:]) value = int(parts[2]) registers[reg] = value elif instruction == 'JMP': label = parts[1] pc = labels[label] continue elif instruction == 'EQ': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = int(registers[reg1] == registers[reg2]) elif instruction == 'JF': label = parts[1] if registers[9] == 0: pc = labels[label] continue elif instruction == 'STORE': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) memory[registers[reg2]] = registers[reg1] elif instruction == 'INC': reg = int(parts[1][1:]) registers[reg] += 1 elif instruction == 'MUL': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] * registers[reg3] elif instruction == 'GT': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = int(registers[reg1] > registers[reg2]) elif instruction == 'JT': label = parts[1] if registers[9] == 1: pc = labels[label] continue elif instruction == 'MOD': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] % registers[reg3] pc += 1 return [value for value in memory if value != 0] |
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 | def evaluate(program): lines = program.strip().split('\n') memory = [0] * 1000 # Simulate memory with 1000 slots registers = [0] * 10 # 10 registers (R0 to R9) labels = {} # First pass: Find labels and store their positions for i, line in enumerate(lines): line = line.strip() if line and not line.startswith('//'): parts = line.split() if len(parts) == 1 and parts[0].endswith(':'): label = parts[0][:-1] labels[label] = i # Second pass: Execute the program pc = 0 # Program counter while pc < len(lines): line = lines[pc].strip() if line and not line.startswith('//'): parts = line.split() instruction = parts[0] if instruction == 'SET': reg = int(parts[1][1:]) value = int(parts[2]) registers[reg] = value elif instruction == 'JMP': label = parts[1] pc = labels[label] continue elif instruction == 'EQ': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = int(registers[reg1] == registers[reg2]) elif instruction == 'JF': label = parts[1] if registers[9] == 0: pc = labels[label] continue elif instruction == 'STORE': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) memory[registers[reg2]] = registers[reg1] elif instruction == 'INC': reg = int(parts[1][1:]) registers[reg] += 1 elif instruction == 'MUL': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] * registers[reg3] elif instruction == 'GT': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) registers[9] = int(registers[reg1] > registers[reg2]) elif instruction == 'JT': label = parts[1] if registers[9] == 1: pc = labels[label] continue elif instruction == 'MOD': reg1 = int(parts[1][1:]) reg2 = int(parts[2][1:]) reg3 = int(parts[3][1:]) registers[reg1] = registers[reg2] % registers[reg3] pc += 1 return [value for value in memory if value != 0] answer = evaluate(""" SET R1 0 SET R2 1 loop: MUL R3 R2 R2 STORE R3 R1 INC R1 INC R2 SET R3 20 EQ R1 R3 JF loop """)[:10] expected = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' answer = evaluate(""" SET R1 2 // Starting number to check for prime start_find_primes: JMP is_prime // Control will return after executing is_prime with R1 as input and R2 containing the result ready_prime: SET R7 1 EQ R2 R7 // Check if R2 is 1 (prime) JF increment // If not prime, skip storing and increment the number // Store prime number in memory and increment count STORE R1 R8 // Store prime number at address pointed by R8 INC R8 // Increment prime count // Check if 100 primes are found SET R7 100 EQ R8 R7 JF increment // If not 100 primes found yet, continue finding JMP end // If 100 primes found, end program increment: INC R1 // Increment number to check for prime JMP start_find_primes // Check next number is_prime: SET R2 1 // Assume number is prime initially SET R3 2 // Start divisor from 2 start_loop: // Label to start the loop // Check if we have exceeded the square root of R1 MUL R4 R3 R3 // R4 = R3 * R3 GT R4 R1 // Set flag if R4 > R1 JT is_prime_end // If not exceeded, continue; else, end loop MOD R6 R1 R3 // R6 = R1 % R3 SET R7 0 EQ R7 R6 // Check if R6 is 0 JT not_prime // If yes, number is not prime INC R3 // Increment divisor JMP start_loop // Repeat loop not_prime: SET R2 0 // Set result to 0 (not prime) is_prime_end: JMP ready_prime end: """)[:10] expected = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
Traceback (most recent call last):
File "/usr/src/app/main.py", line 92, in <module>
answer = evaluate("""
File "/usr/src/app/main.py", line 31, in evaluate
pc = labels[label]
KeyError: 'start_loop'
Testing if the previous output contains the string All tests passed
: False